Skip to content

Instantly share code, notes, and snippets.

@mikeslattery
Last active June 17, 2026 19:06
Show Gist options
  • Select an option

  • Save mikeslattery/453cd33f9ad1c12b83302ee0d9bd0d12 to your computer and use it in GitHub Desktop.

Select an option

Save mikeslattery/453cd33f9ad1c12b83302ee0d9bd0d12 to your computer and use it in GitHub Desktop.
Podman extensions

File: Makefile

SOURCE_DOCS := $(shell find doc -type f -name '*.md')
EXT := docx
TARGET_DOCS = $(patsubst doc/%.md, target/%.$(EXT), $(SOURCE_DOCS))
GIT_LS_FILES := $(shell git ls-files)

.PHONY: all clean

all: $(TARGET_DOCS)
	git ls-files | xargs -r ec

target/%.$(EXT): doc/%.md target pandoc/defaults.yaml
	command pandoc --defaults=pandoc/defaults.yaml -i $< -o $@

target:
	mkdir target

clean:
	rm -rf target/

# See also
# https://tylercipriani.com/blog/2014/05/13/replace-jekyll-with-pandoc-makefile/

File: pandoc/defaults.yaml

# Usage: pandoc --defaults=pandoc/defaults.yaml -i srs.md

filters:
  - pandoc/diagram-generator.lua
  - pandoc/pagebreak.lua

syntax-definitions:
  # https://github.com/jayrepo/pandoc-gherkin-syntax
  - pandoc/gherkin.xml

standalone: true
toc-depth: 2

File: pandoc/diagram-generator.lua

--[[
Pandoc Lua Filter to support plantuml and graphviz dot diagrams.

Supports all formats.  Html will inline svg format.

Usage:
pandoc <input-file>.md --lua-filter=<this-file.lua> -o <output.html>

curl -L -o ~/bin/plant_erd https://github.com/sue445/plant_erd/releases/download/v0.2.0/plant_erd_linux_amd64
chmod +x ~/bin/plant_erd
]]
-- Module pandoc.system is required and was added in version 2.7.3
PANDOC_VERSION:must_be_at_least '2.7.3'

local system = require 'pandoc.system'
local utils = require 'pandoc.utils'
local stringify = utils.stringify
local with_temporary_directory = system.with_temporary_directory
local with_working_directory = system.with_working_directory

function CodeBlock(block)
  if block.classes[1] == "plantuml" then
    if FORMAT == "html" then
      local svg = pandoc.pipe('plantuml', {'-tsvg', '-pipe'}, block.text)
      return pandoc.Div(pandoc.RawBlock('html', svg))
    else
      local fname = "pandoc-" .. pandoc.sha1(block.text) .. ".png"
      local img = pandoc.pipe('plantuml', {"-tpng", "-pipe"}, block.text)
      if FORMAT == "markdown_github" then
        local f = io.open(fname, "w")
        f:write(img)
        f:close()
      else
        pandoc.mediabag.insert(fname, 'image/png', img)
      end
      local attr = {}
      if block.attributes.width then
        attr.width = block.attributes.width
      end
      return pandoc.Para({ pandoc.Image({}, fname, '', attr) })
    end
  end

  if block.classes[1] == "graphviz" then
    if FORMAT == "html" then
      local svg = pandoc.pipe('dot', {'-Tsvg'}, block.text)
      return pandoc.Div(pandoc.RawBlock('html', svg))
    else
      local fname = "pandoc-" .. pandoc.sha1(block.text) .. ".png"
      if FORMAT == "markdown_github" then
        -- some formats need the png to be persistent
        pandoc.pipe('dot', {"-Tpng", "-o" .. fname}, block.text)
      else
        local img = pandoc.pipe('dot', {"-Tpng"}, block.text)
        pandoc.mediabag.insert(fname, 'image/png', img)
      end
      local attr = {}
      if block.attributes.width then
        attr.width = block.attributes.width
      end
      return pandoc.Para({ pandoc.Image({}, fname, '', attr) })
    end
  end

  -- converts SQL to a plantuml ERD
  if block.classes[1] == "sql2erd" then
    os.remove("/tmp/pandoc.db")
    pandoc.pipe('sqlite3', {'/tmp/pandoc.db'}, block.text)
    local puml = pandoc.pipe('plant_erd', {'sqlite3', '--database', '/tmp/pandoc.db'}, '')
    os.remove("/tmp/pandoc.db")
    if FORMAT == "html" then
      local svg = pandoc.pipe('plantuml', {'-tsvg', '-pipe'}, puml)
      return pandoc.Div(pandoc.RawBlock('html', svg))
    else
      local fname = "pandoc-" .. pandoc.sha1(block.text) .. ".png"
      if FORMAT == "gfm" or FORMAT == "markdown_github" then
        local f = io.open(fname, "w")
        f:write(img)
        f:close()
      else
        local img = pandoc.pipe('plantuml', {"-tpng", "-pipe"}, puml)
        pandoc.mediabag.insert(fname, 'image/png', img)
      end
      return pandoc.Para({ pandoc.Image({}, fname) })
    end
  end

  -- convert SQL DDL and select query to markdown table
  if block.classes[1] == "sql-table" then
    local md = pandoc.pipe('sqlite3', {'-markdown'}, block.text)
    return pandoc.read(md).blocks
  end

  -- run script that returns markdown and render
  -- This doesn't pass the script as a file, but rather streams it.
  if block.classes[1] == "script" then
    local shebang = string.match(block.text, "^#![^\n]+\n")
    local command = 'bash'

    if shebang then
      command = string.sub(shebang, 3, -2)
    end

    local output = pandoc.pipe(command, {}, block.text)

    return pandoc.read(output).blocks
  end

  if block.classes[1] == "empty" then
    return pandoc.Para({})
  end

end

function Header(block)
  -- page break before every top level heading
  if block.level == 1 and FORMAT == "latex" then
    --TODO: use table.insert(block.content, 1, pandoc.RawBlock...
    return pandoc.Div({pandoc.RawBlock('latex', '\\newpage'), block})
  end
end

File: pandoc/gherkin.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">
<language name="Gherkin" version="1.0" kateversion="2.4" section="Other" extensions="*.feature;*.FEATURE" mimetype="text/x-gherkin" casesensitive="1" author="jayrepo" license="GPL">
  <highlighting>
    <contexts>
      <context  attribute="Normal Text" lineEndContext="#stay" name="normalText">
        <DetectSpaces />
        <RegExpr attribute="Keyword" context="feature" String="(Feature|Example|Scenario|Scenario Outline|Background|Combinations|Examples|Rule):" />
        <RegExpr attribute="Keyword" context="step" String="(Given|When|Then|And|But)" />
        <DetectChar attribute="Tag" char="@" firstNonSpace="true" context="tag" />
        <DetectChar attribute="Keyword" char="|" firstNonSpace="true" context="table" />
        <DetectChar attribute="Comment" context="comment" char="#" firstNonSpace="true" />
        <StringDetect attribute="String" String="&quot;&quot;&quot;" context="multilinestring" />
      </context>
      <context attribute="Function" name="feature" lineEndContext="#pop">
        <DetectSpaces attribute="Normal Text"/>
      </context>
      <context attribute="Normal Text" name="step" lineEndContext="#pop">
        <Float attribute="Float" context="#stay"/>
        <Int attribute="Decimal" context="#stay"/>
        <RangeDetect char="&lt;" char1="&gt;" attribute="Placeholder" context="#stay" />
        <RangeDetect char="'" char1="'" attribute="Placeholder" context="#stay" />
        <RangeDetect char="&quot;" char1="&quot;" attribute="Placeholder" context="#stay" />
      </context>
      <context attribute="Tag" lineEndContext="#pop" name="tag" >
        <DetectSpaces attribute="Normal Text"/>
        <DetectChar attribute="Tag" char="@" context="tag" />
      </context>
      <context attribute="String" lineEndContext="#stay" name="multilinestring">
        <StringDetect String="&quot;&quot;&quot;" attribute="String" context="#pop" />
      </context>
      <context attribute="Placeholder" lineEndContext="#pop" name="table">
        <DetectSpaces attribute="Normal Text"/>
        <RegExpr attribute="Placeholder" context="table" String="[^|\s]+"/>
        <DetectChar char="|" attribute="Keyword" context="table" />
      </context>
      <context attribute="Comment" lineEndContext="#pop" name="comment" />

    </contexts>
    <itemDatas>
      <itemData name="Tag"               defStyleNum="dsAnnotation"/>
      <itemData name="Normal Text"       defStyleNum="dsNormal"/>
      <itemData name="Keyword"           defStyleNum="dsKeyword"/>
      <itemData name="Function"          defStyleNum="dsFunction"/>
      <itemData name="Decimal"           defStyleNum="dsDecVal"/>
      <itemData name="Float"             defStyleNum="dsFloat"/>
      <itemData name="Placeholder"       defStyleNum="dsString"/>
      <itemData name="String"            defStyleNum="dsString"/>
      <itemData name="Comment"           defStyleNum="dsComment"/>
    </itemDatas>
  </highlighting>
  <general>
    <comments>
      <comment name="singleLine" start="#"/>
    </comments>
    <keywords casesensitive="1" weakDeliminator=":"/>
  </general>
</language>

File: pandoc/no-cb-indent.lua

function CodeBlock(block)
  -- never indent blocks
  if len(#block.classes) == 0 then
    block.classes = {""}
  end
  return block
end

File: pandoc/pagebreak.lua

--[[
pagebreak – convert raw LaTeX page breaks to other formats

Copyright © 2017-2020 Benct Philip Jonsson, Albert Krewinkel

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Source:
https://github.com/benmarwick/rrtools/blob/master/inst/templates/pagebreak.lua

Usage:
\newpage

]]
local stringify_orig = (require 'pandoc.utils').stringify

local function stringify(x)
  return type(x) == 'string' and x or stringify_orig(x)
end

--- configs – these are populated in the Meta filter.
local pagebreak = {
  asciidoc = '<<<\n\n',
  context = '\\page',
  epub = '<p style="page-break-after: always;"> </p>',
  html = '<div style="page-break-after: always;"></div>',
  latex = '\\newpage{}',
  ms = '.bp',
  ooxml = '<w:p><w:r><w:br w:type="page"/></w:r></w:p>',
  odt = '<text:p text:style-name="Pagebreak"/>'
}

local function pagebreaks_from_config (meta)
  local html_class =
    (meta.newpage_html_class and stringify(meta.newpage_html_class))
    or os.getenv 'PANDOC_NEWPAGE_HTML_CLASS'
  if html_class and html_class ~= '' then
    pagebreak.html = string.format('<div class="%s"></div>', html_class)
  end

  local odt_style =
    (meta.newpage_odt_style and stringify(meta.newpage_odt_style))
    or os.getenv 'PANDOC_NEWPAGE_ODT_STYLE'
  if odt_style and odt_style ~= '' then
    pagebreak.odt = string.format('<text:p text:style-name="%s"/>', odt_style)
  end
end

--- Return a block element causing a page break in the given format.
local function newpage(format)
  if format:match 'asciidoc' then
    return pandoc.RawBlock('asciidoc', pagebreak.asciidoc)
  elseif format == 'context' then
    return pandoc.RawBlock('context', pagebreak.context)
  elseif format == 'docx' then
    return pandoc.RawBlock('openxml', pagebreak.ooxml)
  elseif format:match 'epub' then
    return pandoc.RawBlock('html', pagebreak.epub)
  elseif format:match 'html.*' then
    return pandoc.RawBlock('html', pagebreak.html)
  elseif format:match 'latex' then
    return pandoc.RawBlock('tex', pagebreak.latex)
  elseif format:match 'ms' then
    return pandoc.RawBlock('ms', pagebreak.ms)
  elseif format:match 'odt' then
    return pandoc.RawBlock('opendocument', pagebreak.odt)
  else
    -- fall back to insert a form feed character
    return pandoc.Para{pandoc.Str '\f'}
  end
end

local function is_newpage_command(command)
  return command:match '^\\newpage%{?%}?$'
    or command:match '^\\pagebreak%{?%}?$'
end

-- Filter function called on each RawBlock element.
function RawBlock (el)
  -- Don't do anything if the output is TeX
  if FORMAT:match 'tex$' then
    return nil
  end
  -- check that the block is TeX or LaTeX and contains only
  -- \newpage or \pagebreak.
  if el.format:match 'tex' and is_newpage_command(el.text) then
    -- use format-specific pagebreak marker. FORMAT is set by pandoc to
    -- the targeted output format.
    return newpage(FORMAT)
  end
  -- otherwise, leave the block unchanged
  return nil
end

-- Turning paragraphs which contain nothing but a form feed
-- characters into line breaks.
function Para (el)
  if #el.content == 1 and el.content[1].text == '\f' then
    return newpage(FORMAT)
  end
end

return {
  {Meta = pagebreaks_from_config},
  {RawBlock = RawBlock, Para = Para}
}

File: scripts/pandoc-setup.sh

#!/bin/bash

# Installs dependencies needed for markdown -> pdf generation.

set -eu

. /etc/os-release

case "$ID" in
    fedora)
        dnf -y install texlive-scheme-medium --setopt=install_weak_deps=false
        dnf -y install pandoc graphviz plantuml unzip curl podman

        if ! command -v docker &>/dev/null; then
            ln -sfn "$(command -v podman)" /usr/local/bin/docker
        fi
        ;;
    *)
        echo "Unsupported distro: $ID"
        exit 1
        ;;
esac

# TODO: plant_erd 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment