rdiscountwl 1.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.
- checksums.yaml +7 -0
- data/BUILDING +151 -0
- data/COPYING +33 -0
- data/README.markdown +73 -0
- data/Rakefile +224 -0
- data/bin/rdiscount +13 -0
- data/ext/Csio.c +61 -0
- data/ext/VERSION +1 -0
- data/ext/amalloc.c +135 -0
- data/ext/amalloc.h +29 -0
- data/ext/basename.c +43 -0
- data/ext/blocktags +33 -0
- data/ext/config.h +25 -0
- data/ext/css.c +87 -0
- data/ext/cstring.h +77 -0
- data/ext/docheader.c +49 -0
- data/ext/dumptree.c +151 -0
- data/ext/emmatch.c +188 -0
- data/ext/extconf.rb +49 -0
- data/ext/flags.c +91 -0
- data/ext/generate.c +1865 -0
- data/ext/github_flavoured.c +100 -0
- data/ext/html5.c +22 -0
- data/ext/markdown.c +1361 -0
- data/ext/markdown.h +238 -0
- data/ext/mkdio.c +360 -0
- data/ext/mkdio.h +122 -0
- data/ext/mktags.c +89 -0
- data/ext/pgm_options.c +146 -0
- data/ext/pgm_options.h +9 -0
- data/ext/rdiscount.c +150 -0
- data/ext/resource.c +159 -0
- data/ext/setup.c +39 -0
- data/ext/tags.c +94 -0
- data/ext/tags.h +19 -0
- data/ext/toc.c +114 -0
- data/ext/version.c +13 -0
- data/ext/xml.c +82 -0
- data/ext/xmlpage.c +46 -0
- data/lib/markdown.rb +1 -0
- data/lib/rdiscount.rb +108 -0
- data/man/markdown.7 +1020 -0
- data/man/rdiscount.1 +22 -0
- data/man/rdiscount.1.ronn +24 -0
- data/rdiscount.gemspec +71 -0
- data/test/benchmark.rb +56 -0
- data/test/benchmark.txt +306 -0
- data/test/markdown_test.rb +158 -0
- data/test/rdiscount_test.rb +276 -0
- metadata +99 -0
data/ext/markdown.h
ADDED
@@ -0,0 +1,238 @@
|
|
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
|
+
int dealloc; /* deallocation needed? */
|
15
|
+
int refnumber;
|
16
|
+
int flags;
|
17
|
+
#define EXTRA_BOOKMARK 0x01
|
18
|
+
#define REFERENCED 0x02
|
19
|
+
} Footnote;
|
20
|
+
|
21
|
+
/* each input line is read into a Line, which contains the line,
|
22
|
+
* the offset of the first non-space character [this assumes
|
23
|
+
* that all tabs will be expanded to spaces!], and a pointer to
|
24
|
+
* the next line.
|
25
|
+
*/
|
26
|
+
typedef enum { chk_text, chk_code,
|
27
|
+
chk_hr, chk_dash,
|
28
|
+
chk_tilde, chk_backtick,
|
29
|
+
chk_equal } line_type;
|
30
|
+
typedef struct line {
|
31
|
+
Cstring text;
|
32
|
+
struct line *next;
|
33
|
+
int dle; /* leading indent on the line */
|
34
|
+
int flags; /* special attributes for this line */
|
35
|
+
#define PIPECHAR 0x01 /* line contains a | */
|
36
|
+
#define CHECKED 0x02
|
37
|
+
|
38
|
+
line_type kind;
|
39
|
+
int count;
|
40
|
+
} Line;
|
41
|
+
|
42
|
+
|
43
|
+
/* a paragraph is a collection of Lines, with links to the next paragraph
|
44
|
+
* and (if it's a QUOTE, UL, or OL) to the reparsed contents of this
|
45
|
+
* paragraph.
|
46
|
+
*/
|
47
|
+
typedef struct paragraph {
|
48
|
+
struct paragraph *next; /* next paragraph */
|
49
|
+
struct paragraph *down; /* recompiled contents of this paragraph */
|
50
|
+
struct line *text; /* all the text in this paragraph */
|
51
|
+
char *ident; /* %id% tag for QUOTE */
|
52
|
+
char *lang; /* lang attribute for CODE */
|
53
|
+
enum { WHITESPACE=0, CODE, QUOTE, MARKUP,
|
54
|
+
HTML, STYLE, DL, UL, OL, AL, LISTITEM,
|
55
|
+
HDR, HR, TABLE, SOURCE } typ;
|
56
|
+
enum { IMPLICIT=0, PARA, CENTER} align;
|
57
|
+
int hnumber; /* <Hn> for typ == HDR */
|
58
|
+
} Paragraph;
|
59
|
+
|
60
|
+
enum { ETX, SETEXT }; /* header types */
|
61
|
+
|
62
|
+
|
63
|
+
typedef struct block {
|
64
|
+
enum { bTEXT, bSTAR, bUNDER } b_type;
|
65
|
+
int b_count;
|
66
|
+
char b_char;
|
67
|
+
Cstring b_text;
|
68
|
+
Cstring b_post;
|
69
|
+
} block;
|
70
|
+
|
71
|
+
typedef STRING(block) Qblock;
|
72
|
+
|
73
|
+
|
74
|
+
typedef char* (*mkd_callback_t)(const char*, const int, void*);
|
75
|
+
typedef void (*mkd_free_t)(char*, void*);
|
76
|
+
|
77
|
+
typedef struct callback_data {
|
78
|
+
void *e_data; /* private data for callbacks */
|
79
|
+
mkd_callback_t e_url; /* url edit callback */
|
80
|
+
mkd_callback_t e_flags; /* extra href flags callback */
|
81
|
+
mkd_free_t e_free; /* edit/flags callback memory deallocator */
|
82
|
+
} Callback_data;
|
83
|
+
|
84
|
+
|
85
|
+
struct escaped {
|
86
|
+
char *text;
|
87
|
+
struct escaped *up;
|
88
|
+
} ;
|
89
|
+
|
90
|
+
|
91
|
+
struct footnote_list {
|
92
|
+
int reference;
|
93
|
+
STRING(Footnote) note;
|
94
|
+
} ;
|
95
|
+
|
96
|
+
|
97
|
+
/* a magic markdown io thing holds all the data structures needed to
|
98
|
+
* do the backend processing of a markdown document
|
99
|
+
*/
|
100
|
+
typedef struct mmiot {
|
101
|
+
Cstring out;
|
102
|
+
Cstring in;
|
103
|
+
Qblock Q;
|
104
|
+
int isp;
|
105
|
+
struct escaped *esc;
|
106
|
+
char *ref_prefix;
|
107
|
+
struct footnote_list *footnotes;
|
108
|
+
DWORD flags;
|
109
|
+
#define MKD_NOLINKS 0x00000001
|
110
|
+
#define MKD_NOIMAGE 0x00000002
|
111
|
+
#define MKD_NOPANTS 0x00000004
|
112
|
+
#define MKD_NOHTML 0x00000008
|
113
|
+
#define MKD_STRICT 0x00000010
|
114
|
+
#define MKD_TAGTEXT 0x00000020
|
115
|
+
#define MKD_NO_EXT 0x00000040
|
116
|
+
#define MKD_CDATA 0x00000080
|
117
|
+
#define MKD_NOSUPERSCRIPT 0x00000100
|
118
|
+
#define MKD_NORELAXED 0x00000200
|
119
|
+
#define MKD_NOTABLES 0x00000400
|
120
|
+
#define MKD_NOSTRIKETHROUGH 0x00000800
|
121
|
+
#define MKD_TOC 0x00001000
|
122
|
+
#define MKD_1_COMPAT 0x00002000
|
123
|
+
#define MKD_AUTOLINK 0x00004000
|
124
|
+
#define MKD_SAFELINK 0x00008000
|
125
|
+
#define MKD_NOHEADER 0x00010000
|
126
|
+
#define MKD_TABSTOP 0x00020000
|
127
|
+
#define MKD_NODIVQUOTE 0x00040000
|
128
|
+
#define MKD_NOALPHALIST 0x00080000
|
129
|
+
#define MKD_NODLIST 0x00100000
|
130
|
+
#define MKD_EXTRA_FOOTNOTE 0x00200000
|
131
|
+
#define MKD_NOSTYLE 0x00400000
|
132
|
+
#define MKD_NODLDISCOUNT 0x00800000
|
133
|
+
#define MKD_DLEXTRA 0x01000000
|
134
|
+
#define MKD_FENCEDCODE 0x02000000
|
135
|
+
#define MKD_IDANCHOR 0x04000000
|
136
|
+
#define MKD_GITHUBTAGS 0x08000000
|
137
|
+
#define MKD_URLENCODEDANCHOR 0x10000000
|
138
|
+
#define IS_LABEL 0x20000000
|
139
|
+
#define USER_FLAGS 0x3FFFFFFF
|
140
|
+
#define INPUT_MASK (MKD_NOHEADER|MKD_TABSTOP)
|
141
|
+
|
142
|
+
Callback_data *cb;
|
143
|
+
} MMIOT;
|
144
|
+
|
145
|
+
|
146
|
+
#define MKD_EOLN 3
|
147
|
+
|
148
|
+
|
149
|
+
/*
|
150
|
+
* the mkdio text input functions return a document structure,
|
151
|
+
* which contains a header (retrieved from the document if
|
152
|
+
* markdown was configured * with the * --enable-pandoc-header
|
153
|
+
* and the document begins with a pandoc-style header) and the
|
154
|
+
* root of the linked list of Lines.
|
155
|
+
*/
|
156
|
+
typedef struct document {
|
157
|
+
int magic; /* "I AM VALID" magic number */
|
158
|
+
#define VALID_DOCUMENT 0x19600731
|
159
|
+
Line *title;
|
160
|
+
Line *author;
|
161
|
+
Line *date;
|
162
|
+
ANCHOR(Line) content; /* uncompiled text, not valid after compile() */
|
163
|
+
Paragraph *code; /* intermediate code generated by compile() */
|
164
|
+
int compiled; /* set after mkd_compile() */
|
165
|
+
int html; /* set after (internal) htmlify() */
|
166
|
+
int tabstop; /* for properly expanding tabs (ick) */
|
167
|
+
char *ref_prefix;
|
168
|
+
MMIOT *ctx; /* backend buffers, flags, and structures */
|
169
|
+
Callback_data cb; /* callback functions & private data */
|
170
|
+
} Document;
|
171
|
+
|
172
|
+
|
173
|
+
/*
|
174
|
+
* economy FILE-type structure for pulling characters out of a
|
175
|
+
* fixed-length string.
|
176
|
+
*/
|
177
|
+
struct string_stream {
|
178
|
+
const char *data; /* the unread data */
|
179
|
+
int size; /* and how much is there? */
|
180
|
+
} ;
|
181
|
+
|
182
|
+
|
183
|
+
extern int mkd_firstnonblank(Line *);
|
184
|
+
extern int mkd_compile(Document *, DWORD);
|
185
|
+
extern int mkd_document(Document *, char **);
|
186
|
+
extern int mkd_generatehtml(Document *, FILE *);
|
187
|
+
extern int mkd_css(Document *, char **);
|
188
|
+
extern int mkd_generatecss(Document *, FILE *);
|
189
|
+
#define mkd_style mkd_generatecss
|
190
|
+
extern int mkd_xml(char *, int , char **);
|
191
|
+
extern int mkd_generatexml(char *, int, FILE *);
|
192
|
+
extern void mkd_cleanup(Document *);
|
193
|
+
extern int mkd_line(char *, int, char **, DWORD);
|
194
|
+
extern int mkd_generateline(char *, int, FILE*, DWORD);
|
195
|
+
#define mkd_text mkd_generateline
|
196
|
+
extern void mkd_basename(Document*, char *);
|
197
|
+
|
198
|
+
typedef int (*mkd_sta_function_t)(const int,const void*);
|
199
|
+
extern void mkd_string_to_anchor(char*,int, mkd_sta_function_t, void*, int, DWORD);
|
200
|
+
|
201
|
+
extern Document *mkd_in(FILE *, DWORD);
|
202
|
+
extern Document *mkd_string(const char*,int, DWORD);
|
203
|
+
|
204
|
+
extern Document *gfm_in(FILE *, DWORD);
|
205
|
+
extern Document *gfm_string(const char*,int, DWORD);
|
206
|
+
|
207
|
+
extern void mkd_initialize();
|
208
|
+
extern void mkd_shlib_destructor();
|
209
|
+
|
210
|
+
extern void mkd_ref_prefix(Document*, char*);
|
211
|
+
|
212
|
+
/* internal resource handling functions.
|
213
|
+
*/
|
214
|
+
extern void ___mkd_freeLine(Line *);
|
215
|
+
extern void ___mkd_freeLines(Line *);
|
216
|
+
extern void ___mkd_freeParagraph(Paragraph *);
|
217
|
+
extern void ___mkd_freefootnote(Footnote *);
|
218
|
+
extern void ___mkd_freefootnotes(MMIOT *);
|
219
|
+
extern void ___mkd_initmmiot(MMIOT *, void *);
|
220
|
+
extern void ___mkd_freemmiot(MMIOT *, void *);
|
221
|
+
extern void ___mkd_freeLineRange(Line *, Line *);
|
222
|
+
extern void ___mkd_xml(char *, int, FILE *);
|
223
|
+
extern void ___mkd_reparse(char *, int, int, MMIOT*, char*);
|
224
|
+
extern void ___mkd_emblock(MMIOT*);
|
225
|
+
extern void ___mkd_tidy(Cstring *);
|
226
|
+
|
227
|
+
extern Document *__mkd_new_Document();
|
228
|
+
extern void __mkd_enqueue(Document*, Cstring *);
|
229
|
+
extern void __mkd_header_dle(Line *);
|
230
|
+
|
231
|
+
extern int __mkd_io_strget(struct string_stream *);
|
232
|
+
|
233
|
+
/* utility function to do some operation and exit the current function
|
234
|
+
* if it fails
|
235
|
+
*/
|
236
|
+
#define DO_OR_DIE(op) if ( (op) == EOF ) return EOF; else 1
|
237
|
+
|
238
|
+
#endif/*_MARKDOWN_D*/
|
data/ext/mkdio.c
ADDED
@@ -0,0 +1,360 @@
|
|
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
|
+
|
20
|
+
/* create a new blank Document
|
21
|
+
*/
|
22
|
+
Document*
|
23
|
+
__mkd_new_Document()
|
24
|
+
{
|
25
|
+
Document *ret = calloc(sizeof(Document), 1);
|
26
|
+
|
27
|
+
if ( ret ) {
|
28
|
+
if ( ret->ctx = calloc(sizeof(MMIOT), 1) ) {
|
29
|
+
ret->magic = VALID_DOCUMENT;
|
30
|
+
return ret;
|
31
|
+
}
|
32
|
+
free(ret);
|
33
|
+
}
|
34
|
+
return 0;
|
35
|
+
}
|
36
|
+
|
37
|
+
|
38
|
+
/* add a line to the markdown input chain, expanding tabs and
|
39
|
+
* noting the presence of special characters as we go.
|
40
|
+
*/
|
41
|
+
void
|
42
|
+
__mkd_enqueue(Document* a, Cstring *line)
|
43
|
+
{
|
44
|
+
Line *p = calloc(sizeof *p, 1);
|
45
|
+
unsigned char c;
|
46
|
+
int xp = 0;
|
47
|
+
int size = S(*line);
|
48
|
+
unsigned char *str = (unsigned char*)T(*line);
|
49
|
+
|
50
|
+
CREATE(p->text);
|
51
|
+
ATTACH(a->content, p);
|
52
|
+
|
53
|
+
while ( size-- ) {
|
54
|
+
if ( (c = *str++) == '\t' ) {
|
55
|
+
/* expand tabs into ->tabstop spaces. We use ->tabstop
|
56
|
+
* because the ENTIRE FREAKING COMPUTER WORLD uses editors
|
57
|
+
* that don't do ^T/^D, but instead use tabs for indentation,
|
58
|
+
* and, of course, set their tabs down to 4 spaces
|
59
|
+
*/
|
60
|
+
do {
|
61
|
+
EXPAND(p->text) = ' ';
|
62
|
+
} while ( ++xp % a->tabstop );
|
63
|
+
}
|
64
|
+
else if ( c >= ' ' ) {
|
65
|
+
if ( c == '|' )
|
66
|
+
p->flags |= PIPECHAR;
|
67
|
+
EXPAND(p->text) = c;
|
68
|
+
++xp;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
EXPAND(p->text) = 0;
|
72
|
+
S(p->text)--;
|
73
|
+
p->dle = mkd_firstnonblank(p);
|
74
|
+
}
|
75
|
+
|
76
|
+
|
77
|
+
/* trim leading blanks from a header line
|
78
|
+
*/
|
79
|
+
void
|
80
|
+
__mkd_header_dle(Line *p)
|
81
|
+
{
|
82
|
+
CLIP(p->text, 0, 1);
|
83
|
+
p->dle = mkd_firstnonblank(p);
|
84
|
+
}
|
85
|
+
|
86
|
+
|
87
|
+
/* build a Document from any old input.
|
88
|
+
*/
|
89
|
+
typedef int (*getc_func)(void*);
|
90
|
+
|
91
|
+
Document *
|
92
|
+
populate(getc_func getc, void* ctx, int flags)
|
93
|
+
{
|
94
|
+
Cstring line;
|
95
|
+
Document *a = __mkd_new_Document();
|
96
|
+
int c;
|
97
|
+
int pandoc = 0;
|
98
|
+
|
99
|
+
if ( !a ) return 0;
|
100
|
+
|
101
|
+
a->tabstop = (flags & MKD_TABSTOP) ? 4 : TABSTOP;
|
102
|
+
|
103
|
+
CREATE(line);
|
104
|
+
|
105
|
+
while ( (c = (*getc)(ctx)) != EOF ) {
|
106
|
+
if ( c == '\n' ) {
|
107
|
+
if ( pandoc != EOF && pandoc < 3 ) {
|
108
|
+
if ( S(line) && (T(line)[0] == '%') )
|
109
|
+
pandoc++;
|
110
|
+
else
|
111
|
+
pandoc = EOF;
|
112
|
+
}
|
113
|
+
__mkd_enqueue(a, &line);
|
114
|
+
S(line) = 0;
|
115
|
+
}
|
116
|
+
else if ( isprint(c) || isspace(c) || (c & 0x80) )
|
117
|
+
EXPAND(line) = c;
|
118
|
+
}
|
119
|
+
|
120
|
+
if ( S(line) )
|
121
|
+
__mkd_enqueue(a, &line);
|
122
|
+
|
123
|
+
DELETE(line);
|
124
|
+
|
125
|
+
if ( (pandoc == 3) && !(flags & (MKD_NOHEADER|MKD_STRICT)) ) {
|
126
|
+
/* the first three lines started with %, so we have a header.
|
127
|
+
* clip the first three lines out of content and hang them
|
128
|
+
* off header.
|
129
|
+
*/
|
130
|
+
Line *headers = T(a->content);
|
131
|
+
|
132
|
+
a->title = headers; __mkd_header_dle(a->title);
|
133
|
+
a->author= headers->next; __mkd_header_dle(a->author);
|
134
|
+
a->date = headers->next->next; __mkd_header_dle(a->date);
|
135
|
+
|
136
|
+
T(a->content) = headers->next->next->next;
|
137
|
+
}
|
138
|
+
|
139
|
+
return a;
|
140
|
+
}
|
141
|
+
|
142
|
+
|
143
|
+
/* convert a file into a linked list
|
144
|
+
*/
|
145
|
+
Document *
|
146
|
+
mkd_in(FILE *f, DWORD flags)
|
147
|
+
{
|
148
|
+
return populate((getc_func)fgetc, f, flags & INPUT_MASK);
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
/* return a single character out of a buffer
|
153
|
+
*/
|
154
|
+
int
|
155
|
+
__mkd_io_strget(struct string_stream *in)
|
156
|
+
{
|
157
|
+
if ( !in->size ) return EOF;
|
158
|
+
|
159
|
+
--(in->size);
|
160
|
+
|
161
|
+
return *(in->data)++;
|
162
|
+
}
|
163
|
+
|
164
|
+
|
165
|
+
/* convert a block of text into a linked list
|
166
|
+
*/
|
167
|
+
Document *
|
168
|
+
mkd_string(const char *buf, int len, DWORD flags)
|
169
|
+
{
|
170
|
+
struct string_stream about;
|
171
|
+
|
172
|
+
about.data = buf;
|
173
|
+
about.size = len;
|
174
|
+
|
175
|
+
return populate((getc_func)__mkd_io_strget, &about, flags & INPUT_MASK);
|
176
|
+
}
|
177
|
+
|
178
|
+
|
179
|
+
/* write the html to a file (xmlified if necessary)
|
180
|
+
*/
|
181
|
+
int
|
182
|
+
mkd_generatehtml(Document *p, FILE *output)
|
183
|
+
{
|
184
|
+
char *doc;
|
185
|
+
int szdoc;
|
186
|
+
|
187
|
+
DO_OR_DIE( szdoc = mkd_document(p,&doc) );
|
188
|
+
if ( p->ctx->flags & MKD_CDATA )
|
189
|
+
DO_OR_DIE( mkd_generatexml(doc, szdoc, output) );
|
190
|
+
else if ( fwrite(doc, szdoc, 1, output) != 1 )
|
191
|
+
return EOF;
|
192
|
+
DO_OR_DIE( putc('\n', output) );
|
193
|
+
return 0;
|
194
|
+
}
|
195
|
+
|
196
|
+
|
197
|
+
/* convert some markdown text to html
|
198
|
+
*/
|
199
|
+
int
|
200
|
+
markdown(Document *document, FILE *out, int flags)
|
201
|
+
{
|
202
|
+
if ( mkd_compile(document, flags) ) {
|
203
|
+
mkd_generatehtml(document, out);
|
204
|
+
mkd_cleanup(document);
|
205
|
+
return 0;
|
206
|
+
}
|
207
|
+
return -1;
|
208
|
+
}
|
209
|
+
|
210
|
+
|
211
|
+
/* write out a Cstring, mangled into a form suitable for `<a href=` or `<a id=`
|
212
|
+
*/
|
213
|
+
void
|
214
|
+
mkd_string_to_anchor(char *s, int len, mkd_sta_function_t outchar,
|
215
|
+
void *out, int labelformat,
|
216
|
+
DWORD flags)
|
217
|
+
{
|
218
|
+
static const unsigned char hexchars[] = "0123456789abcdef";
|
219
|
+
unsigned char c;
|
220
|
+
|
221
|
+
int i, size;
|
222
|
+
char *line;
|
223
|
+
|
224
|
+
size = mkd_line(s, len, &line, IS_LABEL);
|
225
|
+
|
226
|
+
if ( !(flags & MKD_URLENCODEDANCHOR)
|
227
|
+
&& labelformat
|
228
|
+
&& (size>0) && !isalpha(line[0]) )
|
229
|
+
(*outchar)('L',out);
|
230
|
+
for ( i=0; i < size ; i++ ) {
|
231
|
+
c = line[i];
|
232
|
+
if ( labelformat ) {
|
233
|
+
if ( isalnum(c) || (c == '_') || (c == ':') || (c == '-') || (c == '.' ) )
|
234
|
+
(*outchar)(c, out);
|
235
|
+
else if ( flags & MKD_URLENCODEDANCHOR ) {
|
236
|
+
(*outchar)('%', out);
|
237
|
+
(*outchar)(hexchars[c >> 4 & 0xf], out);
|
238
|
+
(*outchar)(hexchars[c & 0xf], out);
|
239
|
+
}
|
240
|
+
else
|
241
|
+
(*outchar)('.', out);
|
242
|
+
}
|
243
|
+
else
|
244
|
+
(*outchar)(c,out);
|
245
|
+
}
|
246
|
+
|
247
|
+
if (line)
|
248
|
+
free(line);
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
/* ___mkd_reparse() a line
|
253
|
+
*/
|
254
|
+
static void
|
255
|
+
mkd_parse_line(char *bfr, int size, MMIOT *f, int flags)
|
256
|
+
{
|
257
|
+
___mkd_initmmiot(f, 0);
|
258
|
+
f->flags = flags & USER_FLAGS;
|
259
|
+
___mkd_reparse(bfr, size, 0, f, 0);
|
260
|
+
___mkd_emblock(f);
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
/* ___mkd_reparse() a line, returning it in malloc()ed memory
|
265
|
+
*/
|
266
|
+
int
|
267
|
+
mkd_line(char *bfr, int size, char **res, DWORD flags)
|
268
|
+
{
|
269
|
+
MMIOT f;
|
270
|
+
int len;
|
271
|
+
|
272
|
+
mkd_parse_line(bfr, size, &f, flags);
|
273
|
+
|
274
|
+
if ( len = S(f.out) ) {
|
275
|
+
/* kludge alert; we know that T(f.out) is malloced memory,
|
276
|
+
* so we can just steal it away. This is awful -- there
|
277
|
+
* should be an opaque method that transparently moves
|
278
|
+
* the pointer out of the embedded Cstring.
|
279
|
+
*/
|
280
|
+
EXPAND(f.out) = 0;
|
281
|
+
*res = T(f.out);
|
282
|
+
T(f.out) = 0;
|
283
|
+
S(f.out) = ALLOCATED(f.out) = 0;
|
284
|
+
}
|
285
|
+
else {
|
286
|
+
*res = 0;
|
287
|
+
len = EOF;
|
288
|
+
}
|
289
|
+
___mkd_freemmiot(&f, 0);
|
290
|
+
return len;
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
/* ___mkd_reparse() a line, writing it to a FILE
|
295
|
+
*/
|
296
|
+
int
|
297
|
+
mkd_generateline(char *bfr, int size, FILE *output, DWORD flags)
|
298
|
+
{
|
299
|
+
MMIOT f;
|
300
|
+
int status;
|
301
|
+
|
302
|
+
mkd_parse_line(bfr, size, &f, flags);
|
303
|
+
if ( flags & MKD_CDATA )
|
304
|
+
status = mkd_generatexml(T(f.out), S(f.out), output) != EOF;
|
305
|
+
else
|
306
|
+
status = fwrite(T(f.out), S(f.out), 1, output) == S(f.out);
|
307
|
+
|
308
|
+
___mkd_freemmiot(&f, 0);
|
309
|
+
return status ? 0 : EOF;
|
310
|
+
}
|
311
|
+
|
312
|
+
|
313
|
+
/* set the url display callback
|
314
|
+
*/
|
315
|
+
void
|
316
|
+
mkd_e_url(Document *f, mkd_callback_t edit)
|
317
|
+
{
|
318
|
+
if ( f )
|
319
|
+
f->cb.e_url = edit;
|
320
|
+
}
|
321
|
+
|
322
|
+
|
323
|
+
/* set the url options callback
|
324
|
+
*/
|
325
|
+
void
|
326
|
+
mkd_e_flags(Document *f, mkd_callback_t edit)
|
327
|
+
{
|
328
|
+
if ( f )
|
329
|
+
f->cb.e_flags = edit;
|
330
|
+
}
|
331
|
+
|
332
|
+
|
333
|
+
/* set the url display/options deallocator
|
334
|
+
*/
|
335
|
+
void
|
336
|
+
mkd_e_free(Document *f, mkd_free_t dealloc)
|
337
|
+
{
|
338
|
+
if ( f )
|
339
|
+
f->cb.e_free = dealloc;
|
340
|
+
}
|
341
|
+
|
342
|
+
|
343
|
+
/* set the url display/options context data field
|
344
|
+
*/
|
345
|
+
void
|
346
|
+
mkd_e_data(Document *f, void *data)
|
347
|
+
{
|
348
|
+
if ( f )
|
349
|
+
f->cb.e_data = data;
|
350
|
+
}
|
351
|
+
|
352
|
+
|
353
|
+
/* set the href prefix for markdown extra style footnotes
|
354
|
+
*/
|
355
|
+
void
|
356
|
+
mkd_ref_prefix(Document *f, char *data)
|
357
|
+
{
|
358
|
+
if ( f )
|
359
|
+
f->ref_prefix = data;
|
360
|
+
}
|
data/ext/mkdio.h
ADDED
@@ -0,0 +1,122 @@
|
|
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(const char*,int,mkd_flag_t); /* assemble input from a buffer */
|
14
|
+
|
15
|
+
/* line builder for github flavoured markdown
|
16
|
+
*/
|
17
|
+
MMIOT *gfm_in(FILE*,mkd_flag_t); /* assemble input from a file */
|
18
|
+
MMIOT *gfm_string(const char*,int,mkd_flag_t); /* assemble input from a buffer */
|
19
|
+
|
20
|
+
void mkd_basename(MMIOT*,char*);
|
21
|
+
|
22
|
+
void mkd_initialize();
|
23
|
+
void mkd_with_html5_tags();
|
24
|
+
void mkd_shlib_destructor();
|
25
|
+
|
26
|
+
/* compilation, debugging, cleanup
|
27
|
+
*/
|
28
|
+
int mkd_compile(MMIOT*, mkd_flag_t);
|
29
|
+
void mkd_cleanup(MMIOT*);
|
30
|
+
|
31
|
+
/* markup functions
|
32
|
+
*/
|
33
|
+
int mkd_dump(MMIOT*, FILE*, int, char*);
|
34
|
+
int markdown(MMIOT*, FILE*, mkd_flag_t);
|
35
|
+
int mkd_line(char *, int, char **, mkd_flag_t);
|
36
|
+
typedef int (*mkd_sta_function_t)(const int,const void*);
|
37
|
+
void mkd_string_to_anchor(char *, int, mkd_sta_function_t, void*, int);
|
38
|
+
int mkd_xhtmlpage(MMIOT*,int,FILE*);
|
39
|
+
|
40
|
+
/* header block access
|
41
|
+
*/
|
42
|
+
char* mkd_doc_title(MMIOT*);
|
43
|
+
char* mkd_doc_author(MMIOT*);
|
44
|
+
char* mkd_doc_date(MMIOT*);
|
45
|
+
|
46
|
+
/* compiled data access
|
47
|
+
*/
|
48
|
+
int mkd_document(MMIOT*, char**);
|
49
|
+
int mkd_toc(MMIOT*, char**);
|
50
|
+
int mkd_css(MMIOT*, char **);
|
51
|
+
int mkd_xml(char *, int, char **);
|
52
|
+
|
53
|
+
/* write-to-file functions
|
54
|
+
*/
|
55
|
+
int mkd_generatehtml(MMIOT*,FILE*);
|
56
|
+
int mkd_generatetoc(MMIOT*,FILE*);
|
57
|
+
int mkd_generatexml(char *, int,FILE*);
|
58
|
+
int mkd_generatecss(MMIOT*,FILE*);
|
59
|
+
#define mkd_style mkd_generatecss
|
60
|
+
int mkd_generateline(char *, int, FILE*, mkd_flag_t);
|
61
|
+
#define mkd_text mkd_generateline
|
62
|
+
|
63
|
+
/* url generator callbacks
|
64
|
+
*/
|
65
|
+
typedef char * (*mkd_callback_t)(const char*, const int, void*);
|
66
|
+
typedef void (*mkd_free_t)(char*, void*);
|
67
|
+
|
68
|
+
void mkd_e_url(void *, mkd_callback_t);
|
69
|
+
void mkd_e_flags(void *, mkd_callback_t);
|
70
|
+
void mkd_e_free(void *, mkd_free_t );
|
71
|
+
void mkd_e_data(void *, void *);
|
72
|
+
|
73
|
+
/* version#.
|
74
|
+
*/
|
75
|
+
extern char markdown_version[];
|
76
|
+
void mkd_mmiot_flags(FILE *, MMIOT *, int);
|
77
|
+
void mkd_flags_are(FILE*, mkd_flag_t, int);
|
78
|
+
|
79
|
+
void mkd_ref_prefix(MMIOT*, char*);
|
80
|
+
|
81
|
+
|
82
|
+
/* special flags for markdown() and mkd_text()
|
83
|
+
*/
|
84
|
+
#define MKD_NOLINKS 0x00000001 /* don't do link processing, block <a> tags */
|
85
|
+
#define MKD_NOIMAGE 0x00000002 /* don't do image processing, block <img> */
|
86
|
+
#define MKD_NOPANTS 0x00000004 /* don't run smartypants() */
|
87
|
+
#define MKD_NOHTML 0x00000008 /* don't allow raw html through AT ALL */
|
88
|
+
#define MKD_STRICT 0x00000010 /* disable SUPERSCRIPT, RELAXED_EMPHASIS */
|
89
|
+
#define MKD_TAGTEXT 0x00000020 /* process text inside an html tag; no
|
90
|
+
* <em>, no <bold>, no html or [] expansion */
|
91
|
+
#define MKD_NO_EXT 0x00000040 /* don't allow pseudo-protocols */
|
92
|
+
#define MKD_NOEXT MKD_NO_EXT /* ^^^ (aliased for user convenience) */
|
93
|
+
#define MKD_CDATA 0x00000080 /* generate code for xml ![CDATA[...]] */
|
94
|
+
#define MKD_NOSUPERSCRIPT 0x00000100 /* no A^B */
|
95
|
+
#define MKD_NORELAXED 0x00000200 /* emphasis happens /everywhere/ */
|
96
|
+
#define MKD_NOTABLES 0x00000400 /* disallow tables */
|
97
|
+
#define MKD_NOSTRIKETHROUGH 0x00000800 /* forbid ~~strikethrough~~ */
|
98
|
+
#define MKD_TOC 0x00001000 /* do table-of-contents processing */
|
99
|
+
#define MKD_1_COMPAT 0x00002000 /* compatibility with MarkdownTest_1.0 */
|
100
|
+
#define MKD_AUTOLINK 0x00004000 /* make http://foo.com link even without <>s */
|
101
|
+
#define MKD_SAFELINK 0x00008000 /* paranoid check for link protocol */
|
102
|
+
#define MKD_NOHEADER 0x00010000 /* don't process header blocks */
|
103
|
+
#define MKD_TABSTOP 0x00020000 /* expand tabs to 4 spaces */
|
104
|
+
#define MKD_NODIVQUOTE 0x00040000 /* forbid >%class% blocks */
|
105
|
+
#define MKD_NOALPHALIST 0x00080000 /* forbid alphabetic lists */
|
106
|
+
#define MKD_NODLIST 0x00100000 /* forbid definition lists */
|
107
|
+
#define MKD_EXTRA_FOOTNOTE 0x00200000 /* enable markdown extra-style footnotes */
|
108
|
+
#define MKD_NOSTYLE 0x00400000 /* don't extract <style> blocks */
|
109
|
+
#define MKD_NODLDISCOUNT 0x00800000 /* disable discount-style definition lists */
|
110
|
+
#define MKD_DLEXTRA 0x01000000 /* enable extra-style definition lists */
|
111
|
+
#define MKD_FENCEDCODE 0x02000000 /* enabled fenced code blocks */
|
112
|
+
#define MKD_IDANCHOR 0x04000000 /* use id= anchors for TOC links */
|
113
|
+
#define MKD_GITHUBTAGS 0x08000000 /* allow dash and underscore in element names */
|
114
|
+
#define MKD_URLENCODEDANCHOR 0x10000000 /* urlencode non-identifier chars instead of replacing with dots */
|
115
|
+
|
116
|
+
#define MKD_EMBED MKD_NOLINKS|MKD_NOIMAGE|MKD_TAGTEXT
|
117
|
+
|
118
|
+
/* special flags for mkd_in() and mkd_string()
|
119
|
+
*/
|
120
|
+
|
121
|
+
|
122
|
+
#endif/*_MKDIO_D*/
|