piko-fast-lib 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/piko-fast-lib.gemspec +11 -0
- data/redcarpet-3.6.1/CHANGELOG.md +487 -0
- data/redcarpet-3.6.1/CONTRIBUTING.md +33 -0
- data/redcarpet-3.6.1/COPYING +20 -0
- data/redcarpet-3.6.1/Gemfile +9 -0
- data/redcarpet-3.6.1/README.markdown +405 -0
- data/redcarpet-3.6.1/Rakefile +60 -0
- data/redcarpet-3.6.1/bin/redcarpet +7 -0
- data/redcarpet-3.6.1/ext/redcarpet/autolink.c +308 -0
- data/redcarpet-3.6.1/ext/redcarpet/autolink.h +55 -0
- data/redcarpet-3.6.1/ext/redcarpet/buffer.c +203 -0
- data/redcarpet-3.6.1/ext/redcarpet/buffer.h +88 -0
- data/redcarpet-3.6.1/ext/redcarpet/extconf.rb +6 -0
- data/redcarpet-3.6.1/ext/redcarpet/houdini.h +51 -0
- data/redcarpet-3.6.1/ext/redcarpet/houdini_href_e.c +124 -0
- data/redcarpet-3.6.1/ext/redcarpet/houdini_html_e.c +104 -0
- data/redcarpet-3.6.1/ext/redcarpet/html.c +869 -0
- data/redcarpet-3.6.1/ext/redcarpet/html.h +83 -0
- data/redcarpet-3.6.1/ext/redcarpet/html_block_names.txt +44 -0
- data/redcarpet-3.6.1/ext/redcarpet/html_blocks.h +222 -0
- data/redcarpet-3.6.1/ext/redcarpet/html_smartypants.c +472 -0
- data/redcarpet-3.6.1/ext/redcarpet/markdown.c +2946 -0
- data/redcarpet-3.6.1/ext/redcarpet/markdown.h +143 -0
- data/redcarpet-3.6.1/ext/redcarpet/rc_markdown.c +194 -0
- data/redcarpet-3.6.1/ext/redcarpet/rc_render.c +601 -0
- data/redcarpet-3.6.1/ext/redcarpet/redcarpet.h +54 -0
- data/redcarpet-3.6.1/ext/redcarpet/stack.c +84 -0
- data/redcarpet-3.6.1/ext/redcarpet/stack.h +48 -0
- data/redcarpet-3.6.1/lib/redcarpet/cli.rb +86 -0
- data/redcarpet-3.6.1/lib/redcarpet/compat.rb +71 -0
- data/redcarpet-3.6.1/lib/redcarpet/render_man.rb +65 -0
- data/redcarpet-3.6.1/lib/redcarpet/render_strip.rb +60 -0
- data/redcarpet-3.6.1/lib/redcarpet.rb +92 -0
- data/redcarpet-3.6.1/redcarpet.gemspec +59 -0
- metadata +74 -0
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2015, Vicent Marti
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
|
12
|
+
* all copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
* THE SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
#include "buffer.h"
|
|
24
|
+
#include "html.h"
|
|
25
|
+
|
|
26
|
+
#include <string.h>
|
|
27
|
+
#include <stdlib.h>
|
|
28
|
+
#include <stdio.h>
|
|
29
|
+
#include <ctype.h>
|
|
30
|
+
|
|
31
|
+
#if defined(_WIN32)
|
|
32
|
+
#define snprintf _snprintf
|
|
33
|
+
#endif
|
|
34
|
+
|
|
35
|
+
struct smartypants_data {
|
|
36
|
+
int in_squote;
|
|
37
|
+
int in_dquote;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
static size_t smartypants_cb__ltag(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size);
|
|
41
|
+
static size_t smartypants_cb__dquote(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size);
|
|
42
|
+
static size_t smartypants_cb__amp(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size);
|
|
43
|
+
static size_t smartypants_cb__period(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size);
|
|
44
|
+
static size_t smartypants_cb__number(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size);
|
|
45
|
+
static size_t smartypants_cb__dash(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size);
|
|
46
|
+
static size_t smartypants_cb__parens(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size);
|
|
47
|
+
static size_t smartypants_cb__squote(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size);
|
|
48
|
+
static size_t smartypants_cb__backtick(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size);
|
|
49
|
+
static size_t smartypants_cb__escape(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size);
|
|
50
|
+
|
|
51
|
+
static size_t (*smartypants_cb_ptrs[])
|
|
52
|
+
(struct buf *, struct smartypants_data *, uint8_t, const uint8_t *, size_t) =
|
|
53
|
+
{
|
|
54
|
+
NULL, /* 0 */
|
|
55
|
+
smartypants_cb__dash, /* 1 */
|
|
56
|
+
smartypants_cb__parens, /* 2 */
|
|
57
|
+
smartypants_cb__squote, /* 3 */
|
|
58
|
+
smartypants_cb__dquote, /* 4 */
|
|
59
|
+
smartypants_cb__amp, /* 5 */
|
|
60
|
+
smartypants_cb__period, /* 6 */
|
|
61
|
+
smartypants_cb__number, /* 7 */
|
|
62
|
+
smartypants_cb__ltag, /* 8 */
|
|
63
|
+
smartypants_cb__backtick, /* 9 */
|
|
64
|
+
smartypants_cb__escape, /* 10 */
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
static const uint8_t smartypants_cb_chars[] = {
|
|
68
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
69
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
70
|
+
0, 0, 4, 0, 0, 0, 5, 3, 2, 0, 0, 0, 0, 1, 6, 0,
|
|
71
|
+
0, 7, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0,
|
|
72
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
73
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0,
|
|
74
|
+
9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
75
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
76
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
77
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
78
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
79
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
80
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
81
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
82
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
83
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
static inline int
|
|
87
|
+
word_boundary(uint8_t c)
|
|
88
|
+
{
|
|
89
|
+
return c == 0 || isspace(c) || ispunct(c);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static inline int
|
|
93
|
+
fraction_boundary(uint8_t c)
|
|
94
|
+
{
|
|
95
|
+
return c == 0 || isspace(c) || (c != '/' && ispunct(c));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// If 'text' begins with any kind of single quote (e.g. "'" or "'" etc.),
|
|
99
|
+
// returns the length of the sequence of characters that makes up the single-
|
|
100
|
+
// quote. Otherwise, returns zero.
|
|
101
|
+
static size_t
|
|
102
|
+
squote_len(const uint8_t *text, size_t size)
|
|
103
|
+
{
|
|
104
|
+
static char* single_quote_list[] = { "'", "'", "'", "'", NULL };
|
|
105
|
+
char** p;
|
|
106
|
+
|
|
107
|
+
for (p = single_quote_list; *p; ++p) {
|
|
108
|
+
size_t len = strlen(*p);
|
|
109
|
+
if (size >= len && memcmp(text, *p, len) == 0) {
|
|
110
|
+
return len;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Converts " or ' at very beginning or end of a word to left or right quote
|
|
118
|
+
static int
|
|
119
|
+
smartypants_quotes(struct buf *ob, uint8_t previous_char, uint8_t next_char, uint8_t quote, int *is_open)
|
|
120
|
+
{
|
|
121
|
+
char ent[8];
|
|
122
|
+
|
|
123
|
+
if (*is_open && !word_boundary(next_char))
|
|
124
|
+
return 0;
|
|
125
|
+
|
|
126
|
+
if (!(*is_open) && !word_boundary(previous_char))
|
|
127
|
+
return 0;
|
|
128
|
+
|
|
129
|
+
snprintf(ent, sizeof(ent), "&%c%cquo;", (*is_open) ? 'r' : 'l', quote);
|
|
130
|
+
*is_open = !(*is_open);
|
|
131
|
+
bufputs(ob, ent);
|
|
132
|
+
return 1;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Converts ' to left or right single quote; but the initial ' might be in
|
|
136
|
+
// different forms, e.g. ' or ' or '.
|
|
137
|
+
// 'squote_text' points to the original single quote, and 'squote_size' is its length.
|
|
138
|
+
// 'text' points at the last character of the single-quote, e.g. ' or ;
|
|
139
|
+
static size_t
|
|
140
|
+
smartypants_squote(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size,
|
|
141
|
+
const uint8_t *squote_text, size_t squote_size)
|
|
142
|
+
{
|
|
143
|
+
if (size >= 2) {
|
|
144
|
+
uint8_t t1 = tolower(text[1]);
|
|
145
|
+
int next_squote_len = squote_len(text+1, size-1);
|
|
146
|
+
|
|
147
|
+
// convert '' to “ or ”
|
|
148
|
+
if (next_squote_len > 0) {
|
|
149
|
+
uint8_t next_char = (size > 1+next_squote_len) ? text[1+next_squote_len] : 0;
|
|
150
|
+
if (smartypants_quotes(ob, previous_char, next_char, 'd', &smrt->in_dquote))
|
|
151
|
+
return next_squote_len;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (smartypants_quotes(ob, previous_char, size > 0 ? text[1] : 0, 's', &smrt->in_squote))
|
|
155
|
+
return 0;
|
|
156
|
+
|
|
157
|
+
// trailing single quotes: students', tryin'
|
|
158
|
+
if (word_boundary(t1)) {
|
|
159
|
+
BUFPUTSL(ob, "’");
|
|
160
|
+
return 0;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Tom's, isn't, I'm, I'd
|
|
164
|
+
if ((t1 == 's' || t1 == 't' || t1 == 'm' || t1 == 'd') &&
|
|
165
|
+
(size == 3 || word_boundary(text[2]))) {
|
|
166
|
+
BUFPUTSL(ob, "’");
|
|
167
|
+
return 0;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// you're, you'll, you've
|
|
171
|
+
if (size >= 3) {
|
|
172
|
+
uint8_t t2 = tolower(text[2]);
|
|
173
|
+
|
|
174
|
+
if (((t1 == 'r' && t2 == 'e') ||
|
|
175
|
+
(t1 == 'l' && t2 == 'l') ||
|
|
176
|
+
(t1 == 'v' && t2 == 'e')) &&
|
|
177
|
+
(size == 4 || word_boundary(text[3]))) {
|
|
178
|
+
BUFPUTSL(ob, "’");
|
|
179
|
+
return 0;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
bufput(ob, squote_text, squote_size);
|
|
185
|
+
return 0;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Converts ' to left or right single quote.
|
|
189
|
+
static size_t
|
|
190
|
+
smartypants_cb__squote(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size)
|
|
191
|
+
{
|
|
192
|
+
return smartypants_squote(ob, smrt, previous_char, text, size, text, 1);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Converts (c), (r), (tm)
|
|
196
|
+
static size_t
|
|
197
|
+
smartypants_cb__parens(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size)
|
|
198
|
+
{
|
|
199
|
+
if (size >= 3) {
|
|
200
|
+
uint8_t t1 = tolower(text[1]);
|
|
201
|
+
uint8_t t2 = tolower(text[2]);
|
|
202
|
+
|
|
203
|
+
if (t1 == 'c' && t2 == ')') {
|
|
204
|
+
BUFPUTSL(ob, "©");
|
|
205
|
+
return 2;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (t1 == 'r' && t2 == ')') {
|
|
209
|
+
BUFPUTSL(ob, "®");
|
|
210
|
+
return 2;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (size >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')') {
|
|
214
|
+
BUFPUTSL(ob, "™");
|
|
215
|
+
return 3;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
bufputc(ob, text[0]);
|
|
220
|
+
return 0;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Converts "--" to em-dash, etc.
|
|
224
|
+
static size_t
|
|
225
|
+
smartypants_cb__dash(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size)
|
|
226
|
+
{
|
|
227
|
+
if (size >= 3 && text[1] == '-' && text[2] == '-') {
|
|
228
|
+
BUFPUTSL(ob, "—");
|
|
229
|
+
return 2;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (size >= 2 && text[1] == '-') {
|
|
233
|
+
BUFPUTSL(ob, "–");
|
|
234
|
+
return 1;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
bufputc(ob, text[0]);
|
|
238
|
+
return 0;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Converts " etc.
|
|
242
|
+
static size_t
|
|
243
|
+
smartypants_cb__amp(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size)
|
|
244
|
+
{
|
|
245
|
+
if (size >= 6 && memcmp(text, """, 6) == 0) {
|
|
246
|
+
if (smartypants_quotes(ob, previous_char, size >= 7 ? text[6] : 0, 'd', &smrt->in_dquote))
|
|
247
|
+
return 5;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
int len = squote_len(text, size);
|
|
251
|
+
if (len > 0) {
|
|
252
|
+
return (len-1) + smartypants_squote(ob, smrt, previous_char, text+(len-1), size-(len-1), text, len);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (size >= 4 && memcmp(text, "�", 4) == 0)
|
|
256
|
+
return 3;
|
|
257
|
+
|
|
258
|
+
bufputc(ob, '&');
|
|
259
|
+
return 0;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Converts "..." to ellipsis
|
|
263
|
+
static size_t
|
|
264
|
+
smartypants_cb__period(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size)
|
|
265
|
+
{
|
|
266
|
+
if (size >= 3 && text[1] == '.' && text[2] == '.') {
|
|
267
|
+
BUFPUTSL(ob, "…");
|
|
268
|
+
return 2;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (size >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.') {
|
|
272
|
+
BUFPUTSL(ob, "…");
|
|
273
|
+
return 4;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
bufputc(ob, text[0]);
|
|
277
|
+
return 0;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Converts `` to opening double quote
|
|
281
|
+
static size_t
|
|
282
|
+
smartypants_cb__backtick(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size)
|
|
283
|
+
{
|
|
284
|
+
if (size >= 2 && text[1] == '`') {
|
|
285
|
+
if (smartypants_quotes(ob, previous_char, size >= 3 ? text[2] : 0, 'd', &smrt->in_dquote))
|
|
286
|
+
return 1;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
bufputc(ob, text[0]);
|
|
290
|
+
return 0;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Converts 1/2, 1/4, 3/4
|
|
294
|
+
static size_t
|
|
295
|
+
smartypants_cb__number(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size)
|
|
296
|
+
{
|
|
297
|
+
if (fraction_boundary(previous_char) && size >= 3) {
|
|
298
|
+
if (text[0] == '1' && text[1] == '/' && text[2] == '2') {
|
|
299
|
+
if (size == 3 || fraction_boundary(text[3])) {
|
|
300
|
+
BUFPUTSL(ob, "½");
|
|
301
|
+
return 2;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (text[0] == '1' && text[1] == '/' && text[2] == '4') {
|
|
306
|
+
if (size == 3 || fraction_boundary(text[3]) ||
|
|
307
|
+
(size >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h')) {
|
|
308
|
+
BUFPUTSL(ob, "¼");
|
|
309
|
+
return 2;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (text[0] == '3' && text[1] == '/' && text[2] == '4') {
|
|
314
|
+
if (size == 3 || fraction_boundary(text[3]) ||
|
|
315
|
+
(size >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's')) {
|
|
316
|
+
BUFPUTSL(ob, "¾");
|
|
317
|
+
return 2;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
bufputc(ob, text[0]);
|
|
323
|
+
return 0;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Converts " to left or right double quote
|
|
327
|
+
static size_t
|
|
328
|
+
smartypants_cb__dquote(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size)
|
|
329
|
+
{
|
|
330
|
+
if (!smartypants_quotes(ob, previous_char, size > 0 ? text[1] : 0, 'd', &smrt->in_dquote))
|
|
331
|
+
BUFPUTSL(ob, """);
|
|
332
|
+
|
|
333
|
+
return 0;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
static size_t
|
|
337
|
+
smartypants_cb__ltag(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size)
|
|
338
|
+
{
|
|
339
|
+
static const char *skip_tags[] = {
|
|
340
|
+
"pre", "code", "var", "samp", "kbd", "math", "script", "style"
|
|
341
|
+
};
|
|
342
|
+
static const size_t skip_tags_count = 8;
|
|
343
|
+
|
|
344
|
+
size_t next_to_closing_a = 0;
|
|
345
|
+
size_t tag, i = 0;
|
|
346
|
+
|
|
347
|
+
while (i < size && text[i] != '>')
|
|
348
|
+
i++;
|
|
349
|
+
|
|
350
|
+
for (tag = 0; tag < skip_tags_count; ++tag) {
|
|
351
|
+
if (sdhtml_is_tag(text, size, skip_tags[tag]) == HTML_TAG_OPEN)
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (tag < skip_tags_count) {
|
|
356
|
+
for (;;) {
|
|
357
|
+
while (i < size && text[i] != '<')
|
|
358
|
+
i++;
|
|
359
|
+
|
|
360
|
+
if (i == size)
|
|
361
|
+
break;
|
|
362
|
+
|
|
363
|
+
if (sdhtml_is_tag(text + i, size - i, skip_tags[tag]) == HTML_TAG_CLOSE)
|
|
364
|
+
break;
|
|
365
|
+
|
|
366
|
+
i++;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
while (i < size && text[i] != '>')
|
|
370
|
+
i++;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (sdhtml_is_tag(text, size, "a") == HTML_TAG_CLOSE) {
|
|
374
|
+
while (i < size && text[i] != '>')
|
|
375
|
+
i++;
|
|
376
|
+
|
|
377
|
+
next_to_closing_a = 1;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
bufput(ob, text, i + 1);
|
|
381
|
+
|
|
382
|
+
// Pretty tricky: since people may refer to something or someone
|
|
383
|
+
// with a link but use the possessive form right after it, we need
|
|
384
|
+
// to check whether a single quote is next to a closing "</a"> tag.
|
|
385
|
+
if (next_to_closing_a && strncmp("'", text+(i+1), 5) == 0) {
|
|
386
|
+
bufput(ob, "’", 7);
|
|
387
|
+
i += 5;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return i;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
static size_t
|
|
394
|
+
smartypants_cb__escape(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size)
|
|
395
|
+
{
|
|
396
|
+
if (size < 2)
|
|
397
|
+
return 0;
|
|
398
|
+
|
|
399
|
+
switch (text[1]) {
|
|
400
|
+
case '\\':
|
|
401
|
+
case '"':
|
|
402
|
+
case '\'':
|
|
403
|
+
case '.':
|
|
404
|
+
case '-':
|
|
405
|
+
case '`':
|
|
406
|
+
bufputc(ob, text[1]);
|
|
407
|
+
return 1;
|
|
408
|
+
|
|
409
|
+
default:
|
|
410
|
+
bufputc(ob, '\\');
|
|
411
|
+
return 0;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
#if 0
|
|
416
|
+
static struct {
|
|
417
|
+
uint8_t c0;
|
|
418
|
+
const uint8_t *pattern;
|
|
419
|
+
const uint8_t *entity;
|
|
420
|
+
int skip;
|
|
421
|
+
} smartypants_subs[] = {
|
|
422
|
+
{ '\'', "'s>", "’", 0 },
|
|
423
|
+
{ '\'', "'t>", "’", 0 },
|
|
424
|
+
{ '\'', "'re>", "’", 0 },
|
|
425
|
+
{ '\'', "'ll>", "’", 0 },
|
|
426
|
+
{ '\'', "'ve>", "’", 0 },
|
|
427
|
+
{ '\'', "'m>", "’", 0 },
|
|
428
|
+
{ '\'', "'d>", "’", 0 },
|
|
429
|
+
{ '-', "--", "—", 1 },
|
|
430
|
+
{ '-', "<->", "–", 0 },
|
|
431
|
+
{ '.', "...", "…", 2 },
|
|
432
|
+
{ '.', ". . .", "…", 4 },
|
|
433
|
+
{ '(', "(c)", "©", 2 },
|
|
434
|
+
{ '(', "(r)", "®", 2 },
|
|
435
|
+
{ '(', "(tm)", "™", 3 },
|
|
436
|
+
{ '3', "<3/4>", "¾", 2 },
|
|
437
|
+
{ '3', "<3/4ths>", "¾", 2 },
|
|
438
|
+
{ '1', "<1/2>", "½", 2 },
|
|
439
|
+
{ '1', "<1/4>", "¼", 2 },
|
|
440
|
+
{ '1', "<1/4th>", "¼", 2 },
|
|
441
|
+
{ '&', "�", 0, 3 },
|
|
442
|
+
};
|
|
443
|
+
#endif
|
|
444
|
+
|
|
445
|
+
void
|
|
446
|
+
sdhtml_smartypants(struct buf *ob, const uint8_t *text, size_t size)
|
|
447
|
+
{
|
|
448
|
+
size_t i;
|
|
449
|
+
struct smartypants_data smrt = {0, 0};
|
|
450
|
+
|
|
451
|
+
if (!text)
|
|
452
|
+
return;
|
|
453
|
+
|
|
454
|
+
bufgrow(ob, size);
|
|
455
|
+
|
|
456
|
+
for (i = 0; i < size; ++i) {
|
|
457
|
+
size_t org;
|
|
458
|
+
uint8_t action = 0;
|
|
459
|
+
|
|
460
|
+
org = i;
|
|
461
|
+
while (i < size && (action = smartypants_cb_chars[text[i]]) == 0)
|
|
462
|
+
i++;
|
|
463
|
+
|
|
464
|
+
if (i > org)
|
|
465
|
+
bufput(ob, text + org, i - org);
|
|
466
|
+
|
|
467
|
+
if (i < size) {
|
|
468
|
+
i += smartypants_cb_ptrs[(int)action]
|
|
469
|
+
(ob, &smrt, i ? text[i - 1] : 0, text + i, size - i);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|