Debian Codegraph @

sys/ManPages_toHtml.cpp

raw
1 /* ManPages_toHtml.cpp
2 *
3 * Copyright (C) 1996-2026 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "ManPages.h"
20 #include "praat.h"
21
22 #define LONGEST_FILE_NAME 55
23 #define PNG_RESOLUTION 300 /* dpi */
24
25 static const struct stylesInfo {
26 conststring32 htmlIn, htmlOut;
27 } stylesInfo [] = {
28 { nullptr, nullptr },
29 /* INTRO: */ { U"<p>", U"</p>" },
30 /* ENTRY: */ { U"<h2>", U"</h2>" },
31 /* NORMAL: */ { U"<p>", U"</p>" },
32 /* LIST_ITEM: */ { U"<dd style=\"position:relative;padding-left:1em;text-indent:-2em\">", U"</dd>" },
33 /* TERM: */ { U"<dt>", U"" },
34 /* DEFINITION: */ { U"<dd>", U"</dd>" },
35 /* CODE: */ { U"<code> ", U"<br></code>" },
36 /* PROTOTYPE: */ { U"<p>", U"</p>" },
37 /* EQUATION: */ { U"<table width=\"100%\" style=\"white-space:pre-wrap\"><tr><td align=middle>", U"</table>" },
38 /* PICTURE: */ { U"<p>", U"</p>" },
39 /* SCRIPT: */ { U"<p>", U"</p>" },
40 /* LIST_ITEM1: */ { U"<dd>&nbsp;&nbsp;&nbsp;", U"</dd>" },
41 /* LIST_ITEM2: */ { U"<dd>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", U"</dd>" },
42 /* LIST_ITEM3: */ { U"<dd>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", U"</dd>" },
43 /* TERM1: */ { U"<dt>&nbsp;&nbsp;&nbsp;", U"" },
44 /* TERM2: */ { U"<dt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", U"" },
45 /* TERM3: */ { U"<dt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", U"" },
46 /* DEFINITION1: */ { U"<dd>&nbsp;&nbsp;&nbsp;", U"</dd>" },
47 /* DEFINITION2: */ { U"<dd>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", U"</dd>" },
48 /* DEFINITION3: */ { U"<dd>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", U"</dd>" },
49 /* CODE1: */ { U"<code> ", U"<br></code>" },
50 /* CODE2: */ { U"<code> ", U"<br></code>" },
51 /* CODE3: */ { U"<code> ", U"<br></code>" },
52 /* CODE4: */ { U"<code> ", U"<br></code>" },
53 /* CODE5: */ { U"<code> ", U"<br></code>" },
54 /* CAPTION: */ { U"<p style=\"position:relative;padding-left:4em;text-indent:-2em;font-size:86%\">", U"</font></p>" },
55 /* QUOTE: */ { U"<p style=\"position:relative;padding-left:4em;font-size:86%\">", U"</font></p>" },
56 /* QUOTE1: */ { U"<p style=\"position:relative;padding-left:8em;font-size:86%\">", U"</font></p>" },
57 /* QUOTE2: */ { U"<p style=\"position:relative;padding-left:12em;font-size:86%\">", U"</font></p>" },
58 /* QUOTE3: */ { U"<p style=\"position:relative;padding-left:16em;font-size:86%\">", U"</font></p>" },
59 /* SUBHEADER: */ { U"<h3>", U"</h3>" },
60 };
61
62 static void writeLinkAsHtml (ManPages me, mutablestring32 link, conststring32 linkText, MelderString *buffer, conststring32 pageTitle) {
63 /*
64 We write the link in the following format:
65 <a href="link.html">linkText</a>
66 */
67 MelderString_append (buffer, U"<a href=\"");
68 if (str32nequ (link, U"\\FI", 3)) {
69 MelderString_append (buffer, link + 3); // file link
70 } else {
71 /*
72 If "link" (initial lower case) is not in the manual, we write "Link.html" instead.
73 All spaces and strange symbols in "link" are replaced by underscores,
74 because it will be a file name (see ManPages_writeAllToHtmlDir).
75 The file name will have no more than 30 or 60 characters, and no less than 1.
76
77 The first character of the link text can have the wrong case.
78 */
79 integer linkPageNumber = ManPages_lookUp (me, link);
80 if (linkPageNumber == 0)
81 Melder_throw (U"No such manual page: ", link, U" (from page “", pageTitle, U"”).");
82 link [0] = my pages.at [linkPageNumber] -> title [0];
83 char32 *q = link;
84 if (! ManPages_lookUp_caseSensitive (me, link)) {
85 MelderString_appendCharacter (buffer, Melder_toUpperCase (link [0]));
86 if (*q)
87 q ++; // first letter already written
88 }
89 while (*q && q - link < LONGEST_FILE_NAME) {
90 if (isAllowedFileNameCharacter (*q))
91 MelderString_appendCharacter (buffer, *q);
92 else if (*q == U'#')
93 MelderString_append (buffer, U"-H");
94 else if (*q == U'$')
95 MelderString_append (buffer, U"-S");
96 else if (*q == U'@')
97 MelderString_append (buffer, U"-C");
98 else
99 MelderString_appendCharacter (buffer, U'_');
100 q ++;
101 }
102 if (link [0] == U'\0')
103 MelderString_appendCharacter (buffer, U'_'); // otherwise Mac problems or Unix invisibility
104 MelderString_append (buffer, U".html");
105 }
106 MelderString_append (buffer, U"\">", linkText, U"</a>");
107 }
108
109 static void writeParagraphsAsHtml (ManPages me, Interpreter optionalInterpreterReference, MelderFile file, ManPage page, MelderString *buffer) {
110 static structPraatApplication praatApplication;
111 static structPraatObjects praatObjects;
112 static structPraatPicture praatPicture;
113
114 ManPage_runAllChunksToCache (page, optionalInterpreterReference, kGraphics_font::TIMES, 12.0,
115 & praatApplication, & praatObjects, & praatPicture, & my rootDirectory);
116 integer chunkNumber = 0;
117 autostring32 theErrorThatOccurred;
118
119 integer numberOfPictures = 0;
120 bool inList = false, inItalic = false, inBold = false;
121 bool inSub = false, inCode = false, inSuper = false, ul = false, inSmall = false;
122 bool wordItalic = false, wordBold = false, wordCode = false, letterSub = false, letterSuper = false;
123 for (integer ipar = 1; ipar <= page -> paragraphs.size; ipar ++) {
124 const bool verbatimAware = true; //page -> verbatimAware;
125 // we ignore saving old-fashioned man pages to HTML (was undocumented anyway) 2023-07-19
126 const structManPage_Paragraph *paragraph = & page -> paragraphs [ipar];
127 const bool paragraphIsVerbatim = ( paragraph -> couldVerbatim () );
128 const char32 *p = & paragraph -> text [0];
129 const bool isListItem = paragraph -> type == kManPage_type::LIST_ITEM ||
130 (paragraph -> type >= kManPage_type::LIST_ITEM1 && paragraph -> type <= kManPage_type::LIST_ITEM3);
131 const bool isTag = paragraph -> type == kManPage_type::TERM ||
132 (paragraph -> type >= kManPage_type::TERM1 && paragraph -> type <= kManPage_type::TERM3);
133 const bool isDefinition = paragraph -> type == kManPage_type::DEFINITION ||
134 (paragraph -> type >= kManPage_type::DEFINITION1 && paragraph -> type <= kManPage_type::DEFINITION3);
135 /*const bool isCode = paragraph -> type == kManPage_type::CODE ||
136 (paragraph -> type >= kManPage_type::CODE1 && paragraph -> type <= kManPage_type::CODE5);*/
137
138 if (paragraph -> type == kManPage_type::PICTURE) {
139 numberOfPictures ++;
140 structMelderFile pngFile { };
141 MelderFile_copy (file, & pngFile);
142 pngFile. path [Melder_length (pngFile. path) - 5] = U'\0'; // delete extension ".html"
143 str32cat (pngFile. path, Melder_cat (U"_", numberOfPictures, U".png"));
144 {// scope
145 autoGraphics graphics = Graphics_create_pngfile (& pngFile, 300, 0.0, paragraph -> width, 0.0, paragraph -> height);
146 Graphics_setFont (graphics.get(), kGraphics_font::TIMES);
147 Graphics_setFontStyle (graphics.get(), 0);
148 Graphics_setFontSize (graphics.get(), 12);
149 Graphics_setWrapWidth (graphics.get(), 0);
150 Graphics_setViewport (graphics.get(), 0.0, paragraph -> width, 0.0, paragraph -> height);
151 paragraph -> draw (graphics.get());
152 Graphics_setViewport (graphics.get(), 0, 1, 0, 1);
153 Graphics_setWindow (graphics.get(), 0, 1, 0, 1);
154 Graphics_setTextAlignment (graphics.get(), Graphics_LEFT, Graphics_BOTTOM);
155 }
156 MelderString_append (buffer,
157 U"<p align=middle><img height=", paragraph -> height * 100,
158 U" width=", paragraph -> width * 100, U" src=", MelderFile_name (& pngFile), U"></p>"
159 );
160 continue;
161 }
162 if (paragraph -> type == kManPage_type::SCRIPT) {
163 chunkNumber += 1;
164 if (paragraph -> cacheInfo.string && paragraph -> cacheInfo.string [0] != U'\0') {
165 trace (U"text output");
166 MelderString_append (buffer, U"<code style=\"color:red\">=&gt;</code><br>\n"); // show a red arrow "=>"
167 static MelderString lineBuffer;
168 MelderString_empty (& lineBuffer);
169 bool inOutputTable = false;
170 integer outputTableRowNumber = 0;
171 bool hasError = false;
172 for (const char32 *paragraphPointer = & paragraph -> cacheInfo.string [0]; *paragraphPointer != U'\0'; paragraphPointer ++) {
173 if (Melder_isEndOfLine (*paragraphPointer)) {
174 hasError |=
175 str32str (lineBuffer.string, U"**AN ERROR OCCURRED IN THIS CODE CHUNK:**") ||
176 str32str (lineBuffer.string, U"**ERROR** This code chunk was not run,");
177 const bool isTableRow = str32chr (lineBuffer.string, U'\t');
178 if (hasError)
179 MelderString_append (buffer, U"<code style=\"color:red\"> ");
180 else if (isTableRow) {
181 if (! inOutputTable) {
182 /*
183 A new table in the chunk output.
184 */
185 MelderString_append (buffer, U"<code><table cellpadding=0 style='text-align:center;border-spacing:0'>\n"
186 "<tr><th>");
187 inOutputTable = true;
188 outputTableRowNumber = 1;
189 } else {
190 /*
191 A new row in an existing table in the chunk output.
192 */
193 MelderString_append (buffer, U"<tr>"
194 "<td>");
195 outputTableRowNumber += 1;
196 }
197 } else {
198 if (inOutputTable) {
199 MelderString_append (buffer, U"</table></code>\n");
200 inOutputTable = false;
201 outputTableRowNumber = 0;
202 }
203 MelderString_append (buffer, U"<code> ");
204 }
205 for (const char32 *plineBuffer = & lineBuffer.string [0]; *plineBuffer != U'\0'; plineBuffer ++) {
206 /*if (plineBuffer [0] == U' ' && plineBuffer [1] == U' ') {
207 MelderString_append (buffer, U" &nbsp;");
208 plineBuffer += 1;
209 } else*/
210 if (plineBuffer [0] == U' ') {
211 MelderString_append (buffer, U" ");
212 } else if (plineBuffer [0] == U'\t') {
213 if (inBold) {
214 inBold = false;
215 MelderString_append (buffer, U"</b>");
216 } else if (inItalic) {
217 inItalic = false;
218 MelderString_append (buffer, U"</i>");
219 }
220 if (outputTableRowNumber == 1)
221 MelderString_append (buffer, U"<th>");
222 else
223 MelderString_append (buffer, U"<td>");
224 } else if (plineBuffer [0] == U'\\' && plineBuffer [1] == U'#' && plineBuffer [2] == U'{') {
225 inBold = true;
226 MelderString_append (buffer, U"<b>");
227 plineBuffer += 2;
228 } else if (plineBuffer [0] == U'\\' && plineBuffer [1] == U'%' && plineBuffer [2] == U'{') {
229 inItalic = true;
230 MelderString_append (buffer, U"<i>");
231 plineBuffer += 2;
232 } else if (plineBuffer [0] == U'}') {
233 if (inBold) {
234 inBold = false;
235 MelderString_append (buffer, U"</b>");
236 } else if (inItalic) {
237 inItalic = false;
238 MelderString_append (buffer, U"</i>");
239 } else
240 MelderString_appendCharacter (buffer, plineBuffer [0]);
241 } else
242 MelderString_appendCharacter (buffer, plineBuffer [0]);
243 }
244 if (inBold) {
245 inBold = false;
246 MelderString_append (buffer, U"</b>");
247 }
248 if (inItalic) {
249 inItalic = false;
250 MelderString_append (buffer, U"</i>");
251 }
252 if (isTableRow)
253 MelderString_append (buffer, U"</tr>\n");
254 else
255 MelderString_append (buffer, U"<br></code>\n");
256 MelderString_empty (& lineBuffer);
257 } else {
258 MelderString_appendCharacter (& lineBuffer, *paragraphPointer);
259 }
260 }
261 if (hasError)
262 continue; // no longer show any graphics
263 if (inOutputTable) {
264 MelderString_append (buffer, U"</table></code>\n");
265 inOutputTable = false;
266 outputTableRowNumber = 0;
267 }
268 }
269 if (paragraph -> height == 0.001)
270 continue;
271 numberOfPictures ++;
272 structMelderFile pngFile { };
273 MelderFile_copy (file, & pngFile);
274 pngFile. path [Melder_length (pngFile. path) - 5] = U'\0'; // delete extension ".html"
275 str32cat (pngFile. path, Melder_cat (U"_", numberOfPictures, U".png"));
276 {// scope
277 autoGraphics graphics = Graphics_create_pngfile (& pngFile, PNG_RESOLUTION, 0.0, paragraph -> width, 0.0, paragraph -> height);
278 Graphics_setFont (graphics.get(), kGraphics_font::TIMES);
279 Graphics_setFontStyle (graphics.get(), 0);
280 Graphics_setFontSize (graphics.get(), 12.0);
281 Graphics_setWrapWidth (graphics.get(), 0);
282 theCurrentPraatApplication = & praatApplication;
283 theCurrentPraatApplication -> batch = true;
284 theCurrentPraatApplication -> topShell = theForegroundPraatApplication. topShell; // needed for UiForm_create () in dialogs
285 theCurrentPraatObjects = & praatObjects;
286 theCurrentPraatPicture = & praatPicture;
287 theCurrentPraatPicture -> graphics = graphics.get(); // FIXME: should be move()?
288 theCurrentPraatPicture -> font = kGraphics_font::TIMES;
289 theCurrentPraatPicture -> fontSize = 12.0;
290 theCurrentPraatPicture -> lineType = Graphics_DRAWN;
291 theCurrentPraatPicture -> colour = Melder_BLACK;
292 theCurrentPraatPicture -> lineWidth = 1.0;
293 theCurrentPraatPicture -> arrowSize = 1.0;
294 theCurrentPraatPicture -> speckleSize = 1.0;
295 theCurrentPraatPicture -> x1NDC = 0.0;
296 theCurrentPraatPicture -> x2NDC = paragraph -> width;
297 theCurrentPraatPicture -> y1NDC = 0.0;
298 theCurrentPraatPicture -> y2NDC = paragraph -> height;
299 Graphics_setViewport (graphics.get(),
300 theCurrentPraatPicture -> x1NDC, theCurrentPraatPicture -> x2NDC,
301 theCurrentPraatPicture -> y1NDC, theCurrentPraatPicture -> y2NDC
302 );
303 Graphics_setWindow (graphics.get(), 0.0, 1.0, 0.0, 1.0);
304 integer x1DC, y1DC, x2DC, y2DC;
305 Graphics_WCtoDC (graphics.get(), 0.0, 0.0, & x1DC, & y2DC);
306 Graphics_WCtoDC (graphics.get(), 1.0, 1.0, & x2DC, & y1DC);
307 Graphics_resetWsViewport (graphics.get(), x1DC, x2DC, y1DC, y2DC);
308 Graphics_setWsWindow (graphics.get(), 0.0, paragraph -> width, 0.0, paragraph -> height);
309 theCurrentPraatPicture -> x1NDC = 0.0;
310 theCurrentPraatPicture -> x2NDC = paragraph -> width;
311 theCurrentPraatPicture -> y1NDC = 0.0;
312 theCurrentPraatPicture -> y2NDC = paragraph -> height;
313 Graphics_setViewport (graphics.get(),
314 theCurrentPraatPicture -> x1NDC, theCurrentPraatPicture -> x2NDC,
315 theCurrentPraatPicture -> y1NDC, theCurrentPraatPicture -> y2NDC
316 );
317 {// scope
318 autoMelderProgressOff progress;
319 autoMelderWarningOff nowarn;
320 autoMelderSaveCurrentFolder saveFolder;
321 if (! MelderFolder_isNull (& my rootDirectory))
322 Melder_setCurrentFolder (& my rootDirectory);
323 const bool dollarSignWasCode = graphics -> dollarSignIsCode;
324 const bool backquoteWasVerbatim = graphics -> backquoteIsVerbatim;
325 const bool atSignWasLink = graphics -> atSignIsLink;
326 Graphics_setDollarSignIsCode (graphics.get(), false);
327 Graphics_setBackquoteIsVerbatim (graphics.get(), false);
328 Graphics_setAtSignIsLink (graphics.get(), false);
329 Graphics_play (paragraph -> cacheGraphics.get(), graphics.get());
330 if (dollarSignWasCode)
331 Graphics_setDollarSignIsCode (graphics.get(), true);
332 if (backquoteWasVerbatim)
333 Graphics_setBackquoteIsVerbatim (graphics.get(), true);
334 if (atSignWasLink)
335 Graphics_setAtSignIsLink (graphics.get(), true);
336 }
337 Graphics_setViewport (graphics.get(), 0.0, 1.0, 0.0, 1.0);
338 Graphics_setWindow (graphics.get(), 0.0, 1.0, 0.0, 1.0);
339 Graphics_setTextAlignment (graphics.get(), Graphics_LEFT, Graphics_BOTTOM);
340 }
341 MelderString_append (buffer,
342 U"<p align=middle><img height=", paragraph -> height * 100,
343 U" width=", paragraph -> width * 100, U" src=", MelderFile_name (& pngFile), U"></p>"
344 );
345 theCurrentPraatApplication = & theForegroundPraatApplication;
346 theCurrentPraatObjects = & theForegroundPraatObjects;
347 theCurrentPraatPicture = & theForegroundPraatPicture;
348 continue;
349 }
350
351 if (isListItem || isTag || isDefinition) {
352 if (! inList) {
353 ul = ( isListItem && (p [0] == U'•' || (p [0] == U'\\' && p [1] == U'b' && p [2] == U'u')) );
354 ul = false; // TODO
355 MelderString_append (buffer, ul ? U"<ul>\n" : U"<dl>\n");
356 inList = true;
357 }
358 if (ul) {
359 if (p [0] == U'•' && p [1] == U' ')
360 p += 1;
361 if (p [0] == U'\\' && p [1] == U'b' && p [2] == U'u' && p [3] == U' ')
362 p += 3;
363 }
364 MelderString_append (buffer, ul ? U"<li>" : stylesInfo [(int) paragraph -> type]. htmlIn/*, U"\n"*/);
365 } else {
366 if (inList) {
367 MelderString_append (buffer, ul ? U"</ul>\n" : U"</dl>\n");
368 inList = ul = false;
369 }
370 MelderString_append (buffer, stylesInfo [(int) paragraph -> type]. htmlIn,
371 paragraphIsVerbatim ? U"" : U""); // in a verbatim paragraph we would actually get a newline
372 }
373 /* mutable */ bool inTable = !! str32chr (p, U'\t');
374 /* mutable */ bool inPromptedTable = false;
375 if (inTable) {
376 if (*p == U'\t') {
377 MelderString_append (buffer, U"<table border=0 cellpadding=0 cellspacing=0><tr><td width=100 align=middle>");
378 p ++;
379 } else {
380 MelderString_append (buffer, U"<table border=0 cellpadding=0 cellspacing=0><tr><td width=100 align=left>");
381 inPromptedTable = true;
382 }
383 }
384 /*
385 Leading spaces should be visible, so we convert them to a non-breaking space ("&nbsp;").
386 This is not needed in code fragments, because we have already have a "whitespace:pre-wrap" style there.
387 */
388 if (! paragraphIsVerbatim)
389 while (*p == U' ') {
390 MelderString_append (buffer, U"&nbsp;");
391 p ++;
392 }
393 if (paragraphIsVerbatim) {
394 while (*p != U'\0') {
395 if (*p == U'\\' && p [1] == U'#' && p [2] == U'{') {
396 MelderString_append (buffer, U"<b>");
397 p += 3;
398 while (*p != U'}' && *p != U'\0')
399 MelderString_append (buffer, *p ++);
400 MelderString_append (buffer, U"</b>");
401 if (*p)
402 p ++; // skip closing brace
403 } else if (*p == U'\\' && p [1] == U'%' && p [2] == U'{') {
404 MelderString_append (buffer, U"<i>");
405 p += 3;
406 while (*p != U'}' && *p != U'\0')
407 MelderString_append (buffer, *p ++);
408 MelderString_append (buffer, U"</i>");
409 if (*p)
410 p ++; // skip closing brace
411 } else if (
412 *p == U'\\' && p [1] == U'@' && p [2] == U'{' ||
413 *p == U'\\' && p [1] == U'#' && p [2] == U'@' && p [3] == U'{' ||
414 *p == U'\\' && p [1] == U'`' && p [2] == U'{' ||
415 *p == U'\\' && p [1] == U'#' && p [2] == U'`' && p [3] == U'{'
416 ) {
417 const bool isBold = ( p [1] == U'#' );
418 p += 3 + isBold;
419 if (isBold)
420 MelderString_append (buffer, U"<b>");
421 const bool isVerbatim = ( p [-2] == U'`' );
422 static MelderString link, linkText;
423 MelderString_empty (& link);
424 if (isVerbatim)
425 MelderString_appendCharacter (& link, U'`');
426 MelderString_empty (& linkText);
427 while (*p != U'}' && *p != U'\0') {
428 if (*p == U'|' && p [1] == U'|') {
429 p += 2;
430 MelderString_empty (& linkText);
431 } else {
432 MelderString_append (& link, *p);
433 MelderString_append (& linkText, *p);
434 p ++;
435 }
436 }
437 if (*p)
438 p ++; // skip closing brace
439 if (link.length > 0 && link.string [link.length - 1] == U':') {
440 link.string [link.length - 1] = U'.';
441 MelderString_append (& link, U"..");
442 }
443 if (isVerbatim)
444 MelderString_appendCharacter (& link, U'`');
445 writeLinkAsHtml (me, link.string, linkText.string, buffer, page -> title.get());
446 if (isBold)
447 MelderString_append (buffer, U"</b>");
448 } else if (*p == U'<') {
449 MelderString_append (buffer, U"&lt;");
450 p ++;
451 } else if (*p == U'>') {
452 MelderString_append (buffer, U"&gt;");
453 p ++;
454 } else if (*p == U'&') {
455 MelderString_append (buffer, U"&amp;");
456 p ++;
457 } else
458 MelderString_append (buffer, *p ++);
459 }
460 MelderString_append (buffer, stylesInfo [(int) paragraph -> type]. htmlOut, U"\n");
461 continue;
462 }
463 while (*p) {
464 if (wordItalic && ! isSingleWordCharacter (*p)) {
465 if (*p == U'\\' && p [1] != U'\0' && p [2] != U'\0') {
466 Longchar_Info info = Longchar_getInfo (p [1], p [2]);
467 if (! isSingleWordCharacter (info -> unicode)) {
468 MelderString_append (buffer, U"</i>");
469 wordItalic = false;
470 }
471 } else {
472 MelderString_append (buffer, U"</i>");
473 wordItalic = false;
474 }
475 }
476 if (wordBold && ! isSingleWordCharacter (*p)) {
477 if (*p == U'\\' && p [1] != U'\0' && p [2] != U'\0') {
478 Longchar_Info info = Longchar_getInfo (p [1], p [2]);
479 if (! isSingleWordCharacter (info -> unicode)) {
480 MelderString_append (buffer, U"</b>");
481 wordBold = false;
482 }
483 } else {
484 MelderString_append (buffer, U"</b>");
485 wordBold = false;
486 }
487 }
488 if (wordCode && ! isSingleWordCharacter (*p)) { // TODO: remove, because wordCode should no longer exist
489 if (*p == U'\\' && p [1] != U'\0' && p [2] != U'\0') {
490 Longchar_Info info = Longchar_getInfo (p [1], p [2]);
491 if (! isSingleWordCharacter (info -> unicode)) {
492 MelderString_append (buffer, U"</font></code>");
493 wordCode = false;
494 }
495 } else {
496 MelderString_append (buffer, U"</font></code>");
497 wordCode = false;
498 }
499 }
500 if (*p == U'@') {
501 static MelderString link, linkText;
502 MelderString_empty (& link);
503 MelderString_empty (& linkText);
504 if (p [1] == U'@' && ! paragraphIsVerbatim) {
505 p += 2;
506 while (*p != U'@' && *p != U'|' && *p != U'\0')
507 MelderString_appendCharacter (& link, * p ++);
508 if (*p == U'|') {
509 if (p [1] == U'|') {
510 const char32 *p2 = p + 2;
511 while (*p2 != U'@' && *p2 != U'\0')
512 MelderString_appendCharacter (& link, * p2 ++);
513 p += 2; // skip "||"
514 } else
515 p += 1; // skip "|"
516 while (*p != U'@' && *p != U'\0') {
517 if (*p == U'^') {
518 if (inSuper) {
519 MelderString_append (& linkText, U"</sup>");
520 inSuper = false;
521 p ++;
522 } else if (p [1] == U'^') {
523 MelderString_append (& linkText, U"<sup>");
524 inSuper = true;
525 p += 2;
526 } else {
527 MelderString_append (& linkText, U"<sup>");
528 letterSuper = true;
529 p ++;
530 }
531 } else {
532 if (*p == U'\\') {
533 const char32 kar1 = *++p, kar2 = *++p;
534 Longchar_Info info = Longchar_getInfo (kar1, kar2);
535 if (info -> unicode < 127) {
536 MelderString_appendCharacter (& linkText, info -> unicode ? info -> unicode : U'?');
537 } else {
538 MelderString_append (& linkText, U"&#", (int) info -> unicode, U";");
539 }
540 p ++;
541 } else {
542 if (*p < 127) {
543 MelderString_appendCharacter (& linkText, *p);
544 } else {
545 MelderString_append (& linkText, U"&#", (int) *p, U";");
546 }
547 p ++;
548 }
549 if (letterSuper) {
550 //if (wordItalic) { MelderString_append (buffer, U"</i>"); wordItalic = false; }
551 //if (wordBold) { MelderString_append (buffer, U"</b>"); wordBold = false; }
552 MelderString_append (& linkText, U"</sup>");
553 letterSuper = false;
554 }
555 }
556 }
557 } else {
558 MelderString_copy (& linkText, link.string);
559 }
560 if (*p)
561 p ++;
562 } else if (p [1] == U'`') {
563 p += 2;
564 MelderString_append (& link, U'`');
565 MelderString_append (& linkText, U"<b><code><font size=+1>");
566 while (*p != U'`' && *p != U'\0') {
567 MelderString_append (& link, * p);
568 MelderString_append (& linkText, * p);
569 p ++;
570 }
571 if (*p)
572 p ++; // skip closing backquote
573 MelderString_append (& linkText, U"</font></code></b>");
574 MelderString_append (& link, U'`');
575 } else {
576 p ++;
577 while (isSingleWordCharacter (*p) && *p != U'\0')
578 MelderString_append (& link, *p++);
579 MelderString_copy (& linkText, link.string);
580 }
581 writeLinkAsHtml (me, link.string, linkText.string, buffer, page -> title.get());
582 } else if (*p == U'%') {
583 if (p [1] == U'`') {
584 p += 2;
585 MelderString_append (buffer, U"<i><code><font size=+1>");
586 while (*p != U'`' && *p != U'\0')
587 MelderString_append (buffer, * p ++);
588 if (*p)
589 p ++;
590 MelderString_append (buffer, U"</font></code></i>");
591 } else if (inItalic) {
592 MelderString_append (buffer, U"</i>");
593 inItalic = false;
594 p ++;
595 } else if (p [1] == U'%') {
596 MelderString_append (buffer, U"<i>");
597 inItalic = true;
598 p += 2;
599 } else if (p [1] == U'#') {
600 MelderString_append (buffer, U"<i><b>");
601 wordItalic = true;
602 wordBold = true;
603 p += 2;
604 } else {
605 MelderString_append (buffer, U"<i>");
606 wordItalic = true;
607 p ++;
608 }
609 } else if (*p == U'_') {
610 if (inSub) {
611 if (wordItalic) { MelderString_append (buffer, U"</i>"); wordItalic = false; }
612 if (wordBold) { MelderString_append (buffer, U"</b>"); wordBold = false; }
613 MelderString_append (buffer, U"</sub>");
614 inSub = false;
615 p ++;
616 } else if (p [1] == U'_') {
617 if (wordItalic) { MelderString_append (buffer, U"</i>"); wordItalic = false; }
618 if (wordBold) { MelderString_append (buffer, U"</b>"); wordBold = false; }
619 MelderString_append (buffer, U"<sub>");
620 inSub = true;
621 p += 2;
622 } else {
623 if (verbatimAware) {
624 if (wordItalic) { MelderString_append (buffer, U"</i>"); wordItalic = false; }
625 if (wordBold) { MelderString_append (buffer, U"</b>"); wordBold = false; }
626 MelderString_append (buffer, U"<sub>");
627 letterSub = true;
628 } else
629 MelderString_append (buffer, U"_");
630 p ++;
631 }
632 } else if (*p == U'#') {
633 if (p [1] == U'`') {
634 p += 2;
635 MelderString_append (buffer, U"<b><code><font size=+1>");
636 while (*p != U'`' && *p != U'\0')
637 MelderString_append (buffer, * p ++);
638 if (*p)
639 p ++;
640 MelderString_append (buffer, U"</font></code></b>");
641 } else if (inBold) {
642 MelderString_append (buffer, U"</b>");
643 inBold = false;
644 p ++;
645 } else if (p [1] == U'#') {
646 MelderString_append (buffer, U"<b>");
647 inBold = true;
648 p += 2;
649 } else if (p [1] == U'%') {
650 MelderString_append (buffer, U"<b><i>");
651 wordBold = true;
652 wordItalic = true;
653 p += 2;
654 } else {
655 MelderString_append (buffer, U"<b>");
656 wordBold = true;
657 p ++;
658 }
659 } else if (*p == U'$') {
660 if (inCode) {
661 MelderString_append (buffer, U"</font></code>");
662 inCode = false;
663 p ++;
664 } else if (p [1] == U'$') {
665 MelderString_append (buffer, U"<code><font size=+1>");
666 inCode = true;
667 p += 2;
668 } else if (p [1] == U'%') {
669 MelderString_append (buffer, U"<code><font size=+1><i>");
670 wordCode = true;
671 wordItalic = true;
672 p += 2;
673 } else if (p [1] == U'#') {
674 MelderString_append (buffer, U"<code><font size=+1><b>");
675 wordCode = true;
676 wordBold = true;
677 p += 2;
678 } else {
679 MelderString_append (buffer, U"<code><font size=+1>");
680 wordCode = true;
681 p ++;
682 }
683 } else if (verbatimAware && *p == U'`') {
684 MelderString_append (buffer, U"<code><font size=+1>");
685 ++ p;
686 while (*p != U'\0' && *p != U'`')
687 MelderString_append (buffer, *p ++);
688 if (*p)
689 ++ p; // skip closing backquote
690 MelderString_append (buffer, U"</font></code>");
691 } else if (*p == U'^') {
692 if (inSuper) {
693 /*if (wordItalic) { MelderString_append (buffer, U"</i>"); wordItalic = false; }
694 if (wordBold) { MelderString_append (buffer, U"</b>"); wordBold = false; }*/
695 MelderString_append (buffer, U"</sup>");
696 inSuper = false;
697 p ++;
698 } else if (p [1] == U'^') {
699 /*if (wordItalic) { MelderString_append (buffer, U"</i>"); wordItalic = false; }
700 if (wordBold) { MelderString_append (buffer, U"</b>"); wordBold = false; }*/
701 MelderString_append (buffer, U"<sup>");
702 inSuper = true;
703 p += 2;
704 } else {
705 /*if (wordItalic) { MelderString_append (buffer, U"</i>"); wordItalic = false; }
706 if (wordBold) { MelderString_append (buffer, U"</b>"); wordBold = false; }*/
707 MelderString_append (buffer, U"<sup>");
708 letterSuper = true;
709 p ++;
710 }
711 } else if (*p == U'}') {
712 if (inSmall) {
713 MelderString_append (buffer, U"</font>");
714 inSmall = false;
715 p ++;
716 } else {
717 MelderString_append (buffer, U"}");
718 p ++;
719 }
720 } else if (*p == U'\\' && p [1] == U's' && p [2] == U'{') {
721 MelderString_append (buffer, U"<font size=-1>");
722 inSmall = true;
723 p += 3;
724 } else if (*p == U'\t' && inTable) {
725 if (inPromptedTable) {
726 inPromptedTable = false;
727 p ++; // skip one tab
728 } else {
729 MelderString_append (buffer, U"<td width=100 align=middle>");
730 p ++;
731 }
732 } else if (*p == U'<') {
733 MelderString_append (buffer, U"&lt;");
734 p ++;
735 } else if (*p == U'>') {
736 MelderString_append (buffer, U"&gt;");
737 p ++;
738 } else if (*p == U'&') {
739 MelderString_append (buffer, U"&amp;");
740 p ++;
741 } else {
742 //if (wordItalic && ! isSingleWordCharacter (*p)) { MelderString_append (buffer, U"</i>"); wordItalic = false; }
743 //if (wordBold && ! isSingleWordCharacter (*p)) { MelderString_append (buffer, U"</b>"); wordBold = false; }
744 //if (wordCode && ! isSingleWordCharacter (*p)) { MelderString_append (buffer, U"</code>"); wordCode = false; }
745 if (*p == U'\\' && p [1] != U'\0' && p [2] != U'\0') {
746 char32 kar1 = *++p, kar2 = *++p;
747 Longchar_Info info = Longchar_getInfo (kar1, kar2);
748 if (info -> unicode < 127)
749 MelderString_appendCharacter (buffer, info -> unicode ? info -> unicode : U'?');
750 else
751 MelderString_append (buffer, U"&#", (int) info -> unicode, U";");
752 p ++;
753 } else {
754 if (*p < 127)
755 MelderString_appendCharacter (buffer, *p);
756 else
757 MelderString_append (buffer, U"&#", (int) *p, U";");
758 p ++;
759 }
760 if (letterSub) {
761 if (wordItalic) {
762 MelderString_append (buffer, U"</i>");
763 wordItalic = false;
764 }
765 if (wordBold) {
766 MelderString_append (buffer, U"</b>");
767 wordBold = false;
768 }
769 MelderString_append (buffer, U"</sub>");
770 letterSub = false;
771 }
772 if (letterSuper) {
773 if (wordItalic) {
774 MelderString_append (buffer, U"</i>");
775 wordItalic = false;
776 }
777 if (wordBold) {
778 MelderString_append (buffer, U"</b>");
779 wordBold = false;
780 }
781 MelderString_append (buffer, U"</sup>");
782 letterSuper = false;
783 }
784 }
785 }
786 if (inItalic || wordItalic) {
787 MelderString_append (buffer, U"</i>");
788 inItalic = wordItalic = false;
789 }
790 if (inBold || wordBold) {
791 MelderString_append (buffer, U"</b>");
792 inBold = wordBold = false;
793 }
794 if (inCode || wordCode) {
795 MelderString_append (buffer, U"</font></code>");
796 inCode = wordCode = false;
797 }
798 if (inSub || letterSub) {
799 MelderString_append (buffer, U"</sub>");
800 inSub = letterSub = false;
801 }
802 if (inSuper || letterSuper) {
803 MelderString_append (buffer, U"</sup>");
804 inSuper = letterSuper = false;
805 }
806 if (inTable) {
807 MelderString_append (buffer, U"</table>");
808 inTable = false;
809 }
810 MelderString_append (buffer, stylesInfo [(int) paragraph -> type]. htmlOut, U"\n");
811 }
812 if (inList) {
813 MelderString_append (buffer, ul ? U"</ul>\n" : U"</dl>\n");
814 inList = false;
815 }
816 praatObjects. reset();
817 }
818
819 static void writeTitleAsHtml (conststring32 title, MelderString *buffer) {
820 static MelderString titleBuffer;
821 const bool titleHasToBeVerbatim = ( title [0] == U'`' );
822 if (titleHasToBeVerbatim)
823 MelderString_ncopy (& titleBuffer, & title [1], Melder_length (& title [1]) - 1);
824 else
825 MelderString_copy (& titleBuffer, title);
826 MelderString_append (buffer,
827 U"<html><head><meta name=\"robots\" content=\"index,follow\">"
828 U"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n"
829 U"<title>", titleBuffer.string, U"</title>\n"
830 U"<style>\n"
831 U" td { padding-left: 5pt; padding-right: 5pt; }\n"
832 U" th { padding-left: 5pt; padding-right: 5pt; }\n"
833 U" code { white-space: pre-wrap; }\n"
834 U" dd { white-space: pre-wrap; }\n"
835 U"</style>\n"
836 U"</head><body bgcolor=\"#FFFFFF\">\n\n"
837 );
838 MelderString_append (buffer,
839 U"<table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor=\"#CCCC00\">"
840 U"<table border=4 cellpadding=9><tr><td align=middle bgcolor=\"#000000\">",
841 titleHasToBeVerbatim ?
842 U"<font face=\"Monaco,Courier\" size=6 color=\"#999900\"><b>\n" :
843 U"<font face=\"Palatino,Times\" size=6 color=\"#999900\"><b>\n",
844 titleBuffer.string, U"\n</b></font></table></table>\n"
845 );
846 }
847
848 static void writePageAsHtml (ManPages me, Interpreter optionalInterpreterReference, MelderFile file, integer ipage, MelderString *buffer) {
849 ManPage page = my pages.at [ipage];
850 writeTitleAsHtml (page -> title.get(), buffer);
851 writeParagraphsAsHtml (me, optionalInterpreterReference, file, page, buffer);
852 if (ManPages_uniqueLinksHither (me, ipage)) {
853 integer ilink, jlink;
854 if (page -> paragraphs.size > 0) {
855 conststring32 text = page -> paragraphs [page -> paragraphs.size]. text;
856 if (text && text [0] != U'\0' && text [Melder_length (text) - 1] != U':')
857 MelderString_append (buffer, U"<h3>Links to this page</h3>\n");
858 }
859 MelderString_append (buffer, U"<ul>\n");
860 for (ilink = 1; ilink <= page -> linksHither.size; ilink ++) {
861 integer link = page -> linksHither [ilink];
862 bool alreadyShown = false;
863 for (jlink = 1; jlink <= page -> linksThither.size; jlink ++)
864 if (page -> linksThither [jlink] == link)
865 alreadyShown = true;
866 if (! alreadyShown) {
867 ManPage linkingPage = my pages.at [page -> linksHither [ilink]];
868 conststring32 title = linkingPage -> title.get();
869 const char32 *p;
870 MelderString_append (buffer, U"<li><a href=\"");
871 for (p = & title [0]; *p; p ++) {
872 if (p - title >= LONGEST_FILE_NAME)
873 break;
874 if (isAllowedFileNameCharacter (*p))
875 MelderString_appendCharacter (buffer, *p);
876 else if (*p == U'#')
877 MelderString_append (buffer, U"-H");
878 else if (*p == U'$')
879 MelderString_append (buffer, U"-S");
880 else if (*p == U'@')
881 MelderString_append (buffer, U"-C");
882 else
883 MelderString_appendCharacter (buffer, U'_');
884 }
885 if (title [0] == U'\0')
886 MelderString_append (buffer, U"_");
887 if (title [0] == U'`') {
888 static MelderString visibleTitle;
889 MelderString_copy (& visibleTitle, title);
890 if (visibleTitle.string [visibleTitle.length - 1] == U'`')
891 visibleTitle.string [visibleTitle.length - 1] = U'\0';
892 MelderString_append (buffer, U".html\"><code><font size=+1>",
893 & visibleTitle.string [1], U"</font></code></a>\n");
894 } else
895 MelderString_append (buffer, U".html\">", title, U"</a>\n");
896 }
897 }
898 MelderString_append (buffer, U"</ul>\n");
899 }
900 MelderString_append (buffer, U"<hr>\n<address>\n\t<p>", page -> signature.get());
901 MelderString_append (buffer, U"</p>\n</address>\n</body>\n</html>\n");
902 }
903
904 void ManPages_writeOneToHtmlFile (ManPages me, Interpreter optionalInterpreterReference, integer ipage, MelderFile file) {
905 static MelderString buffer;
906 MelderString_empty (& buffer);
907 writePageAsHtml (me, optionalInterpreterReference, file, ipage, & buffer);
908 MelderFile_writeText_e (file, buffer.string, kMelder_textOutputEncoding::UTF8);
909 }
910
911 void ManPages_writeAllToHtmlDir (ManPages me, Interpreter optionalInterpreterReference, conststring32 dirPath) {
912 structMelderFolder dir { };
913 Melder_pathToFolder (dirPath, & dir);
914 for (integer ipage = 1; ipage <= my pages.size; ipage ++) {
915 ManPage page = my pages.at [ipage];
916 char32 fileName [ManPages_FILENAME_BUFFER_SIZE];
917 Melder_assert (Melder_length (page -> title.get()) < ManPages_FILENAME_BUFFER_SIZE - 100);
918 trace (U"page ", ipage, U": ", page -> title.get());
919 char32 *to = fileName, *max = fileName + ManPages_FILENAME_BUFFER_SIZE - (8 + 1);
920 for (const char32 *from = & page -> title [0]; *from != U'\0'; from ++) {
921 if (isAllowedFileNameCharacter (*from)) {
922 if (to < max)
923 *to ++ = *from;
924 } else if (*from == U'#') {
925 if (to < max)
926 *to ++ = U'-';
927 if (to < max)
928 *to ++ = U'H';
929 } else if (*from == U'$') {
930 if (to < max)
931 *to ++ = U'-';
932 if (to < max)
933 *to ++ = U'S';
934 } else if (*from == U'@') {
935 if (to < max)
936 *to ++ = U'-';
937 if (to < max)
938 *to ++ = U'C';
939 } else {
940 if (to < max)
941 *to ++ = U'_';
942 }
943 }
944 if (to == fileName)
945 *to ++ = U'_'; // no empty file names please
946 *to = U'\0';
947 fileName [LONGEST_FILE_NAME] = U'\0'; //
948 str32cat (fileName, U".html");
949 static MelderString buffer;
950 MelderString_empty (& buffer);
951 structMelderFile file { };
952 MelderFolder_getFile (& dir, fileName, & file);
953 writePageAsHtml (me, optionalInterpreterReference, & file, ipage, & buffer);
954 /*
955 An optimization because reading is much faster than writing:
956 we write the file only if the old file is different or doesn't exist.
957 */
958 autostring32 oldText;
959 try {
960 oldText = MelderFile_readText (& file);
961 } catch (MelderError) {
962 Melder_clearError ();
963 }
964 if (! oldText // doesn't the file exist yet?
965 || str32cmp (buffer.string, oldText.get())) // isn't the old file identical to the new text?
966 {
967 MelderFile_writeText_e (& file, buffer.string, kMelder_textOutputEncoding::UTF8); // then write the new text
968 }
969 }
970 }
971
972 /* End of file ManPages_toHtml.cpp */