Debian Codegraph @

ext/emmatch.c

raw symbols SCIP
1 /* markdown: a C implementation of John Gruber's Markdown markup language.
2 *
3 * Copyright (C) 2010 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
21 /* emmatch: the emphasis mangler that's run after a block
22 * of html has been generated.
23 *
24 * It should create MarkdownTest_1.0 (and _1.0.3)
25 * compatible emphasis for non-pathological cases
26 * and it should fail in a standards-compliant way
27 * when someone attempts to feed it junk.
28 *
29 * Emmatching is done after the input has been
30 * processed into a STRING (f->Q) of text and
31 * emphasis blocks. After ___mkd_emblock() finishes,
32 * it truncates f->Q and leaves the rendered paragraph
33 * if f->out.
34 */
35
36
37 /* empair() -- find the NEAREST matching emphasis token (or
38 * subtoken of a 3+ long emphasis token.
39 */
40 static int
41 empair(MMIOT *f, int first, int last, int match)
42 {
43
44 int i;
45 block *begin, *p;
46
47 begin = &T(f->Q)[first];
48
49 for (i=first+1; i <= last; i++) {
50 p = &T(f->Q)[i];
51
52 if ( (p->b_type != bTEXT) && (p->b_count <= 0) )
53 continue; /* break? */
54
55 if ( p->b_type == begin->b_type ) {
56 if ( p->b_count == match ) /* exact match */
57 return i;
58
59 if ( p->b_count > 2 ) /* fuzzy match */
60 return i;
61 }
62 }
63 return 0;
64 } /* empair */
65
66
67 /* emfill() -- if an emphasis token has leftover stars or underscores,
68 * convert them back into character and append them to b_text.
69 */
70 static void
71 emfill(block *p)
72 {
73 int j;
74
75 if ( p->b_type == bTEXT )
76 return;
77
78 for (j=0; j < p->b_count; j++)
79 EXPAND(p->b_text) = p->b_char;
80 p->b_count = 0;
81 } /* emfill */
82
83
84 static void
85 emclose(MMIOT *f, int first, int last)
86 {
87 int j;
88
89 for (j=first+1; j<last-1; j++)
90 emfill(&T(f->Q)[j]);
91 }
92
93
94 static struct emtags {
95 char open[10];
96 char close[10];
97 int size;
98 } emtags[] = { { "<em>" , "</em>", 5 }, { "<strong>", "</strong>", 9 } };
99
100
101 static void emblock(MMIOT*,int,int);
102
103
104 /* emmatch() -- match emphasis for a single emphasis token.
105 */
106 static void
107 emmatch(MMIOT *f, int first, int last)
108 {
109 block *start = &T(f->Q)[first];
110 int e, e2, match;
111
112 switch (start->b_count) {
113 case 2: if ( e = empair(f,first,last,match=2) )
114 break;
115 case 1: e = empair(f,first,last,match=1);
116 break;
117 case 0: return;
118 default:
119 e = empair(f,first,last,1);
120 e2= empair(f,first,last,2);
121
122 if ( e2 >= e ) {
123 e = e2;
124 match = 2;
125 }
126 else
127 match = 1;
128 break;
129 }
130
131 if ( e ) {
132 /* if we found emphasis to match, match it, recursively call
133 * emblock to match emphasis inside the new html block, add
134 * the emphasis markers for the block, then (tail) recursively
135 * call ourself to match any remaining emphasis on this token.
136 */
137 block *end = &T(f->Q)[e];
138
139 end->b_count -= match;
140 start->b_count -= match;
141
142 emblock(f, first, e);
143
144 PREFIX(start->b_text, emtags[match-1].open, emtags[match-1].size-1);
145 SUFFIX(end->b_post, emtags[match-1].close, emtags[match-1].size);
146
147 emmatch(f, first, last);
148 }
149 } /* emmatch */
150
151
152 /* emblock() -- walk a blocklist, attempting to match emphasis
153 */
154 static void
155 emblock(MMIOT *f, int first, int last)
156 {
157 int i;
158
159 for ( i = first; i <= last; i++ )
160 if ( T(f->Q)[i].b_type != bTEXT )
161 emmatch(f, i, last);
162 emclose(f, first, last);
163 } /* emblock */
164
165
166 /* ___mkd_emblock() -- emblock a string of blocks, then concatenate the
167 * resulting text onto f->out.
168 */
169 void
170 ___mkd_emblock(MMIOT *f)
171 {
172 int i;
173 block *p;
174
175 emblock(f, 0, S(f->Q)-1);
176
177 for (i=0; i < S(f->Q); i++) {
178 p = &T(f->Q)[i];
179 emfill(p);
180
181 if ( S(p->b_post) ) { SUFFIX(f->out, T(p->b_post), S(p->b_post));
182 DELETE(p->b_post); }
183 if ( S(p->b_text) ) { SUFFIX(f->out, T(p->b_text), S(p->b_text));
184 DELETE(p->b_text); }
185 }
186
187 S(f->Q) = 0;
188 } /* ___mkd_emblock */