Skip to content

Commit

Permalink
Check in the initial version of the Bazel Pandoc rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Schouten committed Oct 2, 2018
0 parents commit 7f77596
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bazel-*
94 changes: 94 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
load(":toolchain.bzl", "pandoc_toolchain")
load(":pandoc.bzl", "pandoc")

# Precompiled Pandoc binaries provided by upstream.

pandoc_toolchain(
exec_compatible_with = [
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:x86_64",
],
platform = "linux",
)

pandoc_toolchain(
exec_compatible_with = [
"@bazel_tools//platforms:osx",
"@bazel_tools//platforms:x86_64",
],
platform = "macOS",
)

pandoc_toolchain(
exec_compatible_with = [
"@bazel_tools//platforms:windows",
"@bazel_tools//platforms:x86_32",
],
platform = "windows-i386",
)

pandoc_toolchain(
exec_compatible_with = [
"@bazel_tools//platforms:windows",
"@bazel_tools//platforms:x86_64",
],
platform = "windows-x86_64",
)

# Conversion of README to various formats for testing.

[pandoc(
name = "readme_" + fmt,
src = "README.md",
from_format = "markdown",
to_format = fmt,
) for fmt in [
"asciidoc",
"beamer",
"commonmark",
"context",
"docbook",
"docbook4",
"docbook5",
"docx",
"dokuwiki",
"dzslides",
"epub",
"epub2",
"epub3",
"fb2",
"haddock",
"html",
"html4",
"html5",
"icml",
"jats",
"json",
"latex",
"man",
"markdown",
"markdown_github",
"markdown_mmd",
"markdown_phpextra",
"markdown_strict",
"mediawiki",
"ms",
"muse",
"native",
"odt",
"opendocument",
"opml",
"org",
"plain",
"pptx",
"revealjs",
"rst",
"rtf",
"s5",
"slideous",
"slidy",
"tei",
"texinfo",
"textile",
"zimwiki",
]]
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
workspace(name = "bazel_pandoc")

load("//:repositories.bzl", "pandoc_repositories")

pandoc_repositories()
89 changes: 89 additions & 0 deletions pandoc.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
def _pandoc_impl(ctx):
toolchain = ctx.toolchains["@bazel_pandoc//:pandoc_toolchain_type"]
ctx.actions.run(
executable = toolchain.pandoc.files.to_list()[0].path,
arguments = ctx.attr.options + [
"--from",
ctx.attr.from_format,
"--to",
ctx.attr.to_format,
"-o",
ctx.outputs.out.path,
ctx.files.src[0].path,
],
inputs = toolchain.pandoc.files + ctx.files.src,
outputs = [ctx.outputs.out],
)

_pandoc = rule(
attrs = {
"extension": attr.string(),
"from_format": attr.string(),
"options": attr.string_list(),
"src": attr.label(allow_files = True),
"to_format": attr.string(),
},
outputs = {"out": "%{name}.%{extension}"},
toolchains = ["@bazel_pandoc//:pandoc_toolchain_type"],
implementation = _pandoc_impl,
)

def pandoc(**kwargs):
# Derive extension of the 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.
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",
}
to_format = kwargs["to_format"]
if to_format not in extensions:
fail("Unknown output format: " + to_format)

_pandoc(extension = extensions[to_format], **kwargs)
61 changes: 61 additions & 0 deletions repositories.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

def pandoc_repositories():
http_archive(
name = "pandoc_bin_linux",
build_file_content = """
filegroup(
name = "pandoc",
srcs = ["bin/pandoc"],
visibility = ["//visibility:public"],
)""",
sha256 = "859609cdba5af61aefd7c93d174e412d6a38f5c1be90dfc357158638ff5e7059",
strip_prefix = "pandoc-2.3.1",
url = "https://github.com/jgm/pandoc/releases/download/2.3.1/pandoc-2.3.1-linux.tar.gz",
)

http_archive(
name = "pandoc_bin_macOS",
build_file_content = """
filegroup(
name = "pandoc",
srcs = ["bin/pandoc"],
visibility = ["//visibility:public"],
)""",
sha256 = "bc9ba6f1f4f447deff811554603edcdb13344b07b969151569b6e46e1c8c81b7",
strip_prefix = "pandoc-2.3.1",
url = "https://github.com/jgm/pandoc/releases/download/2.3.1/pandoc-2.3.1-macOS.zip",
)

http_archive(
name = "pandoc_bin_windows-i386",
build_file_content = """
filegroup(
name = "pandoc",
srcs = ["pandoc.exe"],
visibility = ["//visibility:public"],
)""",
sha256 = "4b878dfc094af245621581cf30afbe2eb401dc886f59edbe67356e691a4c72cc",
strip_prefix = "pandoc-2.3.1-windows-i386",
url = "https://github.com/jgm/pandoc/releases/download/2.3.1/pandoc-2.3.1-windows-i386.zip",
)

http_archive(
name = "pandoc_bin_windows-x86_64",
build_file_content = """
filegroup(
name = "pandoc",
srcs = ["pandoc.exe"],
visibility = ["//visibility:public"],
)""",
sha256 = "c84377a6ddb45b149c297af3e37aacaa8f82535c929aa74723f7a75d7d7b15ab",
strip_prefix = "pandoc-2.3.1-windows-x86_64",
url = "https://github.com/jgm/pandoc/releases/download/2.3.1/pandoc-2.3.1-windows-x86_64.zip",
)

native.register_toolchains(
"@bazel_pandoc//:pandoc_toolchain_linux",
"@bazel_pandoc//:pandoc_toolchain_macOS",
"@bazel_pandoc//:pandoc_toolchain_windows-i386",
"@bazel_pandoc//:pandoc_toolchain_windows-x86_64",
)
31 changes: 31 additions & 0 deletions toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def _pandoc_toolchain_info_impl(ctx):
return [
platform_common.ToolchainInfo(
pandoc = ctx.attr.pandoc,
),
]

_pandoc_toolchain_info = rule(
attrs = {
"pandoc": attr.label(
allow_single_file = True,
cfg = "host",
executable = True,
),
},
implementation = _pandoc_toolchain_info_impl,
)

def pandoc_toolchain(platform, exec_compatible_with):
_pandoc_toolchain_info(
name = "pandoc_toolchain_info_%s" % platform,
pandoc = "@pandoc_bin_%s//:pandoc" % platform,
visibility = ["//visibility:public"],
)

native.toolchain(
name = "pandoc_toolchain_%s" % platform,
exec_compatible_with = exec_compatible_with,
toolchain = ":pandoc_toolchain_info_%s" % platform,
toolchain_type = ":pandoc_toolchain_type",
)

0 comments on commit 7f77596

Please sign in to comment.