Debian Codegraph @

texmf-dist/doc/support/arara/htmlmanualtopdf.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 set -e
4
5 # check the required tools; we make these available through the `nix develop`
6 # shell so you may use that to build the manual
7 required_tools=("htmlq" "sed" "weasyprint")
8 for tool in "${required_tools[@]}"
9 do
10 if ! [ -x "$(command -v "$tool")" ]; then
11 echo "Please make sure $tool is accessible." >&2
12 exit 1
13 fi
14 done
15
16 # this script fetches the manual content from the remote rendered version at
17 # arara's website; caveat: chapters have to be manually adjusted
18 currdir="$PWD"
19 basedir="$(dirname "$currdir")/website/public/"
20 baseurl="file://$basedir"
21 chapters=("introduction" "concepts" "cli" "configuration" "logging"
22 "projects" "methods" "rules" "building" "deploying" "yaml"
23 "mvel")
24
25 if [ ! -d "$basedir" ]
26 then
27 echo "Please run zola before converting to PDF." >&2
28 exit 1
29 fi
30
31 # create a temporary directory to build the manual in; this will be created
32 # in /tmp, so users of operating systems without that directory must change
33 # it here
34 tempdir="/tmp/arara-manual"
35 if [ -d "$tempdir" ]
36 then
37 rm -r "$tempdir"
38 fi
39 mkdir -p "$tempdir"
40 cp ./resources/* "$tempdir/"
41 cd "$tempdir"
42
43 # these files are created in the temporary directory; no need to change
44 # anything at this point except that the name of the PDF file will be used
45 # when copying back to the source directory
46 htmlfile="manual.html"
47 pdffile="arara-manual.pdf"
48
49 cat <<EOF > $htmlfile
50 <!DOCTYPE html>
51 <html lang="en-US">
52 <head>
53 <meta charset="UTF-8">
54 <title>arara</title>
55
56 <meta name="keywords" content="tex, latex, context, build tool, automation">
57 <meta name="name" content="The cool TeX automation tool">
58 <meta name="description" content="arara is a TeX automation tool based on directives and rules.">
59
60 <link href="file://$tempdir/manual.css" rel="stylesheet">
61 </head>
62 <body>
63 <article id="cover">
64 <div id="coverimg">
65 <img src="file://$tempdir/arara-logo-with-text-bottom.svg"
66 alt="arara logo" />
67 </div>
68 <div id="refbox">Reference manual</div>
69 <div id="metadata">
70 <span>The Island of TeX</span>
71 <a href="https://gitlab.com/islandoftex/arara">
72 <img src="file://$tempdir/gitlab-icon.svg"
73 alt="GitLab" />
74 </a>
75 <span>Version $(cat version.txt)</span>
76 </div>
77 </article>
78 <article id="license">
79 <h1>License</h1>
80 <p>arara is licensed under the
81 <a href="https://opensource.org/licenses/BSD-3-Clause">New BSD
82 License</a>. It is important to observe that the New BSD License has been
83 verified as a GPL-compatible free software license by the
84 <a href=http://www.fsf.org/">Free Software Foundation</a>, and has been
85 vetted as an open source license by the
86 <a href="http://www.opensource.org/">Open Source Initiative</a>.
87 </p>
88 <blockquote>
89 <strong>New BSD License</strong>
90 <p>
91 Copyright 2012–2023, Island of TeX<br/>
92 All rights reserved.
93 </p>
94 <p>
95 Redistribution and use in source and binary forms, with or without
96 modification, are permitted provided that the following conditions are
97 met:
98 </p>
99 <ol>
100 <li>Redistributions of source code must retain the above copyright
101 notice, this list of conditions and the following disclaimer.</li>
102 <li>Redistributions in binary form must reproduce the above copyright
103 notice, this list of conditions and the following disclaimer in the
104 documentation and/or other materials provided with the distribution.
105 </li>
106 <li>Neither the name of the copyright holder nor the names of its
107 contributors may be used to endorse or promote products derived from
108 this software without specific prior written permission.</li>
109 </ol>
110 <p>
111 This software is provided by the copyright holders and contributors “as
112 is” and any express or implied warranties, including, but not limited to,
113 the implied warranties of merchantability and fitness for a particular
114 purpose are disclaimed. In no event shall the copyright holder or
115 contributors be liable for any direct, indirect, incidental, special,
116 exemplary, or consequential damages (including, but not limited to,
117 procurement of substitute goods or services; loss of use, data, or
118 profits; or business interruption) however caused and on any theory of
119 liability, whether in contract, strict liability, or tort (including
120 negligence or otherwise) arising in any way out of the use of this
121 software, even if advised of the possibility of such damage.
122 </p>
123 </blockquote>
124 </article>
125 EOF
126
127 # assemble the manual content by fetching chapters from the webpage;
128 # while doing so assemble the toc for that chapter as well
129 toc_content=""
130 chapter_content=""
131 for chapter in "${chapters[@]}"
132 do
133 cp "$(echo "${baseurl}manual/$chapter/index.html" | cut -c8-)" "tmp-$htmlfile"
134 chapter_title="$(cat "tmp-$htmlfile" | htmlq -t 'div.heading-text' \
135 | sed '/^[[:space:]]*$/d' | sed 's/ *$//g' | sed 's/^ *//g')"
136 this_chapter_content="$(cat <<EOF
137 <article>
138 <h1 id="chapter-$chapter">$chapter_title</h1>
139 $(cat "tmp-$htmlfile" | htmlq 'div.content.text' --remove-nodes 'div.heading-text' \
140 | tail -n +3 | head -n -1 \
141 | sed -r 's/<(\/?)h5/<\1h6/g' | sed -r 's/<(\/?)h4/<\1h5/g' \
142 | sed -r 's/<(\/?)h3/<\1h4/g' | sed -r 's/<(\/?)h2/<\1h3/g' \
143 | sed -r 's/<(\/?)h1/<\1h2/g')
144 </article>
145 EOF
146 )"
147
148 # create toc entries by iterating over all headings of the chapter;
149 # because we normalized headings above and alredy added the chapter
150 # title, this will create the whole chapter toc
151 toc_headings="$(echo "$this_chapter_content" \
152 | sed -n -r 's/^.*<h([1-6]) id="(.*)">.*$/\1;\2/p')"
153 current_level=0
154 while IFS=";" read -r heading_level heading_id
155 do
156 if [ $heading_level -eq $current_level ]
157 then
158 toc_content+="</li>"
159 fi
160 # we are assuming well-formed content where a new heading will always
161 # be of the next lower level
162 if [ $heading_level -gt $current_level ]
163 then
164 toc_content+="<ul>"
165 current_level=$heading_level
166 fi
167 # the assumption above does not hold for closing so we have to process
168 # levels properly at this point
169 while [ $current_level -gt $heading_level ]
170 do
171 toc_content+="</ul></li>"
172 ((current_level--))
173 done
174 # create table of contents entry; the link destination is sufficient,
175 # the actual title is inserted by CSS following the link
176 toc_content+="<li><a href=\"#$heading_id\"></a>"
177 done <<< "$toc_headings"
178 toc_content+="</li>"
179 while [ $current_level -gt 1 ]
180 do
181 toc_content+="</ul></li>"
182 ((current_level--))
183 done
184 toc_content+="</ul>"
185
186 # append chapter content to actual content
187 chapter_content+="$this_chapter_content"
188 done
189 rm "tmp-$htmlfile"
190
191 cat <<EOF >> $htmlfile
192 <article id="contents">
193 <h1>Contents</h1>
194 <ul>
195 $toc_content
196 </ul>
197 </article>
198 $chapter_content
199 </body>
200 </html>
201 EOF
202
203 # respect non-empty PAGES_URL that holds a customized base_url
204 if [ -z "${PAGES_URL}" ]; then
205 PAGES_URL="https://islandoftex.gitlab.io/arara"
206 fi
207
208 # convert absolute urls (manual sub-pages)
209 # <a href="https://.../manual/yaml/">text</a>
210 # to internal links (page anchors of section titles)
211 # <a href="#chapter-yaml">text</a>
212 # To avoid doing case conversions, this replacement is postponed to here.
213 sed -i 's~<a href="'$PAGES_URL'/manual/\([^/]*\)/">~<a href="#chapter-\1">~g' "$htmlfile"
214
215 # convert the created HTML manual to PDF
216 weasyprint -u "$baseurl" "$htmlfile" "$pdffile"
217
218 # change back to the docs directory and copy output PDF there
219 cd "$currdir"
220 cp "$tempdir/$pdffile" ./