rtomayko-rdiscount 1.3.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/ext/markdown.h ADDED
@@ -0,0 +1,131 @@
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 USER_FLAGS 0xF0FF
78
+ #define EMBEDDED DENY_A|DENY_IMG|NO_PSEUDO_PROTO|CDATA_OUTPUT
79
+ char *base;
80
+ } MMIOT;
81
+
82
+
83
+ /*
84
+ * the mkdio text input functions return a document structure,
85
+ * which contains a header (retrieved from the document if
86
+ * markdown was configured * with the * --enable-pandoc-header
87
+ * and the document begins with a pandoc-style header) and the
88
+ * root of the linked list of Lines.
89
+ */
90
+ typedef struct document {
91
+ Line *headers; /* title -> author(s) -> date */
92
+ ANCHOR(Line) content; /* uncompiled text, not valid after compile() */
93
+ Paragraph *code; /* intermediate code generated by compile() */
94
+ int compiled; /* set after mkd_compile() */
95
+ int html; /* set after (internal) htmlify() */
96
+ int tabstop; /* for properly expanding tabs (ick) */
97
+ MMIOT *ctx; /* backend buffers, flags, and structures */
98
+ char *base; /* url basename for url fragments */
99
+ } Document;
100
+
101
+
102
+ extern int mkd_firstnonblank(Line *);
103
+ extern int mkd_compile(Document *, int);
104
+ extern int mkd_document(Document *, char **);
105
+ extern int mkd_generatehtml(Document *, FILE *);
106
+ extern int mkd_style(Document *, FILE *);
107
+ extern void mkd_cleanup(Document *);
108
+ extern int mkd_text(char *, int, FILE*, int);
109
+ extern void mkd_basename(Document*, char *);
110
+ extern void mkd_string_to_anchor(char*,int, void(*)(int,void*), void*);
111
+
112
+ extern Document *mkd_in(FILE *, int);
113
+ extern Document *mkd_string(char*,int, int);
114
+
115
+ #define NO_HEADER 0x0100
116
+ #define STD_TABSTOP 0x0200
117
+ #define INPUT_MASK (NO_HEADER|STD_TABSTOP)
118
+
119
+
120
+ /* internal resource handling functions.
121
+ */
122
+ extern void ___mkd_freeLine(Line *);
123
+ extern void ___mkd_freeLines(Line *);
124
+ extern void ___mkd_freeParagraph(Paragraph *);
125
+ extern void ___mkd_freefootnotes(MMIOT *);
126
+ extern void ___mkd_initmmiot(MMIOT *, void *);
127
+ extern void ___mkd_freemmiot(MMIOT *, void *);
128
+ extern void ___mkd_freeLineRange(Line *, Line *);
129
+ extern void ___mkd_xml(char *, int, FILE *);
130
+
131
+ #endif/*_MARKDOWN_D*/
data/ext/mkdio.c ADDED
@@ -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
+ }
data/ext/mkdio.h ADDED
@@ -0,0 +1,65 @@
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_EMBED MKD_NOLINKS|MKD_NOIMAGE|MKD_TAGTEXT
58
+
59
+ /* special flags for mkd_in() and mkd_string()
60
+ */
61
+ #define MKD_NOHEADER 0x0100 /* don't process header blocks */
62
+ #define MKD_TABSTOP 0x0200 /* expand tabs to 4 spaces */
63
+
64
+
65
+ #endif/*_MKDIO_D*/
data/ext/rbstrio.c ADDED
@@ -0,0 +1,48 @@
1
+ #if defined(HAVE_FOPENCOOKIE)
2
+ # define _GNU_SOURCE
3
+ #endif
4
+
5
+ #include <stdlib.h>
6
+ #include "rbstrio.h"
7
+
8
+ #define INCREMENT 1024
9
+
10
+ /* called when data is written to the stream. */
11
+ static int rb_str_io_write(void *cookie, const char *data, int len) {
12
+ VALUE buf = (VALUE)cookie;
13
+ rb_str_cat(buf, data, len);
14
+ return len;
15
+ }
16
+
17
+ /* called when the stream is closed */
18
+ static int rb_str_io_close(void *cookie) {
19
+ VALUE buf = (VALUE)cookie;
20
+ rb_gc_unregister_address(&buf);
21
+ return 0;
22
+ }
23
+
24
+ #if defined(HAVE_FOPENCOOKIE)
25
+ cookie_io_functions_t rb_str_io_functions =
26
+ {
27
+ (cookie_read_function_t*)NULL,
28
+ (cookie_write_function_t*)rb_str_io_write,
29
+ (cookie_seek_function_t*)NULL,
30
+ (cookie_close_function_t*)rb_str_io_close
31
+ };
32
+ #endif
33
+
34
+ /* create a stream backed by a Ruby string. */
35
+ FILE *rb_str_io_new(VALUE buf) {
36
+ FILE *rv;
37
+ Check_Type(buf, T_STRING);
38
+ #if defined(HAVE_FOPENCOOKIE)
39
+ rv = fopencookie((void*)buf, "w", rb_str_io_functions);
40
+ #else
41
+ rv = funopen((void*)buf, NULL, rb_str_io_write, NULL, rb_str_io_close);
42
+ #endif
43
+ /* TODO if (rv == NULL) */
44
+ rb_gc_register_address(&buf);
45
+ return rv;
46
+ }
47
+
48
+ /* vim: set ts=4 sw=4: */
data/ext/rbstrio.h ADDED
@@ -0,0 +1,4 @@
1
+ #include <stdio.h>
2
+ #include "ruby.h"
3
+
4
+ FILE *rb_str_io_new(VALUE buf);
data/ext/rdiscount.c ADDED
@@ -0,0 +1,79 @@
1
+ #include <stdio.h>
2
+ #include "ruby.h"
3
+ #include "mkdio.h"
4
+ #include "rbstrio.h"
5
+
6
+ static VALUE rb_cRDiscount;
7
+
8
+ static VALUE
9
+ rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self)
10
+ {
11
+ /* grab char pointer to markdown input text */
12
+ VALUE text = rb_funcall(self, rb_intern("text"), 0);
13
+ Check_Type(text, T_STRING);
14
+
15
+ /* allocate a ruby string buffer and wrap it in a stream */
16
+ VALUE buf = rb_str_buf_new(4096);
17
+ FILE *stream = rb_str_io_new(buf);
18
+
19
+ int flags = rb_rdiscount__get_flags(self);
20
+
21
+ MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags);
22
+ markdown(doc, stream, flags);
23
+
24
+ fclose(stream);
25
+
26
+ return buf;
27
+ }
28
+
29
+ static VALUE
30
+ rb_rdiscount_toc_content(int argc, VALUE *argv, VALUE self)
31
+ {
32
+ int flags = rb_rdiscount__get_flags(self);
33
+
34
+ /* grab char pointer to markdown input text */
35
+ VALUE text = rb_funcall(self, rb_intern("text"), 0);
36
+ Check_Type(text, T_STRING);
37
+
38
+ /* allocate a ruby string buffer and wrap it in a stream */
39
+ VALUE buf = rb_str_buf_new(4096);
40
+ FILE *stream = rb_str_io_new(buf);
41
+
42
+ MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags);
43
+ mkd_compile(doc, flags);
44
+ mkd_generatetoc(doc, stream);
45
+
46
+ fclose(stream);
47
+
48
+ return buf;
49
+ }
50
+
51
+ int rb_rdiscount__get_flags(VALUE ruby_obj)
52
+ {
53
+ /* compile flags */
54
+ int flags = MKD_TABSTOP | MKD_NOHEADER;
55
+
56
+ /* smart */
57
+ if ( rb_funcall(ruby_obj, rb_intern("smart"), 0) != Qtrue )
58
+ flags = flags | MKD_NOPANTS;
59
+
60
+ /* filter_html */
61
+ if ( rb_funcall(ruby_obj, rb_intern("filter_html"), 0) == Qtrue )
62
+ flags = flags | MKD_NOHTML;
63
+
64
+ /* generate_toc */
65
+ if ( rb_funcall(ruby_obj, rb_intern("generate_toc"), 0) == Qtrue)
66
+ flags = flags | MKD_TOC;
67
+
68
+ return flags;
69
+ }
70
+
71
+
72
+ void Init_rdiscount()
73
+ {
74
+ rb_cRDiscount = rb_define_class("RDiscount", rb_cObject);
75
+ rb_define_method(rb_cRDiscount, "to_html", rb_rdiscount_to_html, -1);
76
+ rb_define_method(rb_cRDiscount, "toc_content", rb_rdiscount_toc_content, -1);
77
+ }
78
+
79
+ /* vim: set ts=4 sw=4: */