summaryrefslogtreecommitdiff
path: root/latex
diff options
context:
space:
mode:
authorKlaus Aehlig <klaus.aehlig@huawei.com>2022-11-09 14:53:19 +0100
committerKlaus Aehlig <klaus.aehlig@huawei.com>2022-11-09 15:41:49 +0100
commitfe008d9a8e24e043de00cecc2754908f57d0a00e (patch)
treea43f899bfd366fd3b8bfcd90cf5a794a33d17094 /latex
downloadrules-typesetting-fe008d9a8e24e043de00cecc2754908f57d0a00e.tar.gz
Initial commit
The rules are still very basic; in particular, the latex rules do not support bibtex. Nevertheless, having typesetting rules in that basic form is already useful, especially when creating documents (slides, handbooks, web pages, etc) containing generated files, like sample logs of tools under development. Co-authored-by: Oliver Reiche <oliver.reiche@huawei.com>
Diffstat (limited to 'latex')
-rw-r--r--latex/RULES314
-rw-r--r--latex/TARGETS1
-rwxr-xr-xlatex/latex_runner.sh17
3 files changed, 332 insertions, 0 deletions
diff --git a/latex/RULES b/latex/RULES
new file mode 100644
index 0000000..89683a9
--- /dev/null
+++ b/latex/RULES
@@ -0,0 +1,314 @@
+{ "standalone":
+ { "doc":
+ [ "A standalone latex run"
+ , ""
+ , "Define a pdf file as the output of running pdflatex. A correct staging"
+ , "of the needed files has to be given through the runfiles of \"deps\"."
+ ]
+ , "target_fields": ["deps", "srcs"]
+ , "string_fields": ["main", "stage"]
+ , "field_doc":
+ { "srcs": ["The files needed for the latex run."]
+ , "stage":
+ [ "The directory the \"srcs\" logically reside in."
+ , "Entries are joined with \"/\"."
+ ]
+ , "deps":
+ ["Runfiles needed for the run of the standalone latex invocation"]
+ , "main":
+ [ "The entry point for the latex run; should be the base name"
+ , "of a .tex file in \"srcs\"; the \"stage\" is prepended automatically."
+ ]
+ }
+ , "config_fields": ["env", "latex"]
+ , "config_doc":
+ { "latex": ["Name of the latex command, defaults to \"pdflatex\"."]
+ , "env":
+ [ "Any override to the default environment which sets only"
+ , "PATH and SOURCE_DATE_EPOCH"
+ ]
+ }
+ , "implicit": {"runner": ["latex_runner.sh"]}
+ , "imports": {"singleton": ["./", "..", "stage_singleton_field"]}
+ , "expression":
+ { "type": "let*"
+ , "bindings":
+ [ [ "stage"
+ , { "type": "join"
+ , "separator": "/"
+ , "$1": {"type": "FIELD", "name": "stage"}
+ }
+ ]
+ , [ "srcs"
+ , { "type": "disjoint_map_union"
+ , "msg": "Sources may not conflict"
+ , "$1":
+ { "type": "foreach"
+ , "var": "x"
+ , "range": {"type": "FIELD", "name": "srcs"}
+ , "body":
+ { "type": "map_union"
+ , "$1":
+ [ {"type": "DEP_RUNFILES", "dep": {"type": "var", "name": "x"}}
+ , { "type": "DEP_ARTIFACTS"
+ , "dep": {"type": "var", "name": "x"}
+ }
+ ]
+ }
+ }
+ }
+ ]
+ , [ "main"
+ , { "type": "assert_non_empty"
+ , "msg": "Entry-point main cannot be empty"
+ , "$1": {"type": "join", "$1": {"type": "FIELD", "name": "main"}}
+ }
+ ]
+ , [ "_"
+ , { "type": "if"
+ , "cond":
+ { "type": "=="
+ , "$1":
+ { "type": "lookup"
+ , "map": {"type": "var", "name": "srcs"}
+ , "key":
+ { "type": "join"
+ , "$1": [{"type": "var", "name": "main"}, ".tex"]
+ }
+ }
+ , "$2": null
+ }
+ , "then":
+ { "type": "fail"
+ , "msg":
+ [ "main not the base name of a .tex file in srcs"
+ , "main has value"
+ , {"type": "var", "name": "main"}
+ , "srcs have file names"
+ , {"type": "keys", "$1": {"type": "var", "name": "srcs"}}
+ ]
+ }
+ }
+ ]
+ , [ "main"
+ , { "type": "if"
+ , "cond":
+ {"type": "==", "$1": {"type": "var", "name": "stage"}, "$2": ""}
+ , "then": {"type": "var", "name": "main"}
+ , "else":
+ { "type": "join"
+ , "separator": "/"
+ , "$1":
+ [ {"type": "var", "name": "stage"}
+ , {"type": "var", "name": "main"}
+ ]
+ }
+ }
+ ]
+ , [ "srcs"
+ , { "type": "to_subdir"
+ , "subdir": {"type": "var", "name": "stage"}
+ , "$1": {"type": "var", "name": "srcs"}
+ }
+ ]
+ , [ "deps"
+ , { "type": "disjoint_map_union"
+ , "msg": "Dependencies may not conflict"
+ , "$1":
+ { "type": "foreach"
+ , "var": "x"
+ , "range": {"type": "FIELD", "name": "deps"}
+ , "body":
+ {"type": "DEP_RUNFILES", "dep": {"type": "var", "name": "x"}}
+ }
+ }
+ ]
+ , [ "tex inputs"
+ , { "type": "to_subdir"
+ , "subdir": "work"
+ , "$1":
+ { "type": "disjoint_map_union"
+ , "msg": "Staging conflict between staged sources and deps"
+ , "$1":
+ [ {"type": "var", "name": "deps"}
+ , {"type": "var", "name": "srcs"}
+ ]
+ }
+ }
+ ]
+ , [ "env"
+ , { "type": "map_union"
+ , "$1":
+ [ { "type": "singleton_map"
+ , "key": "PATH"
+ , "value": "/bin:/usr/bin:/usr/local/bin"
+ }
+ , { "type": "singleton_map"
+ , "key": "SOURCE_DATE_EPOCH"
+ , "value": "0"
+ }
+ , {"type": "var", "name": "env", "default": {"type": "empty_map"}}
+ ]
+ }
+ ]
+ , [ "runner"
+ , { "type": "let*"
+ , "bindings": [["fieldname", "runner"], ["location", "runner"]]
+ , "body": {"type": "CALL_EXPRESSION", "name": "singleton"}
+ }
+ ]
+ , [ "cmd"
+ , [ "./runner"
+ , {"type": "var", "name": "latex", "default": "pdflatex"}
+ , {"type": "var", "name": "main"}
+ ]
+ ]
+ , [ "pdf"
+ , { "type": "ACTION"
+ , "inputs":
+ { "type": "map_union"
+ , "$1":
+ [ {"type": "var", "name": "tex inputs"}
+ , {"type": "var", "name": "runner"}
+ ]
+ }
+ , "outs":
+ [ { "type": "join"
+ , "$1": ["work/", {"type": "var", "name": "main"}, ".pdf"]
+ }
+ ]
+ , "cmd": {"type": "var", "name": "cmd"}
+ , "env": {"type": "var", "name": "env"}
+ }
+ ]
+ , [ "pdf"
+ , { "type": "map_union"
+ , "$1":
+ { "type": "foreach_map"
+ , "var_key": "name"
+ , "var_val": "file"
+ , "range": {"type": "var", "name": "pdf"}
+ , "body":
+ { "type": "singleton_map"
+ , "key":
+ {"type": "basename", "$1": {"type": "var", "name": "name"}}
+ , "value": {"type": "var", "name": "file"}
+ }
+ }
+ }
+ ]
+ ]
+ , "body":
+ { "type": "RESULT"
+ , "artifacts": {"type": "var", "name": "pdf"}
+ , "runfiles": {"type": "var", "name": "pdf"}
+ }
+ }
+ }
+, "verbatim":
+ { "doc": ["Add \\begin{verbatim}...\\end{verbatim} around files"]
+ , "target_fields": ["srcs"]
+ , "string_fields": ["prefix", "stage"]
+ , "field_doc":
+ { "srcs":
+ [ "The files to transform to tex files containing them verbatim."
+ , "In the output file, the ending is changed to .tex"
+ ]
+ , "prefix": ["Additional prefix to add to the output file name"]
+ , "stage": ["The logical directory to stage the outputs to"]
+ }
+ , "expression":
+ { "type": "let*"
+ , "bindings":
+ [ [ "srcs"
+ , { "type": "disjoint_map_union"
+ , "msg": "Source files must not overlap, as this would lead to overlapping output"
+ , "$1":
+ { "type": "foreach"
+ , "var": "src"
+ , "range": {"type": "FIELD", "name": "srcs"}
+ , "body":
+ {"type": "DEP_ARTIFACTS", "dep": {"type": "var", "name": "src"}}
+ }
+ }
+ ]
+ , [ "verbatim files"
+ , { "type": "disjoint_map_union"
+ , "msg":
+ [ "Outputfiles must not overlap"
+ , "Input file names:"
+ , {"type": "keys", "map": {"type": "var", "name": "srcs"}}
+ ]
+ , "$1":
+ { "type": "foreach_map"
+ , "var_key": "src"
+ , "var_val": "input"
+ , "range": {"type": "var", "name": "srcs"}
+ , "body":
+ { "type": "let*"
+ , "bindings":
+ [ [ "action out"
+ , { "type": "ACTION"
+ , "inputs":
+ { "type": "singleton_map"
+ , "key": "in"
+ , "value": {"type": "var", "name": "input"}
+ }
+ , "outs": ["out"]
+ , "cmd":
+ [ "sh"
+ , "-c"
+ , "echo '\\\\begin{verbatim}' > out && cat in >> out && echo '\\\\end{verbatim}' >> out"
+ ]
+ }
+ ]
+ , [ "out"
+ , { "type": "lookup"
+ , "map": {"type": "var", "name": "action out"}
+ , "key": "out"
+ }
+ ]
+ , [ "out name"
+ , { "type": "join"
+ , "$1":
+ [ { "type": "join"
+ , "$1": {"type": "FIELD", "name": "prefix"}
+ }
+ , { "type": "change_ending"
+ , "ending": ".tex"
+ , "$1": {"type": "var", "name": "src"}
+ }
+ ]
+ }
+ ]
+ ]
+ , "body":
+ { "type": "singleton_map"
+ , "key": {"type": "var", "name": "out name"}
+ , "value": {"type": "var", "name": "out"}
+ }
+ }
+ }
+ }
+ ]
+ , [ "stage"
+ , { "type": "join"
+ , "separator": "/"
+ , "$1": {"type": "FIELD", "name": "stage"}
+ }
+ ]
+ , [ "staged verbatim files"
+ , { "type": "to_subdir"
+ , "subdir": {"type": "var", "name": "stage"}
+ , "$1": {"type": "var", "name": "verbatim files"}
+ }
+ ]
+ ]
+ , "body":
+ { "type": "RESULT"
+ , "artifacts": {"type": "var", "name": "staged verbatim files"}
+ , "runfiles": {"type": "var", "name": "staged verbatim files"}
+ }
+ }
+ }
+}
diff --git a/latex/TARGETS b/latex/TARGETS
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/latex/TARGETS
@@ -0,0 +1 @@
+{}
diff --git a/latex/latex_runner.sh b/latex/latex_runner.sh
new file mode 100755
index 0000000..69f63cd
--- /dev/null
+++ b/latex/latex_runner.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+PDFLATEX=$1
+shift
+MAIN=$1
+shift
+
+cd work
+echo First latex run >> ../log
+$PDFLATEX $MAIN >> ../log 2>&1 || { cat ../log ; exit 1; }
+echo Second latex run >> ../log
+$PDFLATEX $MAIN >> ../log 2>&1 || { cat ../log ; exit 1; }
+echo Third latex run >> ../log
+$PDFLATEX $MAIN >> ../log 2>&1 || { cat ../log ; exit 1; }
+[ -f $MAIN.pdf ] \
+ || ( [ -f $(basename $MAIN).pdf ] && cp $(basename $MAIN).pdf $MAIN.pdf ) \
+ || { cat ../log ; exit 1; }