Debian Codegraph @

ext/github_flavoured.c

raw symbols SCIP
1
2 /*
3 * github_flavoured -- implement the obnoxious "returns are hard newlines"
4 * feature in github flavoured markdown.
5 *
6 * Copyright (C) 2012 David L Parsons.
7 * The redistribution terms are provided in the COPYRIGHT file that must
8 * be distributed with this source code.
9 */
10 #include "config.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14
15 #include "cstring.h"
16 #include "markdown.h"
17 #include "amalloc.h"
18
19 /* build a Document from any old input.
20 */
21 typedef int (*getc_func)(void*);
22
23 Document *
24 gfm_populate(getc_func getc, void* ctx, int flags)
25 {
26 Cstring line;
27 Document *a = __mkd_new_Document();
28 int c;
29 int pandoc = 0;
30
31 if ( !a ) return 0;
32
33 a->tabstop = is_flag_set(flags, MKD_TABSTOP) ? 4 : TABSTOP;
34
35 CREATE(line);
36
37 while ( (c = (*getc)(ctx)) != EOF ) {
38 if ( c == '\n' ) {
39 if ( pandoc != EOF && pandoc < 3 ) {
40 if ( S(line) && (T(line)[0] == '%') )
41 pandoc++;
42 else
43 pandoc = EOF;
44 }
45
46 if (pandoc == EOF) {
47 EXPAND(line) = ' ';
48 EXPAND(line) = ' ';
49 }
50 __mkd_enqueue(a, &line);
51 S(line) = 0;
52 }
53 else if ( isprint(c) || isspace(c) || (c & 0x80) )
54 EXPAND(line) = c;
55 }
56
57 if ( S(line) )
58 __mkd_enqueue(a, &line);
59
60 DELETE(line);
61
62 if ( (pandoc == 3) && !(is_flag_set(flags, MKD_NOHEADER)
63 || is_flag_set(flags, MKD_STRICT)) ) {
64 /* the first three lines started with %, so we have a header.
65 * clip the first three lines out of content and hang them
66 * off header.
67 */
68 Line *headers = T(a->content);
69
70 a->title = headers; __mkd_trim_line(a->title, 1);
71 a->author= headers->next; __mkd_trim_line(a->author, 1);
72 a->date = headers->next->next; __mkd_trim_line(a->date, 1);
73
74 T(a->content) = headers->next->next->next;
75 }
76
77 return a;
78 }
79
80
81 /* convert a block of text into a linked list
82 */
83 Document *
84 gfm_string(const char *buf, int len, mkd_flag_t flags)
85 {
86 struct string_stream about;
87
88 about.data = buf;
89 about.size = len;
90
91 return gfm_populate((getc_func)__mkd_io_strget, &about, flags & INPUT_MASK);
92 }
93
94
95 /* convert a file into a linked list
96 */
97 Document *
98 gfm_in(FILE *f, mkd_flag_t flags)
99 {
100 return gfm_populate((getc_func)fgetc, f, flags & INPUT_MASK);
101 }