From 194ba6d4e03efd2ad7b5a736e3579e7970677127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Fri, 31 Dec 2021 00:29:13 +0100 Subject: [PATCH] Fix xrefs (#402) --- VERSION | 2 +- flake-info/src/data/fix-xrefs.lua | 21 +++++++++++++++++++++ flake-info/src/data/pandoc.rs | 9 +++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 flake-info/src/data/fix-xrefs.lua diff --git a/VERSION b/VERSION index 7273c0f..6f4247a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -25 +26 diff --git a/flake-info/src/data/fix-xrefs.lua b/flake-info/src/data/fix-xrefs.lua new file mode 100644 index 0000000..1761e0c --- /dev/null +++ b/flake-info/src/data/fix-xrefs.lua @@ -0,0 +1,21 @@ +--[[ +Resolve cross-references to NixOS options in a hacky way and link them to the +unstable channel's option search page on search.nixos.org +]] + +function Link(elem) + prefix = '#opt-' + if elem.target:sub(1, #prefix) == prefix then + option_name = elem.target:sub(#prefix + 1) + option_name = option_name:gsub('%._name_%.', '..') + option_name = option_name:gsub('%._%.', '.*.') + + elem.target = 'https://search.nixos.org/options?channel=unstable&show=' .. option_name .. '&query=' .. option_name + + if #elem.content == 1 and elem.content[1].tag == 'Str' and elem.content[1].text == '???' then + elem.content[1].text = option_name + end + + return elem + end +end diff --git a/flake-info/src/data/pandoc.rs b/flake-info/src/data/pandoc.rs index a80b544..5314914 100644 --- a/flake-info/src/data/pandoc.rs +++ b/flake-info/src/data/pandoc.rs @@ -1,3 +1,5 @@ +use std::fs::File; +use std::io::Write; use std::path::PathBuf; use lazy_static::lazy_static; @@ -6,6 +8,8 @@ use pandoc::{ InputFormat, InputKind, OutputFormat, OutputKind, PandocError, PandocOption, PandocOutput, }; +const XREF_FILTER: &str = include_str!("fix-xrefs.lua"); + lazy_static! { static ref FILTERS_PATH: PathBuf = std::env::var("NIXPKGS_PANDOC_FILTERS_PATH") .unwrap_or("".into()) @@ -35,6 +39,10 @@ impl> PandocExt for T { p.push("link-unix-man-references.lua"); p }; + let tmpdir = tempfile::tempdir()?; + let xref_filter = tmpdir.path().join("fix-xrefs.lua"); + writeln!(File::create(&xref_filter)?, "{}", XREF_FILTER)?; + let mut pandoc = pandoc::new(); let wrapper_xml = format!( " @@ -52,6 +60,7 @@ impl> PandocExt for T { pandoc.add_options(&[ PandocOption::LuaFilter(citeref_filter), PandocOption::LuaFilter(man_filter), + PandocOption::LuaFilter(xref_filter), ]); pandoc.execute().map(|result| match result {