This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraite.ksh
203 lines (173 loc) Β· 6.26 KB
/
traite.ksh
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env ksh93
# TraitΓ© de la tabula: HTML (and maybe PDF?) generator for Markdown
# documentation in a "tabula-style".
# Include the global configuration file.
. /usr/etc/traite.conf
# Include some libraries that we will be using.
. "${ADDERE}/posix-alt.shi"
# Cuz We're real
documents="$(realpath "$DOCUMENTS")"
www_output_directory="$(realpath "$WWW_DIRECTORY")"
# Create a temporary directory for temporary files
temporary_dir="$(mktemp -d /tmp/traite.$$.XXXXXX)"
# Cheap hack for getting a list for all the directories in the $documents
# directory, containing or not a valid tabula.
# This probably isn't bad-name safe, be aware.
docdir=( $(cd "$documents"; echo *) )
# Get the current time, for the log file.
current_time="$(date '+%Y-%m-%d %H.%M.%S')"
# This will be used when we will generate a custom HTML footer for the document
# in question.
# Except for this one, which will only be used in print_help()
program_name="$0"
traite_version='0.1-a'
ksh_version="${.sh.version}"
pandoc_version="$(pandoc -v | sed 1q)"
kernel_name="$(uname -s)"
kernel_release="$(uname -r)"
machine_host="$(uname -n)"
function main {
# Non-UNIX(?) command-line options. Not using getopt() for now, maybe in
# the future.
action="$1"
case "$action" in
build) generate_html_documents ;;
nuke) nuke_html_documents $2;;
*|help) print_help ;;
esac
}
function generate_html_documents {
output="${OUTPUT:-index.html}"
owd="$PWD"
for ((i=0; i < $(n ${docdir[@]}); i++)); do
# First of all: check if tabula.conf exist in all the
# directories; if not, it's not a tabula.
test -f "$documents/${docdir[$i]}/tabula.conf" \
|| { printL 'ERROR: %s is not a tabula\n' \
"${docdir[$i]}"; continue; } \
&& cd "$documents/${docdir[$i]}" \
&& printL 'ACTION: Entered directory %s\n' "$documents/${docdir[$i]}"
printL 'ACTION: Reading tabula.conf\n'
. ./tabula.conf
printL 'INFO:\nTitle: %s\nNumber of files: %s\nFiles: %s\n' \
"$title" $(n ${files[@]}) "${files[@]}"
# Determine absolute paths for files listed in tabula.conf
for ((j=0; j < $(n ${files[@]}); j++)); do
realfiles+=" $(realpath "${files[$j]}")"
done
printL 'ACTION: realpath'\''d files.\n'
printL 'INFO:\nOriginal files: %s\nrealpath'\''d files: %s\n' \
"${files[@]}" "${realfiles[@]}"
deploy_directory="$www_output_directory/${docdir[$i]}"
printL 'ACTION: Creating directory %s for the compiled files\n' \
"$deploy_directory"
test -d "$deploy_directory" -a -w "$deploy_directory" \
|| { mkdir -pv "$deploy_directory" 2>&1 | tee "$LOG"; }
{ echo "$USE_EXTERNAL_CSS" | grep -i '^y' 1>&2; } \
&& pandoc_opts+="-c "$EXTERNAL_CSS""
{ echo "$USE_FOOTER" | grep -i '^y' 1>&2; } \
&& generate_html_footer
printL 'ACTION: Compiling documentation\n'
md2html "${realfiles[@]}" "$deploy_directory/$output" "$title" \
"$lang" \
&& { echo "$USE_MAIN_PAGE" | grep -i '^y' 1>&2; } \
&& hydrate_tabulas
# Go back to that same old working place...
cd "$owd"
done
}
function nuke_html_documents {
# This function shall clean a specific directory that already contains
# a built tabula --- in other words, that is already in HTML.
printL 'NUKE: Nuking %s, which is expected to be present at %s\n' \
"$2" "$www_output_directory/$2"
find "$www_output_directory/$2" -type f -name '*.htm?' \
-exec rm -iv {} \;
}
function print_help {
printf 'usage: %s build\n %s nuke [tabula name...]\n' \
$program_name $program_name 1>&2
return 1
}
# Just a boilerplate for calling Pandoc, of course.
function md2html {
# It must have a better way of doing this.
files=( $(echo $1) )
output="$2"
title="$3"
lang="$4"
pandoc --highlight-style="$THEME" \
--metadata title="$title" \
--metadata lang="$lang"\
${files[@]} \
--output "$output" \
--table-of-contents \
-s --self-contained \
--file-scope \
--pdf-engine="${pdfengine:-xelatex}" \
--verbose \
${pandoc_opts[@]} \
2>&1 | tee "$LOG"
}
function hydrate_tabulas {
# Create a temporary files
tabulas_list="$(mktemp $temporary_dir/aqua.XXXXXX.html)"
temp_main_page="$(mktemp $temporary_dir/temp.index.XXXXXX.html)"
for ((i=0; i<$(n ${docdir[@]}); i++)); do
# Test if there's a tabula.conf that correspond to the
# generated tabula
{ test -f "$documents/${docdir[$i]}/tabula.conf"; \
test -d "$www_output_directory/${docdir[$i]}"; }\
|| { printL \
'ERROR: %s was not generated from a tabula...
Strange enough, it'\''s present on the directory meant for HTML files built from tabulas\n' \
"${docdir[$i]}"; continue; } \
&& cd "$documents/${docdir[$i]}" \
&& printL 'ACTION: Entered directory %s\n' "$documents/${docdir[$i]}"
printL 'ACTION: Reading tabula.conf\n'
. ./tabula.conf
cat >> "$tabulas_list" << EOF
<dl>
<dt>π <a href="./${docdir[$i]}">$title</a></dt>
<dd>Description: $desc</dd>
<dd>Authors: ${author[@]} </dd>
<dd>Languages: $lang</dd>
</dl>
EOF
done
printL 'ACTION: Adding tabulas to the main page.\n'
nawk -vaqua="$tabulas_list" \
'/<!--TABULA LIST END-->/{flag=0} !flag;
sub(/<!--TABULA LIST START-->/, ""){
# Insert the HTML files containing all the tabulas
system("cat '\''"aqua"'\''");
flag=1
}
' < "$(realpath "$MAIN_PAGE")" > "$temp_main_page" \
&& mv "$temp_main_page" "$www_output_directory/${MAIN_PAGE##*/}"
printL 'INFO: Now you can publish everything.\nThe compiled tabulas with the new main page are available at %s.\n' \
"$www_output_directory"
}
function generate_html_footer {
# I really hope all this works (in fact, it does).
footer="$(realpath "$FOOTER")"
tmpfooter="$(mktemp $temporary_dir/footer.XXXXXX.html)"
printL \
'ACTION: Generating a footer for the final HTML at %s, using %s as a base\n' \
"$tmpfooter" "$footer"
sed -e "s@%%author%%@$author@" \
-e "s@%%project%%@$project@" \
-e "s@%%copyright_year%%@$copyright_year@" \
-e "s@%%date%%@$date@" \
-e "s@%%pandoc_version%%@$pandoc_version@" \
-e "s@%%ksh_version%%@$ksh_version@" \
-e "s@%%machine_host%%@$machine_host@" \
-e "s@%%kernel_name%%@$kernel_name@" \
-e "s@%%kernel_release%%@$kernel_release@" < "$footer" > "$tmpfooter"
pandoc_opts+=" -A "$tmpfooter""
}
# printL - print to log
# I did not put this into another file (library) because I don't think it's
# worth for now.
function printL { printf "$@" | tee "$LOG"; }
main $@