summaryrefslogtreecommitdiff
path: root/legacy/groffdown
blob: dbf9c85388129e47a5641176f2784d96013e7379 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/sh

################################################################################
#
# groffdown - (legacy shellscript version)
#
# Usage: groffdown [--debug] [-o PREFIX] FILE
#
################################################################################

set -eu
umask 0022
export LC_ALL=C

# usage [CODE]
# CODE: default to 1. Error code.
usage(){
    set -- "${1:-1}"

    _redirect=2
    case "$1" in 0) _redirect=1; esac

    printf '%s\n' "Usage: ${0##*/} [--debug] [-o PREFIX] FILE" 1>&$_redirect
    exit $1
}

# error_exit MSG
error_exit(){
    printf '%s\n' "${0##*/}: $1" 1>&2
    exit 1
}

# == Flags and global arguments ==
DEBUG=
OUTPUT_FILE=

# == Parse argument ==
case $# in 0) usage 1; esac

while :; do
    case $# in 0) break; esac

    case $1 in -|[!-]*)        break; esac
    case $1 in --)      shift; break; esac

    case $1 in --help|--usage|'-?')
	 usage 0
    esac

    case $1 in --d|--de|--deb|--debu|--debug)
	DEBUG=1
	shift
	continue
    esac

    case $1 in -o)
	 OUTPUT_FILE="$2"
	 shift 2
	 continue
    esac

    case $1 in -?*)
	 error_exit "$1: Unknown option"
    esac
done

case $# in 0) usage 1; esac

# == Variables ==
file="$OUTPUT_FILE"; : ${file:="$( readlink -f "$1" )"}
dir=$(dirname "$file")
base="${file%.*}"

# == Readable regular file? ==
if test -f "$1" && test -r "$1"; then
    : # NOP
else
    error_exit "${1##*/}: Not a readable regular file"
fi

# == Hyphened file to be real argument ==
case "$1" in -*) set -- "./$1";; esac

# == Main section ==

# -- Translate markdown to groff ms. --

cat "$1" |
#
# Yaml-specifics.
awk '
BEGIN {
    is_in_yaml = 0;
}

# -- Beginning of yaml. --
NR == 1 && /^-+$/ {
    is_in_yaml = 1;
    next;
}

# -- End of yaml. --
is_in_yaml && /^-+$/ {
    is_in_yaml = 0;
    # YYY(Tpaefawzen): Seems like line for end of yaml should be preserved?
}

is_in_yaml {
    # -- Yaml metadata. --
    sub("title: *", ".TL\n");
    sub("author: *", ".AU\n");
    sub("date: *", ".ND\n");
    sub("institution: *", ".AI\n");
}

1' |
#
# Others are context-free.
sed '
    # -- LaTeX-style abstract. --
    s/\\begin{abstract}/\.AB/
    s/\\end{abstract}/\.AE/

    # -- Sections. --
    s/^\#####.\(.*\)/\.NH 5\n\1\n\.PP/g
    s/^\####.\(.*\)/\.NH 4\n\1\n\.PP/g
    s/^\###.\(.*\)/\.NH 3\n\1\n\.PP/g
    s/^\##.\(.*\)/\.NH 2\n\1\n\.PP/g
    s/^\#.\(.*\)/\.NH 1\n\1\n\.PP/g

    # -- Bold and italic. --
    s/\*\*\*\(.*\)\*\*\*$/\n\.BI\ \"\1\"\ /g
    s/\*\*\*\(.*\)\*\*\*\(.\)$/\n\.BI\ \"\1\"\ \"\2\"/g
    s/\*\*\*\(.*\)\*\*\*\(.\)/\n\.BI\ \"\1\"\ \"\2\"\n/g

    # -- Bold. --
    s/\*\*\(.*\)\*\*$/\n\.B\ \"\1\"\ /g
    s/\*\*\(.*\)\*\*\(.\)$/\n\.B\ \"\1\"\ \"\2\"/g
    s/\*\*\(.*\)\*\*\(.\)/\n\.B\ \"\1\"\ \"\2\"\n/g

    # -- Italic. --
    s/\*\(.*\)\*$/\n\.I\ \"\1\"\ /g
    s/\*\(.*\)\*\(.\)$/\n\.I\ \"\1\"\ \"\2\"/g
    s/\*\(.*\)\*\(.\)/\n\.I\ \"\1\"\ \"\2\"\n/g

    # -- Inline code. --
    s/`\(.*\)`$/\n\.CW\ \"\1\"\ /g
    s/`\(.*\)`\(.\)$/\n\.CW\ \"\1\"\ \"\2\"/g
    s/`\(.*\)`\(.\)/\n\.CW\ \"\1\"\ \"\2\"\n/g

    # -- List items. --
    s/^\ ...............-\ /.IP\ \\(bu\ 10\n/g
    s/^\ ...........-\ /.IP\ \\(bu\ 8\n/g
    s/^\ .......-\ /.IP\ \\(bu\ 6\n/g
    s/^\ ...-\ /.IP\ \\(bu\ 4\n/g
    s/^-\ /.IP\ \\(bu\ 2\n/g
    s/^\ .*-\ /.IP\ \\(bu\ 12\n/g

    # -- Begin a new paragraph. --
    s/^$/.PP/g
' |
#
# Parsing for ms things ends.
cat > "$base.ms.tmp"

tmpfname="$base.ms.tmp"

# To PDF when required.
case "$DEBUG" in "")
    if cat "$base.ms.tmp" | groff -ms -Tpdf > "$base.pdf.tmp"; then
	rm "$base.ms.tmp"
    else
	error_exit "could not convert to PDF with groff (code: $?)"
    fi

    tmpfname="$base.pdf.tmp"
    ;;
esac

mv "$tmpfname" "${tmpfname%.tmp}"

# == Finally ==
exit $?

# vim: set shiftwidth=4 softtabstop=4: