xref: /aosp_15_r20/external/libcap/doc/mkmd.sh (revision 2810ac1b38eead2603277920c78344c84ddf3aff)
1#!/bin/bash
2#
3# Handy script to rebuild the markdown version of the man pages.
4# This uses pandoc if it is installed.
5#
6# For rendering the md, we can use a different command:
7#
8#   cd md; for x in *.md ; do pandoc -s $x --metadata pagetitle="${x%.md}" -o ${x%.md}.html --lua-filter=../md2html.lua ; done
9
10if [[ -z "$(which pandoc)" ]]; then
11    echo "pandoc not found - skipping conversion"
12    exit 0
13fi
14
15outdir="$1"
16if [[ -z "${outdir}" ]]; then
17    echo "usage $0 <outdir>"
18    exit 1
19fi
20
21mkdir -p "${outdir}"
22if [[ $? -ne 0 ]]; then
23    echo "failed to make output directory: ${outdir}"
24    exit 1
25fi
26
27index="${outdir}/index.md"
28
29function do_page () {
30    m="$1"
31    base="${m%.*}"
32    sect="${m#*.}"
33    output="${base}-${sect}.md"
34
35    echo "converting ${m}" 1>&2
36
37    redir="$(grep '^.so man' "${m}")"
38    if [[ $? -eq 0 ]]; then
39	r="${redir#*/}"
40	rbase="${r%.*}"
41	rsect="${r#*.}"
42	echo "* [${base}(${sect})](${rbase}-${rsect}.md)" >> "${index}"
43	return
44    fi
45
46    pandoc -f man -t markdown < "${m}" | sed 's/\*\*\([^*]\+\)\*\*(\([138]\+\))/[\1(\2)](\1-\2.md)/g' > "${outdir}/${base}-${sect}.md"
47    echo "* [${base}(${sect})](${base}-${sect}.md)" >> "${index}"
48}
49
50cat > "${index}" <<EOF
51# Manpages for libcap and libpsx
52
53EOF
54
55if [[ -f "local-md.preamble" ]]; then
56    cat "local-md.preamble" >> "${index}"
57fi
58
59cat >> "${index}" <<EOF
60
61## Individual reference pages
62EOF
63
64# Assumes the m's are listed alphabetically.
65for n in 1 3 8 ; do
66	cat >> "${index}" <<EOF
67
68### Section ${n}
69
70EOF
71    for m in *.${n}; do
72	do_page "${m}"
73    done
74done
75
76cat >> "${index}" <<EOF
77
78## More information
79
80EOF
81
82if [[ -f "local-md.postscript" ]]; then
83    cat "local-md.postscript" >> "${index}"
84fi
85
86cat >> "${index}" <<EOF
87
88For further information, see the
89[FullyCapable](https://sites.google.com/site/fullycapable/) homepage
90for libcap.
91
92## MD page generation
93
94These official man pages for libcap and libpsx were converted to
95markdown using [pandoc](https://pandoc.org).
96
97EOF
98