1#! /usr/bin/env nix-shell
2#! nix-shell -I nixpkgs=channel:nixpkgs-unstable -i bash -p pandoc
3
4# This script is temporarily needed while we transition the manual to
5# CommonMark. It converts the .md files in the regular manual folder
6# into DocBook files in the from_md folder.
7
8DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
9pushd "$DIR"
10
11# NOTE: Keep in sync with Nixpkgs manual (/doc/Makefile).
12# TODO: Remove raw-attribute when we can get rid of DocBook altogether.
13pandoc_commonmark_enabled_extensions=+attributes+fenced_divs+footnotes+bracketed_spans+definition_lists+pipe_tables+raw_attribute
14pandoc_flags=(
15 # Not needed:
16 # - diagram-generator.lua (we do not support that in NixOS manual to limit dependencies)
17 # - media extraction (was only required for diagram generator)
18 # - docbook-reader/citerefentry-to-rst-role.lua (only relevant for DocBook → MarkDown/rST/MyST)
19 "--lua-filter=$DIR/../../../doc/build-aux/pandoc-filters/myst-reader/roles.lua"
20 "--lua-filter=$DIR/../../../doc/build-aux/pandoc-filters/link-unix-man-references.lua"
21 "--lua-filter=$DIR/../../../doc/build-aux/pandoc-filters/docbook-writer/rst-roles.lua"
22 "--lua-filter=$DIR/../../../doc/build-aux/pandoc-filters/docbook-writer/labelless-link-is-xref.lua"
23 -f "commonmark${pandoc_commonmark_enabled_extensions}+smart"
24 -t docbook
25)
26
27OUT="$DIR/from_md"
28mapfile -t MD_FILES < <(find . -type f -regex '.*\.md$')
29
30for mf in ${MD_FILES[*]}; do
31 if [ "${mf: -11}" == ".section.md" ]; then
32 mkdir -p "$(dirname "$OUT/$mf")"
33 OUTFILE="$OUT/${mf%".section.md"}.section.xml"
34 pandoc "$mf" "${pandoc_flags[@]}" \
35 -o "$OUTFILE"
36 grep -q -m 1 "xi:include" "$OUTFILE" && sed -i 's|xmlns:xlink="http://www.w3.org/1999/xlink"| xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude"|' "$OUTFILE"
37 fi
38
39 if [ "${mf: -11}" == ".chapter.md" ]; then
40 mkdir -p "$(dirname "$OUT/$mf")"
41 OUTFILE="$OUT/${mf%".chapter.md"}.chapter.xml"
42 pandoc "$mf" "${pandoc_flags[@]}" \
43 --top-level-division=chapter \
44 -o "$OUTFILE"
45 grep -q -m 1 "xi:include" "$OUTFILE" && sed -i 's|xmlns:xlink="http://www.w3.org/1999/xlink"| xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude"|' "$OUTFILE"
46 fi
47done
48
49popd