nano-pure-box 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.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/nano-pure-box.gemspec +12 -0
  3. data/redcarpet-3.6.1/CHANGELOG.md +487 -0
  4. data/redcarpet-3.6.1/CONTRIBUTING.md +33 -0
  5. data/redcarpet-3.6.1/COPYING +20 -0
  6. data/redcarpet-3.6.1/Gemfile +9 -0
  7. data/redcarpet-3.6.1/README.markdown +405 -0
  8. data/redcarpet-3.6.1/Rakefile +60 -0
  9. data/redcarpet-3.6.1/bin/redcarpet +7 -0
  10. data/redcarpet-3.6.1/ext/redcarpet/autolink.c +308 -0
  11. data/redcarpet-3.6.1/ext/redcarpet/autolink.h +55 -0
  12. data/redcarpet-3.6.1/ext/redcarpet/buffer.c +203 -0
  13. data/redcarpet-3.6.1/ext/redcarpet/buffer.h +88 -0
  14. data/redcarpet-3.6.1/ext/redcarpet/extconf.rb +6 -0
  15. data/redcarpet-3.6.1/ext/redcarpet/houdini.h +51 -0
  16. data/redcarpet-3.6.1/ext/redcarpet/houdini_href_e.c +124 -0
  17. data/redcarpet-3.6.1/ext/redcarpet/houdini_html_e.c +104 -0
  18. data/redcarpet-3.6.1/ext/redcarpet/html.c +869 -0
  19. data/redcarpet-3.6.1/ext/redcarpet/html.h +83 -0
  20. data/redcarpet-3.6.1/ext/redcarpet/html_block_names.txt +44 -0
  21. data/redcarpet-3.6.1/ext/redcarpet/html_blocks.h +222 -0
  22. data/redcarpet-3.6.1/ext/redcarpet/html_smartypants.c +472 -0
  23. data/redcarpet-3.6.1/ext/redcarpet/markdown.c +2946 -0
  24. data/redcarpet-3.6.1/ext/redcarpet/markdown.h +143 -0
  25. data/redcarpet-3.6.1/ext/redcarpet/rc_markdown.c +194 -0
  26. data/redcarpet-3.6.1/ext/redcarpet/rc_render.c +601 -0
  27. data/redcarpet-3.6.1/ext/redcarpet/redcarpet.h +54 -0
  28. data/redcarpet-3.6.1/ext/redcarpet/stack.c +84 -0
  29. data/redcarpet-3.6.1/ext/redcarpet/stack.h +48 -0
  30. data/redcarpet-3.6.1/lib/redcarpet/cli.rb +86 -0
  31. data/redcarpet-3.6.1/lib/redcarpet/compat.rb +71 -0
  32. data/redcarpet-3.6.1/lib/redcarpet/render_man.rb +65 -0
  33. data/redcarpet-3.6.1/lib/redcarpet/render_strip.rb +60 -0
  34. data/redcarpet-3.6.1/lib/redcarpet.rb +92 -0
  35. data/redcarpet-3.6.1/redcarpet.gemspec +59 -0
  36. metadata +75 -0
@@ -0,0 +1,869 @@
1
+ /*
2
+ * Copyright (c) 2009, Natacha Porté
3
+ * Copyright (c) 2015, Vicent Marti
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+ #include "markdown.h"
25
+ #include "html.h"
26
+ #include <string.h>
27
+ #include <stdlib.h>
28
+ #include <stdio.h>
29
+ #include <ctype.h>
30
+
31
+ #include "houdini.h"
32
+
33
+ #define USE_XHTML(opt) (opt->flags & HTML_USE_XHTML)
34
+
35
+ int
36
+ sdhtml_is_tag(const uint8_t *tag_data, size_t tag_size, const char *tagname)
37
+ {
38
+ size_t i;
39
+ int closed = 0;
40
+
41
+ if (tag_size < 3 || tag_data[0] != '<')
42
+ return HTML_TAG_NONE;
43
+
44
+ i = 1;
45
+
46
+ if (tag_data[i] == '/') {
47
+ closed = 1;
48
+ i++;
49
+ }
50
+
51
+ for (; i < tag_size; ++i, ++tagname) {
52
+ if (*tagname == 0)
53
+ break;
54
+
55
+ if (tag_data[i] != *tagname)
56
+ return HTML_TAG_NONE;
57
+ }
58
+
59
+ if (i == tag_size)
60
+ return HTML_TAG_NONE;
61
+
62
+ if (isspace(tag_data[i]) || tag_data[i] == '>')
63
+ return closed ? HTML_TAG_CLOSE : HTML_TAG_OPEN;
64
+
65
+ return HTML_TAG_NONE;
66
+ }
67
+
68
+ static inline void escape_html(struct buf *ob, const uint8_t *source, size_t length)
69
+ {
70
+ houdini_escape_html0(ob, source, length, 0);
71
+ }
72
+
73
+ static inline void escape_href(struct buf *ob, const uint8_t *source, size_t length)
74
+ {
75
+ houdini_escape_href(ob, source, length);
76
+ }
77
+
78
+ /********************
79
+ * GENERIC RENDERER *
80
+ ********************/
81
+ static int
82
+ rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque)
83
+ {
84
+ struct html_renderopt *options = opaque;
85
+
86
+ if (!link || !link->size)
87
+ return 0;
88
+
89
+ if ((options->flags & HTML_SAFELINK) != 0 &&
90
+ !sd_autolink_issafe(link->data, link->size) &&
91
+ type != MKDA_EMAIL)
92
+ return 0;
93
+
94
+ BUFPUTSL(ob, "<a href=\"");
95
+ if (type == MKDA_EMAIL)
96
+ BUFPUTSL(ob, "mailto:");
97
+ escape_href(ob, link->data, link->size);
98
+
99
+ if (options->link_attributes) {
100
+ bufputc(ob, '\"');
101
+ options->link_attributes(ob, link, opaque);
102
+ bufputc(ob, '>');
103
+ } else {
104
+ BUFPUTSL(ob, "\">");
105
+ }
106
+
107
+ /*
108
+ * Pretty printing: if we get an email address as
109
+ * an actual URI, e.g. `mailto:foo@bar.com`, we don't
110
+ * want to print the `mailto:` prefix
111
+ */
112
+ if (bufprefix(link, "mailto:") == 0) {
113
+ escape_html(ob, link->data + 7, link->size - 7);
114
+ } else {
115
+ escape_html(ob, link->data, link->size);
116
+ }
117
+
118
+ BUFPUTSL(ob, "</a>");
119
+
120
+ return 1;
121
+ }
122
+
123
+ static void
124
+ rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque)
125
+ {
126
+ struct html_renderopt *options = opaque;
127
+
128
+ if (ob->size) bufputc(ob, '\n');
129
+
130
+ if (lang && lang->size) {
131
+ size_t i, cls;
132
+ if (options->flags & HTML_PRETTIFY) {
133
+ BUFPUTSL(ob, "<pre><code class=\"prettyprint lang-");
134
+ cls++;
135
+ } else {
136
+ BUFPUTSL(ob, "<pre><code class=\"");
137
+ }
138
+
139
+ for (i = 0, cls = 0; i < lang->size; ++i, ++cls) {
140
+ while (i < lang->size && isspace(lang->data[i]))
141
+ i++;
142
+
143
+ if (i < lang->size) {
144
+ size_t org = i;
145
+ while (i < lang->size && !isspace(lang->data[i]))
146
+ i++;
147
+
148
+ if (lang->data[org] == '.')
149
+ org++;
150
+
151
+ if (cls) bufputc(ob, ' ');
152
+ escape_html(ob, lang->data + org, i - org);
153
+ }
154
+ }
155
+
156
+ BUFPUTSL(ob, "\">");
157
+ } else if (options->flags & HTML_PRETTIFY) {
158
+ BUFPUTSL(ob, "<pre><code class=\"prettyprint\">");
159
+ } else {
160
+ BUFPUTSL(ob, "<pre><code>");
161
+ }
162
+
163
+ if (text)
164
+ escape_html(ob, text->data, text->size);
165
+
166
+ BUFPUTSL(ob, "</code></pre>\n");
167
+ }
168
+
169
+ static void
170
+ rndr_blockquote(struct buf *ob, const struct buf *text, void *opaque)
171
+ {
172
+ if (ob->size) bufputc(ob, '\n');
173
+ BUFPUTSL(ob, "<blockquote>\n");
174
+ if (text) bufput(ob, text->data, text->size);
175
+ BUFPUTSL(ob, "</blockquote>\n");
176
+ }
177
+
178
+ static int
179
+ rndr_codespan(struct buf *ob, const struct buf *text, void *opaque)
180
+ {
181
+ struct html_renderopt *options = opaque;
182
+ if (options->flags & HTML_PRETTIFY)
183
+ BUFPUTSL(ob, "<code class=\"prettyprint\">");
184
+ else
185
+ BUFPUTSL(ob, "<code>");
186
+ if (text) escape_html(ob, text->data, text->size);
187
+ BUFPUTSL(ob, "</code>");
188
+ return 1;
189
+ }
190
+
191
+ static int
192
+ rndr_strikethrough(struct buf *ob, const struct buf *text, void *opaque)
193
+ {
194
+ if (!text || !text->size)
195
+ return 0;
196
+
197
+ BUFPUTSL(ob, "<del>");
198
+ bufput(ob, text->data, text->size);
199
+ BUFPUTSL(ob, "</del>");
200
+ return 1;
201
+ }
202
+
203
+ static int
204
+ rndr_double_emphasis(struct buf *ob, const struct buf *text, void *opaque)
205
+ {
206
+ if (!text || !text->size)
207
+ return 0;
208
+
209
+ BUFPUTSL(ob, "<strong>");
210
+ bufput(ob, text->data, text->size);
211
+ BUFPUTSL(ob, "</strong>");
212
+
213
+ return 1;
214
+ }
215
+
216
+ static int
217
+ rndr_emphasis(struct buf *ob, const struct buf *text, void *opaque)
218
+ {
219
+ if (!text || !text->size) return 0;
220
+ BUFPUTSL(ob, "<em>");
221
+ if (text) bufput(ob, text->data, text->size);
222
+ BUFPUTSL(ob, "</em>");
223
+ return 1;
224
+ }
225
+
226
+ static int
227
+ rndr_underline(struct buf *ob, const struct buf *text, void *opaque)
228
+ {
229
+ if (!text || !text->size)
230
+ return 0;
231
+
232
+ BUFPUTSL(ob, "<u>");
233
+ bufput(ob, text->data, text->size);
234
+ BUFPUTSL(ob, "</u>");
235
+
236
+ return 1;
237
+ }
238
+
239
+ static int
240
+ rndr_highlight(struct buf *ob, const struct buf *text, void *opaque)
241
+ {
242
+ if (!text || !text->size)
243
+ return 0;
244
+
245
+ BUFPUTSL(ob, "<mark>");
246
+ bufput(ob, text->data, text->size);
247
+ BUFPUTSL(ob, "</mark>");
248
+
249
+ return 1;
250
+ }
251
+
252
+ static int
253
+ rndr_quote(struct buf *ob, const struct buf *text, void *opaque)
254
+ {
255
+ if (!text || !text->size)
256
+ return 0;
257
+
258
+ struct html_renderopt *options = opaque;
259
+
260
+ BUFPUTSL(ob, "<q>");
261
+
262
+ if (options->flags & HTML_ESCAPE)
263
+ escape_html(ob, text->data, text->size);
264
+ else
265
+ bufput(ob, text->data, text->size);
266
+
267
+ BUFPUTSL(ob, "</q>");
268
+
269
+ return 1;
270
+ }
271
+
272
+ static int
273
+ rndr_linebreak(struct buf *ob, void *opaque)
274
+ {
275
+ struct html_renderopt *options = opaque;
276
+ bufputs(ob, USE_XHTML(options) ? "<br/>\n" : "<br>\n");
277
+ return 1;
278
+ }
279
+
280
+ static int html_entity_ahead(const uint8_t *text, size_t start, size_t size) {
281
+ size_t i = start;
282
+
283
+ if (text[i] != '&')
284
+ return 0;
285
+
286
+ for (; i < size; ++i) {
287
+ if (text[i] == ' ')
288
+ return 0;
289
+ else if (text[i] == ';')
290
+ return 1;
291
+ }
292
+
293
+ return 0;
294
+ }
295
+
296
+ static void
297
+ rndr_header_anchor(struct buf *out, const struct buf *anchor)
298
+ {
299
+ static const char *STRIPPED = " -&+$,/:;=?@\"#{}|^~[]`\\*()%.!'";
300
+
301
+ const uint8_t *a = anchor->data;
302
+ const size_t size = anchor->size;
303
+ size_t i = 0;
304
+ int stripped = 0, inserted = 0;
305
+
306
+ for (; i < size; ++i) {
307
+ // skip html tags
308
+ if (a[i] == '<') {
309
+ while (i < size && a[i] != '>')
310
+ i++;
311
+ // skip html entities
312
+ } else if (a[i] == '&' && html_entity_ahead(a, i, size)) {
313
+ while (i < size && a[i] != ';')
314
+ i++;
315
+ }
316
+
317
+ // replace non-ascii or invalid characters with dashes
318
+ else if (!isascii(a[i]) || strchr(STRIPPED, a[i])) {
319
+ if (inserted && !stripped)
320
+ bufputc(out, '-');
321
+ // and do it only once
322
+ stripped = 1;
323
+ }
324
+ else {
325
+ bufputc(out, tolower(a[i]));
326
+ stripped = 0;
327
+ inserted++;
328
+ }
329
+ }
330
+
331
+ // replace the last dash if there was anything added
332
+ if (stripped && inserted)
333
+ out->size--;
334
+
335
+ // if anchor found empty, use djb2 hash for it
336
+ if (!inserted && anchor->size) {
337
+ unsigned long hash = 5381;
338
+ for (i = 0; i < size; ++i) {
339
+ hash = ((hash << 5) + hash) + a[i]; /* h * 33 + c */
340
+ }
341
+ bufprintf(out, "part-%lx", hash);
342
+ }
343
+ }
344
+
345
+ static void
346
+ rndr_header(struct buf *ob, const struct buf *text, int level, void *opaque)
347
+ {
348
+ struct html_renderopt *options = opaque;
349
+
350
+ if (ob->size)
351
+ bufputc(ob, '\n');
352
+
353
+ if ((options->flags & HTML_TOC) && level >= options->toc_data.nesting_bounds[0] &&
354
+ level <= options->toc_data.nesting_bounds[1]) {
355
+ bufprintf(ob, "<h%d id=\"", level);
356
+ rndr_header_anchor(ob, text);
357
+ BUFPUTSL(ob, "\">");
358
+ }
359
+ else
360
+ bufprintf(ob, "<h%d>", level);
361
+
362
+ if (text) bufput(ob, text->data, text->size);
363
+ bufprintf(ob, "</h%d>\n", level);
364
+ }
365
+
366
+ static int
367
+ rndr_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque)
368
+ {
369
+ struct html_renderopt *options = opaque;
370
+
371
+ if (link != NULL && (options->flags & HTML_SAFELINK) != 0 && !sd_autolink_issafe(link->data, link->size))
372
+ return 0;
373
+
374
+ BUFPUTSL(ob, "<a href=\"");
375
+
376
+ if (link && link->size)
377
+ escape_href(ob, link->data, link->size);
378
+
379
+ if (title && title->size) {
380
+ BUFPUTSL(ob, "\" title=\"");
381
+ escape_html(ob, title->data, title->size);
382
+ }
383
+
384
+ if (options->link_attributes) {
385
+ bufputc(ob, '\"');
386
+ options->link_attributes(ob, link, opaque);
387
+ bufputc(ob, '>');
388
+ } else {
389
+ BUFPUTSL(ob, "\">");
390
+ }
391
+
392
+ if (content && content->size) bufput(ob, content->data, content->size);
393
+ BUFPUTSL(ob, "</a>");
394
+ return 1;
395
+ }
396
+
397
+ static void
398
+ rndr_list(struct buf *ob, const struct buf *text, int flags, void *opaque)
399
+ {
400
+ if (ob->size) bufputc(ob, '\n');
401
+ bufput(ob, flags & MKD_LIST_ORDERED ? "<ol>\n" : "<ul>\n", 5);
402
+ if (text) bufput(ob, text->data, text->size);
403
+ bufput(ob, flags & MKD_LIST_ORDERED ? "</ol>\n" : "</ul>\n", 6);
404
+ }
405
+
406
+ static void
407
+ rndr_listitem(struct buf *ob, const struct buf *text, int flags, void *opaque)
408
+ {
409
+ BUFPUTSL(ob, "<li>");
410
+ if (text) {
411
+ size_t size = text->size;
412
+ while (size && text->data[size - 1] == '\n')
413
+ size--;
414
+
415
+ bufput(ob, text->data, size);
416
+ }
417
+ BUFPUTSL(ob, "</li>\n");
418
+ }
419
+
420
+ static void
421
+ rndr_paragraph(struct buf *ob, const struct buf *text, void *opaque)
422
+ {
423
+ struct html_renderopt *options = opaque;
424
+ size_t i = 0;
425
+
426
+ if (ob->size) bufputc(ob, '\n');
427
+
428
+ if (!text || !text->size)
429
+ return;
430
+
431
+ while (i < text->size && isspace(text->data[i])) i++;
432
+
433
+ if (i == text->size)
434
+ return;
435
+
436
+ BUFPUTSL(ob, "<p>");
437
+ if (options->flags & HTML_HARD_WRAP) {
438
+ size_t org;
439
+ while (i < text->size) {
440
+ org = i;
441
+ while (i < text->size && text->data[i] != '\n')
442
+ i++;
443
+
444
+ if (i > org)
445
+ bufput(ob, text->data + org, i - org);
446
+
447
+ /*
448
+ * do not insert a line break if this newline
449
+ * is the last character on the paragraph
450
+ */
451
+ if (i >= text->size - 1)
452
+ break;
453
+
454
+ rndr_linebreak(ob, opaque);
455
+ i++;
456
+ }
457
+ } else {
458
+ bufput(ob, &text->data[i], text->size - i);
459
+ }
460
+ BUFPUTSL(ob, "</p>\n");
461
+ }
462
+
463
+ static void
464
+ rndr_raw_block(struct buf *ob, const struct buf *text, void *opaque)
465
+ {
466
+ size_t org, size;
467
+ struct html_renderopt *options = opaque;
468
+
469
+ if (!text)
470
+ return;
471
+
472
+ size = text->size;
473
+ while (size > 0 && text->data[size - 1] == '\n')
474
+ size--;
475
+
476
+ for (org = 0; org < size && text->data[org] == '\n'; ++org)
477
+
478
+ if (org >= size)
479
+ return;
480
+
481
+ /* Remove style tags if the `:no_styles` option is enabled */
482
+ if ((options->flags & HTML_SKIP_STYLE) != 0 &&
483
+ sdhtml_is_tag(text->data, size, "style"))
484
+ return;
485
+
486
+ if (ob->size)
487
+ bufputc(ob, '\n');
488
+
489
+ bufput(ob, text->data + org, size - org);
490
+ bufputc(ob, '\n');
491
+ }
492
+
493
+ static int
494
+ rndr_triple_emphasis(struct buf *ob, const struct buf *text, void *opaque)
495
+ {
496
+ if (!text || !text->size) return 0;
497
+ BUFPUTSL(ob, "<strong><em>");
498
+ bufput(ob, text->data, text->size);
499
+ BUFPUTSL(ob, "</em></strong>");
500
+ return 1;
501
+ }
502
+
503
+ static void
504
+ rndr_hrule(struct buf *ob, void *opaque)
505
+ {
506
+ struct html_renderopt *options = opaque;
507
+ if (ob->size) bufputc(ob, '\n');
508
+ bufputs(ob, USE_XHTML(options) ? "<hr/>\n" : "<hr>\n");
509
+ }
510
+
511
+ static int
512
+ rndr_image(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque)
513
+ {
514
+ struct html_renderopt *options = opaque;
515
+
516
+ if (link != NULL && (options->flags & HTML_SAFELINK) != 0 && !sd_autolink_issafe(link->data, link->size))
517
+ return 0;
518
+
519
+ BUFPUTSL(ob, "<img src=\"");
520
+
521
+ if (link && link->size)
522
+ escape_href(ob, link->data, link->size);
523
+
524
+ BUFPUTSL(ob, "\" alt=\"");
525
+
526
+ if (alt && alt->size)
527
+ escape_html(ob, alt->data, alt->size);
528
+
529
+ if (title && title->size) {
530
+ BUFPUTSL(ob, "\" title=\"");
531
+ escape_html(ob, title->data, title->size);
532
+ }
533
+
534
+ bufputs(ob, USE_XHTML(options) ? "\"/>" : "\">");
535
+ return 1;
536
+ }
537
+
538
+ static int
539
+ rndr_raw_html(struct buf *ob, const struct buf *text, void *opaque)
540
+ {
541
+ struct html_renderopt *options = opaque;
542
+
543
+ /* HTML_ESCAPE overrides SKIP_HTML, SKIP_STYLE, SKIP_LINKS and SKIP_IMAGES
544
+ It doesn't see if there are any valid tags, just escape all of them. */
545
+ if((options->flags & HTML_ESCAPE) != 0) {
546
+ escape_html(ob, text->data, text->size);
547
+ return 1;
548
+ }
549
+
550
+ if ((options->flags & HTML_SKIP_HTML) != 0)
551
+ return 1;
552
+
553
+ if ((options->flags & HTML_SKIP_STYLE) != 0 &&
554
+ sdhtml_is_tag(text->data, text->size, "style"))
555
+ return 1;
556
+
557
+ if ((options->flags & HTML_SKIP_LINKS) != 0 &&
558
+ sdhtml_is_tag(text->data, text->size, "a"))
559
+ return 1;
560
+
561
+ if ((options->flags & HTML_SKIP_IMAGES) != 0 &&
562
+ sdhtml_is_tag(text->data, text->size, "img"))
563
+ return 1;
564
+
565
+ bufput(ob, text->data, text->size);
566
+ return 1;
567
+ }
568
+
569
+ static void
570
+ rndr_table(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque)
571
+ {
572
+ if (ob->size) bufputc(ob, '\n');
573
+ BUFPUTSL(ob, "<table><thead>\n");
574
+ if (header)
575
+ bufput(ob, header->data, header->size);
576
+ BUFPUTSL(ob, "</thead><tbody>\n");
577
+ if (body)
578
+ bufput(ob, body->data, body->size);
579
+ BUFPUTSL(ob, "</tbody></table>\n");
580
+ }
581
+
582
+ static void
583
+ rndr_tablerow(struct buf *ob, const struct buf *text, void *opaque)
584
+ {
585
+ BUFPUTSL(ob, "<tr>\n");
586
+ if (text)
587
+ bufput(ob, text->data, text->size);
588
+ BUFPUTSL(ob, "</tr>\n");
589
+ }
590
+
591
+ static void
592
+ rndr_tablecell(struct buf *ob, const struct buf *text, int flags, void *opaque)
593
+ {
594
+ if (flags & MKD_TABLE_HEADER) {
595
+ BUFPUTSL(ob, "<th");
596
+ } else {
597
+ BUFPUTSL(ob, "<td");
598
+ }
599
+
600
+ switch (flags & MKD_TABLE_ALIGNMASK) {
601
+ case MKD_TABLE_ALIGN_CENTER:
602
+ BUFPUTSL(ob, " style=\"text-align: center\">");
603
+ break;
604
+
605
+ case MKD_TABLE_ALIGN_L:
606
+ BUFPUTSL(ob, " style=\"text-align: left\">");
607
+ break;
608
+
609
+ case MKD_TABLE_ALIGN_R:
610
+ BUFPUTSL(ob, " style=\"text-align: right\">");
611
+ break;
612
+
613
+ default:
614
+ BUFPUTSL(ob, ">");
615
+ }
616
+
617
+ if (text)
618
+ bufput(ob, text->data, text->size);
619
+
620
+ if (flags & MKD_TABLE_HEADER) {
621
+ BUFPUTSL(ob, "</th>\n");
622
+ } else {
623
+ BUFPUTSL(ob, "</td>\n");
624
+ }
625
+ }
626
+
627
+ static int
628
+ rndr_superscript(struct buf *ob, const struct buf *text, void *opaque)
629
+ {
630
+ if (!text || !text->size) return 0;
631
+ BUFPUTSL(ob, "<sup>");
632
+ bufput(ob, text->data, text->size);
633
+ BUFPUTSL(ob, "</sup>");
634
+ return 1;
635
+ }
636
+
637
+ static void
638
+ rndr_normal_text(struct buf *ob, const struct buf *text, void *opaque)
639
+ {
640
+ if (text)
641
+ escape_html(ob, text->data, text->size);
642
+ }
643
+
644
+ static void
645
+ rndr_footnotes(struct buf *ob, const struct buf *text, void *opaque)
646
+ {
647
+ struct html_renderopt *options = opaque;
648
+
649
+ if (ob->size) bufputc(ob, '\n');
650
+
651
+ BUFPUTSL(ob, "<div class=\"footnotes\">\n");
652
+ bufputs(ob, USE_XHTML(options) ? "<hr/>\n" : "<hr>\n");
653
+ BUFPUTSL(ob, "<ol>\n");
654
+
655
+ if (text)
656
+ bufput(ob, text->data, text->size);
657
+
658
+ BUFPUTSL(ob, "\n</ol>\n</div>\n");
659
+ }
660
+
661
+ static void
662
+ rndr_footnote_def(struct buf *ob, const struct buf *text, unsigned int num, void *opaque)
663
+ {
664
+ size_t i = 0;
665
+ int pfound = 0;
666
+
667
+ /* insert anchor at the end of first paragraph block */
668
+ if (text) {
669
+ while ((i+3) < text->size) {
670
+ if (text->data[i++] != '<') continue;
671
+ if (text->data[i++] != '/') continue;
672
+ if (text->data[i++] != 'p' && text->data[i] != 'P') continue;
673
+ if (text->data[i] != '>') continue;
674
+ i -= 3;
675
+ pfound = 1;
676
+ break;
677
+ }
678
+ }
679
+
680
+ bufprintf(ob, "\n<li id=\"fn%d\">\n", num);
681
+ if (pfound) {
682
+ bufput(ob, text->data, i);
683
+ bufprintf(ob, "&nbsp;<a href=\"#fnref%d\">&#8617;</a>", num);
684
+ bufput(ob, text->data + i, text->size - i);
685
+ } else if (text) {
686
+ bufput(ob, text->data, text->size);
687
+ }
688
+ BUFPUTSL(ob, "</li>\n");
689
+ }
690
+
691
+ static int
692
+ rndr_footnote_ref(struct buf *ob, unsigned int num, void *opaque)
693
+ {
694
+ bufprintf(ob, "<sup id=\"fnref%d\"><a href=\"#fn%d\">%d</a></sup>", num, num, num);
695
+ return 1;
696
+ }
697
+
698
+ static void
699
+ toc_header(struct buf *ob, const struct buf *text, int level, void *opaque)
700
+ {
701
+ struct html_renderopt *options = opaque;
702
+
703
+ if (level >= options->toc_data.nesting_bounds[0] &&
704
+ level <= options->toc_data.nesting_bounds[1]) {
705
+ /* set the level offset if this is the first header
706
+ * we're parsing for the document */
707
+ if (options->toc_data.current_level == 0)
708
+ options->toc_data.level_offset = level - 1;
709
+
710
+ level -= options->toc_data.level_offset;
711
+
712
+ if (level > options->toc_data.current_level) {
713
+ while (level > options->toc_data.current_level) {
714
+ BUFPUTSL(ob, "<ul>\n<li>\n");
715
+ options->toc_data.current_level++;
716
+ }
717
+ } else if (level < options->toc_data.current_level) {
718
+ BUFPUTSL(ob, "</li>\n");
719
+ while (level < options->toc_data.current_level) {
720
+ BUFPUTSL(ob, "</ul>\n</li>\n");
721
+ options->toc_data.current_level--;
722
+ }
723
+ BUFPUTSL(ob,"<li>\n");
724
+ } else {
725
+ BUFPUTSL(ob,"</li>\n<li>\n");
726
+ }
727
+
728
+ bufprintf(ob, "<a href=\"#");
729
+ rndr_header_anchor(ob, text);
730
+ BUFPUTSL(ob, "\">");
731
+
732
+ if (text) {
733
+ if (options->flags & HTML_ESCAPE)
734
+ escape_html(ob, text->data, text->size);
735
+ else
736
+ bufput(ob, text->data, text->size);
737
+ }
738
+
739
+ BUFPUTSL(ob, "</a>\n");
740
+ }
741
+ }
742
+
743
+ static int
744
+ toc_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque)
745
+ {
746
+ if (content && content->size)
747
+ bufput(ob, content->data, content->size);
748
+ return 1;
749
+ }
750
+
751
+ static void
752
+ toc_finalize(struct buf *ob, void *opaque)
753
+ {
754
+ struct html_renderopt *options = opaque;
755
+
756
+ while (options->toc_data.current_level > 0) {
757
+ BUFPUTSL(ob, "</li>\n</ul>\n");
758
+ options->toc_data.current_level--;
759
+ }
760
+ }
761
+
762
+ void
763
+ sdhtml_toc_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options, unsigned int render_flags)
764
+ {
765
+ static const struct sd_callbacks cb_default = {
766
+ NULL,
767
+ NULL,
768
+ NULL,
769
+ toc_header,
770
+ NULL,
771
+ NULL,
772
+ NULL,
773
+ NULL,
774
+ NULL,
775
+ NULL,
776
+ NULL,
777
+ rndr_footnotes,
778
+ rndr_footnote_def,
779
+
780
+ NULL,
781
+ rndr_codespan,
782
+ rndr_double_emphasis,
783
+ rndr_emphasis,
784
+ rndr_underline,
785
+ rndr_highlight,
786
+ rndr_quote,
787
+ NULL,
788
+ NULL,
789
+ toc_link,
790
+ NULL,
791
+ rndr_triple_emphasis,
792
+ rndr_strikethrough,
793
+ rndr_superscript,
794
+ rndr_footnote_ref,
795
+
796
+ NULL,
797
+ NULL,
798
+
799
+ NULL,
800
+ toc_finalize,
801
+ };
802
+
803
+ memset(options, 0x0, sizeof(struct html_renderopt));
804
+ options->flags = render_flags;
805
+
806
+ memcpy(callbacks, &cb_default, sizeof(struct sd_callbacks));
807
+ }
808
+
809
+ void
810
+ sdhtml_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options, unsigned int render_flags)
811
+ {
812
+ static const struct sd_callbacks cb_default = {
813
+ rndr_blockcode,
814
+ rndr_blockquote,
815
+ rndr_raw_block,
816
+ rndr_header,
817
+ rndr_hrule,
818
+ rndr_list,
819
+ rndr_listitem,
820
+ rndr_paragraph,
821
+ rndr_table,
822
+ rndr_tablerow,
823
+ rndr_tablecell,
824
+ rndr_footnotes,
825
+ rndr_footnote_def,
826
+
827
+ rndr_autolink,
828
+ rndr_codespan,
829
+ rndr_double_emphasis,
830
+ rndr_emphasis,
831
+ rndr_underline,
832
+ rndr_highlight,
833
+ rndr_quote,
834
+ rndr_image,
835
+ rndr_linebreak,
836
+ rndr_link,
837
+ rndr_raw_html,
838
+ rndr_triple_emphasis,
839
+ rndr_strikethrough,
840
+ rndr_superscript,
841
+ rndr_footnote_ref,
842
+
843
+ NULL,
844
+ rndr_normal_text,
845
+
846
+ NULL,
847
+ NULL,
848
+ };
849
+
850
+ /* Prepare the options pointer */
851
+ memset(options, 0x0, sizeof(struct html_renderopt));
852
+ options->flags = render_flags;
853
+ options->toc_data.nesting_bounds[0] = 1;
854
+ options->toc_data.nesting_bounds[1] = 6;
855
+
856
+ /* Prepare the callbacks */
857
+ memcpy(callbacks, &cb_default, sizeof(struct sd_callbacks));
858
+
859
+ if (render_flags & HTML_SKIP_IMAGES)
860
+ callbacks->image = NULL;
861
+
862
+ if (render_flags & HTML_SKIP_LINKS) {
863
+ callbacks->link = NULL;
864
+ callbacks->autolink = NULL;
865
+ }
866
+
867
+ if (render_flags & HTML_SKIP_HTML || render_flags & HTML_ESCAPE)
868
+ callbacks->blockhtml = NULL;
869
+ }