bluecloth 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. data/ChangeLog +629 -0
  2. data/LICENSE +27 -0
  3. data/LICENSE.discount +47 -0
  4. data/README +71 -0
  5. data/Rakefile +319 -0
  6. data/Rakefile.local +63 -0
  7. data/bin/bluecloth +84 -0
  8. data/ext/VERSION +1 -0
  9. data/ext/amalloc.h +29 -0
  10. data/ext/bluecloth.c +373 -0
  11. data/ext/config.h +47 -0
  12. data/ext/cstring.h +73 -0
  13. data/ext/docheader.c +43 -0
  14. data/ext/extconf.rb +45 -0
  15. data/ext/generate.c +1387 -0
  16. data/ext/markdown.c +939 -0
  17. data/ext/markdown.h +135 -0
  18. data/ext/mkdio.c +241 -0
  19. data/ext/mkdio.h +66 -0
  20. data/ext/resource.c +169 -0
  21. data/ext/version.c +28 -0
  22. data/lib/bluecloth.rb +148 -0
  23. data/rake/191_compat.rb +26 -0
  24. data/rake/dependencies.rb +76 -0
  25. data/rake/helpers.rb +412 -0
  26. data/rake/manual.rb +782 -0
  27. data/rake/packaging.rb +116 -0
  28. data/rake/publishing.rb +321 -0
  29. data/rake/rdoc.rb +40 -0
  30. data/rake/style.rb +62 -0
  31. data/rake/svn.rb +639 -0
  32. data/rake/testing.rb +204 -0
  33. data/rake/verifytask.rb +64 -0
  34. data/rake/win32.rb +186 -0
  35. data/spec/bluecloth/101_changes_spec.rb +141 -0
  36. data/spec/bluecloth/autolinks_spec.rb +49 -0
  37. data/spec/bluecloth/blockquotes_spec.rb +143 -0
  38. data/spec/bluecloth/code_spans_spec.rb +164 -0
  39. data/spec/bluecloth/emphasis_spec.rb +164 -0
  40. data/spec/bluecloth/entities_spec.rb +65 -0
  41. data/spec/bluecloth/hrules_spec.rb +90 -0
  42. data/spec/bluecloth/images_spec.rb +92 -0
  43. data/spec/bluecloth/inline_html_spec.rb +238 -0
  44. data/spec/bluecloth/links_spec.rb +171 -0
  45. data/spec/bluecloth/lists_spec.rb +294 -0
  46. data/spec/bluecloth/paragraphs_spec.rb +75 -0
  47. data/spec/bluecloth/titles_spec.rb +305 -0
  48. data/spec/bluecloth_spec.rb +209 -0
  49. data/spec/bugfix_spec.rb +123 -0
  50. data/spec/contributions_spec.rb +85 -0
  51. data/spec/data/antsugar.txt +34 -0
  52. data/spec/data/markdowntest/Amps and angle encoding.html +17 -0
  53. data/spec/data/markdowntest/Amps and angle encoding.text +21 -0
  54. data/spec/data/markdowntest/Auto links.html +18 -0
  55. data/spec/data/markdowntest/Auto links.text +13 -0
  56. data/spec/data/markdowntest/Backslash escapes.html +118 -0
  57. data/spec/data/markdowntest/Backslash escapes.text +120 -0
  58. data/spec/data/markdowntest/Blockquotes with code blocks.html +15 -0
  59. data/spec/data/markdowntest/Blockquotes with code blocks.text +11 -0
  60. data/spec/data/markdowntest/Code Blocks.html +18 -0
  61. data/spec/data/markdowntest/Code Blocks.text +14 -0
  62. data/spec/data/markdowntest/Code Spans.html +5 -0
  63. data/spec/data/markdowntest/Code Spans.text +5 -0
  64. data/spec/data/markdowntest/Hard-wrapped paragraphs with list-like lines.html +8 -0
  65. data/spec/data/markdowntest/Hard-wrapped paragraphs with list-like lines.text +8 -0
  66. data/spec/data/markdowntest/Horizontal rules.html +71 -0
  67. data/spec/data/markdowntest/Horizontal rules.text +67 -0
  68. data/spec/data/markdowntest/Inline HTML (Advanced).html +15 -0
  69. data/spec/data/markdowntest/Inline HTML (Advanced).text +15 -0
  70. data/spec/data/markdowntest/Inline HTML (Simple).html +72 -0
  71. data/spec/data/markdowntest/Inline HTML (Simple).text +69 -0
  72. data/spec/data/markdowntest/Inline HTML comments.html +13 -0
  73. data/spec/data/markdowntest/Inline HTML comments.text +13 -0
  74. data/spec/data/markdowntest/Links, inline style.html +11 -0
  75. data/spec/data/markdowntest/Links, inline style.text +12 -0
  76. data/spec/data/markdowntest/Links, reference style.html +52 -0
  77. data/spec/data/markdowntest/Links, reference style.text +71 -0
  78. data/spec/data/markdowntest/Links, shortcut references.html +9 -0
  79. data/spec/data/markdowntest/Links, shortcut references.text +20 -0
  80. data/spec/data/markdowntest/Literal quotes in titles.html +3 -0
  81. data/spec/data/markdowntest/Literal quotes in titles.text +7 -0
  82. data/spec/data/markdowntest/Markdown Documentation - Basics.html +314 -0
  83. data/spec/data/markdowntest/Markdown Documentation - Basics.text +306 -0
  84. data/spec/data/markdowntest/Markdown Documentation - Syntax.html +942 -0
  85. data/spec/data/markdowntest/Markdown Documentation - Syntax.text +888 -0
  86. data/spec/data/markdowntest/Nested blockquotes.html +9 -0
  87. data/spec/data/markdowntest/Nested blockquotes.text +5 -0
  88. data/spec/data/markdowntest/Ordered and unordered lists.html +148 -0
  89. data/spec/data/markdowntest/Ordered and unordered lists.text +131 -0
  90. data/spec/data/markdowntest/Strong and em together.html +7 -0
  91. data/spec/data/markdowntest/Strong and em together.text +7 -0
  92. data/spec/data/markdowntest/Tabs.html +25 -0
  93. data/spec/data/markdowntest/Tabs.text +21 -0
  94. data/spec/data/markdowntest/Tidyness.html +8 -0
  95. data/spec/data/markdowntest/Tidyness.text +5 -0
  96. data/spec/data/ml-announce.txt +17 -0
  97. data/spec/data/re-overflow.txt +67 -0
  98. data/spec/data/re-overflow2.txt +281 -0
  99. data/spec/lib/constants.rb +5 -0
  100. data/spec/lib/helpers.rb +137 -0
  101. data/spec/lib/matchers.rb +235 -0
  102. data/spec/markdowntest_spec.rb +76 -0
  103. metadata +305 -0
@@ -0,0 +1,135 @@
1
+ #ifndef _MARKDOWN_D
2
+ #define _MARKDOWN_D
3
+
4
+ #include "cstring.h"
5
+
6
+ /* reference-style links (and images) are stored in an array
7
+ * of footnotes.
8
+ */
9
+ typedef struct footnote {
10
+ Cstring tag; /* the tag for the reference link */
11
+ Cstring link; /* what this footnote points to */
12
+ Cstring title; /* what it's called (TITLE= attribute) */
13
+ int height, width; /* dimensions (for image link) */
14
+ } Footnote;
15
+
16
+ /* each input line is read into a Line, which contains the line,
17
+ * the offset of the first non-space character [this assumes
18
+ * that all tabs will be expanded to spaces!], and a pointer to
19
+ * the next line.
20
+ */
21
+ typedef struct line {
22
+ Cstring text;
23
+ struct line *next;
24
+ int dle;
25
+ } Line;
26
+
27
+
28
+ /* a paragraph is a collection of Lines, with links to the next paragraph
29
+ * and (if it's a QUOTE, UL, or OL) to the reparsed contents of this
30
+ * paragraph.
31
+ */
32
+ typedef struct paragraph {
33
+ struct paragraph *next; /* next paragraph */
34
+ struct paragraph *down; /* recompiled contents of this paragraph */
35
+ struct line *text; /* all the text in this paragraph */
36
+ char *ident; /* %id% tag for QUOTE */
37
+ enum { WHITESPACE=0, CODE, QUOTE, MARKUP,
38
+ HTML, STYLE, DL, UL, OL, AL, LISTITEM,
39
+ HDR, HR } typ;
40
+ enum { IMPLICIT=0, PARA, CENTER} align;
41
+ int hnumber; /* <Hn> for typ == HDR */
42
+ } Paragraph;
43
+
44
+ enum { ETX, SETEXT }; /* header types */
45
+
46
+
47
+ typedef struct block {
48
+ enum { bTEXT, bSTAR, bUNDER } b_type;
49
+ int b_count;
50
+ char b_char;
51
+ Cstring b_text;
52
+ Cstring b_post;
53
+ } block;
54
+
55
+ typedef STRING(block) Qblock;
56
+
57
+
58
+ /* a magic markdown io thing holds all the data structures needed to
59
+ * do the backend processing of a markdown document
60
+ */
61
+ typedef struct mmiot {
62
+ Cstring out;
63
+ Cstring in;
64
+ Qblock Q;
65
+ int isp;
66
+ STRING(Footnote) *footnotes;
67
+ int flags;
68
+ #define DENY_A 0x0001
69
+ #define DENY_IMG 0x0002
70
+ #define DENY_SMARTY 0x0004
71
+ #define DENY_HTML 0x0008
72
+ #define STRICT 0x0010
73
+ #define INSIDE_TAG 0x0020
74
+ #define NO_PSEUDO_PROTO 0x0040
75
+ #define CDATA_OUTPUT 0x0080
76
+ #define TOC 0x1000
77
+ #define MKD_1_COMPAT 0x2000
78
+ #define USER_FLAGS 0xF0FF
79
+ #define EMBEDDED DENY_A|DENY_IMG|NO_PSEUDO_PROTO|CDATA_OUTPUT
80
+ char *base;
81
+ } MMIOT;
82
+
83
+
84
+ /*
85
+ * the mkdio text input functions return a document structure,
86
+ * which contains a header (retrieved from the document if
87
+ * markdown was configured * with the * --enable-pandoc-header
88
+ * and the document begins with a pandoc-style header) and the
89
+ * root of the linked list of Lines.
90
+ */
91
+ typedef struct document {
92
+ Line *headers; /* title -> author(s) -> date */
93
+ ANCHOR(Line) content; /* uncompiled text, not valid after compile() */
94
+ Paragraph *code; /* intermediate code generated by compile() */
95
+ int compiled; /* set after mkd_compile() */
96
+ int html; /* set after (internal) htmlify() */
97
+ int tabstop; /* for properly expanding tabs (ick) */
98
+ MMIOT *ctx; /* backend buffers, flags, and structures */
99
+ char *base; /* url basename for url fragments */
100
+ } Document;
101
+
102
+
103
+ extern int mkd_firstnonblank(Line *);
104
+ extern int mkd_compile(Document *, int);
105
+ extern int mkd_document(Document *, char **);
106
+ extern int mkd_generatehtml(Document *, FILE *);
107
+ extern int mkd_style(Document *, FILE *);
108
+ extern void mkd_cleanup(Document *);
109
+ extern int mkd_text(char *, int, FILE*, int);
110
+ extern void mkd_basename(Document*, char *);
111
+ extern void mkd_string_to_anchor(char*,int, void(*)(int,void*), void*);
112
+
113
+ extern Document *mkd_in(FILE *, int);
114
+ extern Document *mkd_string(char*,int, int);
115
+
116
+ #define NO_HEADER 0x0100
117
+ #define STD_TABSTOP 0x0200
118
+ #define INPUT_MASK (NO_HEADER|STD_TABSTOP)
119
+
120
+
121
+ /* internal resource handling functions.
122
+ */
123
+ extern void ___mkd_freeLine(Line *);
124
+ extern void ___mkd_freeLines(Line *);
125
+ extern void ___mkd_freeParagraph(Paragraph *);
126
+ extern void ___mkd_freefootnotes(MMIOT *);
127
+ extern void ___mkd_initmmiot(MMIOT *, void *);
128
+ extern void ___mkd_freemmiot(MMIOT *, void *);
129
+ extern void ___mkd_freeLineRange(Line *, Line *);
130
+ extern void ___mkd_xml(char *, int, FILE *);
131
+ extern void ___mkd_reparse(char *, int, int, MMIOT*);
132
+ extern void ___mkd_emblock(MMIOT*);
133
+ extern void ___mkd_tidy(Line *);
134
+
135
+ #endif/*_MARKDOWN_D*/
@@ -0,0 +1,241 @@
1
+ /*
2
+ * mkdio -- markdown front end input functions
3
+ *
4
+ * Copyright (C) 2007 David L Parsons.
5
+ * The redistribution terms are provided in the COPYRIGHT file that must
6
+ * be distributed with this source code.
7
+ */
8
+ #include "config.h"
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <ctype.h>
12
+
13
+ #include "cstring.h"
14
+ #include "markdown.h"
15
+ #include "amalloc.h"
16
+
17
+ typedef ANCHOR(Line) LineAnchor;
18
+
19
+ /* create a new blank Document
20
+ */
21
+ static Document*
22
+ new_Document()
23
+ {
24
+ Document *ret = calloc(sizeof(Document), 1);
25
+
26
+ if ( ret ) {
27
+ if (( ret->ctx = calloc(sizeof(MMIOT), 1) ))
28
+ return ret;
29
+ free(ret);
30
+ }
31
+ return 0;
32
+ }
33
+
34
+
35
+ /* add a line to the markdown input chain
36
+ */
37
+ static void
38
+ queue(Document* a, Cstring *line)
39
+ {
40
+ Line *p = calloc(sizeof *p, 1);
41
+ unsigned char c;
42
+ int xp = 0;
43
+ int size = S(*line);
44
+ unsigned char *str = (unsigned char*)T(*line);
45
+
46
+ CREATE(p->text);
47
+ ATTACH(a->content, p);
48
+
49
+ while ( size-- ) {
50
+ if ( (c = *str++) == '\t' ) {
51
+ /* expand tabs into ->tabstop spaces. We use ->tabstop
52
+ * because the ENTIRE FREAKING COMPUTER WORLD uses editors
53
+ * that don't do ^T/^D, but instead use tabs for indentation,
54
+ * and, of course, set their tabs down to 4 spaces
55
+ */
56
+ do {
57
+ EXPAND(p->text) = ' ';
58
+ } while ( ++xp % a->tabstop );
59
+ }
60
+ else if ( c >= ' ' ) {
61
+ EXPAND(p->text) = c;
62
+ ++xp;
63
+ }
64
+ }
65
+ EXPAND(p->text) = 0;
66
+ S(p->text)--;
67
+ p->dle = mkd_firstnonblank(p);
68
+ }
69
+
70
+
71
+ #ifdef PANDOC_HEADER
72
+ /* trim leading blanks from a header line
73
+ */
74
+ static void
75
+ snip(Line *p)
76
+ {
77
+ CLIP(p->text, 0, 1);
78
+ p->dle = mkd_firstnonblank(p);
79
+ }
80
+ #endif
81
+
82
+
83
+ /* build a Document from any old input.
84
+ */
85
+ typedef int (*getc_func)(void*);
86
+
87
+ Document *
88
+ populate(getc_func getc, void* ctx, int flags)
89
+ {
90
+ Cstring line;
91
+ Document *a = new_Document();
92
+ int c;
93
+ #ifdef PANDOC_HEADER
94
+ int pandoc = 0;
95
+ #endif
96
+
97
+ if ( !a ) return 0;
98
+
99
+ a->tabstop = (flags & STD_TABSTOP) ? 4 : TABSTOP;
100
+
101
+ CREATE(line);
102
+
103
+ while ( (c = (*getc)(ctx)) != EOF ) {
104
+ if ( c == '\n' ) {
105
+ #ifdef PANDOC_HEADER
106
+ if ( pandoc != EOF && pandoc < 3 ) {
107
+ if ( S(line) && (T(line)[0] == '%') )
108
+ pandoc++;
109
+ else
110
+ pandoc = EOF;
111
+ }
112
+ #endif
113
+ queue(a, &line);
114
+ S(line) = 0;
115
+ }
116
+ else
117
+ EXPAND(line) = c;
118
+ }
119
+
120
+ if ( S(line) )
121
+ queue(a, &line);
122
+
123
+ DELETE(line);
124
+
125
+ #ifdef PANDOC_HEADER
126
+ if ( (pandoc == 3) && !(flags & NO_HEADER) ) {
127
+ /* the first three lines started with %, so we have a header.
128
+ * clip the first three lines out of content and hang them
129
+ * off header.
130
+ */
131
+ a->headers = T(a->content);
132
+ T(a->content) = a->headers->next->next->next;
133
+ a->headers->next->next->next = 0;
134
+ snip(a->headers);
135
+ snip(a->headers->next);
136
+ snip(a->headers->next->next);
137
+ }
138
+ #endif
139
+
140
+ return a;
141
+ }
142
+
143
+
144
+ /* convert a file into a linked list
145
+ */
146
+ Document *
147
+ mkd_in(FILE *f, int flags)
148
+ {
149
+ return populate((getc_func)fgetc, f, flags & INPUT_MASK);
150
+ }
151
+
152
+
153
+ /* return a single character out of a buffer
154
+ */
155
+ struct string_ctx {
156
+ char *data; /* the unread data */
157
+ int size; /* and how much is there? */
158
+ } ;
159
+
160
+
161
+ static int
162
+ strget(struct string_ctx *in)
163
+ {
164
+ if ( !in->size ) return EOF;
165
+
166
+ --(in->size);
167
+
168
+ return *(in->data)++;
169
+ }
170
+
171
+
172
+ /* convert a block of text into a linked list
173
+ */
174
+ Document *
175
+ mkd_string(char *buf, int len, int flags)
176
+ {
177
+ struct string_ctx about;
178
+
179
+ about.data = buf;
180
+ about.size = len;
181
+
182
+ return populate((getc_func)strget, &about, flags & INPUT_MASK);
183
+ }
184
+
185
+
186
+ /* write the html to a file (xmlified if necessary)
187
+ */
188
+ int
189
+ mkd_generatehtml(Document *p, FILE *output)
190
+ {
191
+ char *doc;
192
+ int szdoc;
193
+
194
+ if ( (szdoc = mkd_document(p, &doc)) != EOF ) {
195
+ if ( p->ctx->flags & CDATA_OUTPUT )
196
+ ___mkd_xml(doc, szdoc, output);
197
+ else
198
+ fwrite(doc, szdoc, 1, output);
199
+ putc('\n', output);
200
+ return 0;
201
+ }
202
+ return -1;
203
+ }
204
+
205
+
206
+ /* convert some markdown text to html
207
+ */
208
+ int
209
+ markdown(Document *document, FILE *out, int flags)
210
+ {
211
+ if ( mkd_compile(document, flags) ) {
212
+ mkd_generatehtml(document, out);
213
+ mkd_cleanup(document);
214
+ return 0;
215
+ }
216
+ return -1;
217
+ }
218
+
219
+
220
+ void
221
+ mkd_basename(Document *document, char *base)
222
+ {
223
+ if ( document )
224
+ document->base = base;
225
+ }
226
+
227
+
228
+ /* write out a Cstring, mangled into a form suitable for `<a href=` or `<a id=`
229
+ */
230
+ void
231
+ mkd_string_to_anchor(char *s, int len, void(*outchar)(int,void*), void *out)
232
+ {
233
+ for ( ; len-- > 0; ++s ) {
234
+ if ( *s == ' ' || *s == '&' || *s == '<' || *s == '"' )
235
+ (*outchar)('+', out);
236
+ else if ( isalnum(*s) || ispunct(*s) )
237
+ (*outchar)(*s, out);
238
+ else
239
+ (*outchar)('~',out);
240
+ }
241
+ }
@@ -0,0 +1,66 @@
1
+ #ifndef _MKDIO_D
2
+ #define _MKDIO_D
3
+
4
+ #include <stdio.h>
5
+
6
+ typedef void MMIOT;
7
+
8
+ /* line builder for markdown()
9
+ */
10
+ MMIOT *mkd_in(FILE*,int); /* assemble input from a file */
11
+ MMIOT *mkd_string(char*,int,int); /* assemble input from a buffer */
12
+
13
+ void mkd_basename(MMIOT*,char*);
14
+
15
+ /* compilation, debugging, cleanup
16
+ */
17
+ int mkd_compile(MMIOT*, int);
18
+ int mkd_generatehtml(MMIOT*,FILE*);
19
+ int mkd_generatetoc(MMIOT*,FILE*);
20
+ int mkd_xhtmlpage(MMIOT*,int,FILE*);
21
+ int mkd_cleanup(MMIOT*);
22
+
23
+ /* markup functions
24
+ */
25
+ int mkd_text(char *, int, FILE*, int);
26
+ int mkd_style(MMIOT*, FILE*);
27
+ int mkd_dump(MMIOT*, FILE*, int, char*);
28
+ int markdown(MMIOT*, FILE*, int);
29
+ void mkd_basename(MMIOT*,char*);
30
+ void mkd_string_to_anchor(char *, int, int (*)(int,void*), void*);
31
+
32
+ /* header block access
33
+ */
34
+ char* mkd_doc_title(MMIOT*);
35
+ char* mkd_doc_author(MMIOT*);
36
+ char* mkd_doc_date(MMIOT*);
37
+
38
+ /* compiled data access
39
+ */
40
+ int mkd_document(MMIOT*, char**);
41
+
42
+ /* version#.
43
+ */
44
+ extern char markdown_version[];
45
+
46
+ /* special flags for markdown() and mkd_text()
47
+ */
48
+ #define MKD_NOLINKS 0x0001 /* don't do link processing, block <a> tags */
49
+ #define MKD_NOIMAGE 0x0002 /* don't do image processing, block <img> */
50
+ #define MKD_NOPANTS 0x0004 /* don't run smartypants() */
51
+ #define MKD_NOHTML 0x0008 /* don't allow raw html through AT ALL */
52
+ #define MKD_STRICT 0x0010 /* disable SUPERSCRIPT, RELAXED_EMPHASIS */
53
+ #define MKD_TAGTEXT 0x0020 /* don't expand `_` and `*` */
54
+ #define MKD_NO_EXT 0x0040 /* don't allow pseudo-protocols */
55
+ #define MKD_CDATA 0x0080 /* generate code for xml ![CDATA[...]] */
56
+ #define MKD_TOC 0x1000 /* do table-of-contents processing */
57
+ #define MKD_1_COMPAT 0x2000 /* compatability with MarkdownTest_1.0 */
58
+ #define MKD_EMBED MKD_NOLINKS|MKD_NOIMAGE|MKD_TAGTEXT
59
+
60
+ /* special flags for mkd_in() and mkd_string()
61
+ */
62
+ #define MKD_NOHEADER 0x0100 /* don't process header blocks */
63
+ #define MKD_TABSTOP 0x0200 /* expand tabs to 4 spaces */
64
+
65
+
66
+ #endif/*_MKDIO_D*/
@@ -0,0 +1,169 @@
1
+ /* markdown: a C implementation of John Gruber's Markdown markup language.
2
+ *
3
+ * Copyright (C) 2007 David L Parsons.
4
+ * The redistribution terms are provided in the COPYRIGHT file that must
5
+ * be distributed with this source code.
6
+ */
7
+ #include <stdio.h>
8
+ #include <string.h>
9
+ #include <stdarg.h>
10
+ #include <stdlib.h>
11
+ #include <time.h>
12
+ #include <ctype.h>
13
+
14
+ #include "config.h"
15
+
16
+ #include "cstring.h"
17
+ #include "markdown.h"
18
+ #include "amalloc.h"
19
+
20
+ /* free a (single) line
21
+ */
22
+ void
23
+ ___mkd_freeLine(Line *ptr)
24
+ {
25
+ DELETE(ptr->text);
26
+ free(ptr);
27
+ }
28
+
29
+
30
+ /* free a list of lines
31
+ */
32
+ void
33
+ ___mkd_freeLines(Line *p)
34
+ {
35
+ if (p->next)
36
+ ___mkd_freeLines(p->next);
37
+ ___mkd_freeLine(p);
38
+ }
39
+
40
+
41
+ /* bye bye paragraph.
42
+ */
43
+ void
44
+ ___mkd_freeParagraph(Paragraph *p)
45
+ {
46
+ if (p->next)
47
+ ___mkd_freeParagraph(p->next);
48
+ if (p->down)
49
+ ___mkd_freeParagraph(p->down);
50
+ if (p->text)
51
+ ___mkd_freeLines(p->text);
52
+ if (p->ident)
53
+ free(p->ident);
54
+ free(p);
55
+ }
56
+
57
+
58
+ /* bye bye footnotes.
59
+ */
60
+ void
61
+ ___mkd_freefootnotes(MMIOT *f)
62
+ {
63
+ int i;
64
+
65
+ if ( f->footnotes ) {
66
+ for (i=0; i < S(*f->footnotes); i++) {
67
+ DELETE(T(*f->footnotes)[i].tag);
68
+ DELETE(T(*f->footnotes)[i].link);
69
+ DELETE(T(*f->footnotes)[i].title);
70
+ }
71
+ DELETE(*f->footnotes);
72
+ free(f->footnotes);
73
+ }
74
+ }
75
+
76
+
77
+ /* initialize a new MMIOT
78
+ */
79
+ void
80
+ ___mkd_initmmiot(MMIOT *f, void *footnotes)
81
+ {
82
+ if ( f ) {
83
+ memset(f, 0, sizeof *f);
84
+ CREATE(f->in);
85
+ CREATE(f->out);
86
+ CREATE(f->Q);
87
+ if ( footnotes )
88
+ f->footnotes = footnotes;
89
+ else {
90
+ f->footnotes = malloc(sizeof f->footnotes[0]);
91
+ CREATE(*f->footnotes);
92
+ }
93
+ }
94
+ }
95
+
96
+
97
+ /* free the contents of a MMIOT, but leave the object alone.
98
+ */
99
+ void
100
+ ___mkd_freemmiot(MMIOT *f, void *footnotes)
101
+ {
102
+ if ( f ) {
103
+ DELETE(f->in);
104
+ DELETE(f->out);
105
+ DELETE(f->Q);
106
+ if ( f->footnotes != footnotes )
107
+ ___mkd_freefootnotes(f);
108
+ memset(f, 0, sizeof *f);
109
+ }
110
+ }
111
+
112
+
113
+ /* free lines up to an barrier.
114
+ */
115
+ void
116
+ ___mkd_freeLineRange(Line *anchor, Line *stop)
117
+ {
118
+ Line *r = anchor->next;
119
+
120
+ if ( r != stop ) {
121
+ while ( r && (r->next != stop) )
122
+ r = r->next;
123
+ if ( r ) r->next = 0;
124
+ ___mkd_freeLines(anchor->next);
125
+ }
126
+ anchor->next = 0;
127
+ }
128
+
129
+
130
+ /* clean up everything allocated in __mkd_compile()
131
+ */
132
+ void
133
+ mkd_cleanup(Document *doc)
134
+ {
135
+ if ( doc ) {
136
+ if ( doc->ctx ) {
137
+ ___mkd_freemmiot(doc->ctx, 0);
138
+ free(doc->ctx);
139
+ }
140
+
141
+ if ( doc->code) ___mkd_freeParagraph(doc->code);
142
+ if ( doc->headers ) ___mkd_freeLines(doc->headers);
143
+ if ( T(doc->content) ) ___mkd_freeLines(T(doc->content));
144
+ memset(doc, 0, sizeof doc[0]);
145
+ free(doc);
146
+ }
147
+ }
148
+
149
+
150
+ /* write output in XML format
151
+ */
152
+ void
153
+ ___mkd_xml(char *p, int size, FILE *out)
154
+ {
155
+ char c;
156
+
157
+ while ( size-- > 0 ) {
158
+ if ( !isascii(c = *p++) )
159
+ continue;
160
+ switch (c) {
161
+ case '<': fputs("&lt;", out); break;
162
+ case '>': fputs("&gt;", out); break;
163
+ case '&': fputs("&amp;", out); break;
164
+ case '"': fputs("&quot;", out); break;
165
+ case '\'':fputs("&apos;", out); break;
166
+ default: putc(c,out); break;
167
+ }
168
+ }
169
+ }