aboutsummaryrefslogtreecommitdiff
path: root/doc/gen-man.sh
blob: 276679e59bee51b449f92d72d358b0eb18399ad4 (plain) (blame)
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
#!/bin/sh
#
# gen-man
#
# Generates kristall's man page from the gemtext about:help file.
#
# How we do this:
# 0.) We insert a pre-defined "head" first, which contains flags, options, etc
#     which are not present in the Help file.
#
# 1.) Convert the main stuff the Help file to a roff format.
#
# 2.) Append a pre-defined "tail".

# Locations of the "head" and "tail"
man_head="./kristall-head.man"
man_tail="./kristall-tail.man"
man_output="./kristall.1"
gemtext_in="../src/about/help.gemini"
gemtext_converter="./gem-to-man.awk"

# Make sure we have all the stuff we need
if [ ! -f "$man_head" ]; then
    echo "man page head does not exist."
    exit 1
fi
if [ ! -f "$man_tail" ]; then
    echo "man page tail does not exist"
    exit 1
fi
if [ -z "$man_output" ]; then
    echo "No output file"
    exit 1
fi
if [ ! -f "$gemtext_in" ]; then
    echo "Input gemtext file does not exist"
    exit 1
fi
if [ ! -f "$gemtext_converter" ]; then
    echo "Gemtext converter script does not exist"
    exit 1
fi

# Write the head to the output file.
cp "$man_head" "$man_output"

# Some pre-processing before giving our gemtext to the awk script.
gem_in=$(
    # Read input file
    cat "$gemtext_in" |

    # Strip a few lines from beginning/end of file.
    nl | awk 'FNR > 9' | sort -nr | awk 'FNR > 9' | sort -n | cut -f 2- |

    # First expression replaces all [Text like this] with bold text.
    # Second expression replaces text like *This* or _this_ with italic text.
    sed -E \
      -e 's#\[([^]]*)\]#\\fB\1\\fR#g' \
      -e 's#(^|[.,!? ]+)[*_]([^*_ ]+[^*_]+[^*_ ]+)[*_]($|[.,!? ])#\1\\fI\2\\fR\3#g'
)

# Convert gemtext to man format
printf "%s" "$gem_in" | "$gemtext_converter" >> "$man_output"

# Write the tail to the output file.
cat "$man_tail" >> "$man_output"