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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/*
*
* Copyright (c) 2007-2011, Nick Treleaven
* Copyright (c) 2012, Lex Trotman
* Copyright (c) 2021, Jiri Techet
*
* This source code is released for free distribution under the terms of the
* GNU General Public License version 2 or (at your option) any later version.
*
* This module contains functions for generating tags for markdown files.
*
* This parser was based on the asciidoc parser.
*
* Extended syntax like footnotes is described in
* https://www.markdownguide.org/extended-syntax/
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include <ctype.h>
#include <string.h>
#include "debug.h"
#include "entry.h"
#include "parse.h"
#include "read.h"
#include "vstring.h"
#include "nestlevel.h"
#include "routines.h"
#include "promise.h"
#include "htable.h"
#include "markdown.h"
/*
* DATA DEFINITIONS
*/
typedef enum {
K_CHAPTER = 0,
K_SECTION,
K_SUBSECTION,
K_SUBSUBSECTION,
K_LEVEL4SECTION,
K_LEVEL5SECTION,
K_SECTION_COUNT,
K_FOOTNOTE = K_SECTION_COUNT,
} markdownKind;
static kindDefinition MarkdownKinds[] = {
{ true, 'c', "chapter", "chapters"},
{ true, 's', "section", "sections" },
{ true, 'S', "subsection", "level 2 sections" },
{ true, 't', "subsubsection", "level 3 sections" },
{ true, 'T', "l4subsection", "level 4 sections" },
{ true, 'u', "l5subsection", "level 5 sections" },
{ true, 'n', "footnote", "footnotes" },
};
static fieldDefinition MarkdownFields [] = {
{
.enabled = false,
.name = "sectionMarker",
.description = "character used for declaring section(#, ##, =, or -)",
},
};
typedef enum {
F_MARKER,
} markdownField;
static NestingLevels *nestingLevels = NULL;
/*
* FUNCTION DEFINITIONS
*/
static NestingLevel *getNestingLevel (const int kind, unsigned long adjustmentWhenPop)
{
NestingLevel *nl;
tagEntryInfo *e;
unsigned long line = getInputLineNumber ();
line = (line > adjustmentWhenPop)? (line - adjustmentWhenPop): 0;
while (1)
{
nl = nestingLevelsGetCurrent (nestingLevels);
e = getEntryOfNestingLevel (nl);
if ((nl && (e == NULL)) || (e && (e->kindIndex >= kind)))
nestingLevelsPopFull (nestingLevels, HT_UINT_TO_PTR ((unsigned int)line));
else
break;
}
return nl;
}
static int makeMarkdownTag (const vString* const name, const int kind, const bool twoLine)
{
int r = CORK_NIL;
if (vStringLength (name) > 0)
{
const NestingLevel *const nl = getNestingLevel (kind, twoLine? 2: 1);
tagEntryInfo *parent = getEntryOfNestingLevel (nl);
tagEntryInfo e;
initTagEntry (&e, vStringValue (name), kind);
if (twoLine)
{
/* we want the line before the '---' underline chars */
const unsigned long line = getInputLineNumber ();
Assert (line > 0);
if (line > 0)
{
e.lineNumber--;
e.filePosition = getInputFilePositionForLine (line - 1);
}
}
if (parent && (parent->kindIndex < kind))
e.extensionFields.scopeIndex = nl->corkIndex;
r = makeTagEntry (&e);
}
return r;
}
static int makeSectionMarkdownTag (const vString* const name, const int kind, const char *marker)
{
int r = makeMarkdownTag (name, kind, marker[0] != '#');
attachParserFieldToCorkEntry (r, MarkdownFields [F_MARKER].ftype, marker);
nestingLevelsPush (nestingLevels, r);
return r;
}
static vString *getHeading (const int kind, const unsigned char *line,
const int lineLen, bool *delimited)
{
int pos = 0;
int start = kind + 1;
int end = lineLen - 1;
vString *name = vStringNew ();
Assert (kind >= 0 && kind < K_SECTION_COUNT);
Assert (lineLen > start);
*delimited = false;
while (isspace (line[pos])) ++pos;
while (line[end] == line[pos] && end - 1 >= 0 && line[end - 1] != '\\')
{
--end;
*delimited = true;
}
while (isspace (line[start])) ++start;
while (isspace (line[end])) --end;
if (start <= end)
vStringNCatS (name, (const char*)(&(line[start])), end - start + 1);
return name;
}
static int getFirstCharPos (const unsigned char *line, int lineLen, bool *indented)
{
int indent = 0;
int i;
for (i = 0; i < lineLen && isspace (line[i]); i++)
indent += line[i] == '\t' ? 4 : 1;
*indented = indent >= 4;
return i;
}
static void fillEndField (NestingLevel *nl, void *ctxData)
{
tagEntryInfo *e = getEntryOfNestingLevel (nl);
if (e)
{
unsigned long line = (unsigned long)(HT_PTR_TO_UINT (ctxData));
e->extensionFields.endLine = line;
}
}
static void getFootnoteMaybe (const char *line)
{
const char *start = strstr (line, "[^");
const char *end = start? strstr(start + 2, "]:"): NULL;
if (! (start && end))
return;
if (! (end > (start + 2)))
return;
vString * footnote = vStringNewNInit (start + 2, end - (start + 2));
const NestingLevel *const nl = nestingLevelsGetCurrent (nestingLevels);
tagEntryInfo e;
initTagEntry (&e, vStringValue (footnote), K_FOOTNOTE);
if (nl)
e.extensionFields.scopeIndex = nl->corkIndex;
makeTagEntry (&e);
vStringDelete (footnote);
}
static bool extractLanguageForCodeBlock (const char *langMarker,
vString *codeLang)
{
subparser *s;
bool r = false;
foreachSubparser (s, false)
{
markdownSubparser *m = (markdownSubparser *)s;
enterSubparser(s);
if (m->extractLanguageForCodeBlock)
r = m->extractLanguageForCodeBlock (m, langMarker, codeLang);
leaveSubparser();
if (r)
break;
}
return r;
}
static void findMarkdownTags (void)
{
vString *prevLine = vStringNew ();
vString *codeLang = vStringNew ();
const unsigned char *line;
char inCodeChar = 0;
long startSourceLineNumber = 0;
long startLineNumber = 0;
bool inPreambule = false;
bool inComment = false;
subparser *sub = getSubparserRunningBaseparser();
if (sub)
chooseExclusiveSubparser (sub, NULL);
nestingLevels = nestingLevelsNewFull (0, fillEndField);
while ((line = readLineFromInputFile ()) != NULL)
{
int lineLen = strlen ((const char*) line);
bool lineProcessed = false;
bool indented;
int pos = getFirstCharPos (line, lineLen, &indented);
const int lineNum = getInputLineNumber ();
if (lineNum == 1 || inPreambule)
{
if (line[pos] == '-' && line[pos + 1] == '-' && line[pos + 2] == '-')
{
if (inPreambule)
{
long endLineNumber = lineNum;
if (startLineNumber < endLineNumber)
makePromise ("FrontMatter", startLineNumber, 0,
endLineNumber, 0, startSourceLineNumber);
}
else
startSourceLineNumber = startLineNumber = lineNum;
inPreambule = !inPreambule;
}
}
if (inPreambule)
continue;
/* fenced code block */
if (line[pos] == '`' || line[pos] == '~')
{
char c = line[pos];
char otherC = c == '`' ? '~' : '`';
int nSame;
for (nSame = 1; line[nSame] == line[pos]; ++nSame);
if (inCodeChar != otherC && nSame >= 3)
{
inCodeChar = inCodeChar ? 0 : c;
if (inCodeChar == c && strstr ((const char *)(line + pos + nSame), "```") != NULL)
inCodeChar = 0;
else if (inCodeChar)
{
const char *langMarker = (const char *)(line + pos + nSame);
startLineNumber = startSourceLineNumber = lineNum + 1;
vStringClear (codeLang);
if (! extractLanguageForCodeBlock (langMarker, codeLang))
{
vStringCopyS (codeLang, langMarker);
vStringStripLeading (codeLang);
vStringStripTrailing (codeLang);
}
}
else
{
long endLineNumber = lineNum;
if (vStringLength (codeLang) > 0
&& startLineNumber < endLineNumber)
makePromise (vStringValue (codeLang), startLineNumber, 0,
endLineNumber, 0, startSourceLineNumber);
}
lineProcessed = true;
}
}
/* XML comment start */
else if (lineLen >= pos + 4 && line[pos] == '<' && line[pos + 1] == '!' &&
line[pos + 2] == '-' && line[pos + 3] == '-')
{
if (strstr ((const char *)(line + pos + 4), "-->") == NULL)
inComment = true;
lineProcessed = true;
}
/* XML comment end */
else if (inComment && strstr ((const char *)(line + pos), "-->"))
{
inComment = false;
lineProcessed = true;
}
/* code block or comment */
if (inCodeChar || inComment)
lineProcessed = true;
/* code block using indent */
else if (indented)
lineProcessed = true;
/* if it's a title underline, or a delimited block marking character */
else if (line[pos] == '=' || line[pos] == '-' || line[pos] == '#' || line[pos] == '>')
{
int nSame;
for (nSame = 1; line[nSame] == line[pos]; ++nSame);
/* quote */
if (line[pos] == '>')
; /* just to make sure lineProcessed = true so it won't be in a heading */
/* is it a two line title */
else if (line[pos] == '=' || line[pos] == '-')
{
char marker[2] = { line[pos], '\0' };
int kind = line[pos] == '=' ? K_CHAPTER : K_SECTION;
bool whitespaceTerminated = true;
for (int i = pos + nSame; i < lineLen; i++)
{
if (!isspace (line[i]))
{
whitespaceTerminated = false;
break;
}
}
vStringStripLeading (prevLine);
vStringStripTrailing (prevLine);
if (whitespaceTerminated && vStringLength (prevLine) > 0)
makeSectionMarkdownTag (prevLine, kind, marker);
}
/* otherwise is it a one line title */
else if (line[pos] == '#' && nSame <= K_SECTION_COUNT && isspace (line[nSame]))
{
int kind = nSame - 1;
bool delimited = false;
vString *name = getHeading (kind, line, lineLen, &delimited);
if (vStringLength (name) > 0)
makeSectionMarkdownTag (name, kind, delimited ? "##" : "#");
vStringDelete (name);
}
lineProcessed = true;
}
vStringClear (prevLine);
if (!lineProcessed)
{
getFootnoteMaybe ((const char *)line);
vStringCatS (prevLine, (const char*) line);
}
}
vStringDelete (prevLine);
vStringDelete (codeLang);
{
unsigned int line = (unsigned int)getInputLineNumber ();
nestingLevelsFreeFull (nestingLevels, HT_UINT_TO_PTR (line));
}
}
extern parserDefinition* MarkdownParser (void)
{
parserDefinition* const def = parserNew ("Markdown");
static const char *const extensions [] = { "md", "markdown", NULL };
def->enabled = true;
def->extensions = extensions;
def->useCork = CORK_QUEUE;
def->kindTable = MarkdownKinds;
def->kindCount = ARRAY_SIZE (MarkdownKinds);
def->fieldTable = MarkdownFields;
def->fieldCount = ARRAY_SIZE (MarkdownFields);
def->defaultScopeSeparator = "\"\"";
def->parser = findMarkdownTags;
/*
* This setting (useMemoryStreamInput) is for running
* Yaml parser from YamlFrontMatter as subparser.
* YamlFrontMatter is run from FrontMatter as a geust parser.
* FrontMatter is run from Markdown as a guest parser.
* This stacked structure hits the limitation of the main
* part: subparser's requirement for memory based input stream
* is not propagated to the main part.
*
* TODO: instead of setting useMemoryStreamInput here, we
* should remove the limitation.
*/
def->useMemoryStreamInput = true;
return def;
}
|