summaryrefslogtreecommitdiff
path: root/Meta/build-manpages-website.sh
blob: f46dece75dc28850f8cf8f924592e1690db0979c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# shellcheck disable=SC1004 # literal backslash+linefeed is intended

set -e

script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
cd "${script_path}/.."

export LC_ALL=C  # Make the directory order reproducible
export MAN_DIR=Base/usr/share/man/

if [[ -e output ]]; then
    echo "Directory 'output/' already exists. Delete it first."
    exit 1
fi

# Use case-insensitive sorting, which will lead to more intuitive index pages.
SORT="sort -f"

# Prepare output directories
for d in "${MAN_DIR}"*/; do
    dir_name=$(basename "$d")
    section="${dir_name/man}"
    mkdir -p "output/${dir_name}"
done

# Convert markdown to html

# If you're here because your local results are different from the website:
# Check that your pandoc version matches the pandoc-version specified in manpages.yaml.

for md_file in $(find "${MAN_DIR}" -iname '*.md' | ${SORT}); do
    relative_path="$(realpath --relative-to="${MAN_DIR}" "${md_file}")"
    section="${relative_path%%/*}"
    section_number="${section#man}"
    filename="${relative_path#*/}"
    name="${filename%.md}"
    output_file="output/${section}/${name}.html"

    echo "Generating $md_file -> $output_file"
    mkdir -p "$(dirname "${output_file}")"
    pandoc -f gfm -t html5 -s \
        -B Meta/Websites/man.serenityos.org/banner-preamble.inc \
        --lua-filter=Meta/convert-markdown-links.lua \
        --metadata title="${name}(${section_number}) - SerenityOS man pages" \
        -o "${output_file}" \
        "${md_file}" &
done

# Wait for all pandoc executions to finish so that man page indices are always correct.
# shellcheck disable=SC2046 # Word splitting is intentional here
wait $(jobs -p)

# Generate man page listings through pandoc
for section_directory in output/*/; do
    section=$(basename "${section_directory}")
    section_number="${section#man}"
    case "${section_number}" in
        1) section_title="User Programs";;
        2) section_title="System Calls";;
        3) section_title="Library Functions";;
        4) section_title="Special Files";;
        5) section_title="File Formats";;
        6) section_title="Games";;
        7) section_title="Miscellanea";;
        8) section_title="Sysadmin Tools";;
        *) section_title="SerenityOS man pages"; echo "WARNING: Unrecognized section ${section_number}?!";;
    esac
    output="output/${section}/index.html"

    echo "Generating section ${section_number} index -> ${output}"
    pandoc -f gfm -t html5 -s \
        -B Meta/Websites/man.serenityos.org/banner-preamble.inc \
        --metadata title="Section ${section_number} - ${section_title}" \
        -o "${output}" \
        <(
            for f in $(find "${section_directory}" -iname '*.html' | ${SORT}); do
                filename=$(realpath --relative-to="${section_directory}" "$f")
                if [[ "$filename" == "index.html" ]]; then
                    continue
                fi
                filename_no_extension="${filename%.html}"
                # Generate indentation by subdirectory count: Replace each slash by the two Markdown indentation spaces, then remove all non-space characters.
                indentation=$(echo "${filename_no_extension}" | sed -e 's/ //g' -e 's/\//  /g' -e 's/[^ ]//g')
                name="$(basename "${filename}")"
                name="${name%.html}"
                echo "${indentation}- [${name}](${filename})"
            done
        ) &
done

# Generate main landing page listings through pandoc
echo 'Generating main pages'
pandoc -f gfm -t html5 -s \
    -B Meta/Websites/man.serenityos.org/banner-preamble.inc \
    --metadata title="SerenityOS man pages" \
    -o output/index.html \
    Meta/Websites/man.serenityos.org/index.md &
pandoc -f gfm -t html5 -s \
    -B Meta/Websites/man.serenityos.org/banner-preamble.inc \
    --metadata title="Can't run applications" \
    -o output/cant-run-application.html \
    Meta/Websites/man.serenityos.org/cant-run-application.md &

# Copy pre-made files
echo 'Copying images'
rsync -a Meta/Websites/man.serenityos.org/banner.png output/ &
rsync -a Base/usr/share/man/man7/LibDSP_classes.svg output/ &
find Base/usr/share/man/ -iname '*.png' -exec rsync -a {} output/ \; &

# Copy icons
mkdir output/icons

while read -r p; do
  rsync -a --relative Base/res/icons/./"$p" output/icons/
done < icons.txt

rm icons.txt

# shellcheck disable=SC2046 # Word splitting is intentional here
wait $(jobs -p)