From 1128c8fb69d4a8557baeee13053e7792339554f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Mon, 3 Jul 2023 22:03:00 +0200 Subject: [PATCH] Bundle pandoc filters (#670) Continuation of https://github.com/NixOS/nixos-search/pull/669 --- .../citerefentry-to-rst-role.lua | 23 ++++++++++++ flake-info/assets/data/myst-reader/roles.lua | 36 +++++++++++++++++++ flake-info/default.nix | 1 - flake-info/src/data/pandoc.rs | 9 ++--- 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 flake-info/assets/data/docbook-reader/citerefentry-to-rst-role.lua create mode 100644 flake-info/assets/data/myst-reader/roles.lua diff --git a/flake-info/assets/data/docbook-reader/citerefentry-to-rst-role.lua b/flake-info/assets/data/docbook-reader/citerefentry-to-rst-role.lua new file mode 100644 index 0000000..281e85a --- /dev/null +++ b/flake-info/assets/data/docbook-reader/citerefentry-to-rst-role.lua @@ -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 diff --git a/flake-info/assets/data/myst-reader/roles.lua b/flake-info/assets/data/myst-reader/roles.lua new file mode 100644 index 0000000..f4ef6d3 --- /dev/null +++ b/flake-info/assets/data/myst-reader/roles.lua @@ -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 diff --git a/flake-info/default.nix b/flake-info/default.nix index c93e010..6d6461a 100644 --- a/flake-info/default.nix +++ b/flake-info/default.nix @@ -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 = [ diff --git a/flake-info/src/data/pandoc.rs b/flake-info/src/data/pandoc.rs index d533788..c43f315 100644 --- a/flake-info/src/data/pandoc.rs +++ b/flake-info/src/data/pandoc.rs @@ -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"); }