|
| 1 | +""" |
| 2 | + Implementation of the "markdown_to_html" rule. The implementation runs the transform |
| 3 | + executable in order to create the outputs for the specified source files. |
| 4 | +""" |
| 5 | +def _markdown_to_html(ctx): |
| 6 | + input_files = ctx.files.srcs; |
| 7 | + args = ctx.actions.args() |
| 8 | + expected_outputs = []; |
| 9 | + |
| 10 | + for input_file in input_files: |
| 11 | + basename = input_file.basename.replace('.md', '') |
| 12 | + output_file = ctx.actions.declare_file("%s.html" % basename) |
| 13 | + expected_outputs += [output_file] |
| 14 | + |
| 15 | + # Add the input file and it's related output to the arguments that |
| 16 | + # will be passed to the transformer executable. |
| 17 | + args.add("%s=%s" % (input_file.path, output_file.path)) |
| 18 | + |
| 19 | + # Run the transform markdown executable that transforms the specified source files. |
| 20 | + # Note that we should specify the outputs here because Bazel can then throw an error |
| 21 | + # if the script didn't generate the required outputs. |
| 22 | + ctx.actions.run( |
| 23 | + inputs = input_files, |
| 24 | + executable = ctx.executable._transform_markdown, |
| 25 | + outputs = expected_outputs, |
| 26 | + arguments = [args], |
| 27 | + ) |
| 28 | + |
| 29 | + return DefaultInfo(files = depset(expected_outputs)) |
| 30 | + |
| 31 | +""" |
| 32 | + Rule definition for the "markdown_to_html" rule that can accept arbritary source files |
| 33 | + that will be transformed into HTML files. The outputs can be referenced through the |
| 34 | + default output provider. |
| 35 | +""" |
| 36 | +markdown_to_html = rule( |
| 37 | + implementation = _markdown_to_html, |
| 38 | + attrs = { |
| 39 | + "srcs": attr.label_list(allow_files = [".md"]), |
| 40 | + |
| 41 | + # Executable for this rule that is responsible for converting the specified |
| 42 | + # markdown files into HTML files. |
| 43 | + "_transform_markdown": attr.label( |
| 44 | + default = Label("//tools/markdown-to-html"), |
| 45 | + executable = True, |
| 46 | + cfg = "host" |
| 47 | + )}, |
| 48 | +) |
0 commit comments