|
| 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 | + # Add the bazel bin directory to the command arguments. The script needs to know about |
| 11 | + # the output directory because the input files are not in the same location as the bazel |
| 12 | + # bin directory. |
| 13 | + args.add(ctx.bin_dir.path) |
| 14 | + |
| 15 | + for input_file in input_files: |
| 16 | + # Remove the extension from the input file path. Note that we should not use `.replace` |
| 17 | + # here because the extension can be also in upper case. |
| 18 | + basename = input_file.basename[:-len(".md")] |
| 19 | + |
| 20 | + # For each input file "xxx.md", we want to write an output file "xxx.html" |
| 21 | + expected_outputs += [ctx.actions.declare_file("%s.html" % basename)] |
| 22 | + |
| 23 | + # Add the input file to the command line arguments that will be passed to the |
| 24 | + # transform-markdown executable. |
| 25 | + args.add(input_file.path) |
| 26 | + |
| 27 | + # Run the transform markdown executable that transforms the specified source files. |
| 28 | + # Note that we should specify the outputs here because Bazel can then throw an error |
| 29 | + # if the script didn't generate the required outputs. |
| 30 | + ctx.actions.run( |
| 31 | + inputs = input_files, |
| 32 | + executable = ctx.executable._transform_markdown, |
| 33 | + outputs = expected_outputs, |
| 34 | + arguments = [args], |
| 35 | + ) |
| 36 | + |
| 37 | + return DefaultInfo(files = depset(expected_outputs)) |
| 38 | + |
| 39 | +""" |
| 40 | + Rule definition for the "markdown_to_html" rule that can accept arbritary source files |
| 41 | + that will be transformed into HTML files. The outputs can be referenced through the |
| 42 | + default output provider. |
| 43 | +""" |
| 44 | +markdown_to_html = rule( |
| 45 | + implementation = _markdown_to_html, |
| 46 | + attrs = { |
| 47 | + "srcs": attr.label_list(allow_files = [".md"]), |
| 48 | + |
| 49 | + # Executable for this rule that is responsible for converting the specified |
| 50 | + # markdown files into HTML files. |
| 51 | + "_transform_markdown": attr.label( |
| 52 | + default = Label("//tools/markdown-to-html"), |
| 53 | + executable = True, |
| 54 | + cfg = "host" |
| 55 | + )}, |
| 56 | +) |
0 commit comments