moredown 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,135 @@
1
+ #ifndef _MARKDOWN_D
2
+ #define _MARKDOWN_D
3
+
4
+ #include "cstring.h"
5
+
6
+ /* reference-style links (and images) are stored in an array
7
+ * of footnotes.
8
+ */
9
+ typedef struct footnote {
10
+ Cstring tag; /* the tag for the reference link */
11
+ Cstring link; /* what this footnote points to */
12
+ Cstring title; /* what it's called (TITLE= attribute) */
13
+ int height, width; /* dimensions (for image link) */
14
+ } Footnote;
15
+
16
+ /* each input line is read into a Line, which contains the line,
17
+ * the offset of the first non-space character [this assumes
18
+ * that all tabs will be expanded to spaces!], and a pointer to
19
+ * the next line.
20
+ */
21
+ typedef struct line {
22
+ Cstring text;
23
+ struct line *next;
24
+ int dle;
25
+ } Line;
26
+
27
+
28
+ /* a paragraph is a collection of Lines, with links to the next paragraph
29
+ * and (if it's a QUOTE, UL, or OL) to the reparsed contents of this
30
+ * paragraph.
31
+ */
32
+ typedef struct paragraph {
33
+ struct paragraph *next; /* next paragraph */
34
+ struct paragraph *down; /* recompiled contents of this paragraph */
35
+ struct line *text; /* all the text in this paragraph */
36
+ char *ident; /* %id% tag for QUOTE */
37
+ enum { WHITESPACE=0, CODE, QUOTE, MARKUP,
38
+ HTML, STYLE, DL, UL, OL, AL, LISTITEM,
39
+ HDR, HR } typ;
40
+ enum { IMPLICIT=0, PARA, CENTER} align;
41
+ int hnumber; /* <Hn> for typ == HDR */
42
+ } Paragraph;
43
+
44
+ enum { ETX, SETEXT }; /* header types */
45
+
46
+
47
+ typedef struct block {
48
+ enum { bTEXT, bSTAR, bUNDER } b_type;
49
+ int b_count;
50
+ char b_char;
51
+ Cstring b_text;
52
+ Cstring b_post;
53
+ } block;
54
+
55
+ typedef STRING(block) Qblock;
56
+
57
+
58
+ /* a magic markdown io thing holds all the data structures needed to
59
+ * do the backend processing of a markdown document
60
+ */
61
+ typedef struct mmiot {
62
+ Cstring out;
63
+ Cstring in;
64
+ Qblock Q;
65
+ int isp;
66
+ STRING(Footnote) *footnotes;
67
+ int flags;
68
+ #define DENY_A 0x0001
69
+ #define DENY_IMG 0x0002
70
+ #define DENY_SMARTY 0x0004
71
+ #define DENY_HTML 0x0008
72
+ #define STRICT 0x0010
73
+ #define INSIDE_TAG 0x0020
74
+ #define NO_PSEUDO_PROTO 0x0040
75
+ #define CDATA_OUTPUT 0x0080
76
+ #define TOC 0x1000
77
+ #define MKD_1_COMPAT 0x2000
78
+ #define USER_FLAGS 0xF0FF
79
+ #define EMBEDDED DENY_A|DENY_IMG|NO_PSEUDO_PROTO|CDATA_OUTPUT
80
+ char *base;
81
+ } MMIOT;
82
+
83
+
84
+ /*
85
+ * the mkdio text input functions return a document structure,
86
+ * which contains a header (retrieved from the document if
87
+ * markdown was configured * with the * --enable-pandoc-header
88
+ * and the document begins with a pandoc-style header) and the
89
+ * root of the linked list of Lines.
90
+ */
91
+ typedef struct document {
92
+ Line *headers; /* title -> author(s) -> date */
93
+ ANCHOR(Line) content; /* uncompiled text, not valid after compile() */
94
+ Paragraph *code; /* intermediate code generated by compile() */
95
+ int compiled; /* set after mkd_compile() */
96
+ int html; /* set after (internal) htmlify() */
97
+ int tabstop; /* for properly expanding tabs (ick) */
98
+ MMIOT *ctx; /* backend buffers, flags, and structures */
99
+ char *base; /* url basename for url fragments */
100
+ } Document;
101
+
102
+
103
+ extern int mkd_firstnonblank(Line *);
104
+ extern int mkd_compile(Document *, int);
105
+ extern int mkd_document(Document *, char **);
106
+ extern int mkd_generatehtml(Document *, FILE *);
107
+ extern int mkd_style(Document *, FILE *);
108
+ extern void mkd_cleanup(Document *);
109
+ extern int mkd_text(char *, int, FILE*, int);
110
+ extern void mkd_basename(Document*, char *);
111
+ extern void mkd_string_to_anchor(char*,int, void(*)(int,void*), void*);
112
+
113
+ extern Document *mkd_in(FILE *, int);
114
+ extern Document *mkd_string(char*,int, int);
115
+
116
+ #define NO_HEADER 0x0100
117
+ #define STD_TABSTOP 0x0200
118
+ #define INPUT_MASK (NO_HEADER|STD_TABSTOP)
119
+
120
+
121
+ /* internal resource handling functions.
122
+ */
123
+ extern void ___mkd_freeLine(Line *);
124
+ extern void ___mkd_freeLines(Line *);
125
+ extern void ___mkd_freeParagraph(Paragraph *);
126
+ extern void ___mkd_freefootnotes(MMIOT *);
127
+ extern void ___mkd_initmmiot(MMIOT *, void *);
128
+ extern void ___mkd_freemmiot(MMIOT *, void *);
129
+ extern void ___mkd_freeLineRange(Line *, Line *);
130
+ extern void ___mkd_xml(char *, int, FILE *);
131
+ extern void ___mkd_reparse(char *, int, int, MMIOT*);
132
+ extern void ___mkd_emblock(MMIOT*);
133
+ extern void ___mkd_tidy(Line *);
134
+
135
+ #endif/*_MARKDOWN_D*/
@@ -0,0 +1,241 @@
1
+ /*
2
+ * mkdio -- markdown front end input functions
3
+ *
4
+ * Copyright (C) 2007 David L Parsons.
5
+ * The redistribution terms are provided in the COPYRIGHT file that must
6
+ * be distributed with this source code.
7
+ */
8
+ #include "config.h"
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <ctype.h>
12
+
13
+ #include "cstring.h"
14
+ #include "markdown.h"
15
+ #include "amalloc.h"
16
+
17
+ typedef ANCHOR(Line) LineAnchor;
18
+
19
+ /* create a new blank Document
20
+ */
21
+ static Document*
22
+ new_Document()
23
+ {
24
+ Document *ret = calloc(sizeof(Document), 1);
25
+
26
+ if ( ret ) {
27
+ if (( ret->ctx = calloc(sizeof(MMIOT), 1) ))
28
+ return ret;
29
+ free(ret);
30
+ }
31
+ return 0;
32
+ }
33
+
34
+
35
+ /* add a line to the markdown input chain
36
+ */
37
+ static void
38
+ queue(Document* a, Cstring *line)
39
+ {
40
+ Line *p = calloc(sizeof *p, 1);
41
+ unsigned char c;
42
+ int xp = 0;
43
+ int size = S(*line);
44
+ unsigned char *str = (unsigned char*)T(*line);
45
+
46
+ CREATE(p->text);
47
+ ATTACH(a->content, p);
48
+
49
+ while ( size-- ) {
50
+ if ( (c = *str++) == '\t' ) {
51
+ /* expand tabs into ->tabstop spaces. We use ->tabstop
52
+ * because the ENTIRE FREAKING COMPUTER WORLD uses editors
53
+ * that don't do ^T/^D, but instead use tabs for indentation,
54
+ * and, of course, set their tabs down to 4 spaces
55
+ */
56
+ do {
57
+ EXPAND(p->text) = ' ';
58
+ } while ( ++xp % a->tabstop );
59
+ }
60
+ else if ( c >= ' ' ) {
61
+ EXPAND(p->text) = c;
62
+ ++xp;
63
+ }
64
+ }
65
+ EXPAND(p->text) = 0;
66
+ S(p->text)--;
67
+ p->dle = mkd_firstnonblank(p);
68
+ }
69
+
70
+
71
+ #ifdef PANDOC_HEADER
72
+ /* trim leading blanks from a header line
73
+ */
74
+ static void
75
+ snip(Line *p)
76
+ {
77
+ CLIP(p->text, 0, 1);
78
+ p->dle = mkd_firstnonblank(p);
79
+ }
80
+ #endif
81
+
82
+
83
+ /* build a Document from any old input.
84
+ */
85
+ typedef int (*getc_func)(void*);
86
+
87
+ Document *
88
+ populate(getc_func getc, void* ctx, int flags)
89
+ {
90
+ Cstring line;
91
+ Document *a = new_Document();
92
+ int c;
93
+ #ifdef PANDOC_HEADER
94
+ int pandoc = 0;
95
+ #endif
96
+
97
+ if ( !a ) return 0;
98
+
99
+ a->tabstop = (flags & STD_TABSTOP) ? 4 : TABSTOP;
100
+
101
+ CREATE(line);
102
+
103
+ while ( (c = (*getc)(ctx)) != EOF ) {
104
+ if ( c == '\n' ) {
105
+ #ifdef PANDOC_HEADER
106
+ if ( pandoc != EOF && pandoc < 3 ) {
107
+ if ( S(line) && (T(line)[0] == '%') )
108
+ pandoc++;
109
+ else
110
+ pandoc = EOF;
111
+ }
112
+ #endif
113
+ queue(a, &line);
114
+ S(line) = 0;
115
+ }
116
+ else
117
+ EXPAND(line) = c;
118
+ }
119
+
120
+ if ( S(line) )
121
+ queue(a, &line);
122
+
123
+ DELETE(line);
124
+
125
+ #ifdef PANDOC_HEADER
126
+ if ( (pandoc == 3) && !(flags & NO_HEADER) ) {
127
+ /* the first three lines started with %, so we have a header.
128
+ * clip the first three lines out of content and hang them
129
+ * off header.
130
+ */
131
+ a->headers = T(a->content);
132
+ T(a->content) = a->headers->next->next->next;
133
+ a->headers->next->next->next = 0;
134
+ snip(a->headers);
135
+ snip(a->headers->next);
136
+ snip(a->headers->next->next);
137
+ }
138
+ #endif
139
+
140
+ return a;
141
+ }
142
+
143
+
144
+ /* convert a file into a linked list
145
+ */
146
+ Document *
147
+ mkd_in(FILE *f, int flags)
148
+ {
149
+ return populate((getc_func)fgetc, f, flags & INPUT_MASK);
150
+ }
151
+
152
+
153
+ /* return a single character out of a buffer
154
+ */
155
+ struct string_ctx {
156
+ char *data; /* the unread data */
157
+ int size; /* and how much is there? */
158
+ } ;
159
+
160
+
161
+ static int
162
+ strget(struct string_ctx *in)
163
+ {
164
+ if ( !in->size ) return EOF;
165
+
166
+ --(in->size);
167
+
168
+ return *(in->data)++;
169
+ }
170
+
171
+
172
+ /* convert a block of text into a linked list
173
+ */
174
+ Document *
175
+ mkd_string(char *buf, int len, int flags)
176
+ {
177
+ struct string_ctx about;
178
+
179
+ about.data = buf;
180
+ about.size = len;
181
+
182
+ return populate((getc_func)strget, &about, flags & INPUT_MASK);
183
+ }
184
+
185
+
186
+ /* write the html to a file (xmlified if necessary)
187
+ */
188
+ int
189
+ mkd_generatehtml(Document *p, FILE *output)
190
+ {
191
+ char *doc;
192
+ int szdoc;
193
+
194
+ if ( (szdoc = mkd_document(p, &doc)) != EOF ) {
195
+ if ( p->ctx->flags & CDATA_OUTPUT )
196
+ ___mkd_xml(doc, szdoc, output);
197
+ else
198
+ fwrite(doc, szdoc, 1, output);
199
+ putc('\n', output);
200
+ return 0;
201
+ }
202
+ return -1;
203
+ }
204
+
205
+
206
+ /* convert some markdown text to html
207
+ */
208
+ int
209
+ markdown(Document *document, FILE *out, int flags)
210
+ {
211
+ if ( mkd_compile(document, flags) ) {
212
+ mkd_generatehtml(document, out);
213
+ mkd_cleanup(document);
214
+ return 0;
215
+ }
216
+ return -1;
217
+ }
218
+
219
+
220
+ void
221
+ mkd_basename(Document *document, char *base)
222
+ {
223
+ if ( document )
224
+ document->base = base;
225
+ }
226
+
227
+
228
+ /* write out a Cstring, mangled into a form suitable for `<a href=` or `<a id=`
229
+ */
230
+ void
231
+ mkd_string_to_anchor(char *s, int len, void(*outchar)(int,void*), void *out)
232
+ {
233
+ for ( ; len-- > 0; ++s ) {
234
+ if ( *s == ' ' || *s == '&' || *s == '<' || *s == '"' )
235
+ (*outchar)('+', out);
236
+ else if ( isalnum(*s) || ispunct(*s) )
237
+ (*outchar)(*s, out);
238
+ else
239
+ (*outchar)('~',out);
240
+ }
241
+ }
@@ -0,0 +1,66 @@
1
+ #ifndef _MKDIO_D
2
+ #define _MKDIO_D
3
+
4
+ #include <stdio.h>
5
+
6
+ typedef void MMIOT;
7
+
8
+ /* line builder for markdown()
9
+ */
10
+ MMIOT *mkd_in(FILE*,int); /* assemble input from a file */
11
+ MMIOT *mkd_string(char*,int,int); /* assemble input from a buffer */
12
+
13
+ void mkd_basename(MMIOT*,char*);
14
+
15
+ /* compilation, debugging, cleanup
16
+ */
17
+ int mkd_compile(MMIOT*, int);
18
+ int mkd_generatehtml(MMIOT*,FILE*);
19
+ int mkd_generatetoc(MMIOT*,FILE*);
20
+ int mkd_xhtmlpage(MMIOT*,int,FILE*);
21
+ int mkd_cleanup(MMIOT*);
22
+
23
+ /* markup functions
24
+ */
25
+ int mkd_text(char *, int, FILE*, int);
26
+ int mkd_style(MMIOT*, FILE*);
27
+ int mkd_dump(MMIOT*, FILE*, int, char*);
28
+ int markdown(MMIOT*, FILE*, int);
29
+ void mkd_basename(MMIOT*,char*);
30
+ void mkd_string_to_anchor(char *, int, int (*)(int,void*), void*);
31
+
32
+ /* header block access
33
+ */
34
+ char* mkd_doc_title(MMIOT*);
35
+ char* mkd_doc_author(MMIOT*);
36
+ char* mkd_doc_date(MMIOT*);
37
+
38
+ /* compiled data access
39
+ */
40
+ int mkd_document(MMIOT*, char**);
41
+
42
+ /* version#.
43
+ */
44
+ extern char markdown_version[];
45
+
46
+ /* special flags for markdown() and mkd_text()
47
+ */
48
+ #define MKD_NOLINKS 0x0001 /* don't do link processing, block <a> tags */
49
+ #define MKD_NOIMAGE 0x0002 /* don't do image processing, block <img> */
50
+ #define MKD_NOPANTS 0x0004 /* don't run smartypants() */
51
+ #define MKD_NOHTML 0x0008 /* don't allow raw html through AT ALL */
52
+ #define MKD_STRICT 0x0010 /* disable SUPERSCRIPT, RELAXED_EMPHASIS */
53
+ #define MKD_TAGTEXT 0x0020 /* don't expand `_` and `*` */
54
+ #define MKD_NO_EXT 0x0040 /* don't allow pseudo-protocols */
55
+ #define MKD_CDATA 0x0080 /* generate code for xml ![CDATA[...]] */
56
+ #define MKD_TOC 0x1000 /* do table-of-contents processing */
57
+ #define MKD_1_COMPAT 0x2000 /* compatability with MarkdownTest_1.0 */
58
+ #define MKD_EMBED MKD_NOLINKS|MKD_NOIMAGE|MKD_TAGTEXT
59
+
60
+ /* special flags for mkd_in() and mkd_string()
61
+ */
62
+ #define MKD_NOHEADER 0x0100 /* don't process header blocks */
63
+ #define MKD_TABSTOP 0x0200 /* expand tabs to 4 spaces */
64
+
65
+
66
+ #endif/*_MKDIO_D*/
@@ -0,0 +1,92 @@
1
+ #include <stdio.h>
2
+ #include "ruby.h"
3
+ #include "mkdio.h"
4
+
5
+ static VALUE rb_cRDiscount;
6
+
7
+ static VALUE
8
+ rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self)
9
+ {
10
+ /* grab char pointer to markdown input text */
11
+ char *res;
12
+ int szres;
13
+ VALUE text = rb_funcall(self, rb_intern("text"), 0);
14
+ VALUE buf = rb_str_buf_new(1024);
15
+ Check_Type(text, T_STRING);
16
+
17
+ int flags = rb_rdiscount__get_flags(self);
18
+
19
+ MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags);
20
+
21
+ if ( mkd_compile(doc, flags) ) {
22
+ szres = mkd_document(doc, &res);
23
+
24
+ if ( szres != EOF ) {
25
+ rb_str_cat(buf, res, szres);
26
+ rb_str_cat(buf, "\n", 1);
27
+ }
28
+ }
29
+ mkd_cleanup(doc);
30
+
31
+ return buf;
32
+ }
33
+
34
+ static VALUE
35
+ rb_rdiscount_toc_content(int argc, VALUE *argv, VALUE self)
36
+ {
37
+ char *res;
38
+ int szres;
39
+
40
+ int flags = rb_rdiscount__get_flags(self);
41
+
42
+ /* grab char pointer to markdown input text */
43
+ VALUE text = rb_funcall(self, rb_intern("text"), 0);
44
+ Check_Type(text, T_STRING);
45
+
46
+ /* allocate a ruby string buffer and wrap it in a stream */
47
+ VALUE buf = rb_str_buf_new(4096);
48
+
49
+ MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags);
50
+
51
+ if ( mkd_compile(doc, flags) ) {
52
+ szres = mkd_toc(doc, &res);
53
+
54
+ if ( szres != EOF ) {
55
+ rb_str_cat(buf, res, szres);
56
+ rb_str_cat(buf, "\n", 1);
57
+ }
58
+ }
59
+ mkd_cleanup(doc);
60
+
61
+ return buf;
62
+ }
63
+
64
+ int rb_rdiscount__get_flags(VALUE ruby_obj)
65
+ {
66
+ /* compile flags */
67
+ int flags = MKD_TABSTOP | MKD_NOHEADER;
68
+
69
+ /* smart */
70
+ if ( rb_funcall(ruby_obj, rb_intern("smart"), 0) != Qtrue )
71
+ flags = flags | MKD_NOPANTS;
72
+
73
+ /* filter_html */
74
+ if ( rb_funcall(ruby_obj, rb_intern("filter_html"), 0) == Qtrue )
75
+ flags = flags | MKD_NOHTML;
76
+
77
+ /* generate_toc */
78
+ if ( rb_funcall(ruby_obj, rb_intern("generate_toc"), 0) == Qtrue)
79
+ flags = flags | MKD_TOC;
80
+
81
+ return flags;
82
+ }
83
+
84
+
85
+ void Init_rdiscount()
86
+ {
87
+ rb_cRDiscount = rb_define_class("RDiscount", rb_cObject);
88
+ rb_define_method(rb_cRDiscount, "to_html", rb_rdiscount_to_html, -1);
89
+ rb_define_method(rb_cRDiscount, "toc_content", rb_rdiscount_toc_content, -1);
90
+ }
91
+
92
+ /* vim: set ts=4 sw=4: */