| 1 |
/* ManPages.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 |
|
| 21 |
Thing_implement (ManPages, Daata, 0); |
| 22 |
|
| 23 |
#define MAXIMUM_LINK_LENGTH 500 |
| 24 |
|
| 25 |
static integer lookUp_unsorted (ManPages me, conststring32 title); |
| 26 |
|
| 27 |
void structManPages :: v9_destroy () noexcept { |
| 28 |
for (integer ipage = 1; ipage <= our pages.size; ipage ++) { |
| 29 |
ManPage page = our pages.at [ipage]; |
| 30 |
for (integer ipar = 1; ipar <= page -> paragraphs.size; ipar ++) { |
| 31 |
ManPage_Paragraph par = & page -> paragraphs [ipar]; |
| 32 |
Melder_free (par -> text); // not an autostring32, because it can be a string literal (if not dynamic) |
| 33 |
} |
| 34 |
page -> linksHither. reset(); // TODO automate |
| 35 |
page -> linksThither. reset(); |
| 36 |
} |
| 37 |
ManPages_Parent :: v9_destroy (); |
| 38 |
} |
| 39 |
|
| 40 |
static conststring32 ManPage_Paragraph_extractLink (ManPage_Paragraph par, const char32 *p, char32 *link, bool verbatimAware) { |
| 41 |
const bool paragraphIsVerbatim = ( verbatimAware && par -> couldVerbatim () ); |
| 42 |
conststring32 text = par -> text; |
| 43 |
Melder_assert (text); |
| 44 |
char32 *to = & link [0]; |
| 45 |
if (! p) |
| 46 |
p = text; |
| 47 |
/* |
| 48 |
Search for the next link. |
| 49 |
*/ |
| 50 |
for (;; p ++) { |
| 51 |
if (*p == U'\0') |
| 52 |
return nullptr; // no link found |
| 53 |
if (*p == U'@' && ! paragraphIsVerbatim) { |
| 54 |
/* |
| 55 |
Found a "@" in running text. |
| 56 |
Ignore it if it is inside a backslash trigraph. |
| 57 |
*/ |
| 58 |
if (! (p - text <= 0 || (p [-1] != U'\\' && (p - text <= 1 || p [-2] != U'\\')))) |
| 59 |
continue; |
| 60 |
if (p [1] == U'@') { |
| 61 |
/* |
| 62 |
We found "@@", starting a link in running text. |
| 63 |
*/ |
| 64 |
const char32 *from = p + 2; |
| 65 |
while (*from != U'@' && *from != U'|' && *from != U'\0') { |
| 66 |
if (to - link >= MAXIMUM_LINK_LENGTH) |
| 67 |
Melder_throw (U"(ManPage_Paragraph_extractLink:) Link starting with “@@” is too long:\n", text); |
| 68 |
*to ++ = *from ++; |
| 69 |
} |
| 70 |
/* |
| 71 |
Ignore the "|...@" part, unless it is "||...@". |
| 72 |
*/ |
| 73 |
if (*from == U'|') { |
| 74 |
if (from [1] == U'|') { |
| 75 |
/* |
| 76 |
Found a "||xxx@" part. Append the xxx part. |
| 77 |
*/ |
| 78 |
from += 2; // skip "||" |
| 79 |
while (*from != U'@' && *from != U'\0') { |
| 80 |
if (*from == U'\0') |
| 81 |
break; |
| 82 |
if (to - link >= MAXIMUM_LINK_LENGTH) |
| 83 |
Melder_throw (U"(ManPage_Paragraph_extractLink:) Link starting with “@@” and containing “||” is too long:\n", text); |
| 84 |
*to ++ = *from ++; |
| 85 |
} |
| 86 |
} else { |
| 87 |
/* |
| 88 |
Found a "|xxx@" part. ignore all of it. |
| 89 |
*/ |
| 90 |
from ++; |
| 91 |
while (*from != U'@' && *from != U'\0') |
| 92 |
from ++; |
| 93 |
} |
| 94 |
} |
| 95 |
Melder_assert (to - link <= MAXIMUM_LINK_LENGTH); |
| 96 |
*to = U'\0'; |
| 97 |
p = from + ( *from == U'@' ); // add bool to pointer: skip '@' but not '\0' |
| 98 |
return p; |
| 99 |
} else if (p [1] == U'`' /*&& verbatimAware*/) { // TODO: remove once manuals have been converted to notebooks |
| 100 |
/* |
| 101 |
We found "@`", starting a verbatim link in running text. |
| 102 |
*/ |
| 103 |
const char32 *from = p + 1; |
| 104 |
*to ++ = *from ++; // copy opening backquote |
| 105 |
while (*from != U'`' && *from != U'\0') { |
| 106 |
if (to - link >= MAXIMUM_LINK_LENGTH) |
| 107 |
Melder_throw (U"(ManPage_Paragraph_extractLink:) Link starting with “@`” is too long:\n", text); |
| 108 |
*to ++ = *from ++; |
| 109 |
} |
| 110 |
if (*from == U'`') |
| 111 |
*to ++ = *from ++; // copy closing backquote |
| 112 |
Melder_assert (to - link <= MAXIMUM_LINK_LENGTH); |
| 113 |
*to = U'\0'; |
| 114 |
p = from; |
| 115 |
return p; |
| 116 |
} else { |
| 117 |
const char32 *from = p + 1; |
| 118 |
while (isSingleWordCharacter (*from)) { |
| 119 |
if (to - link >= MAXIMUM_LINK_LENGTH) |
| 120 |
Melder_throw (U"(ManPage_Paragraph_extractLink:) Link starting with “@” is too long:\n", text); |
| 121 |
*to ++ = *from ++; |
| 122 |
} |
| 123 |
Melder_assert (to - link <= MAXIMUM_LINK_LENGTH); |
| 124 |
*to = U'\0'; |
| 125 |
p = from; |
| 126 |
return p; |
| 127 |
} |
| 128 |
} else if (*p == U'`' && verbatimAware && ! paragraphIsVerbatim) { |
| 129 |
/* |
| 130 |
We found "`", starting a verbatim stretch in which '@' is to be ignored. |
| 131 |
Doesn't count if part of a backslash trigraph. |
| 132 |
*/ |
| 133 |
if (p - text <= 0 || (p [-1] != U'\\' && (p - text <= 1 || p [-2] != U'\\'))) { |
| 134 |
/* |
| 135 |
Jump to the matching closing backquote. |
| 136 |
*/ |
| 137 |
p ++; // step over opening backquote |
| 138 |
for (;;) { |
| 139 |
if (*p == U'\0') |
| 140 |
return nullptr; // no more '@' |
| 141 |
if (*p == U'`') { |
| 142 |
if (p [1] == U'`') |
| 143 |
p ++; // jump over the first member of a double backquote |
| 144 |
else |
| 145 |
break; // found the closing backquote |
| 146 |
} |
| 147 |
p ++; |
| 148 |
} |
| 149 |
} |
| 150 |
} else if (paragraphIsVerbatim && |
| 151 |
(*p == U'\\' && p [1] == U'@' && p [2] == U'{' || |
| 152 |
*p == U'\\' && p [1] == U'#' && p [2] == U'@' && p [3] == U'{' || |
| 153 |
*p == U'\\' && p [1] == U'`' && p [2] == U'{' || |
| 154 |
*p == U'\\' && p [1] == U'#' && p [2] == U'`' && p [3] == U'{') |
| 155 |
) { |
| 156 |
/* |
| 157 |
We found "\@{" or "\#@{" or "\`{" or "\#`{", |
| 158 |
starting a link in verbatim text or in code. |
| 159 |
*/ |
| 160 |
const bool boldLink = ( p [1] == U'#' ); |
| 161 |
const char32 *from = p + 3 + boldLink; |
| 162 |
const bool verbatimLink = ( from [-2] == U'`' ); |
| 163 |
const conststring32 startMessage = |
| 164 |
boldLink ? verbatimLink ? U"\\#`{" : U"\\#@{" : verbatimLink ? U"\\`{" : U"\\@{"; |
| 165 |
if (verbatimLink) { |
| 166 |
if (to - link >= MAXIMUM_LINK_LENGTH) |
| 167 |
Melder_throw (U"(ManPage_Paragraph_extractLink:) Link starting with “", startMessage, U"” is too long:\n", text); |
| 168 |
*to ++ = U'`'; |
| 169 |
} |
| 170 |
while (*from != U'}' && *from != U'|' && *from != U'\0') { |
| 171 |
if (to - link >= MAXIMUM_LINK_LENGTH) |
| 172 |
Melder_throw (U"(ManPage_Paragraph_extractLink:) Link starting with “", startMessage, U"” is too long:\n", text); |
| 173 |
*to ++ = *from ++; |
| 174 |
} |
| 175 |
/* |
| 176 |
Ignore the "|...}" part, unless it is "||...}" |
| 177 |
*/ |
| 178 |
if (*from == U'|') { |
| 179 |
if (from [1] == U'|') { |
| 180 |
/* |
| 181 |
Found a "||xxx}" part. Append the xxx part. |
| 182 |
*/ |
| 183 |
from += 2; // skip "||" |
| 184 |
while (*from != U'}' && *from != U'\0') { |
| 185 |
if (*from == U'\0') |
| 186 |
break; |
| 187 |
if (to - link >= MAXIMUM_LINK_LENGTH) |
| 188 |
Melder_throw (U"(ManPage_Paragraph_extractLink:) Link starting with “", startMessage, U"” and containing “||” is too long:\n", text); |
| 189 |
*to ++ = *from ++; |
| 190 |
} |
| 191 |
Melder_assert (to - link <= MAXIMUM_LINK_LENGTH); |
| 192 |
*to = U'\0'; |
| 193 |
} else { |
| 194 |
/* |
| 195 |
Found a "|xxx}" part. Ignore all of it. |
| 196 |
*/ |
| 197 |
from ++; |
| 198 |
while (*from != U'}' && *from != U'\0') |
| 199 |
from ++; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/* |
| 204 |
Replace final colon with three dots. |
| 205 |
For example, the code |
| 206 |
\@{Create Poisson process:} |
| 207 |
should yield a link to |
| 208 |
Create Poisson process... |
| 209 |
*/ |
| 210 |
if (to - link > 0 && to [-1] == U':') { |
| 211 |
to --; |
| 212 |
for (integer idot = 1; idot <= 3; idot ++) { |
| 213 |
if (to - link >= MAXIMUM_LINK_LENGTH) |
| 214 |
Melder_throw (U"(ManPage_Paragraph_extractLink:) Link starting with “\\@{” is too long:\n", text); |
| 215 |
*to ++ = U'.'; |
| 216 |
} |
| 217 |
} |
| 218 |
if (verbatimLink) { |
| 219 |
Melder_assert (to - link <= MAXIMUM_LINK_LENGTH); |
| 220 |
*to ++ = U'`'; |
| 221 |
} |
| 222 |
Melder_assert (to - link <= MAXIMUM_LINK_LENGTH); |
| 223 |
*to = U'\0'; |
| 224 |
|
| 225 |
p = from + ( *from == U'}' ); // add bool to pointer: skip '}' but not '\0' |
| 226 |
return p; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
static void readOnePage (ManPages me, MelderReadText text); // forward |
| 232 |
|
| 233 |
static void resolveLinks (ManPages me, ManPage_Paragraph par, bool verbatimAware) { |
| 234 |
//if (par -> type == kManPage_type::SCRIPT) |
| 235 |
// return; // links would be invisible |
| 236 |
char32 linkBuffer [MAXIMUM_LINK_LENGTH + 1], fileNameBuffer [ManPages_FILENAME_BUFFER_SIZE]; |
| 237 |
for (const char32 *plink = ManPage_Paragraph_extractLink (par, nullptr, linkBuffer, verbatimAware); |
| 238 |
plink != nullptr; |
| 239 |
plink = ManPage_Paragraph_extractLink (par, plink, linkBuffer, verbatimAware) |
| 240 |
) { |
| 241 |
/* |
| 242 |
Now, `linkBuffer` contains the link text, with spaces and all. |
| 243 |
Transform it into a file name. |
| 244 |
*/ |
| 245 |
structMelderFile file2 { }; |
| 246 |
if (linkBuffer [0] == U'\\' && linkBuffer [1] == U'F' && linkBuffer [2] == U'I') { |
| 247 |
/* |
| 248 |
A link to a sound file: see if it exists. |
| 249 |
*/ |
| 250 |
MelderFolder_relativePathToFile (& my rootDirectory, linkBuffer + 3, & file2); |
| 251 |
if (! MelderFile_exists (& file2)) |
| 252 |
Melder_warning (U"Cannot find sound file ", MelderFile_messageName (& file2), U"."); |
| 253 |
} else if (linkBuffer [0] == U'\\' && linkBuffer [1] == U'S' && linkBuffer [2] == U'C') { |
| 254 |
/* |
| 255 |
A link to a script: see if it exists. |
| 256 |
*/ |
| 257 |
char32 *p = linkBuffer + 3; |
| 258 |
if (*p == U'"') { |
| 259 |
char32 *q = fileNameBuffer; |
| 260 |
p ++; |
| 261 |
while (*p != U'"' && *p != U'\0') |
| 262 |
* q ++ = * p ++; |
| 263 |
*q = U'\0'; |
| 264 |
} else { |
| 265 |
char32 *q = fileNameBuffer; |
| 266 |
while (*p != U' ' && *p != U'\0') |
| 267 |
* q ++ = * p ++; // one word, up to the next space |
| 268 |
*q = U'\0'; |
| 269 |
} |
| 270 |
MelderFolder_relativePathToFile (& my rootDirectory, fileNameBuffer, & file2); |
| 271 |
if (! MelderFile_exists (& file2)) |
| 272 |
Melder_warning (U"Cannot find script ", MelderFile_messageName (& file2), U"."); |
| 273 |
my executable = true; |
| 274 |
} else { |
| 275 |
/* |
| 276 |
A link to another page: follow it. |
| 277 |
*/ |
| 278 |
try { |
| 279 |
integer extensionSize = 4; // .man |
| 280 |
Melder_sprint (fileNameBuffer,ManPages_FILENAME_BUFFER_SIZE - extensionSize, linkBuffer); |
| 281 |
/* |
| 282 |
For the `.man` version, we replace every funny symbol with an underscore. |
| 283 |
*/ |
| 284 |
for (char32 *q = fileNameBuffer; *q; q ++) |
| 285 |
if (! isAllowedFileNameCharacter (*q)) |
| 286 |
*q = U'_'; |
| 287 |
str32cat (fileNameBuffer, U".man"); |
| 288 |
MelderFolder_getFile (& my rootDirectory, fileNameBuffer, & file2); |
| 289 |
if (MelderFile_exists (& file2)) { |
| 290 |
autoMelderReadText text2 = MelderReadText_createFromFile (& file2); |
| 291 |
readOnePage (me, text2.get()); |
| 292 |
} else { |
| 293 |
/* |
| 294 |
Second try: with upper case. |
| 295 |
*/ |
| 296 |
linkBuffer [0] = Melder_toUpperCase (linkBuffer [0]); |
| 297 |
Melder_sprint (fileNameBuffer,ManPages_FILENAME_BUFFER_SIZE, linkBuffer, U".man"); |
| 298 |
MelderFolder_getFile (& my rootDirectory, fileNameBuffer, & file2); |
| 299 |
if (MelderFile_exists (& file2)) { |
| 300 |
autoMelderReadText text2 = MelderReadText_createFromFile (& file2); |
| 301 |
readOnePage (me, text2.get()); |
| 302 |
} else { |
| 303 |
/* |
| 304 |
For the `.praatnb` version, we replace most funny symbols with underscores, |
| 305 |
but `#` with `-H`, `$` with `-S`, and `@` with `-C`. |
| 306 |
*/ |
| 307 |
extensionSize = 8; // .praatnb |
| 308 |
char32 *to = fileNameBuffer, *max = fileNameBuffer + ManPages_FILENAME_BUFFER_SIZE - (extensionSize + 1); |
| 309 |
for (char32 *from = & linkBuffer [0]; *from != U'\0'; from ++) { |
| 310 |
if (isAllowedFileNameCharacter (*from)) { |
| 311 |
if (to < max) |
| 312 |
*to ++ = *from; |
| 313 |
} else if (*from == U'#') { |
| 314 |
if (to < max) |
| 315 |
*to ++ = U'-'; |
| 316 |
if (to < max) |
| 317 |
*to ++ = U'H'; |
| 318 |
} else if (*from == U'$') { |
| 319 |
if (to < max) |
| 320 |
*to ++ = U'-'; |
| 321 |
if (to < max) |
| 322 |
*to ++ = U'S'; |
| 323 |
} else if (*from == U'@') { |
| 324 |
if (to < max) |
| 325 |
*to ++ = U'-'; |
| 326 |
if (to < max) |
| 327 |
*to ++ = U'C'; |
| 328 |
} else { |
| 329 |
if (to < max) |
| 330 |
*to ++ = U'_'; |
| 331 |
} |
| 332 |
} |
| 333 |
*to = U'\0'; |
| 334 |
str32cat (fileNameBuffer, U".praatnb"); |
| 335 |
MelderFolder_getFile (& my rootDirectory, fileNameBuffer, & file2); |
| 336 |
if (MelderFile_exists (& file2)) { |
| 337 |
autoMelderReadText text2 = MelderReadText_createFromFile (& file2); |
| 338 |
readOnePage (me, text2.get()); |
| 339 |
} else { |
| 340 |
fileNameBuffer [0] = Melder_toLowerCase (fileNameBuffer [0]); |
| 341 |
MelderFolder_getFile (& my rootDirectory, fileNameBuffer, & file2); |
| 342 |
if (MelderFile_exists (& file2)) { |
| 343 |
autoMelderReadText text2 = MelderReadText_createFromFile (& file2); |
| 344 |
readOnePage (me, text2.get()); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
} catch (MelderError) { |
| 350 |
Melder_throw (U"File ", & file2, U"."); |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
static void readOnePage_man (ManPages me, MelderReadText text) { |
| 357 |
autostring32 title; |
| 358 |
try { |
| 359 |
title = texgetw16 (text); |
| 360 |
} catch (MelderError) { |
| 361 |
Melder_throw (U"Cannot find page title."); |
| 362 |
} |
| 363 |
|
| 364 |
/* |
| 365 |
Check whether a page with this title is already present. |
| 366 |
*/ |
| 367 |
if (lookUp_unsorted (me, title.get())) |
| 368 |
return; |
| 369 |
|
| 370 |
autoManPage autopage = Thing_new (ManPage); |
| 371 |
autopage -> title = title.move(); |
| 372 |
|
| 373 |
/* |
| 374 |
Add the page early, so that lookUp can find it. |
| 375 |
*/ |
| 376 |
ManPage page = my pages. addItem_move (autopage.move()); |
| 377 |
page -> verbatimAware = false; |
| 378 |
|
| 379 |
autostring32 author; |
| 380 |
try { |
| 381 |
author = texgetw16 (text); |
| 382 |
} catch (MelderError) { |
| 383 |
Melder_throw (U"Cannot find author."); |
| 384 |
} |
| 385 |
uinteger date; |
| 386 |
try { |
| 387 |
date = texgetu32 (text); |
| 388 |
} catch (MelderError) { |
| 389 |
Melder_throw (U"Cannot find date."); |
| 390 |
} |
| 391 |
page -> signature = Melder_dup (Melder_cat (U"© ", author.get(), U" ", date)); |
| 392 |
try { |
| 393 |
page -> recordingTime = texgetr64 (text); |
| 394 |
} catch (MelderError) { |
| 395 |
Melder_throw (U"Cannot find recording time."); |
| 396 |
} |
| 397 |
|
| 398 |
for (;;) { |
| 399 |
enum kManPage_type type; |
| 400 |
try { |
| 401 |
type = (kManPage_type) texgete8 (text, (enum_generic_getValue) kManPage_type_getValue); |
| 402 |
} catch (MelderError) { |
| 403 |
if (Melder_hasError (U"end of text")) { |
| 404 |
Melder_clearError (); |
| 405 |
break; |
| 406 |
} else { |
| 407 |
throw; |
| 408 |
} |
| 409 |
} |
| 410 |
ManPage_Paragraph par = page -> paragraphs. append (); |
| 411 |
par -> type = type; |
| 412 |
if (par -> type == kManPage_type::SCRIPT) { |
| 413 |
par -> width = texgetr64 (text); |
| 414 |
par -> height = texgetr64 (text); |
| 415 |
} |
| 416 |
try { |
| 417 |
par -> text = texgetw16 (text).transfer(); |
| 418 |
//if (str32str (par -> text, U":|")) |
| 419 |
// par -> text = replace_STR (par -> text, U":|", U"...|", 0).transfer(); |
| 420 |
//if (str32str (par -> text, U":@")) |
| 421 |
// par -> text = replace_STR (par -> text, U":@", U"...@", 0).transfer(); |
| 422 |
} catch (MelderError) { |
| 423 |
Melder_throw (U"Cannot find text."); |
| 424 |
} |
| 425 |
resolveLinks (me, par, page -> verbatimAware); |
| 426 |
} |
| 427 |
} |
| 428 |
static bool stringHasInk (conststring32 line) { |
| 429 |
const char32 *endOfSpace = Melder_findEndOfHorizontalSpace (line); |
| 430 |
return *endOfSpace != U'\0'; |
| 431 |
} |
| 432 |
static bool isTerm (kManPage_type type) { |
| 433 |
return type == kManPage_type::TERM || type >= kManPage_type::TERM1 && type <= kManPage_type::TERM3; |
| 434 |
} |
| 435 |
static bool stringStartsWithParenthesizedNumber (conststring32 string) { |
| 436 |
const char32 *p = & string [0]; |
| 437 |
if (*p != U'(') |
| 438 |
return false; |
| 439 |
p ++; // skip opening parenthesis |
| 440 |
if (*p < U'0' || *p > U'9') |
| 441 |
return false; |
| 442 |
p ++; // skip first digit |
| 443 |
while (*p >= U'0' && *p <= U'9') |
| 444 |
p ++; |
| 445 |
if (*p != U')') |
| 446 |
return false; |
| 447 |
return true; |
| 448 |
} |
| 449 |
static bool stringStartsWithNumberAndDot (conststring32 string) { |
| 450 |
const char32 *p = & string [0]; |
| 451 |
for (;;) { |
| 452 |
if (*p < U'0' || *p > U'9') |
| 453 |
return false; |
| 454 |
p ++; // skip first digit |
| 455 |
while (*p >= U'0' && *p <= U'9') |
| 456 |
p ++; |
| 457 |
if (*p != U'.') |
| 458 |
return false; |
| 459 |
p ++; |
| 460 |
if (Melder_isHorizontalSpace (*p)) |
| 461 |
return true; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
static void readOnePage_notebook (ManPages me, MelderReadText text) { |
| 466 |
/* |
| 467 |
Handle the title line. |
| 468 |
*/ |
| 469 |
mutablestring32 firstLine = MelderReadText_readLine (text); |
| 470 |
Melder_require (!! firstLine, |
| 471 |
U"Empty notebook."); |
| 472 |
Melder_require (*firstLine == U'"', |
| 473 |
U"There should be a page title (starting with an opening quote) on the first line, at or around “", firstLine, U"”."); |
| 474 |
++ firstLine; // jump over opening quote |
| 475 |
char32 *p_closingQuote = str32rchr (firstLine, U'"'); |
| 476 |
Melder_require (p_closingQuote - firstLine > 0, |
| 477 |
U"There should be a page title (starting with an closing quote) on the first line."); |
| 478 |
*p_closingQuote = U'\0'; |
| 479 |
|
| 480 |
/* |
| 481 |
Check whether a page with this title is already present. |
| 482 |
*/ |
| 483 |
if (lookUp_unsorted (me, firstLine)) |
| 484 |
return; |
| 485 |
|
| 486 |
autoManPage autopage = Thing_new (ManPage); |
| 487 |
autopage -> title = Melder_dup (firstLine); |
| 488 |
|
| 489 |
/* |
| 490 |
Add the page early, so that lookUp can find it. |
| 491 |
*/ |
| 492 |
ManPage page = my pages. addItem_move (autopage.move()); |
| 493 |
page -> verbatimAware = true; |
| 494 |
|
| 495 |
/* |
| 496 |
Handle the signature line. |
| 497 |
*/ |
| 498 |
mutablestring32 line = MelderReadText_readLine (text); |
| 499 |
Melder_require (!! line, |
| 500 |
U"Missing signature line (typically author and date) in page “", firstLine, U"”."); |
| 501 |
page -> signature = Melder_dup (line); |
| 502 |
|
| 503 |
/* |
| 504 |
Find all the notebook attributes. |
| 505 |
All of them are optional. |
| 506 |
The order is not fixed. |
| 507 |
We allow for new attributes in the future. |
| 508 |
*/ |
| 509 |
for (;;) { |
| 510 |
line = MelderReadText_readLine (text); |
| 511 |
if (! line) |
| 512 |
break; // end of file |
| 513 |
char32 *colon = str32chr (line, U':'); |
| 514 |
if (! colon) |
| 515 |
break; // end of header |
| 516 |
const integer lengthOfAttributeName = colon - line; |
| 517 |
if (Melder_nequ (line, U"Recording time", lengthOfAttributeName)) { |
| 518 |
Melder_skipHorizontalSpace (& (colon += 1)); |
| 519 |
page -> recordingTime = Melder_atof (colon); |
| 520 |
} |
| 521 |
/* |
| 522 |
TODO: add: |
| 523 |
Use Praat windows: Objects, Picture(?), Info, Demo(?) |
| 524 |
Keywords: ; lower case, separate by comma |
| 525 |
Code chunk visibility: + ; + (the default) or - |
| 526 |
Text style language: praat ; praat (the default), i.e. % # $ @ _ ^, or markdown, i.e. * ` ** [] <sub> <sup> |
| 527 |
*/ |
| 528 |
} |
| 529 |
|
| 530 |
line = nullptr; |
| 531 |
do { |
| 532 |
line = MelderReadText_readLine (text); |
| 533 |
} while (line && ! stringHasInk (line)); |
| 534 |
ManPage_Paragraph previousParagraph = nullptr; |
| 535 |
autoMelderString buffer_graphical, buffer_graphicalCode; |
| 536 |
for (;;) { |
| 537 |
if (! line) |
| 538 |
return; |
| 539 |
integer numberOfLeadingEmptyLines = 0; |
| 540 |
while (! stringHasInk (line)) { |
| 541 |
numberOfLeadingEmptyLines += 1; |
| 542 |
line = MelderReadText_readLine (text); |
| 543 |
if (! line) |
| 544 |
return; |
| 545 |
} |
| 546 |
integer numberOfLeadingSpaces = 0; |
| 547 |
while (Melder_isHorizontalSpace (*line)) |
| 548 |
numberOfLeadingSpaces += ( *(line ++) == U'\t' ? 4 - ( numberOfLeadingSpaces & 0b11_integer ) : 1 ); |
| 549 |
MelderString_empty (& buffer_graphical); |
| 550 |
kManPage_type type; |
| 551 |
double width = 0.0, height = 0.001; |
| 552 |
if (numberOfLeadingSpaces == 0 && Melder_startsWith (line, U"===")) { |
| 553 |
if (previousParagraph) |
| 554 |
previousParagraph -> type = kManPage_type::ENTRY; |
| 555 |
line = MelderReadText_readLine (text); |
| 556 |
if (! line) |
| 557 |
return; |
| 558 |
continue; |
| 559 |
} else if (numberOfLeadingSpaces == 0 && Melder_startsWith (line, U"---")) { |
| 560 |
if (previousParagraph) |
| 561 |
previousParagraph -> type = kManPage_type::SUBHEADER; |
| 562 |
line = MelderReadText_readLine (text); |
| 563 |
if (! line) |
| 564 |
return; |
| 565 |
continue; |
| 566 |
} else if (line [0] == U'/' && line [1] == U'/') { |
| 567 |
line = MelderReadText_readLine (text); |
| 568 |
if (! line) |
| 569 |
return; |
| 570 |
continue; |
| 571 |
/* |
| 572 |
Now we try several kinds of list items. |
| 573 |
To not prepend a character, use ",". |
| 574 |
To prepend a bullet, use "-" or "*" or "•". |
| 575 |
*/ |
| 576 |
} else if ( |
| 577 |
line [0] == U',' && (Melder_isHorizontalSpace (line [1]) || line [1] == U'\0') || |
| 578 |
line [0] == U'-' && Melder_isHorizontalSpace (line [1]) || |
| 579 |
line [0] == U'*' && Melder_isHorizontalSpace (line [1]) || |
| 580 |
line [0] == U'•' && Melder_isHorizontalSpace (line [1]) || |
| 581 |
stringStartsWithParenthesizedNumber (line) || |
| 582 |
stringStartsWithNumberAndDot (line) || |
| 583 |
line [0] == U'|' && Melder_isHorizontalSpace (line [1]) |
| 584 |
) { |
| 585 |
type = ( |
| 586 |
numberOfLeadingSpaces < 3 ? kManPage_type::LIST_ITEM : |
| 587 |
numberOfLeadingSpaces < 7 ? kManPage_type::LIST_ITEM1 : |
| 588 |
numberOfLeadingSpaces < 11 ? kManPage_type::LIST_ITEM2 : |
| 589 |
kManPage_type::LIST_ITEM3 |
| 590 |
); |
| 591 |
if (line [0] == U',') { |
| 592 |
if (line [1] == U'\0') { |
| 593 |
MelderString_append (& buffer_graphical, U" "); // a dummmy to make sure that the line is drawn at all |
| 594 |
line += 1; |
| 595 |
} else { |
| 596 |
MelderString_append (& buffer_graphical, U" "); |
| 597 |
line += 2; |
| 598 |
//Melder_skipHorizontalSpace (& line); |
| 599 |
} |
| 600 |
MelderString_append (& buffer_graphical, line); |
| 601 |
} else if (line [0] == U'-' || line [0] == U'*' || line [0] == U'•') { |
| 602 |
MelderString_append (& buffer_graphical, U"• "); |
| 603 |
line += 2; |
| 604 |
//Melder_skipHorizontalSpace (& line); |
| 605 |
MelderString_append (& buffer_graphical, line); |
| 606 |
} else if (line [0] == U'|') { |
| 607 |
MelderString_append (& buffer_graphical, U"\t"); |
| 608 |
line += 2; |
| 609 |
while (*line != U'\0') { |
| 610 |
if (line [0] == U'|' && Melder_isHorizontalSpace (line [-1]) && (Melder_isHorizontalSpace (line [1]) || line [1] == U'\0')) |
| 611 |
MelderString_appendCharacter (& buffer_graphical, U'\t'); |
| 612 |
else |
| 613 |
MelderString_appendCharacter (& buffer_graphical, *line); |
| 614 |
line ++; |
| 615 |
} |
| 616 |
} else { |
| 617 |
MelderString_append (& buffer_graphical, line); |
| 618 |
} |
| 619 |
} else if (line [0] == U':' && Melder_isHorizontalSpace (line [1])) { |
| 620 |
type = ( |
| 621 |
numberOfLeadingSpaces < 3 ? kManPage_type::DEFINITION : |
| 622 |
numberOfLeadingSpaces < 7 ? kManPage_type::DEFINITION1 : |
| 623 |
numberOfLeadingSpaces < 11 ? kManPage_type::DEFINITION2 : |
| 624 |
kManPage_type::DEFINITION3 |
| 625 |
); |
| 626 |
if (previousParagraph && (previousParagraph -> type == kManPage_type::NORMAL || previousParagraph -> type == kManPage_type::CAPTION)) { |
| 627 |
if (type == kManPage_type::DEFINITION) |
| 628 |
previousParagraph -> type = kManPage_type::TERM; |
| 629 |
else if (type == kManPage_type::DEFINITION1) |
| 630 |
previousParagraph -> type = kManPage_type::TERM1; |
| 631 |
else if (type == kManPage_type::DEFINITION2) |
| 632 |
previousParagraph -> type = kManPage_type::TERM2; |
| 633 |
else if (type == kManPage_type::DEFINITION3) |
| 634 |
previousParagraph -> type = kManPage_type::TERM3; |
| 635 |
} |
| 636 |
line += 2; |
| 637 |
Melder_skipHorizontalSpace (& line); |
| 638 |
MelderString_append (& buffer_graphical, line); |
| 639 |
} else if (line [0] == U'>' && Melder_isHorizontalSpace (line [1])) { |
| 640 |
type = ( |
| 641 |
numberOfLeadingSpaces < 3 ? kManPage_type::QUOTE : |
| 642 |
numberOfLeadingSpaces < 7 ? kManPage_type::QUOTE1 : |
| 643 |
numberOfLeadingSpaces < 11 ? kManPage_type::QUOTE2 : |
| 644 |
kManPage_type::QUOTE3 |
| 645 |
); |
| 646 |
line += 2; |
| 647 |
Melder_skipHorizontalSpace (& line); |
| 648 |
MelderString_append (& buffer_graphical, line); |
| 649 |
} else if (numberOfLeadingSpaces == 0 && line [0] == U'~' && Melder_isHorizontalSpace (line [1])) { |
| 650 |
type = kManPage_type::EQUATION; |
| 651 |
line += 2; |
| 652 |
Melder_skipHorizontalSpace (& line); |
| 653 |
MelderString_append (& buffer_graphical, line); |
| 654 |
} else if (numberOfLeadingSpaces == 0 && line [0] == U'`' && ! stringHasInk (line + 1)) { |
| 655 |
//TRACE |
| 656 |
trace (U"Verbatim: <<", page -> title.get(), U">>"); |
| 657 |
type = kManPage_type::SCRIPT; // TODO: make different type, such as kManPage_type::VERBATIM |
| 658 |
do { |
| 659 |
line = MelderReadText_readLine (text); |
| 660 |
if (! line) |
| 661 |
break; |
| 662 |
if (line [0] == U'`' && ! stringHasInk (line + 1)) { |
| 663 |
line = MelderReadText_readLine (text); |
| 664 |
break; |
| 665 |
} |
| 666 |
MelderString_empty (& buffer_graphicalCode); |
| 667 |
const char32 *p = & line [0]; |
| 668 |
while (*p) { |
| 669 |
if (*p == U'\t') { |
| 670 |
MelderString_append (& buffer_graphicalCode, p == line ? nullptr : U" "); |
| 671 |
} else |
| 672 |
MelderString_appendCharacter (& buffer_graphicalCode, *p); |
| 673 |
p ++; |
| 674 |
} |
| 675 |
ManPage_Paragraph par = page -> paragraphs. append (); |
| 676 |
par -> type = kManPage_type::CODE; |
| 677 |
par -> text = Melder_dup (buffer_graphicalCode.string).transfer(); |
| 678 |
} while (1); |
| 679 |
MelderString_empty (& buffer_graphical); // this makes sure that no actual SCRIPT (or VERBATIM) paragraph will be added |
| 680 |
} else if (numberOfLeadingSpaces == 0 && line [0] == U'{') { |
| 681 |
const bool shouldShowCode = ! str32chr (line, U'-'); |
| 682 |
const char32 *sizeLocation = str32chr (line, U'x'); |
| 683 |
const bool shouldShowOutput = ! str32chr (line, U';'); |
| 684 |
if (sizeLocation) { |
| 685 |
const char32 *widthLocation = sizeLocation - 1; |
| 686 |
while (Melder_isDecimalNumber (*widthLocation) || *widthLocation == U'.') |
| 687 |
widthLocation --; |
| 688 |
width = Melder_atof (widthLocation); |
| 689 |
height = Melder_atof (sizeLocation + 1); |
| 690 |
} else |
| 691 |
width = 6.0; |
| 692 |
type = kManPage_type::SCRIPT; |
| 693 |
integer procedureDepth = 0; |
| 694 |
do { |
| 695 |
line = MelderReadText_readLine (text); |
| 696 |
if (! line) |
| 697 |
Melder_throw (U"Script chunk not closed."); |
| 698 |
const char32 *firstNonspace = Melder_findEndOfHorizontalSpace (line); |
| 699 |
if (line [0] == U'}') { |
| 700 |
line = MelderReadText_readLine (text); |
| 701 |
break; |
| 702 |
} |
| 703 |
if (Melder_startsWith (firstNonspace, U"procedure ")) |
| 704 |
procedureDepth += 1; |
| 705 |
else if (Melder_startsWith (firstNonspace, U"endproc")) |
| 706 |
procedureDepth -= 1; |
| 707 |
if (shouldShowCode) { |
| 708 |
/* |
| 709 |
Convert to graphical code. |
| 710 |
*/ |
| 711 |
MelderString_empty (& buffer_graphicalCode); |
| 712 |
const char32 *p = & line [0]; |
| 713 |
while (*p) { |
| 714 |
if (*p == U'\t') { |
| 715 |
MelderString_append (& buffer_graphicalCode, p == line ? nullptr : U" "); |
| 716 |
} else |
| 717 |
MelderString_appendCharacter (& buffer_graphicalCode, *p); |
| 718 |
p ++; |
| 719 |
} |
| 720 |
ManPage_Paragraph par = page -> paragraphs. append (); |
| 721 |
par -> type = kManPage_type::CODE; |
| 722 |
par -> text = Melder_dup (buffer_graphicalCode.string).transfer(); |
| 723 |
} |
| 724 |
if (shouldShowOutput) { |
| 725 |
/* |
| 726 |
Collect the code, for later execution when the output is drawn. |
| 727 |
*/ |
| 728 |
|
| 729 |
/* |
| 730 |
Look for drawing commmands. They could either stand on their own, |
| 731 |
or be within "\@{xxx}" or within "\#{xxx}" (note: not within "\`{xxx}"). |
| 732 |
|
| 733 |
TODO: make less brittle. |
| 734 |
*/ |
| 735 |
if (firstNonspace [0] == U'\\' && |
| 736 |
(firstNonspace [1] == U'@' && firstNonspace [2] == U'{' || |
| 737 |
firstNonspace [1] == U'#' && firstNonspace [2] == U'{' || |
| 738 |
firstNonspace [1] == U'#' && firstNonspace [2] == U'@' && firstNonspace [3] == U'{') |
| 739 |
) |
| 740 |
firstNonspace += 3 + ( firstNonspace [2] == U'@' ); |
| 741 |
if ( |
| 742 |
procedureDepth == 0 && |
| 743 |
height == 0.001 && |
| 744 |
(Melder_startsWith (firstNonspace, U"Draw") || |
| 745 |
Melder_startsWith (firstNonspace, U"Paint") || |
| 746 |
Melder_startsWith (firstNonspace, U"Axes:") || |
| 747 |
Melder_startsWith (firstNonspace, U"Axes}:") || |
| 748 |
Melder_startsWith (firstNonspace, U"Text ") || |
| 749 |
Melder_startsWith (firstNonspace, U"Text:") || |
| 750 |
Melder_startsWith (firstNonspace, U"Text}:") || |
| 751 |
Melder_startsWith (firstNonspace, U"Marks ")) |
| 752 |
) |
| 753 |
height = 3.0; |
| 754 |
|
| 755 |
const char32 *p = & line [0]; |
| 756 |
bool inBold = false, inItalic = false; |
| 757 |
while (*p) { |
| 758 |
if (*p == U'\\' && |
| 759 |
(p [1] == U'@' && p [2] == U'{' || |
| 760 |
p [1] == U'`' && p [2] == U'{' || |
| 761 |
p [1] == U'#' && p [2] == U'@' && p [3] == U'{' || |
| 762 |
p [1] == U'#' && p [2] == U'`' && p [3] == U'{')) |
| 763 |
{ |
| 764 |
const bool thinLink = ( p [1] != U'#' ); |
| 765 |
p += 4 - thinLink; // "\@{" should not be included in the code |
| 766 |
/* |
| 767 |
We collect the link text separately, |
| 768 |
because we cannot collect it into buffer_graphical directly, |
| 769 |
because in case of a "|" the link buffer has to be cleared. |
| 770 |
*/ |
| 771 |
static MelderString linkText; |
| 772 |
MelderString_empty (& linkText); |
| 773 |
while (*p != U'\0') { |
| 774 |
if (*p == U'|') { |
| 775 |
MelderString_empty (& linkText); // ignore link target as well as "|" and "||" |
| 776 |
if (p [1] == U'|') |
| 777 |
p += 1; |
| 778 |
} else if (*p == U'}') { |
| 779 |
break; // but a missing closing brace at the end of a line is also fine |
| 780 |
} else { |
| 781 |
MelderString_appendCharacter (& linkText, *p); |
| 782 |
} |
| 783 |
p ++; |
| 784 |
} |
| 785 |
MelderString_append (& buffer_graphical, linkText.string); |
| 786 |
if (*p == U'\0') |
| 787 |
break; // double break |
| 788 |
} else if (*p == U'\\' && p [1] == U'#' && p [2] == U'{') { |
| 789 |
inBold = true; |
| 790 |
p += 2; |
| 791 |
} else if (*p == U'\\' && p [1] == U'%' && p [2] == U'{') { |
| 792 |
inItalic = true; |
| 793 |
p += 2; |
| 794 |
} else if (*p == U'}') { |
| 795 |
if (inBold) |
| 796 |
inBold = false; |
| 797 |
else if (inItalic) |
| 798 |
inItalic = false; |
| 799 |
else |
| 800 |
MelderString_appendCharacter (& buffer_graphical, U'}'); |
| 801 |
} else |
| 802 |
MelderString_appendCharacter (& buffer_graphical, *p); |
| 803 |
p ++; |
| 804 |
} |
| 805 |
MelderString_appendCharacter (& buffer_graphical, U'\n'); |
| 806 |
} |
| 807 |
} while (1); |
| 808 |
if (! shouldShowOutput) |
| 809 |
MelderString_empty (& buffer_graphical); // add no SCRIPT paragraph |
| 810 |
} else if (numberOfLeadingSpaces >= 3) { |
| 811 |
type = kManPage_type::CAPTION; |
| 812 |
MelderString_append (& buffer_graphical, line); |
| 813 |
} else { |
| 814 |
type = kManPage_type::NORMAL; |
| 815 |
MelderString_append (& buffer_graphical, line); |
| 816 |
} |
| 817 |
ManPage_Paragraph par = nullptr; |
| 818 |
if (buffer_graphical.string [0] != U'\0') { |
| 819 |
par = page -> paragraphs. append (); |
| 820 |
par -> type = type; |
| 821 |
par -> text = Melder_dup (buffer_graphical.string). transfer(); |
| 822 |
par -> width = width; |
| 823 |
par -> height = height; |
| 824 |
} |
| 825 |
|
| 826 |
/* |
| 827 |
Do continuation lines, except for code. |
| 828 |
*/ |
| 829 |
const bool isCode = ( type == kManPage_type::CODE || type >= kManPage_type::CODE1 && type <= kManPage_type::CODE5 ); |
| 830 |
if (isCode) { |
| 831 |
line = MelderReadText_readLine (text); |
| 832 |
} else if (type == kManPage_type::SCRIPT) { |
| 833 |
} else if (par) { |
| 834 |
do { |
| 835 |
mutablestring32 continuationLine = MelderReadText_readLine (text); |
| 836 |
if (! continuationLine) { |
| 837 |
line = nullptr; // signals end of text |
| 838 |
break; |
| 839 |
} |
| 840 |
integer nextNumberOfLeadingSpaces = 0, ikar = 0; |
| 841 |
while (Melder_isHorizontalSpace (continuationLine [ikar])) { |
| 842 |
nextNumberOfLeadingSpaces += ( continuationLine [ikar] == U'\t' ? 4 - ( nextNumberOfLeadingSpaces & 0b11_integer ) : 1 ); |
| 843 |
ikar ++; |
| 844 |
} |
| 845 |
char32 *firstNonSpace = continuationLine + ikar; |
| 846 |
if (*firstNonSpace == U':' || |
| 847 |
*firstNonSpace == U'>' || |
| 848 |
*firstNonSpace == U',' && (Melder_isHorizontalSpace (firstNonSpace [1]) || firstNonSpace [1] == U'\0') || |
| 849 |
*firstNonSpace == U'-' && Melder_isHorizontalSpace (firstNonSpace [1]) || |
| 850 |
*firstNonSpace == U'*' && Melder_isHorizontalSpace (firstNonSpace [1]) || |
| 851 |
*firstNonSpace == U'•' && Melder_isHorizontalSpace (firstNonSpace [1]) || |
| 852 |
stringStartsWithParenthesizedNumber (firstNonSpace) || |
| 853 |
stringStartsWithNumberAndDot (firstNonSpace) || |
| 854 |
*firstNonSpace == U'|' && Melder_isHorizontalSpace (firstNonSpace [1]) || |
| 855 |
firstNonSpace == continuationLine && *firstNonSpace == U'{' || |
| 856 |
firstNonSpace == continuationLine && *firstNonSpace == U'`' && ! stringHasInk (firstNonSpace + 1) || |
| 857 |
firstNonSpace == continuationLine && *firstNonSpace == U'~' || |
| 858 |
Melder_startsWith (firstNonSpace, U"===") || |
| 859 |
Melder_startsWith (firstNonSpace, U"---") || |
| 860 |
*firstNonSpace == U'/' && firstNonSpace [1] == U'/' || |
| 861 |
*firstNonSpace == U'\0' |
| 862 |
) { |
| 863 |
line = continuationLine; // not really a continuation line, but a new paragraph |
| 864 |
break; |
| 865 |
} |
| 866 |
conststring32 separator = ( par -> text [0] == U'\0' ? U"" : U" " ); |
| 867 |
Melder_skipHorizontalSpace (& continuationLine); |
| 868 |
par -> text = Melder_dup (Melder_cat (par -> text, separator, continuationLine)).transfer(); |
| 869 |
} while (1); |
| 870 |
} |
| 871 |
if (par) { |
| 872 |
if (my dynamic) |
| 873 |
resolveLinks (me, par, page -> verbatimAware); |
| 874 |
previousParagraph = par; |
| 875 |
} |
| 876 |
} |
| 877 |
trace (U"end"); |
| 878 |
} |
| 879 |
static void readOnePage (ManPages me, MelderReadText text) { |
| 880 |
const bool isNotebook = ( |
| 881 |
text -> string32 ? |
| 882 |
text -> string32 [0] == U'"' |
| 883 |
: |
| 884 |
text -> string8 [0] == '"' |
| 885 |
); |
| 886 |
if (isNotebook) |
| 887 |
readOnePage_notebook (me, text); |
| 888 |
else |
| 889 |
readOnePage_man (me, text); |
| 890 |
} |
| 891 |
void structManPages :: v1_readText (MelderReadText text, int /*formatVersion*/) { |
| 892 |
our dynamic = true; |
| 893 |
MelderFolder_copy (& Data_directoryBeingRead, & our rootDirectory); |
| 894 |
readOnePage (this, text); |
| 895 |
} |
| 896 |
|
| 897 |
autoManPages ManPages_create () { |
| 898 |
autoManPages me = Thing_new (ManPages); |
| 899 |
return me; |
| 900 |
} |
| 901 |
|
| 902 |
autoManPages ManPages_createFromText (MelderReadText text, MelderFile file) { |
| 903 |
autoManPages me = Thing_new (ManPages); |
| 904 |
my dynamic = true; |
| 905 |
MelderFile_getParentFolder (file, & my rootDirectory); |
| 906 |
readOnePage (me.get(), text); |
| 907 |
return me; |
| 908 |
} |
| 909 |
|
| 910 |
/* |
| 911 |
Praat-internal version without references to external files. |
| 912 |
*/ |
| 913 |
autoManPages ManPages_createFromText (MelderReadText text) { |
| 914 |
autoManPages me = Thing_new (ManPages); |
| 915 |
my dynamic = false; |
| 916 |
readOnePage (me.get(), text); |
| 917 |
return me; |
| 918 |
} |
| 919 |
|
| 920 |
void ManPages_addPage (ManPages me, conststring32 title, conststring32 signature, |
| 921 |
structManPage_Paragraph paragraphs []) |
| 922 |
{ |
| 923 |
autoManPage page = Thing_new (ManPage); |
| 924 |
page -> title = Melder_dup (title); |
| 925 |
for (ManPage_Paragraph par = & paragraphs [0]; (int) par -> type != 0; par ++) { |
| 926 |
ManPage_Paragraph targetParagraph = page -> paragraphs. append (); |
| 927 |
targetParagraph -> type = par -> type; |
| 928 |
targetParagraph -> text = par -> text; // static string |
| 929 |
targetParagraph -> width = par -> width; |
| 930 |
targetParagraph -> height = par -> height; |
| 931 |
targetParagraph -> draw = par -> draw; |
| 932 |
} |
| 933 |
page -> signature = Melder_dup (signature); |
| 934 |
my pages. addItem_move (page.move()); |
| 935 |
} |
| 936 |
void ManPages_addPagesFromNotebookText (ManPages me, conststring8 multiplePagesText) { |
| 937 |
autoMelderReadText multiplePagesReader = MelderReadText_createFromText (Melder_8to32_e (multiplePagesText)); |
| 938 |
autoMelderString pageText; |
| 939 |
for (;;) { |
| 940 |
mutablestring32 line = MelderReadText_readLine (multiplePagesReader.get()); |
| 941 |
const bool atEndOfPage = ( ! line || Melder_startsWith (line, U"####################") ); |
| 942 |
if (atEndOfPage) { |
| 943 |
if (pageText.length > 0) { |
| 944 |
autoMelderReadText pageReader = MelderReadText_createFromText (Melder_dup (pageText.string)); |
| 945 |
readOnePage_notebook (me, pageReader.get()); |
| 946 |
} |
| 947 |
if (! line) |
| 948 |
return; |
| 949 |
MelderString_empty (& pageText); |
| 950 |
} else |
| 951 |
MelderString_append (& pageText, line, U"\n"); |
| 952 |
} |
| 953 |
} |
| 954 |
integer ManPages_addPagesFromNotebookReader (ManPages me, MelderReadText multiplePagesReader, integer startOfSelection, integer endOfSelection) { |
| 955 |
bool foundFirstPage = false; |
| 956 |
integer numberOfCharactersRead = 0; |
| 957 |
integer startingPage = -1, numberOfPages = 0; |
| 958 |
autoMelderString pageText; |
| 959 |
for (;;) { |
| 960 |
mutablestring32 line = MelderReadText_readLine (multiplePagesReader); |
| 961 |
if (startingPage == -1) { |
| 962 |
numberOfCharactersRead += Melder_length (line) + 1; |
| 963 |
if (numberOfCharactersRead >= startOfSelection) |
| 964 |
startingPage = numberOfPages; |
| 965 |
} |
| 966 |
const bool atEndOfPage = ( ! line || Melder_startsWith (line, U"####################") ); |
| 967 |
if (atEndOfPage) { |
| 968 |
if (! foundFirstPage) { |
| 969 |
if (! line) |
| 970 |
Melder_throw (U"Empty notebook (no line starting with “####################”."); |
| 971 |
/* |
| 972 |
When we are here, the line starts with "####################". |
| 973 |
*/ |
| 974 |
foundFirstPage = true; |
| 975 |
} |
| 976 |
if (pageText.length > 0) { |
| 977 |
autoMelderReadText pageReader = MelderReadText_createFromText (Melder_dup (pageText.string)); |
| 978 |
readOnePage_notebook (me, pageReader.get()); |
| 979 |
} |
| 980 |
if (! line) |
| 981 |
break; |
| 982 |
MelderString_empty (& pageText); |
| 983 |
numberOfPages += 1; |
| 984 |
} else if (Melder_startsWith (line, U")~~~\"")) { |
| 985 |
break; |
| 986 |
} else if (foundFirstPage) { |
| 987 |
MelderString_append (& pageText, line, U"\n"); |
| 988 |
} else { |
| 989 |
// ignore all the lines before the first line that starts with "####################". |
| 990 |
} |
| 991 |
} |
| 992 |
return startingPage; |
| 993 |
} |
| 994 |
|
| 995 |
static bool pageCompare (ManPage me, ManPage thee) { |
| 996 |
return str32coll_numberAware (my title.get(), thy title.get(), true) < 0; |
| 997 |
} |
| 998 |
|
| 999 |
static integer lookUp_unsorted (ManPages me, conststring32 title) { |
| 1000 |
/* |
| 1001 |
First try to match an unaltered 'title' with the titles of the man pages. |
| 1002 |
*/ |
| 1003 |
for (integer i = 1; i <= my pages.size; i ++) { |
| 1004 |
ManPage page = my pages.at [i]; |
| 1005 |
if (str32equ (page -> title.get(), title)) |
| 1006 |
return i; |
| 1007 |
} |
| 1008 |
/* |
| 1009 |
If that fails, try to find the upper-case variant. |
| 1010 |
*/ |
| 1011 |
if (Melder_isLowerCaseLetter (title [0])) { |
| 1012 |
char32 upperTitle [300]; |
| 1013 |
Melder_sprint (upperTitle,300, title); |
| 1014 |
upperTitle [0] = Melder_toUpperCase (upperTitle [0]); |
| 1015 |
for (integer i = 1; i <= my pages.size; i ++) { |
| 1016 |
ManPage page = my pages.at [i]; |
| 1017 |
if (str32equ (page -> title.get(), upperTitle)) |
| 1018 |
return i; |
| 1019 |
} |
| 1020 |
} |
| 1021 |
return 0; |
| 1022 |
} |
| 1023 |
|
| 1024 |
static integer lookUp_sorted (ManPages me, conststring32 title) { |
| 1025 |
static autoManPage dummy; |
| 1026 |
if (! dummy) |
| 1027 |
dummy = Thing_new (ManPage); |
| 1028 |
dummy -> title = Melder_dup (title); |
| 1029 |
ManPage *page = std::lower_bound (my pages.begin(), my pages.end(), dummy.get(), pageCompare); // noexcept |
| 1030 |
if (page != my pages.end() && Melder_equ ((*page) -> title.get(), dummy -> title.get())) |
| 1031 |
return (page - my pages.begin()) + 1; |
| 1032 |
if (Melder_isLowerCaseLetter (title [0]) || Melder_isUpperCaseLetter (title [0])) { |
| 1033 |
char32 caseSwitchedTitle [300]; |
| 1034 |
Melder_sprint (caseSwitchedTitle,300, title); |
| 1035 |
caseSwitchedTitle [0] = Melder_isLowerCaseLetter (title [0]) ? Melder_toUpperCase (caseSwitchedTitle [0]) : Melder_toLowerCase (caseSwitchedTitle [0]); |
| 1036 |
dummy -> title = Melder_dup (caseSwitchedTitle); |
| 1037 |
page = std::lower_bound (my pages.begin(), my pages.end(), dummy.get(), pageCompare); // noexcept |
| 1038 |
if (page != my pages.end() && Melder_equ ((*page) -> title.get(), dummy -> title.get())) |
| 1039 |
return (page - my pages.begin()) + 1; |
| 1040 |
} |
| 1041 |
return 0; |
| 1042 |
} |
| 1043 |
|
| 1044 |
static void grind (ManPages me) { |
| 1045 |
std::sort (my pages.begin(), my pages.end(), pageCompare); |
| 1046 |
for (integer ipage = 1; ipage <= my pages.size; ipage ++) { |
| 1047 |
ManPage page = my pages.at [ipage]; |
| 1048 |
page -> linksHither = zero_INTVEC (0); // superfluous if not ground twice |
| 1049 |
page -> linksThither = zero_INTVEC (0); // superfluous if not ground twice |
| 1050 |
} |
| 1051 |
integer ndangle = 0; |
| 1052 |
for (integer ipage = 1; ipage <= my pages.size; ipage ++) { |
| 1053 |
ManPage page = my pages.at [ipage]; |
| 1054 |
const bool verbatimAware = page -> verbatimAware; |
| 1055 |
for (int ipar = 1; ipar <= page -> paragraphs.size; ipar ++) { |
| 1056 |
ManPage_Paragraph par = & page -> paragraphs [ipar]; |
| 1057 |
const char32 *p; |
| 1058 |
char32 linkBuffer [MAXIMUM_LINK_LENGTH + 1]; |
| 1059 |
if (par -> text) for (p = ManPage_Paragraph_extractLink (par, nullptr, linkBuffer, verbatimAware); |
| 1060 |
p != nullptr; |
| 1061 |
p = ManPage_Paragraph_extractLink (par, p, linkBuffer, verbatimAware) |
| 1062 |
) { |
| 1063 |
if (linkBuffer [0] == U'\\' && ((linkBuffer [1] == U'F' && linkBuffer [2] == U'I') || (linkBuffer [1] == U'S' && linkBuffer [2] == U'C'))) |
| 1064 |
continue; // ignore "FILE" and "SCRIPT" links |
| 1065 |
const integer jpage = lookUp_sorted (me, linkBuffer); |
| 1066 |
if (jpage == 0) { |
| 1067 |
MelderInfo_writeLine (U"Page “", page -> title.get(), U"” contains a dangling link to “", linkBuffer, U"”."); |
| 1068 |
ndangle ++; |
| 1069 |
} else { |
| 1070 |
bool alreadyPresent = false; |
| 1071 |
for (int ilink = 1; ilink <= page -> linksThither.size; ilink ++) { |
| 1072 |
if (page -> linksThither [ilink] == jpage) { |
| 1073 |
alreadyPresent = true; |
| 1074 |
break; |
| 1075 |
} |
| 1076 |
} |
| 1077 |
if (! alreadyPresent) { |
| 1078 |
ManPage otherPage = my pages.at [jpage]; |
| 1079 |
* page -> linksThither. append () = jpage; |
| 1080 |
* otherPage -> linksHither. append () = ipage; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
} |
| 1084 |
} |
| 1085 |
} |
| 1086 |
if (ndangle) { |
| 1087 |
MelderInfo_close (); |
| 1088 |
Melder_warning (U"(ManPages::grind:) ", ndangle, U" dangling links encountered. See console window."); |
| 1089 |
} |
| 1090 |
my ground = true; |
| 1091 |
} |
| 1092 |
|
| 1093 |
integer ManPages_uniqueLinksHither (ManPages me, integer ipage) { |
| 1094 |
ManPage page = my pages.at [ipage]; |
| 1095 |
integer result = page -> linksHither.size; |
| 1096 |
for (integer ilinkHither = 1; ilinkHither <= page -> linksHither.size; ilinkHither ++) { |
| 1097 |
integer link = page -> linksHither [ilinkHither]; |
| 1098 |
for (integer ilinkThither = 1; ilinkThither <= page -> linksThither.size; ilinkThither ++) { |
| 1099 |
if (page -> linksThither [ilinkThither] == link) { |
| 1100 |
result --; |
| 1101 |
break; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
} |
| 1105 |
return result; |
| 1106 |
} |
| 1107 |
|
| 1108 |
integer ManPages_lookUp (ManPages me, conststring32 title) { |
| 1109 |
if (! my ground) { |
| 1110 |
//Melder_stopwatch (); |
| 1111 |
grind (me); |
| 1112 |
//Melder_information (U"grinding lasted ", Melder_stopwatch (), U" seconds."); |
| 1113 |
} |
| 1114 |
return lookUp_sorted (me, title); |
| 1115 |
} |
| 1116 |
|
| 1117 |
integer ManPages_lookUp_caseSensitive (ManPages me, conststring32 title) { |
| 1118 |
if (! my ground) |
| 1119 |
grind (me); |
| 1120 |
for (integer i = 1; i <= my pages.size; i ++) { |
| 1121 |
ManPage page = my pages.at [i]; |
| 1122 |
if (str32equ (page -> title.get(), title)) |
| 1123 |
return i; |
| 1124 |
} |
| 1125 |
return 0; |
| 1126 |
} |
| 1127 |
|
| 1128 |
constSTRVEC ManPages_getTitles (ManPages me) { |
| 1129 |
if (! my ground) |
| 1130 |
grind (me); |
| 1131 |
if (! my titles) { |
| 1132 |
my titles = autoSTRVEC (my pages.size); |
| 1133 |
for (integer i = 1; i <= my pages.size; i ++) { |
| 1134 |
ManPage page = my pages.at [i]; |
| 1135 |
my titles [i] = Melder_dup (page -> title.get()); |
| 1136 |
} |
| 1137 |
} |
| 1138 |
return my titles.get(); |
| 1139 |
} |
| 1140 |
|
| 1141 |
/* End of file ManPages.cpp */ |