rdiscount-dsc 1.6.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,100 @@
1
+ #ifndef _MKDIO_D
2
+ #define _MKDIO_D
3
+
4
+ #include <stdio.h>
5
+
6
+ typedef void MMIOT;
7
+
8
+ typedef unsigned int mkd_flag_t;
9
+
10
+ /* line builder for markdown()
11
+ */
12
+ MMIOT *mkd_in(FILE*,mkd_flag_t); /* assemble input from a file */
13
+ MMIOT *mkd_string(char*,int,mkd_flag_t); /* assemble input from a buffer */
14
+
15
+ void mkd_basename(MMIOT*,char*);
16
+
17
+ void mkd_initialize();
18
+ void mkd_shlib_destructor();
19
+
20
+ /* compilation, debugging, cleanup
21
+ */
22
+ int mkd_compile(MMIOT*, mkd_flag_t);
23
+ int mkd_cleanup(MMIOT*);
24
+
25
+ /* markup functions
26
+ */
27
+ int mkd_dump(MMIOT*, FILE*, int, char*);
28
+ int markdown(MMIOT*, FILE*, mkd_flag_t);
29
+ int mkd_line(char *, int, char **, mkd_flag_t);
30
+ void mkd_string_to_anchor(char *, int, int (*)(int,void*), void*, int);
31
+ int mkd_xhtmlpage(MMIOT*,int,FILE*);
32
+
33
+ /* header block access
34
+ */
35
+ char* mkd_doc_title(MMIOT*);
36
+ char* mkd_doc_author(MMIOT*);
37
+ char* mkd_doc_date(MMIOT*);
38
+
39
+ /* compiled data access
40
+ */
41
+ int mkd_document(MMIOT*, char**);
42
+ int mkd_toc(MMIOT*, char**);
43
+ int mkd_css(MMIOT*, char **);
44
+ int mkd_xml(char *, int, char **);
45
+
46
+ /* write-to-file functions
47
+ */
48
+ int mkd_generatehtml(MMIOT*,FILE*);
49
+ int mkd_generatetoc(MMIOT*,FILE*);
50
+ int mkd_generatexml(char *, int,FILE*);
51
+ int mkd_generatecss(MMIOT*,FILE*);
52
+ #define mkd_style mkd_generatecss
53
+ int mkd_generateline(char *, int, FILE*, mkd_flag_t);
54
+ #define mkd_text mkd_generateline
55
+
56
+ /* url generator callbacks
57
+ */
58
+ typedef char * (*mkd_callback_t)(const char*, const int, void*);
59
+ typedef void (*mkd_free_t)(char*, void*);
60
+
61
+ void mkd_e_url(void *, mkd_callback_t);
62
+ void mkd_e_flags(void *, mkd_callback_t);
63
+ void mkd_e_free(void *, mkd_free_t );
64
+ void mkd_e_data(void *, void *);
65
+
66
+ /* version#.
67
+ */
68
+ extern char markdown_version[];
69
+
70
+ /* special flags for markdown() and mkd_text()
71
+ */
72
+ #define MKD_NOLINKS 0x00000001 /* don't do link processing, block <a> tags */
73
+ #define MKD_NOIMAGE 0x00000002 /* don't do image processing, block <img> */
74
+ #define MKD_NOPANTS 0x00000004 /* don't run smartypants() */
75
+ #define MKD_NOHTML 0x00000008 /* don't allow raw html through AT ALL */
76
+ #define MKD_STRICT 0x00000010 /* disable SUPERSCRIPT, RELAXED_EMPHASIS */
77
+ #define MKD_TAGTEXT 0x00000020 /* process text inside an html tag; no
78
+ * <em>, no <bold>, no html or [] expansion */
79
+ #define MKD_NO_EXT 0x00000040 /* don't allow pseudo-protocols */
80
+ #define MKD_CDATA 0x00000080 /* generate code for xml ![CDATA[...]] */
81
+ #define MKD_NOSUPERSCRIPT 0x00000100 /* no A^B */
82
+ #define MKD_NORELAXED 0x00000200 /* emphasis happens /everywhere/ */
83
+ #define MKD_NOTABLES 0x00000400 /* disallow tables */
84
+ #define MKD_NOSTRIKETHROUGH 0x00000800 /* forbid ~~strikethrough~~ */
85
+ #define MKD_TOC 0x00001000 /* do table-of-contents processing */
86
+ #define MKD_1_COMPAT 0x00002000 /* compatibility with MarkdownTest_1.0 */
87
+ #define MKD_AUTOLINK 0x00004000 /* make http://foo.com link even without <>s */
88
+ #define MKD_SAFELINK 0x00008000 /* paranoid check for link protocol */
89
+ #define MKD_NOHEADER 0x00010000 /* don't process header blocks */
90
+ #define MKD_TABSTOP 0x00020000 /* expand tabs to 4 spaces */
91
+ #define MKD_NODIVQUOTE 0x00040000 /* forbid >%class% blocks */
92
+ #define MKD_NOALPHALIST 0x00080000 /* forbid alphabetic lists */
93
+ #define MKD_NODLIST 0x00100000 /* forbid definition lists */
94
+ #define MKD_EMBED MKD_NOLINKS|MKD_NOIMAGE|MKD_TAGTEXT
95
+
96
+ /* special flags for mkd_in() and mkd_string()
97
+ */
98
+
99
+
100
+ #endif/*_MKDIO_D*/
@@ -0,0 +1,106 @@
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 encoding;
14
+ VALUE text = rb_funcall(self, rb_intern("text"), 0);
15
+ VALUE buf = rb_str_buf_new(1024);
16
+ Check_Type(text, T_STRING);
17
+
18
+ int flags = rb_rdiscount__get_flags(self);
19
+
20
+ MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags);
21
+
22
+ if ( mkd_compile(doc, flags) ) {
23
+ szres = mkd_document(doc, &res);
24
+
25
+ if ( szres != EOF ) {
26
+ rb_str_cat(buf, res, szres);
27
+ rb_str_cat(buf, "\n", 1);
28
+ }
29
+ }
30
+ mkd_cleanup(doc);
31
+
32
+
33
+ /* force the input encoding */
34
+ if ( rb_respond_to(text, rb_intern("encoding")) ) {
35
+ encoding = rb_funcall(text, rb_intern("encoding"), 0);
36
+ rb_funcall(buf, rb_intern("force_encoding"), 1, encoding);
37
+ }
38
+
39
+ return buf;
40
+ }
41
+
42
+ static VALUE
43
+ rb_rdiscount_toc_content(int argc, VALUE *argv, VALUE self)
44
+ {
45
+ char *res;
46
+ int szres;
47
+
48
+ int flags = rb_rdiscount__get_flags(self);
49
+
50
+ /* grab char pointer to markdown input text */
51
+ VALUE text = rb_funcall(self, rb_intern("text"), 0);
52
+ Check_Type(text, T_STRING);
53
+
54
+ /* allocate a ruby string buffer and wrap it in a stream */
55
+ VALUE buf = rb_str_buf_new(4096);
56
+
57
+ MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags);
58
+
59
+ if ( mkd_compile(doc, flags) ) {
60
+ szres = mkd_toc(doc, &res);
61
+
62
+ if ( szres != EOF ) {
63
+ rb_str_cat(buf, res, szres);
64
+ rb_str_cat(buf, "\n", 1);
65
+ }
66
+ }
67
+ mkd_cleanup(doc);
68
+
69
+ return buf;
70
+ }
71
+
72
+ int rb_rdiscount__get_flags(VALUE ruby_obj)
73
+ {
74
+ VALUE flags = rb_funcall(ruby_obj, rb_intern("flags"), 0);
75
+ return NUM2INT(rb_funcall(flags, rb_intern("to_i"), 0));
76
+ }
77
+
78
+ void Init_rdiscount()
79
+ {
80
+ rb_cRDiscount = rb_define_class("RDiscount", rb_cObject);
81
+ rb_define_const(rb_cRDiscount, "MKD_NOLINKS", INT2NUM(MKD_NOLINKS));
82
+ rb_define_const(rb_cRDiscount, "MKD_NOIMAGE", INT2NUM(MKD_NOIMAGE));
83
+ rb_define_const(rb_cRDiscount, "MKD_NOPANTS", INT2NUM(MKD_NOPANTS));
84
+ rb_define_const(rb_cRDiscount, "MKD_NOHTML", INT2NUM(MKD_NOHTML));
85
+ rb_define_const(rb_cRDiscount, "MKD_STRICT", INT2NUM(MKD_STRICT));
86
+ rb_define_const(rb_cRDiscount, "MKD_TAGTEXT", INT2NUM(MKD_TAGTEXT));
87
+ rb_define_const(rb_cRDiscount, "MKD_NO_EXT", INT2NUM(MKD_NO_EXT));
88
+ rb_define_const(rb_cRDiscount, "MKD_CDATA", INT2NUM(MKD_CDATA));
89
+ rb_define_const(rb_cRDiscount, "MKD_NOSUPERSCRIPT", INT2NUM(MKD_NOSUPERSCRIPT));
90
+ rb_define_const(rb_cRDiscount, "MKD_NORELAXED", INT2NUM(MKD_NORELAXED));
91
+ rb_define_const(rb_cRDiscount, "MKD_NOTABLES", INT2NUM(MKD_NOTABLES));
92
+ rb_define_const(rb_cRDiscount, "MKD_NOSTRIKETHROUGH", INT2NUM(MKD_NOSTRIKETHROUGH));
93
+ rb_define_const(rb_cRDiscount, "MKD_TOC", INT2NUM(MKD_TOC));
94
+ rb_define_const(rb_cRDiscount, "MKD_1_COMPAT", INT2NUM(MKD_1_COMPAT));
95
+ rb_define_const(rb_cRDiscount, "MKD_AUTOLINK", INT2NUM(MKD_AUTOLINK));
96
+ rb_define_const(rb_cRDiscount, "MKD_SAFELINK", INT2NUM(MKD_SAFELINK));
97
+ rb_define_const(rb_cRDiscount, "MKD_NOHEADER", INT2NUM(MKD_NOHEADER));
98
+ rb_define_const(rb_cRDiscount, "MKD_TABSTOP", INT2NUM(MKD_TABSTOP));
99
+ rb_define_const(rb_cRDiscount, "MKD_NODIVQUOTE", INT2NUM(MKD_NODIVQUOTE));
100
+ rb_define_const(rb_cRDiscount, "MKD_NOALPHALIST", INT2NUM(MKD_NOALPHALIST));
101
+ rb_define_const(rb_cRDiscount, "MKD_NODLIST", INT2NUM(MKD_NODLIST));
102
+ rb_define_method(rb_cRDiscount, "to_html", rb_rdiscount_to_html, -1);
103
+ rb_define_method(rb_cRDiscount, "toc_content", rb_rdiscount_toc_content, -1);
104
+ }
105
+
106
+ /* vim: set ts=4 sw=4: */
@@ -0,0 +1,157 @@
1
+ /* markdown: a C implementation of John Gruber's Markdown markup language.
2
+ *
3
+ * Copyright (C) 2007 David L Parsons.
4
+ * The redistribution terms are provided in the COPYRIGHT file that must
5
+ * be distributed with this source code.
6
+ */
7
+ #include <stdio.h>
8
+ #include <string.h>
9
+ #include <stdarg.h>
10
+ #include <stdlib.h>
11
+ #include <time.h>
12
+ #include <ctype.h>
13
+
14
+ #include "config.h"
15
+
16
+ #include "cstring.h"
17
+ #include "markdown.h"
18
+ #include "amalloc.h"
19
+
20
+ /* free a (single) line
21
+ */
22
+ void
23
+ ___mkd_freeLine(Line *ptr)
24
+ {
25
+ DELETE(ptr->text);
26
+ free(ptr);
27
+ }
28
+
29
+
30
+ /* free a list of lines
31
+ */
32
+ void
33
+ ___mkd_freeLines(Line *p)
34
+ {
35
+ if (p->next)
36
+ ___mkd_freeLines(p->next);
37
+ ___mkd_freeLine(p);
38
+ }
39
+
40
+
41
+ /* bye bye paragraph.
42
+ */
43
+ void
44
+ ___mkd_freeParagraph(Paragraph *p)
45
+ {
46
+ if (p->next)
47
+ ___mkd_freeParagraph(p->next);
48
+ if (p->down)
49
+ ___mkd_freeParagraph(p->down);
50
+ if (p->text)
51
+ ___mkd_freeLines(p->text);
52
+ if (p->ident)
53
+ free(p->ident);
54
+ free(p);
55
+ }
56
+
57
+
58
+ /* bye bye footnote.
59
+ */
60
+ void
61
+ ___mkd_freefootnote(Footnote *f)
62
+ {
63
+ DELETE(f->tag);
64
+ DELETE(f->link);
65
+ DELETE(f->title);
66
+ }
67
+
68
+
69
+ /* bye bye footnotes.
70
+ */
71
+ void
72
+ ___mkd_freefootnotes(MMIOT *f)
73
+ {
74
+ int i;
75
+
76
+ if ( f->footnotes ) {
77
+ for (i=0; i < S(*f->footnotes); i++)
78
+ ___mkd_freefootnote( &T(*f->footnotes)[i] );
79
+ DELETE(*f->footnotes);
80
+ free(f->footnotes);
81
+ }
82
+ }
83
+
84
+
85
+ /* initialize a new MMIOT
86
+ */
87
+ void
88
+ ___mkd_initmmiot(MMIOT *f, void *footnotes)
89
+ {
90
+ if ( f ) {
91
+ memset(f, 0, sizeof *f);
92
+ CREATE(f->in);
93
+ CREATE(f->out);
94
+ CREATE(f->Q);
95
+ if ( footnotes )
96
+ f->footnotes = footnotes;
97
+ else {
98
+ f->footnotes = malloc(sizeof f->footnotes[0]);
99
+ CREATE(*f->footnotes);
100
+ }
101
+ }
102
+ }
103
+
104
+
105
+ /* free the contents of a MMIOT, but leave the object alone.
106
+ */
107
+ void
108
+ ___mkd_freemmiot(MMIOT *f, void *footnotes)
109
+ {
110
+ if ( f ) {
111
+ DELETE(f->in);
112
+ DELETE(f->out);
113
+ DELETE(f->Q);
114
+ if ( f->footnotes != footnotes )
115
+ ___mkd_freefootnotes(f);
116
+ memset(f, 0, sizeof *f);
117
+ }
118
+ }
119
+
120
+
121
+ /* free lines up to an barrier.
122
+ */
123
+ void
124
+ ___mkd_freeLineRange(Line *anchor, Line *stop)
125
+ {
126
+ Line *r = anchor->next;
127
+
128
+ if ( r != stop ) {
129
+ while ( r && (r->next != stop) )
130
+ r = r->next;
131
+ if ( r ) r->next = 0;
132
+ ___mkd_freeLines(anchor->next);
133
+ }
134
+ anchor->next = 0;
135
+ }
136
+
137
+
138
+ /* clean up everything allocated in __mkd_compile()
139
+ */
140
+ void
141
+ mkd_cleanup(Document *doc)
142
+ {
143
+ if ( doc && (doc->magic == VALID_DOCUMENT) ) {
144
+ if ( doc->ctx ) {
145
+ ___mkd_freemmiot(doc->ctx, 0);
146
+ free(doc->ctx);
147
+ }
148
+
149
+ if ( doc->code) ___mkd_freeParagraph(doc->code);
150
+ if ( doc->title) ___mkd_freeLine(doc->title);
151
+ if ( doc->author) ___mkd_freeLine(doc->author);
152
+ if ( doc->date) ___mkd_freeLine(doc->date);
153
+ if ( T(doc->content) ) ___mkd_freeLines(T(doc->content));
154
+ memset(doc, 0, sizeof doc[0]);
155
+ free(doc);
156
+ }
157
+ }
@@ -0,0 +1,123 @@
1
+ /* block-level tags for passing html blocks through the blender
2
+ */
3
+ #define __WITHOUT_AMALLOC 1
4
+ #include "cstring.h"
5
+ #include "tags.h"
6
+
7
+ STRING(struct kw) blocktags;
8
+
9
+
10
+ /* define a html block tag
11
+ */
12
+ void
13
+ mkd_define_tag(char *id, int selfclose)
14
+ {
15
+ struct kw *p = &EXPAND(blocktags);
16
+
17
+ p->id = id;
18
+ p->size = strlen(id);
19
+ p->selfclose = selfclose;
20
+ }
21
+
22
+
23
+ /* case insensitive string sort (for qsort() and bsearch() of block tags)
24
+ */
25
+ static int
26
+ casort(struct kw *a, struct kw *b)
27
+ {
28
+ if ( a->size != b->size )
29
+ return a->size - b->size;
30
+ return strncasecmp(a->id, b->id, b->size);
31
+ }
32
+
33
+
34
+ /* stupid cast to make gcc shut up about the function types being
35
+ * passed into qsort() and bsearch()
36
+ */
37
+ typedef int (*stfu)(const void*,const void*);
38
+
39
+
40
+ /* sort the list of html block tags for later searching
41
+ */
42
+ void
43
+ mkd_sort_tags()
44
+ {
45
+ qsort(T(blocktags), S(blocktags), sizeof(struct kw), (stfu)casort);
46
+ }
47
+
48
+
49
+
50
+ /* look for a token in the html block tag list
51
+ */
52
+ struct kw*
53
+ mkd_search_tags(char *pat, int len)
54
+ {
55
+ struct kw key;
56
+
57
+ key.id = pat;
58
+ key.size = len;
59
+
60
+ return bsearch(&key, T(blocktags), S(blocktags), sizeof key, (stfu)casort);
61
+ }
62
+
63
+
64
+ static int populated = 0;
65
+
66
+
67
+ /* load in the standard collection of html tags that markdown supports
68
+ */
69
+ void
70
+ mkd_prepare_tags()
71
+ {
72
+
73
+ #define KW(x) mkd_define_tag(x, 0)
74
+ #define SC(x) mkd_define_tag(x, 1)
75
+
76
+ if ( populated ) return;
77
+ populated = 1;
78
+
79
+ KW("STYLE");
80
+ KW("SCRIPT");
81
+ KW("ADDRESS");
82
+ KW("BDO");
83
+ KW("BLOCKQUOTE");
84
+ KW("CENTER");
85
+ KW("DFN");
86
+ KW("DIV");
87
+ KW("OBJECT");
88
+ KW("H1");
89
+ KW("H2");
90
+ KW("H3");
91
+ KW("H4");
92
+ KW("H5");
93
+ KW("H6");
94
+ KW("LISTING");
95
+ KW("NOBR");
96
+ KW("UL");
97
+ KW("P");
98
+ KW("OL");
99
+ KW("DL");
100
+ KW("PLAINTEXT");
101
+ KW("PRE");
102
+ KW("TABLE");
103
+ KW("WBR");
104
+ KW("XMP");
105
+ SC("HR");
106
+ SC("BR");
107
+ KW("IFRAME");
108
+ KW("MAP");
109
+
110
+ mkd_sort_tags();
111
+ } /* mkd_prepare_tags */
112
+
113
+
114
+ /* destroy the blocktags list (for shared libraries)
115
+ */
116
+ void
117
+ mkd_deallocate_tags()
118
+ {
119
+ if ( S(blocktags) > 0 ) {
120
+ populated = 0;
121
+ DELETE(blocktags);
122
+ }
123
+ } /* mkd_deallocate_tags */