pandoc / lua-filters

A collection of lua filters for pandoc
MIT License
602 stars 165 forks source link

pikchr support #212

Closed jasongibson closed 1 year ago

jasongibson commented 2 years ago

pikchr is a simple markup language for creating graphical diagrams with a nice standalone program to render. It'd be useful to have support for inline pikchr documents in Pandoc.

Here's a diff (as of c40210b658db9f0f958c0b6c280507fc8d7de46b) that implements that:

diff --git a/diagram-generator/README.md b/diagram-generator/README.md
index d04e204..97e00ae 100644
--- a/diagram-generator/README.md
+++ b/diagram-generator/README.md
@@ -11,6 +11,10 @@ To be able to use this Lua filter, the respective external tools must be
 installed. However, it is sufficient if the tools to be used are installed.
 If you only want to use PlantUML, you don't need LaTeX or Python, etc.

+### Pikchr
+To use pikchr, you must install pikchr itself. See the
+[pikchr website](http://pikchr.org/) for more details.
+
 ### PlantUML
 To use PlantUML, you must install PlantUML itself. See the
 [PlantUML website](http://plantuml.com/) for more details. It should be
@@ -233,6 +237,7 @@ pandoc.exe README.md -f markdown -t docx --self-contained --standalone --lua-fil

 All available environment variables:

+- `PIKCHR` e.g. `c:\bin\pikchr.exe`; Default: `pikchr`
 - `PLANTUML` e.g. `c:\ProgramData\chocolatey\lib\plantuml\tools\plantuml.jar`; Default: `plantuml.jar`
 - `INKSCAPE` e.g. `c:\Program Files\Inkscape\inkscape.exe`; Default: `inkscape`
 - `PYTHON` e.g. `c:\ProgramData\Anaconda3\python.exe`; Default: n/a
@@ -243,6 +248,7 @@ All available environment variables:

 All available meta keys:

+- `pikchrPath`
 - `plantumlPath`
 - `inkscapePath`
 - `pythonPath`
diff --git a/diagram-generator/diagram-generator.lua b/diagram-generator/diagram-generator.lua
index 19e36bc..7e0bbcc 100644
--- a/diagram-generator/diagram-generator.lua
+++ b/diagram-generator/diagram-generator.lua
@@ -22,6 +22,12 @@ end
 local with_temporary_directory = system.with_temporary_directory
 local with_working_directory = system.with_working_directory

+-- The pikchr path. If set, uses the environment variable PIKCHR or the
+-- value "pikchr" (local pikchr version). In order to define a
+-- pikchr version per pandoc document, use the meta data to define the key
+-- "pikchr_path".
+local pikchr_path = os.getenv("PIKCHR") or "pikchr"
+
 -- The PlantUML path. If set, uses the environment variable PLANTUML or the
 -- value "plantuml.jar" (local PlantUML version). In order to define a
 -- PlantUML version per pandoc document, use the meta data to define the key
@@ -85,6 +91,9 @@ end
 -- meta options was set, it gets used instead of the corresponding
 -- environment variable:
 function Meta(meta)
+  pikchr_path = stringify(
+    meta.pikchr_path or meta.pikchrPath or pikchr_path
+  )
   plantuml_path = stringify(
     meta.plantuml_path or meta.plantumlPath or plantuml_path
   )
@@ -111,6 +120,11 @@ function Meta(meta)
   )
 end

+-- Call pikchr with some parameters.
+local function pikchr(code, filetype)
+  return pandoc.pipe(pikchr_path, {"--svg-only", "-"}, code)
+end
+
 -- Call plantuml.jar with some parameters (cf. PlantUML help):
 local function plantuml(puml, filetype)
   return pandoc.pipe(
@@ -303,6 +317,7 @@ end
 function CodeBlock(block)
   -- Using a table with all known generators i.e. converters:
   local converters = {
+    pikchr = pikchr,
     plantuml = plantuml,
     graphviz = graphviz,
     tikz = tikz2image,
tarleb commented 1 year ago

I've moved the diagram generator to https://github.com/pandoc-ext/diagram and am maintaining it there. I'm closing the issue here, but please do feel free to re-raise it in the new repo.

I decided to wait with pikchr support simply because I want to test all generators in CI and it isn't in the official repos of either Ubuntu or Alpine Linux. I'm open to adding it if someone wants to provide the necessary installation routine.

Note: there's also https://code.jboy.space/pikchr-filter/dir?ci=tip.

Closing this here.