-
Notifications
You must be signed in to change notification settings - Fork 13
/
pandoc.bzl
106 lines (100 loc) · 2.93 KB
/
pandoc.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
PANDOC_EXTENSIONS = {
"asciidoc": "adoc",
"beamer": "tex",
"commonmark": "md",
"context": "tex",
"docbook": "xml",
"docbook4": "xml",
"docbook5": "xml",
"docx": "docx",
"dokuwiki": "txt",
"dzslides": "html",
"epub": "epub",
"epub2": "epub",
"epub3": "epub",
"fb2": "fb",
"haddock": "txt",
"html": "html",
"html4": "html",
"html5": "html",
"icml": "icml",
"jats": "xml",
"json": "json",
"latex": "tex",
"man": "1",
"markdown": "md",
"markdown_github": "md",
"markdown_mmd": "md",
"markdown_phpextra": "md",
"markdown_strict": "md",
"mediawiki": "txt",
"ms": "1",
"muse": "txt",
"native": "txt",
"odt": "odt",
"opendocument": "odt",
"opml": "openml",
"org": "txt",
"plain": "txt",
"pptx": "pptx",
"revealjs": "html",
"rst": "rst",
"rtf": "rtf",
"s5": "html",
"slideous": "html",
"slidy": "html",
"tei": "html",
"texinfo": "texi",
"textile": "textile",
"zimwiki": "txt",
}
def _pandoc_impl(ctx):
toolchain = ctx.toolchains["@bazel_pandoc//:pandoc_toolchain_type"]
cli_args = []
cli_args.extend(ctx.attr.options)
if ctx.attr.from_format:
cli_args.extend(["--from", ctx.attr.from_format])
if ctx.attr.to_format:
cli_args.extend(["--to", ctx.attr.to_format])
cli_args.extend(["-o", ctx.outputs.output.path])
cli_args.extend([ctx.file.src.path])
ctx.actions.run(
mnemonic = "Pandoc",
executable = toolchain.pandoc.files.to_list()[0].path,
arguments = cli_args,
inputs = depset(
direct = ctx.files.src,
transitive = [toolchain.pandoc.files],
),
outputs = [ctx.outputs.output],
)
_pandoc = rule(
attrs = {
"from_format": attr.string(),
"options": attr.string_list(),
"src": attr.label(allow_single_file = True, mandatory = True),
"to_format": attr.string(),
"output": attr.output(mandatory = True),
},
toolchains = ["@bazel_pandoc//:pandoc_toolchain_type"],
implementation = _pandoc_impl,
)
def _check_format(format, attr_name):
if format not in PANDOC_EXTENSIONS:
fail("Unknown `%{attr}` format: %{format}".fmt(attr = attr_name, format = format))
return format
def _infer_output(name, to_format):
"""Derives output file based on the desired format.
Use the generic .xml syntax for XML-based formats and .txt for
ones with no commonly used extension.
"""
to_format = _check_format(to_format, "to_format")
ext = PANDOC_EXTENSIONS[to_format]
return name + "." + ext
def pandoc(**kwargs):
if "output" not in kwargs:
if "to_format" not in kwargs:
fail("One of `output` or `to_format` attributes must be provided")
to_format = _check_format(kwargs["to_format"], "to_format")
kwargs["output"] = _infer_output(kwargs["name"], to_format)
_pandoc(**kwargs)