@@ -171,7 +171,8 @@ static inline bool can_contain(cmark_node_type parent_type,
171171static inline bool accepts_lines (cmark_node_type block_type ) {
172172 return (block_type == CMARK_NODE_PARAGRAPH ||
173173 block_type == CMARK_NODE_HEADING ||
174- block_type == CMARK_NODE_CODE_BLOCK );
174+ block_type == CMARK_NODE_CODE_BLOCK ||
175+ block_type == CMARK_NODE_FORMULA_BLOCK );
175176}
176177
177178static inline bool contains_inlines (cmark_node_type block_type ) {
@@ -270,6 +271,7 @@ static cmark_node *finalize(cmark_parser *parser, cmark_node *b) {
270271 b -> end_column = parser -> last_line_length ;
271272 } else if (S_type (b ) == CMARK_NODE_DOCUMENT ||
272273 (S_type (b ) == CMARK_NODE_CODE_BLOCK && b -> as .code .fenced ) ||
274+ S_type (b ) == CMARK_NODE_FORMULA_BLOCK ||
273275 (S_type (b ) == CMARK_NODE_HEADING && b -> as .heading .setext )) {
274276 b -> end_line = parser -> line_number ;
275277 b -> end_column = parser -> curline .size ;
@@ -330,6 +332,11 @@ static cmark_node *finalize(cmark_parser *parser, cmark_node *b) {
330332 b -> data = cmark_strbuf_detach (node_content );
331333 break ;
332334
335+ case CMARK_NODE_FORMULA_BLOCK :
336+ b -> len = node_content -> size ;
337+ b -> data = cmark_strbuf_detach (node_content );
338+ break ;
339+
333340 case CMARK_NODE_HEADING :
334341 case CMARK_NODE_HTML_BLOCK :
335342 b -> len = node_content -> size ;
@@ -859,6 +866,35 @@ static bool parse_code_block_prefix(cmark_parser *parser, cmark_chunk *input,
859866 return res ;
860867}
861868
869+ static bool parse_formula_block_prefix (cmark_parser * parser , cmark_chunk * input ,
870+ cmark_node * container , bool * should_continue ) {
871+ bool res = false;
872+ bufsize_t matched = 0 ;
873+
874+ if (parser -> indent <= 3 ) {
875+ matched = scan_formula_block_end (input , parser -> first_nonspace );
876+ }
877+
878+ if (matched ) {
879+ // closing fence - and since we're at
880+ // the end of a line, we can stop processing it:
881+ * should_continue = false;
882+ S_advance_offset (parser , input , matched , false);
883+ parser -> current = finalize (parser , container );
884+ } else {
885+ // skip opt. spaces of fence parser->offset
886+ int i = container -> as .formula .fence_offset ;
887+
888+ while (i > 0 && S_is_space_or_tab (peek_at (input , parser -> offset ))) {
889+ S_advance_offset (parser , input , 1 , true);
890+ i -- ;
891+ }
892+ res = true;
893+ }
894+
895+ return res ;
896+ }
897+
862898static bool parse_html_block_prefix (cmark_parser * parser ,
863899 cmark_node * container ) {
864900 bool res = false;
@@ -924,6 +960,7 @@ static cmark_node *check_open_blocks(cmark_parser *parser, cmark_chunk *input,
924960 // first blank line was processed. Certain block types accept
925961 // empty lines as content, so add them here.
926962 if (parser -> current -> type == CMARK_NODE_CODE_BLOCK ||
963+ parser -> current -> type == CMARK_NODE_FORMULA_BLOCK ||
927964 parser -> current -> type == CMARK_NODE_HTML_BLOCK ) {
928965 add_line (input , parser );
929966 }
@@ -942,6 +979,10 @@ static cmark_node *check_open_blocks(cmark_parser *parser, cmark_chunk *input,
942979 if (!parse_code_block_prefix (parser , input , container , & should_continue ))
943980 goto done ;
944981 break ;
982+ case CMARK_NODE_FORMULA_BLOCK :
983+ if (!parse_formula_block_prefix (parser , input , container , & should_continue ))
984+ goto done ;
985+ break ;
945986 case CMARK_NODE_HEADING :
946987 // a heading can never contain more than one line
947988 goto done ;
@@ -986,6 +1027,7 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container,
9861027 int save_column ;
9871028
9881029 while (cont_type != CMARK_NODE_CODE_BLOCK &&
1030+ cont_type != CMARK_NODE_FORMULA_BLOCK &&
9891031 cont_type != CMARK_NODE_HTML_BLOCK ) {
9901032
9911033 S_find_first_nonspace (parser , input );
@@ -1028,18 +1070,28 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container,
10281070 (* container )-> as .heading .internal_offset = matched ;
10291071
10301072 } else if (!indented && (matched = scan_open_code_fence (
1031- input , parser -> first_nonspace ))) {
1073+ input , parser -> first_nonspace ))) {
10321074 * container = add_child (parser , * container , CMARK_NODE_CODE_BLOCK ,
1033- parser -> first_nonspace + 1 );
1075+ parser -> first_nonspace + 1 );
10341076 (* container )-> as .code .fenced = true;
10351077 (* container )-> as .code .fence_char = peek_at (input , parser -> first_nonspace );
10361078 (* container )-> as .code .fence_length = (matched > 255 ) ? 255 : matched ;
10371079 (* container )-> as .code .fence_offset =
10381080 (int8_t )(parser -> first_nonspace - parser -> offset );
10391081 (* container )-> as .code .info = NULL ;
10401082 S_advance_offset (parser , input ,
1041- parser -> first_nonspace + matched - parser -> offset ,
1042- false);
1083+ parser -> first_nonspace + matched - parser -> offset ,
1084+ false);
1085+
1086+ } else if (!indented && (matched = scan_formula_block_start (
1087+ input , parser -> first_nonspace ))) {
1088+ * container = add_child (parser , * container , CMARK_NODE_FORMULA_BLOCK ,
1089+ parser -> first_nonspace + 1 );
1090+ (* container )-> as .formula .fence_offset =
1091+ (int8_t )(parser -> first_nonspace - parser -> offset );
1092+ S_advance_offset (parser , input ,
1093+ parser -> first_nonspace + matched - parser -> offset ,
1094+ false);
10431095
10441096 } else if (!indented && ((matched = scan_html_block_start (
10451097 input , parser -> first_nonspace )) ||
@@ -1175,6 +1227,7 @@ static void add_text_to_container(cmark_parser *parser, cmark_node *container,
11751227 const bool last_line_blank =
11761228 (parser -> blank && ctype != CMARK_NODE_BLOCK_QUOTE &&
11771229 ctype != CMARK_NODE_HEADING && ctype != CMARK_NODE_THEMATIC_BREAK &&
1230+ ctype != CMARK_NODE_FORMULA_BLOCK &&
11781231 !(ctype == CMARK_NODE_CODE_BLOCK && container -> as .code .fenced ) &&
11791232 !(ctype == CMARK_NODE_ITEM && container -> first_child == NULL &&
11801233 container -> start_line == parser -> line_number ));
@@ -1204,7 +1257,8 @@ static void add_text_to_container(cmark_parser *parser, cmark_node *container,
12041257 assert (parser -> current != NULL );
12051258 }
12061259
1207- if (S_type (container ) == CMARK_NODE_CODE_BLOCK ) {
1260+ if (S_type (container ) == CMARK_NODE_CODE_BLOCK ||
1261+ S_type (container ) == CMARK_NODE_FORMULA_BLOCK ) {
12081262 add_line (input , parser );
12091263 } else if (S_type (container ) == CMARK_NODE_HTML_BLOCK ) {
12101264 add_line (input , parser );
0 commit comments