2020-12-23 06:56:46 +01:00
|
|
|
#! /usr/bin/env nix-shell
|
2022-12-21 22:00:27 +01:00
|
|
|
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/tarball/22.11 -i bash -p pandoc
|
2020-12-23 06:56:46 +01:00
|
|
|
|
|
|
|
# This script is temporarily needed while we transition the manual to
|
|
|
|
# CommonMark. It converts the .md files in the regular manual folder
|
|
|
|
# into DocBook files in the from_md folder.
|
|
|
|
|
|
|
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
2021-10-09 19:08:25 +02:00
|
|
|
pushd "$DIR"
|
2020-12-23 06:56:46 +01:00
|
|
|
|
2023-01-02 14:00:58 +01:00
|
|
|
link_manpages_filter=$(nix-build --no-out-link "$DIR/../../../doc/build-aux/pandoc-filters/link-manpages.nix")
|
|
|
|
|
2021-06-18 00:07:00 +02:00
|
|
|
# NOTE: Keep in sync with Nixpkgs manual (/doc/Makefile).
|
|
|
|
# TODO: Remove raw-attribute when we can get rid of DocBook altogether.
|
|
|
|
pandoc_commonmark_enabled_extensions=+attributes+fenced_divs+footnotes+bracketed_spans+definition_lists+pipe_tables+raw_attribute
|
|
|
|
pandoc_flags=(
|
2021-07-12 15:54:14 +02:00
|
|
|
# Not needed:
|
|
|
|
# - diagram-generator.lua (we do not support that in NixOS manual to limit dependencies)
|
|
|
|
# - media extraction (was only required for diagram generator)
|
|
|
|
# - docbook-reader/citerefentry-to-rst-role.lua (only relevant for DocBook → MarkDown/rST/MyST)
|
|
|
|
"--lua-filter=$DIR/../../../doc/build-aux/pandoc-filters/myst-reader/roles.lua"
|
2023-01-02 14:00:58 +01:00
|
|
|
"--lua-filter=$link_manpages_filter"
|
2021-07-12 15:54:14 +02:00
|
|
|
"--lua-filter=$DIR/../../../doc/build-aux/pandoc-filters/docbook-writer/rst-roles.lua"
|
2022-05-28 18:07:14 +02:00
|
|
|
"--lua-filter=$DIR/../../../doc/build-aux/pandoc-filters/docbook-writer/html-elements.lua"
|
2021-07-12 04:38:41 +02:00
|
|
|
"--lua-filter=$DIR/../../../doc/build-aux/pandoc-filters/docbook-writer/labelless-link-is-xref.lua"
|
2021-06-18 00:07:00 +02:00
|
|
|
-f "commonmark${pandoc_commonmark_enabled_extensions}+smart"
|
|
|
|
-t docbook
|
|
|
|
)
|
|
|
|
|
2020-12-23 06:56:46 +01:00
|
|
|
OUT="$DIR/from_md"
|
|
|
|
mapfile -t MD_FILES < <(find . -type f -regex '.*\.md$')
|
|
|
|
|
|
|
|
for mf in ${MD_FILES[*]}; do
|
|
|
|
if [ "${mf: -11}" == ".section.md" ]; then
|
2021-10-09 19:08:25 +02:00
|
|
|
mkdir -p "$(dirname "$OUT/$mf")"
|
2021-09-07 15:29:21 +02:00
|
|
|
OUTFILE="$OUT/${mf%".section.md"}.section.xml"
|
2021-06-18 00:07:00 +02:00
|
|
|
pandoc "$mf" "${pandoc_flags[@]}" \
|
2021-09-07 15:29:21 +02:00
|
|
|
-o "$OUTFILE"
|
|
|
|
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"
|
2020-12-23 06:56:46 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "${mf: -11}" == ".chapter.md" ]; then
|
2021-10-09 19:08:25 +02:00
|
|
|
mkdir -p "$(dirname "$OUT/$mf")"
|
2021-09-07 15:29:21 +02:00
|
|
|
OUTFILE="$OUT/${mf%".chapter.md"}.chapter.xml"
|
2021-06-18 00:07:00 +02:00
|
|
|
pandoc "$mf" "${pandoc_flags[@]}" \
|
2020-12-23 06:56:46 +01:00
|
|
|
--top-level-division=chapter \
|
2021-09-07 15:29:21 +02:00
|
|
|
-o "$OUTFILE"
|
|
|
|
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"
|
2020-12-23 06:56:46 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
popd
|
2023-01-05 06:16:46 +01:00
|
|
|
|
|
|
|
# now handle module chapters. we'll need extra checks to ensure that we don't process
|
|
|
|
# markdown files we're not interested in, so we'll require an x.nix file for ever x.md
|
|
|
|
# that we'll convert to xml.
|
|
|
|
pushd "$DIR/../../modules"
|
|
|
|
|
|
|
|
mapfile -t MD_FILES < <(find . -type f -regex '.*\.md$')
|
|
|
|
|
|
|
|
for mf in ${MD_FILES[*]}; do
|
|
|
|
[ -f "${mf%.md}.nix" ] || continue
|
|
|
|
|
|
|
|
pandoc --top-level-division=chapter "$mf" "${pandoc_flags[@]}" -o "${mf%.md}.xml"
|
2023-01-10 12:34:37 +01:00
|
|
|
sed -i -e '1 i <!-- Do not edit this file directly, edit its companion .md instead\
|
|
|
|
and regenerate this file using nixos/doc/manual/md-to-db.sh -->' \
|
|
|
|
"${mf%.md}.xml"
|
2023-01-05 06:16:46 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
popd
|