Skip to content

Commit

Permalink
Bundle pandoc filters (#670)
Browse files Browse the repository at this point in the history
Continuation of #669
  • Loading branch information
ncfavier authored Jul 3, 2023
1 parent 67bac7f commit 1128c8f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
23 changes: 23 additions & 0 deletions flake-info/assets/data/docbook-reader/citerefentry-to-rst-role.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--[[
Converts Code AST nodes produced by pandoc’s DocBook reader
from citerefentry elements into AST for corresponding role
for reStructuredText.
We use subset of MyST syntax (CommonMark with features from rST)
so let’s use the rST AST for rST features.
Reference: https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#role-manpage
]]

function Code(elem)
elem.classes = elem.classes:map(function (x)
if x == 'citerefentry' then
elem.attributes['role'] = 'manpage'
return 'interpreted-text'
else
return x
end
end)

return elem
end
36 changes: 36 additions & 0 deletions flake-info/assets/data/myst-reader/roles.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--[[
Replaces Str AST nodes containing {role}, followed by a Code node
by a Code node with attrs that would be produced by rST reader
from the role syntax.
This is to emulate MyST syntax in Pandoc.
(MyST is a CommonMark flavour with rST features mixed in.)
Reference: https://myst-parser.readthedocs.io/en/latest/syntax/syntax.html#roles-an-in-line-extension-point
]]

function Inlines(inlines)
for i = #inlines-1,1,-1 do
local first = inlines[i]
local second = inlines[i+1]
local correct_tags = first.tag == 'Str' and second.tag == 'Code'
if correct_tags then
-- docutils supports alphanumeric strings separated by [-._:]
-- We are slightly more liberal for simplicity.
-- Allow preceding punctuation (eg '('), otherwise '({file}`...`)'
-- does not match. Also allow anything followed by a non-breaking space
-- since pandoc emits those after certain abbreviations (e.g. e.g.).
local prefix, role = first.text:match('^(.*){([-._+:%w]+)}$')
if role ~= nil and (prefix == '' or prefix:match("^.*[%p ]$") ~= nil) then
if prefix == '' then
inlines:remove(i)
else
first.text = prefix
end
second.attributes['role'] = role
second.classes:insert('interpreted-text')
end
end
end
return inlines
end
1 change: 0 additions & 1 deletion flake-info/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pkgs.rustPlatform.buildRustPackage rec {
checkInputs = with pkgs; [ pandoc ];

ROOTDIR = builtins.placeholder "out";
NIXPKGS_PANDOC_FILTERS_PATH = pkgs.path + "/doc/build-aux/pandoc-filters";
LINK_MANPAGES_PANDOC_FILTER = import src/data/link-manpages.nix { inherit pkgs; };

checkFlags = [
Expand Down
9 changes: 3 additions & 6 deletions flake-info/src/data/pandoc.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use lazy_static::lazy_static;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

use pandoc::*;

const FILTERS_PATH: &str = env!("NIXPKGS_PANDOC_FILTERS_PATH");

lazy_static! {
static ref DOCBOOK_ROLES_FILTER: PathBuf =
Path::new(FILTERS_PATH).join("docbook-reader/citerefentry-to-rst-role.lua");
static ref MARKDOWN_ROLES_FILTER: PathBuf =
Path::new(FILTERS_PATH).join("myst-reader/roles.lua");
crate::DATADIR.join("data/docbook-reader/citerefentry-to-rst-role.lua");
static ref MARKDOWN_ROLES_FILTER: PathBuf = crate::DATADIR.join("data/myst-reader/roles.lua");
static ref MANPAGE_LINK_FILTER: PathBuf = PathBuf::from(env!("LINK_MANPAGES_PANDOC_FILTER"));
static ref XREF_FILTER: PathBuf = crate::DATADIR.join("data/fix-xrefs.lua");
}
Expand Down

0 comments on commit 1128c8f

Please sign in to comment.