Skip to content

Commit

Permalink
Add support for parsing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mangerlahn committed Aug 5, 2024
1 parent 371e872 commit df117b0
Show file tree
Hide file tree
Showing 7 changed files with 4,069 additions and 3,783 deletions.
4 changes: 3 additions & 1 deletion markdown_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void print_tree(element * elt, int indent) {
case EMPH: key = "EMPH"; break;
case STRONG: key = "STRONG"; break;
case DELETE: key = "DELETE"; break;
case COMMENT: key = "COMMENT"; break;
case PLAIN: key = "PLAIN"; break;
case PARA: key = "PARA"; break;
case LISTITEM: key = "LISTITEM"; break;
Expand All @@ -72,8 +73,9 @@ void print_tree(element * elt, int indent) {
case H5: key = "H5"; break;
case H6: key = "H6"; break;
case BLOCKQUOTE: key = "BLOCKQUOTE"; break;
case COMMENTBLOCK: key = "COMMENTBLOCK"; break;
case VERBATIM: key = "VERBATIM"; break;
case CODEBLOCK: key = "CODEBLOCK"; break;
case CODEBLOCK: key = "CODEBLOCK"; break;
case HTMLBLOCK: key = "HTMLBLOCK"; break;
case HRULE: key = "HRULE"; break;
case REFERENCE: key = "REFERENCE"; break;
Expand Down
1 change: 1 addition & 0 deletions markdown_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum markdown_extensions {
EXT_MATH = 1 << 12,
EXT_LABELS = 1 << 13,
EXT_DELETE = 1 << 14,
EXT_COMMENT = 1 << 15,
};

enum markdown_formats {
Expand Down
12 changes: 12 additions & 0 deletions markdown_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ static void print_html_element(int ext, GString *out, element *elt, bool obfusca
print_html_element_list(ext, out, elt->children, obfuscate);
g_string_append_printf(out, "</del>");
break;
case COMMENT:
g_string_append_printf(out, "<!--");
print_html_element_list(ext, out, elt->children, obfuscate);
g_string_append_printf(out, "-->");
break;
case LIST:
print_html_element_list(ext, out, elt->children, obfuscate);
break;
Expand Down Expand Up @@ -427,6 +432,13 @@ static void print_html_element(int ext, GString *out, element *elt, bool obfusca
g_string_append_printf(out, "</blockquote>");
padded = 0;
break;
case COMMENTBLOCK:
pad(out, 2);
g_string_append_printf(out, "<!--");
print_html_element_list(ext, out, elt->children, obfuscate);
g_string_append_printf(out, "-->");
padded = 0;
break;
case REFERENCE:
/* Nonprinting */
break;
Expand Down
7,805 changes: 4,023 additions & 3,782 deletions markdown_parser.h

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions markdown_parser.leg
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ MetaDataValue = a:StartList

Block = BlankLine*
( BlockQuote
| &{ extension(yy->state, EXT_COMMENT) } CommentBlock
| Verbatim
| &{ !extension(yy->state, EXT_COMPATIBILITY) } DefinitionList
| &{ !extension(yy->state, EXT_COMPATIBILITY) } Glossary
Expand All @@ -149,6 +150,7 @@ HeadingSectionBlock =
BlankLine*
!Heading
( BlockQuote
| &{ extension(yy->state, EXT_COMMENT) } CommentBlock
| Verbatim
| &{ !extension(yy->state, EXT_COMPATIBILITY) } DefinitionList
| &{ !extension(yy->state, EXT_COMPATIBILITY) } Glossary
Expand Down Expand Up @@ -223,6 +225,23 @@ BlockQuoteRaw = a:StartList
$$->key = RAW;
}

CommentBlock = a:CommentBlockRaw
{ $$ = mk_element(COMMENTBLOCK);
$$->children = a;
}

CommentBlockRaw = a:StartList
('<!--'
(!'-->' CommentBlockLine { a = cons($$, a); } )*
'-->')+
{ a = cons(mk_str("\033"), a); // add some non-whitespace character that will be stripped later. Required to keep the paragraph even for empty comments.
$$ = mk_str_from_list(a, true);
$$->key = RAW;
}

CommentBlockLine = ( < (!'-->' .)* > )
{ $$ = mk_str(yytext); }

NonblankIndentedLine = !BlankLine IndentedLine

FirstVerbatimChunk = a:StartList
Expand Down Expand Up @@ -641,6 +660,7 @@ Inline = &{ check_timeout(yy->state) }
| Link
| NoteReference
# | InlineNote # Not used in Markdown/MultiMarkdown
| &{ extension(yy->state, EXT_COMMENT) } Comment
| Code
| MarkdownHtmlTagOpen
| RawHtml
Expand Down Expand Up @@ -832,6 +852,12 @@ ExplicitLink = a:StartList l:Label '(' Sp s:Source Spnl t:Title ( &{ !extension
free_element(t);
free(l);
}

Comment = '<!--' !Whitespace
a:StartList
( !'-->' b:Inline { a = cons(b, a); })+
'-->'
{ $$ = mk_list(COMMENT, a); }

Source = ( '<' < SourceContents > '>' | < SourceContents > )
{ $$ = mk_str(yytext); }
Expand Down Expand Up @@ -1025,6 +1051,7 @@ ExtendedSpecialChar = &{ extension(yy->state, EXT_SMART) } ('.' | '-' | '\'' | '
| &{ extension(yy->state, EXT_NOTES) } ( '^' )
| &{ extension(yy->state, EXT_MATH) } ( '$' )
| &{ extension(yy->state, EXT_DELETE) } ( '~' )
| &{ extension(yy->state, EXT_COMMENT) } ( '<!--' | '-->' )

Smart = &{ extension(yy->state, EXT_SMART) }
( Ellipsis | Dash | SingleQuoted | DoubleQuoted | Apostrophe )
Expand Down
2 changes: 2 additions & 0 deletions markdown_parser_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ enum keys { LIST, /* A generic list of values. For ordered and bullet lists,
EMPH,
STRONG,
DELETE,
COMMENT,
PLAIN,
PARA,
LISTITEM,
BULLETLIST,
ORDEREDLIST,
H1, H2, H3, H4, H5, H6, H7, /* Code assumes that these are in order. */
BLOCKQUOTE,
COMMENTBLOCK,
VERBATIM,
CODEBLOCK,
HTMLBLOCK,
Expand Down
1 change: 1 addition & 0 deletions utility_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ bool match_inlines(element *l1, element *l2) {
case EMPH:
case STRONG:
case DELETE:
case COMMENT:
case LIST:
case SINGLEQUOTED:
case DOUBLEQUOTED:
Expand Down

0 comments on commit df117b0

Please sign in to comment.