-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mmd.c
2381 lines (1932 loc) · 54.9 KB
/
mmd.c
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Implementation of miniature markdown library.
//
// https://www.msweet.org/mmd
//
// Copyright © 2017-2024 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
//
// Define DEBUG to get debug printf messages to stderr.
//
#define DEBUG 0
#if DEBUG > 0
# define DEBUG_printf(...) fprintf(stderr, __VA_ARGS__)
# define DEBUG_puts(s) fputs(s, stderr);
#else
# define DEBUG_printf(...)
# define DEBUG_puts(s)
#endif // DEBUG > 0
#if DEBUG > 1
# define DEBUG2_printf(...) fprintf(stderr, __VA_ARGS__)
# define DEBUG2_puts(s) fputs(s, stderr);
#else
# define DEBUG2_printf(...)
# define DEBUG2_puts(s)
#endif // DEBUG > 1
//
// Beginning with VC2005, Microsoft breaks ISO C and POSIX conformance
// by deprecating a number of functions in the name of security, even
// when many of the affected functions are otherwise completely secure.
// The _CRT_SECURE_NO_DEPRECATE definition ensures that we won't get
// warnings from their use...
//
// Then Microsoft decided that they should ignore this in VC2008 and use
// yet another define (_CRT_SECURE_NO_WARNINGS) instead...
//
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#if _WIN32
# define strcasecmp stricmp
#endif // _WIN32
#include "mmd.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
//
// Microsoft renames the POSIX functions to _name, and introduces a broken
// compatibility layer using the original names. As a result, random crashes
// can occur when, for example, strdup() allocates memory from a different heap
// than used by malloc() and free().
//
// To avoid moronic problems like this, we #define the POSIX function names to
// the corresponding non-standard Microsoft names.
//
#ifdef _WIN32
# define snprintf _snprintf
# define strdup _strdup
#endif // _WIN32
//
// Private structures...
//
struct _mmd_s
{
mmd_type_t type; // Node type
bool whitespace; // Leading whitespace?
char *text, // Text
*url, // Reference URL (image/link/etc.)
*extra; // Title, language name, etc.
mmd_t *parent, // Parent node
*first_child, // First child node
*last_child, // Last child node
*prev_sibling, // Previous sibling node
*next_sibling; // Next sibling node
};
typedef struct _mmd_filebuf_s // Buffered file
{
mmd_iocb_t cb; // Read callback function
void *cbdata; // Read callback data
char buffer[65536], // Buffer
*bufptr, // Pointer into buffer
*bufend; // End of buffer
} _mmd_filebuf_t;
typedef struct _mmd_ref_s // Reference link
{
char *name, // Name of reference
*url, // Reference URL
*title; // Title, if any
size_t num_pending; // Number of pending nodes
mmd_t **pending; // Pending nodes
} _mmd_ref_t;
typedef struct _mmd_doc_s // Markdown document
{
mmd_t *root; // Root node
size_t num_references; // Number of references
_mmd_ref_t *references; // References
} _mmd_doc_t;
typedef struct _mmd_stack_s // Markdown block stack
{
mmd_t *parent; // Parent node
int indent; // Indentation
char fence; // Code fence character
size_t fencelen; // Length of code fence
} _mmd_stack_t;
//
// Local globals...
//
static mmd_option_t mmd_options = MMD_OPTION_ALL;
// Markdown extensions to support
//
// Local functions...
//
static mmd_t *mmd_add(mmd_t *parent, mmd_type_t type, int whitespace, char *text, char *url);
static void mmd_free(mmd_t *node);
static int mmd_has_continuation(const char *line, _mmd_filebuf_t *file, int indent);
static size_t mmd_iocb_file(FILE *fp, char *buffer, size_t bytes);
static size_t mmd_iocb_string(const char **s, char *buffer, size_t bytes);
static size_t mmd_is_chars(const char *lineptr, const char *chars, size_t minchars);
static size_t mmd_is_codefence(char *lineptr, char fence, size_t fencelen, char **language);
static bool mmd_is_table(_mmd_filebuf_t *file, int indent);
static void mmd_parse_inline(_mmd_doc_t *doc, mmd_t *parent, char *lineptr);
static char *mmd_parse_link(_mmd_doc_t *doc, char *lineptr, char **text, char **url, char **title, char **refname);
static void mmd_read_buffer(_mmd_filebuf_t *file);
static char *mmd_read_line(_mmd_filebuf_t *file, char *line, size_t linesize);
static void mmd_ref_add(_mmd_doc_t *doc, mmd_t *node, const char *name, const char *url, const char *title);
static _mmd_ref_t *mmd_ref_find(_mmd_doc_t *doc, const char *name);
static void mmd_remove(mmd_t *node);
#if DEBUG
static const char *mmd_type_string(mmd_type_t type);
#endif // DEBUG
//
// 'mmdCopyAllText()' - Make a copy of all the text under a given node.
//
// The returned string must be freed using free().
//
char * // O - Copied string
mmdCopyAllText(mmd_t *node) // I - Parent node
{
char *all = NULL, // String buffer
*allptr = NULL, // Pointer into string buffer
*temp; // Temporary pointer
size_t allsize = 0, // Size of "all" buffer
textlen; // Length of "text" string
mmd_t *current, // Current node
*next; // Next node
current = mmdGetFirstChild(node);
while (current != node)
{
if (current->text)
{
// Append this node's text to the string...
textlen = strlen(current->text);
allsize += textlen + (size_t)current->whitespace;
temp = realloc(all, allsize);
if (!temp)
{
free(all);
return (NULL);
}
allptr = temp + (allptr - all);
all = temp;
if (current->whitespace)
*allptr++ = ' ';
memcpy(allptr, current->text, textlen);
allptr += textlen;
}
// Find the next logical node...
if ((next = mmdGetNextSibling(current)) == NULL)
{
next = mmdGetParent(current);
while (next && next != node && mmdGetNextSibling(next) == NULL)
next = mmdGetParent(next);
if (next != node)
next = mmdGetNextSibling(next);
}
current = next;
}
if (allptr)
*allptr = '\0';
return (all);
}
//
// 'mmdFree()' - Free a markdown tree.
//
void
mmdFree(mmd_t *node) // I - First node
{
mmd_t *current, // Current node
*next; // Next node
mmd_remove(node);
for (current = node->first_child; current; current = next)
{
// Get the next node...
if ((next = current->first_child) != NULL)
{
// Free parent nodes after child nodes have been freed...
current->first_child = NULL;
continue;
}
if ((next = current->next_sibling) == NULL)
{
// Next node is the parent, which we'll free as needed...
if ((next = current->parent) == node)
next = NULL;
}
// Free child...
mmd_free(current);
}
// Then free the memory used by the parent node...
mmd_free(node);
}
//
// 'mmdGetExtra()' - Get extra text (title, language, etc.) associated with a
// node.
//
const char * // O - Extra text or NULL if none
mmdGetExtra(mmd_t *node) // I - Node
{
return (node ? node->extra : NULL);
}
//
// 'mmdGetFirstChild()' - Return the first child of a node, if any.
//
mmd_t * // O - First child or @code NULL@ if none
mmdGetFirstChild(mmd_t *node) // I - Node
{
return (node ? node->first_child : NULL);
}
//
// 'mmdGetLastChild()' - Return the last child of a node, if any.
//
mmd_t * // O - Last child or @code NULL@ if none
mmdGetLastChild(mmd_t *node) // I - Node
{
return (node ? node->last_child : NULL);
}
//
// 'mmdGetMetadata()' - Return the metadata for the given keyword.
//
const char * // O - Value or @code NULL@ if none
mmdGetMetadata(mmd_t *doc, // I - Document
const char *keyword) // I - Keyword
{
mmd_t *metadata, // Metadata node
*current; // Current node
char prefix[256]; // Prefix string
size_t prefix_len; // Length of prefix string
const char *value; // Pointer to value
if (!doc || (metadata = doc->first_child) == NULL || metadata->type != MMD_TYPE_METADATA)
return (NULL);
snprintf(prefix, sizeof(prefix), "%s:", keyword);
prefix_len = strlen(prefix);
for (current = metadata->first_child; current; current = current->next_sibling)
{
if (strncmp(current->text, prefix, prefix_len))
continue;
value = current->text + prefix_len;
while (isspace(*value & 255))
value ++;
return (value);
}
return (NULL);
}
//
// 'mmdGetNextSibling()' - Return the next sibling of a node, if any.
//
mmd_t * // O - Next sibling or @code NULL@ if none
mmdGetNextSibling(mmd_t *node) // I - Node
{
return (node ? node->next_sibling : NULL);
}
//
// 'mmdGetOptions()' - Get the enabled markdown processing options/extensions.
//
mmd_option_t // O - Enabled options
mmdGetOptions(void)
{
return (mmd_options);
}
//
// 'mmdGetParent()' - Return the parent of a node, if any.
//
mmd_t * // O - Parent node or @code NULL@ if none
mmdGetParent(mmd_t *node) // I - Node
{
return (node ? node->parent : NULL);
}
//
// 'mmdGetPrevSibling()' - Return the previous sibling of a node, if any.
//
mmd_t * // O - Previous sibling or @code NULL@ if none
mmdGetPrevSibling(mmd_t *node) // I - Node
{
return (node ? node->prev_sibling : NULL);
}
//
// 'mmdGetText()' - Return the text associated with a node, if any.
//
const char * // O - Text or @code NULL@ if none
mmdGetText(mmd_t *node) // I - Node
{
return (node ? node->text : NULL);
}
//
// 'mmdGetType()' - Return the type of a node, if any.
//
mmd_type_t // O - Type or @code MMD_TYPE_NONE@ if none
mmdGetType(mmd_t *node) // I - Node
{
return (node ? node->type : MMD_TYPE_NONE);
}
//
// 'mmdGetURL()' - Return the URL associated with a node, if any.
//
const char * // O - URL or @code NULL@ if none
mmdGetURL(mmd_t *node) // I - Node
{
return (node ? node->url : NULL);
}
//
// 'mmdGetWhitespace()' - Return whether whitespace preceded a node.
//
bool // O - `true` for leading whitespace, `false` for none
mmdGetWhitespace(mmd_t *node) // I - Node
{
return (node ? node->whitespace : false);
}
//
// 'mmdIsBlock()' - Return whether the node is a block.
//
bool // O - `true` for block nodes, `false` otherwise
mmdIsBlock(mmd_t *node) // I - Node
{
return (node ? node->type < MMD_TYPE_NORMAL_TEXT : false);
}
//
// 'mmdLoad()' - Load a markdown file into nodes.
//
mmd_t * // O - Root node in markdown
mmdLoad(mmd_t *root, // I - Root node for document or `NULL` for a new document
const char *filename) // I - File to load
{
FILE *fp; // File
// Open the file and load the document...
if ((fp = fopen(filename, "r")) == NULL)
return (NULL);
root = mmdLoadIO(root, (mmd_iocb_t)mmd_iocb_file, fp);
// Close and return...
fclose(fp);
return (root);
}
//
// 'mmdLoadFile()' - Load a markdown file into nodes from a stdio file.
//
mmd_t * // O - First node in markdown
mmdLoadFile(mmd_t *root, // I - Root node for document or `NULL` for a new document
FILE *fp) // I - File to load
{
return (mmdLoadIO(root, (mmd_iocb_t)mmd_iocb_file, fp));
}
//
// 'mmdLoadIO()' - Load a markdown file into nodes using a callback.
//
mmd_t * // O - First node in markdown
mmdLoadIO(mmd_t *root, // I - Root node for document or `NULL` for a new document
mmd_iocb_t cb, // I - Read callback function
void *cbdata) // I - Read callback data
{
size_t i; // Looping var
_mmd_doc_t doc; // Document
_mmd_ref_t *reference; // Current reference
mmd_t *block = NULL; // Current block
mmd_type_t type; // Type for line
_mmd_filebuf_t file; // File buffer
char line[8192], // Read line
*linestart, // Start of line
*lineptr, // Pointer into line
*lineend, // End of line
*temp; // Temporary pointer
int newindent; // New indentation
int blank_code = 0; // Saved indented blank code line
mmd_type_t columns[256]; // Alignment of table columns
int num_columns = 0, // Number of columns in table
rows = 0; // Number of rows in table
_mmd_stack_t stack[32], // Block stack
*stackptr = stack; // Pointer to top of stack
// Create an empty document as needed...
DEBUG_printf("mmdLoadIO: mmd_options=%d%s%s\n", mmd_options, (mmd_options & MMD_OPTION_METADATA) ? " METADATA" : "", (mmd_options & MMD_OPTION_TABLES) ? " TABLES" : "");
memset(&doc, 0, sizeof(doc));
if (root)
doc.root = root;
else
doc.root = mmd_add(NULL, MMD_TYPE_DOCUMENT, 0, NULL, NULL);
if (!doc.root)
return (NULL);
// Initialize the block stack...
memset(stack, 0, sizeof(stack));
stackptr->parent = doc.root;
// Read lines until end-of-file...
memset(&file, 0, sizeof(file));
file.cb = cb;
file.cbdata = cbdata;
#ifdef __clang_analyzer__
memset(line, 0, sizeof(line));
#endif // __clang_analyzer__
while ((lineptr = mmd_read_line(&file, line, sizeof(line))) != NULL)
{
DEBUG_printf("%03d %-12s %s", stackptr->indent, mmd_type_string(stackptr->parent->type) + 9, lineptr);
#if DEBUG
if (stackptr->parent->type == MMD_TYPE_CODE_BLOCK)
DEBUG2_printf(" blank_code=%d\n", blank_code);
#endif // DEBUG
linestart = lineptr;
while (isspace(*lineptr & 255))
lineptr ++;
DEBUG2_printf(" line indent=%d\n", (int)(lineptr - line));
DEBUG2_printf(" stackptr=%d\n", (int)(stackptr - stack));
if (!*lineptr && stackptr->parent->type == MMD_TYPE_TABLE)
{
DEBUG2_puts("END TABLE\n");
stackptr --;
block = NULL;
continue;
}
else if (*lineptr == '>' && (lineptr - linestart) < 4)
{
// Block quote. See if there is an existing blockquote...
DEBUG_printf(" BLOCKQUOTE (stackptr=%ld)\n", stackptr - stack);
if (stackptr == stack || stack[1].parent->type != MMD_TYPE_BLOCK_QUOTE)
{
block = NULL;
stackptr = stack + 1;
stackptr->parent = mmd_add(doc.root, MMD_TYPE_BLOCK_QUOTE, 0, NULL, NULL);
stackptr->indent = 2;
stackptr->fence = '\0';
}
// Skip whitespace after the ">"...
lineptr ++;
if (isspace(*lineptr & 255))
lineptr ++;
linestart = lineptr;
while (isspace(*lineptr & 255))
lineptr ++;
}
else if (*lineptr != '>' && stackptr > stack && stack[1].parent->type == MMD_TYPE_BLOCK_QUOTE && (!block || *lineptr == '\n' || mmd_is_chars(lineptr, "- \t", 3) || mmd_is_chars(lineptr, "_ \t", 3) || mmd_is_chars(lineptr, "* \t", 3)))
{
// Not a lazy continuation so terminate this block quote...
DEBUG_puts(" Terminating BLOCKQUOTE\n");
block = NULL;
stackptr = stack;
}
// Now handle all other markup not related to block quotes...
DEBUG2_printf(" stackptr=%d (%s), block=%p (%s)\n", (int)(stackptr - stack), mmd_type_string(stackptr->parent->type) + 9, block, block ? mmd_type_string(block->type) + 9 : "");
DEBUG2_printf(" strchr(lineptr, '|')=%p, mmd_is_table(&file, stackptr->indent)=%d\n", strchr(lineptr, '|'), mmd_is_table(&file, stackptr->indent));
DEBUG2_printf(" linestart=%d, lineptr=%d\n", (int)(linestart - line), (int)(lineptr - line));
DEBUG2_printf(" mmd_is_chars(lineptr, \"-\", 1)=%d\n", (int)mmd_is_chars(lineptr, "-", 1));
DEBUG2_printf(" mmd_is_chars(lineptr, \"=\", 1)=%d\n", (int)mmd_is_chars(lineptr, "=", 1));
if ((lineptr - line - stackptr->indent) < 4 && ((stackptr->parent->type != MMD_TYPE_CODE_BLOCK && !stackptr->fence && mmd_is_codefence(lineptr, '\0', 0, NULL)) || (stackptr->fence && mmd_is_codefence(lineptr, stackptr->fence, stackptr->fencelen, NULL))))
{
// Code fence...
DEBUG2_printf("stackptr->indent=%d, fence='%c', fencelen=%d\n", stackptr->indent, stackptr->fence, (int)stackptr->fencelen);
if (stackptr->parent->type == MMD_TYPE_CODE_BLOCK)
{
DEBUG2_puts("Ending code block...\n");
stackptr --;
}
else if (stackptr < (stack + sizeof(stack) / sizeof(stack[0]) - 1))
{
char *language; // Language name, if any
DEBUG2_printf("Starting code block with fence '%c'.\n", *lineptr);
block = NULL;
stackptr[1].parent = mmd_add(stackptr->parent, MMD_TYPE_CODE_BLOCK, 0, NULL, NULL);
stackptr[1].indent = lineptr - line;
stackptr[1].fence = *lineptr;
stackptr[1].fencelen = mmd_is_codefence(lineptr, '\0', 0, &language);
stackptr ++;
DEBUG2_printf("Code language=\"%s\"\n", language);
if (language)
stackptr->parent->extra = strdup(language);
blank_code = 0;
}
continue;
}
else if (stackptr->parent->type == MMD_TYPE_CODE_BLOCK && (lineptr - line) >= stackptr->indent)
{
if (line[stackptr->indent] == '\n')
{
blank_code ++;
}
else
{
while (blank_code > 0)
{
mmd_add(stackptr->parent, MMD_TYPE_CODE_TEXT, 0, "\n", NULL);
blank_code --;
}
mmd_add(stackptr->parent, MMD_TYPE_CODE_TEXT, 0, line + stackptr->indent, NULL);
}
continue;
}
else if (stackptr->parent->type == MMD_TYPE_CODE_BLOCK && stackptr->fence)
{
DEBUG2_printf(" fence='%c'\n", stackptr->fence);
if (!*lineptr)
{
blank_code ++;
}
else
{
while (blank_code > 0)
{
mmd_add(stackptr->parent, MMD_TYPE_CODE_TEXT, 0, "\n", NULL);
blank_code --;
}
mmd_add(stackptr->parent, MMD_TYPE_CODE_TEXT, 0, lineptr, NULL);
}
continue;
}
else if (!strncmp(lineptr, "---", 3) && doc.root->first_child == NULL && (mmd_options & MMD_OPTION_METADATA))
{
// Document metadata...
block = mmd_add(doc.root, MMD_TYPE_METADATA, 0, NULL, NULL);
while ((lineptr = mmd_read_line(&file, line, sizeof(line))) != NULL)
{
while (isspace(*lineptr & 255))
lineptr ++;
if (!strncmp(lineptr, "---", 3) || !strncmp(lineptr, "...", 3))
break;
lineend = lineptr + strlen(lineptr) - 1;
if (lineend > lineptr && *lineend == '\n')
*lineend = '\0';
mmd_add(block, MMD_TYPE_METADATA_TEXT, 0, lineptr, NULL);
}
continue;
}
else if (block && block->type == MMD_TYPE_PARAGRAPH && (lineptr - linestart) < 4 && (lineptr - line) >= stackptr->indent && (mmd_is_chars(lineptr, "-", 1) || mmd_is_chars(lineptr, "=", 1)))
{
int ch = *lineptr;
DEBUG_puts(" SETEXT HEADING\n");
lineptr += 3;
while (*lineptr == ch)
lineptr ++;
while (isspace(*lineptr & 255))
lineptr ++;
if (!*lineptr)
{
if (ch == '=')
block->type = MMD_TYPE_HEADING_1;
else
block->type = MMD_TYPE_HEADING_2;
block = NULL;
continue;
}
type = MMD_TYPE_PARAGRAPH;
}
else if ((lineptr - linestart) < 4 && (mmd_is_chars(lineptr, "- \t", 3) || mmd_is_chars(lineptr, "_ \t", 3) || mmd_is_chars(lineptr, "* \t", 3)))
{
DEBUG_puts(" THEMATIC BREAK\n");
if (line[0] == '>')
stackptr = stack + 1;
else
stackptr = stack;
mmd_add(stackptr->parent, MMD_TYPE_THEMATIC_BREAK, 0, NULL, NULL);
// type = MMD_TYPE_PARAGRAPH;
block = NULL;
continue;
}
else if ((*lineptr == '-' || *lineptr == '+' || *lineptr == '*') && (lineptr[1] == '\t' || lineptr[1] == ' '))
{
// Bulleted list...
DEBUG_puts(" UNORDERED LIST\n");
lineptr += 2;
linestart = lineptr;
newindent = linestart - line;
while (isspace(*lineptr & 255))
lineptr ++;
while (stackptr > stack && stackptr->indent > newindent)
stackptr --;
if (stackptr > stack && stackptr->parent->type == MMD_TYPE_LIST_ITEM && stackptr->indent == newindent)
stackptr --;
if (stackptr > stack && stackptr->parent->type == MMD_TYPE_ORDERED_LIST && stackptr->indent == newindent)
stackptr --;
if (stackptr > stack && stackptr->parent->type == MMD_TYPE_BLOCK_QUOTE && line[0] != '>')
stackptr --;
if (stackptr->parent->type != MMD_TYPE_UNORDERED_LIST && stackptr < (stack + sizeof(stack) / sizeof(stack[0]) - 1))
{
stackptr[1].parent = mmd_add(stackptr->parent, MMD_TYPE_UNORDERED_LIST, 0, NULL, NULL);
stackptr[1].indent = linestart - line;
stackptr[1].fence = '\0';
stackptr ++;
}
if (stackptr < (stack + sizeof(stack) / sizeof(stack[0]) - 1))
{
stackptr[1].parent = mmd_add(stackptr->parent, MMD_TYPE_LIST_ITEM, 0, NULL, NULL);
stackptr[1].indent = linestart - line;
stackptr[1].fence = '\0';
stackptr ++;
}
type = MMD_TYPE_PARAGRAPH;
block = NULL;
if (mmd_is_chars(lineptr, "- \t", 3) || mmd_is_chars(lineptr, "_ \t", 3) || mmd_is_chars(lineptr, "* \t", 3))
{
mmd_add(stackptr->parent, MMD_TYPE_THEMATIC_BREAK, 0, NULL, NULL);
continue;
}
}
else if (isdigit(*lineptr & 255))
{
// Ordered list?
DEBUG_puts(" ORDERED LIST?\n");
temp = lineptr + 1;
while (isdigit(*temp & 255))
temp ++;
if ((*temp == '.' || *temp == ')') && (temp[1] == '\t' || temp[1] == ' '))
{
// Yes, ordered list.
lineptr = temp + 2;
linestart = lineptr;
newindent = linestart - line;
while (isspace(*lineptr & 255))
lineptr ++;
while (stackptr > stack && stackptr->indent > newindent)
stackptr --;
if (stackptr->parent->type == MMD_TYPE_LIST_ITEM && stackptr->indent == newindent)
stackptr --;
if (stackptr->parent->type == MMD_TYPE_UNORDERED_LIST && stackptr->indent == newindent)
stackptr --;
if (stackptr->parent->type == MMD_TYPE_BLOCK_QUOTE && line[0] != '>')
stackptr --;
if (stackptr->parent->type != MMD_TYPE_ORDERED_LIST && stackptr < (stack + sizeof(stack) / sizeof(stack[0]) - 1))
{
stackptr[1].parent = mmd_add(stackptr->parent, MMD_TYPE_ORDERED_LIST, 0, NULL, NULL);
stackptr[1].indent = linestart - line;
stackptr[1].fence = '\0';
stackptr ++;
}
if (stackptr < (stack + sizeof(stack) / sizeof(stack[0]) - 1))
{
stackptr[1].parent = mmd_add(stackptr->parent, MMD_TYPE_LIST_ITEM, 0, NULL, NULL);
stackptr[1].indent = linestart - line;
stackptr[1].fence = '\0';
stackptr ++;
}
type = MMD_TYPE_PARAGRAPH;
block = NULL;
}
else
{
// No, just a regular paragraph...
type = block ? block->type : MMD_TYPE_PARAGRAPH;
}
}
else if (*lineptr == '#' && (lineptr - linestart) < 4)
{
// Heading, count the number of '#' for the heading level...
DEBUG_puts(" HEADING?\n");
newindent = lineptr - line;
temp = lineptr + 1;
while (*temp == '#')
temp ++;
if ((temp - lineptr) <= 6 && isspace(*temp & 255))
{
// Heading 1-6...
type = MMD_TYPE_HEADING_1 + (temp - lineptr - 1);
block = NULL;
// Skip whitespace after "#"...
lineptr = temp;
while (isspace(*lineptr & 255))
lineptr ++;
linestart = lineptr;
// Strip trailing "#" characters and whitespace...
temp = lineptr + strlen(lineptr) - 1;
while (temp > lineptr && isspace(*temp & 255))
*temp-- = '\0';
while (temp > lineptr && *temp == '#')
temp --;
if (isspace(*temp & 255))
{
while (temp > lineptr && isspace(*temp & 255))
*temp-- = '\0';
}
else if (temp == lineptr)
*temp = '\0';
while (stackptr > stack && stackptr->indent > newindent)
stackptr --;
block = mmd_add(stackptr->parent, type, 0, NULL, NULL);
}
else
{
// More than 6 #'s, just treat as a paragraph...
type = MMD_TYPE_PARAGRAPH;
}
}
else if (block && block->type >= MMD_TYPE_HEADING_1 && block->type <= MMD_TYPE_HEADING_6)
{
DEBUG_puts(" PARAGRAPH\n");
type = MMD_TYPE_PARAGRAPH;
block = NULL;
}
else if (!block)
{
type = MMD_TYPE_PARAGRAPH;
if (lineptr == line && stackptr->parent->type != MMD_TYPE_TABLE)
stackptr = stack;
}
else
type = block->type;
if (!*lineptr)
{
if (stackptr->parent->type == MMD_TYPE_CODE_BLOCK)
blank_code ++;
else if (stackptr->parent->type == MMD_TYPE_BLOCK_QUOTE && line[0] != '>')
stackptr --;
block = NULL;
continue;
}
else if (!strcmp(lineptr, "+"))
{
if (block)
{
if (block->type == MMD_TYPE_LIST_ITEM)
block = mmd_add(block, MMD_TYPE_PARAGRAPH, 0, NULL, NULL);
else if (block->parent->type == MMD_TYPE_LIST_ITEM)
block = mmd_add(block->parent, MMD_TYPE_PARAGRAPH, 0, NULL, NULL);
else
block = NULL;
}
continue;
}
else if ((mmd_options & MMD_OPTION_TABLES) && strchr(lineptr, '|') && (stackptr->parent->type == MMD_TYPE_TABLE || mmd_is_table(&file, stackptr->indent)))
{
// Table...
int col; // Current column
char *start, // Start of column/cell
*end; // End of column/cell
mmd_t *row = NULL, // Current row
*cell; // Current cell
DEBUG2_printf("TABLE stackptr->parent=%p (%d), rows=%d\n", stackptr->parent, stackptr->parent->type, rows);
if (stackptr->parent->type != MMD_TYPE_TABLE && stackptr < (stack + sizeof(stack) / sizeof(stack[0]) - 1))
{
DEBUG2_printf("ADDING NEW TABLE to %p (%s)\n", stackptr->parent, mmd_type_string(stackptr->parent->type));
stackptr[1].parent = mmd_add(stackptr->parent, MMD_TYPE_TABLE, 0, NULL, NULL);
stackptr[1].indent = stackptr->indent;
stackptr[1].fence = '\0';
stackptr ++;
block = mmd_add(stackptr->parent, MMD_TYPE_TABLE_HEADER, 0, NULL, NULL);
for (col = 0; col < (int)(sizeof(columns) / sizeof(columns[0])); col ++)
columns[col] = MMD_TYPE_TABLE_BODY_CELL_LEFT;
num_columns = 0;
rows = -1;
}
else if (rows > 0)
{
if (rows == 1)
block = mmd_add(stackptr->parent, MMD_TYPE_TABLE_BODY, 0, NULL, NULL);
}
else
block = NULL;
if (block)
row = mmd_add(block, MMD_TYPE_TABLE_ROW, 0, NULL, NULL);
if (*lineptr == '|')
lineptr ++; // Skip leading pipe
if ((end = lineptr + strlen(lineptr) - 1) > lineptr)
{
while ((*end == '\n' || *end == 'r') && end > lineptr)
end --;
if (end > lineptr && *end == '|')
*end = '\0'; // Truncate trailing pipe
}
for (col = 0; lineptr && *lineptr && col < (int)(sizeof(columns) / sizeof(columns[0])); col ++)
{
// Get the bounds of the stackptr->parent cell...
start = lineptr;
if ((lineptr = strchr(lineptr + 1, '|')) != NULL)
*lineptr++ = '\0';
if (block)
{
// Add a cell to this row...
if (block->type == MMD_TYPE_TABLE_HEADER)
cell = mmd_add(row, MMD_TYPE_TABLE_HEADER_CELL, 0, NULL, NULL);
else
cell = mmd_add(row, columns[col], 0, NULL, NULL);
mmd_parse_inline(&doc, cell, start);
}
else
{
// Process separator row for alignment...
while (isspace(*start & 255))
start ++;
for (end = start + strlen(start) - 1; end > start && isspace(*end & 255); end --)
; // Find the last non-space character
if (*start == ':' && *end == ':')
columns[col] = MMD_TYPE_TABLE_BODY_CELL_CENTER;
else if (*end == ':')
columns[col] = MMD_TYPE_TABLE_BODY_CELL_RIGHT;
DEBUG2_printf("COLUMN %d SEPARATOR=\"%s\", TYPE=%d\n", col, start, columns[col]);
}
}
// Make sure the table is balanced...
if (col > num_columns)
{
num_columns = col;