forked from Orc/discount
-
Notifications
You must be signed in to change notification settings - Fork 3
/
emmatch.c
188 lines (147 loc) · 4.08 KB
/
emmatch.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
/* markdown: a C implementation of John Gruber's Markdown markup language.
*
* Copyright (C) 2010 David L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include "config.h"
#include "cstring.h"
#include "markdown.h"
#include "amalloc.h"
/* emmatch: the emphasis mangler that's run after a block
* of html has been generated.
*
* It should create MarkdownTest_1.0 (and _1.0.3)
* compatible emphasis for non-pathological cases
* and it should fail in a standards-compliant way
* when someone attempts to feed it junk.
*
* Emmatching is done after the input has been
* processed into a STRING (f->Q) of text and
* emphasis blocks. After ___mkd_emblock() finishes,
* it truncates f->Q and leaves the rendered paragraph
* if f->out.
*/
/* empair() -- find the NEAREST matching emphasis token (or
* subtoken of a 3+ long emphasis token.
*/
static int
empair(MMIOT *f, int first, int last, int match)
{
int i;
block *begin, *p;
begin = &T(f->Q)[first];
for (i=first+1; i <= last; i++) {
p = &T(f->Q)[i];
if ( (p->b_type != bTEXT) && (p->b_count <= 0) )
continue; /* break? */
if ( p->b_type == begin->b_type ) {
if ( p->b_count == match ) /* exact match */
return i;
if ( p->b_count > 2 ) /* fuzzy match */
return i;
}
}
return 0;
} /* empair */
/* emfill() -- if an emphasis token has leftover stars or underscores,
* convert them back into character and append them to b_text.
*/
static void
emfill(block *p)
{
int j;
if ( p->b_type == bTEXT )
return;
for (j=0; j < p->b_count; j++)
EXPAND(p->b_text) = p->b_char;
p->b_count = 0;
} /* emfill */
static void
emclose(MMIOT *f, int first, int last)
{
int j;
for (j=first+1; j<last-1; j++)
emfill(&T(f->Q)[j]);
}
static struct emtags {
char open[10];
char close[10];
int size;
} emtags[] = { { "<em>" , "</em>", 5 }, { "<strong>", "</strong>", 9 } };
static void emblock(MMIOT*,int,int);
/* emmatch() -- match emphasis for a single emphasis token.
*/
static void
emmatch(MMIOT *f, int first, int last)
{
block *start = &T(f->Q)[first];
int e, e2, match;
switch (start->b_count) {
case 2: if ( e = empair(f,first,last,match=2) )
break;
case 1: e = empair(f,first,last,match=1);
break;
case 0: return;
default:
e = empair(f,first,last,1);
e2= empair(f,first,last,2);
if ( e2 >= e ) {
e = e2;
match = 2;
}
else
match = 1;
break;
}
if ( e ) {
/* if we found emphasis to match, match it, recursively call
* emblock to match emphasis inside the new html block, add
* the emphasis markers for the block, then (tail) recursively
* call ourself to match any remaining emphasis on this token.
*/
block *end = &T(f->Q)[e];
end->b_count -= match;
start->b_count -= match;
emblock(f, first, e);
PREFIX(start->b_text, emtags[match-1].open, emtags[match-1].size-1);
SUFFIX(end->b_post, emtags[match-1].close, emtags[match-1].size);
emmatch(f, first, last);
}
} /* emmatch */
/* emblock() -- walk a blocklist, attempting to match emphasis
*/
static void
emblock(MMIOT *f, int first, int last)
{
int i;
for ( i = first; i <= last; i++ )
if ( T(f->Q)[i].b_type != bTEXT )
emmatch(f, i, last);
emclose(f, first, last);
} /* emblock */
/* ___mkd_emblock() -- emblock a string of blocks, then concatenate the
* resulting text onto f->out.
*/
void
___mkd_emblock(MMIOT *f)
{
int i;
block *p;
emblock(f, 0, S(f->Q)-1);
for (i=0; i < S(f->Q); i++) {
p = &T(f->Q)[i];
emfill(p);
if ( S(p->b_post) ) { SUFFIX(f->out, T(p->b_post), S(p->b_post));
DELETE(p->b_post); }
if ( S(p->b_text) ) { SUFFIX(f->out, T(p->b_text), S(p->b_text));
DELETE(p->b_text); }
}
S(f->Q) = 0;
} /* ___mkd_emblock */