This repository has been archived by the owner on Sep 24, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
build_ffi.py
344 lines (289 loc) · 15.2 KB
/
build_ffi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# -*- coding: utf-8 -*-
import cffi
# Block-level extensions
EXT_TABLES = (1 << 0)
EXT_FENCED_CODE = (1 << 1)
EXT_FOOTNOTES = (1 << 2)
# Span-level extensions
EXT_AUTOLINK = (1 << 3)
EXT_STRIKETHROUGH = (1 << 4)
EXT_UNDERLINE = (1 << 5)
EXT_HIGHLIGHT = (1 << 6)
EXT_QUOTE = (1 << 7)
EXT_SUPERSCRIPT = (1 << 8)
EXT_MATH = (1 << 9)
# Other flags
EXT_NO_INTRA_EMPHASIS = (1 << 11)
EXT_SPACE_HEADERS = (1 << 12)
EXT_MATH_EXPLICIT = (1 << 13)
# Negative flags
EXT_DISABLE_INDENTED_CODE = (1 << 14)
# List flags
LIST_ORDERED = (1 << 0)
LI_BLOCK = (1 << 1) # <li> containing block data
# Table flags
TABLE_ALIGN_LEFT = 1
TABLE_ALIGN_RIGHT = 2
TABLE_ALIGN_CENTER = 3
TABLE_ALIGNMASK = 3
TABLE_HEADER = 4
# HTML flags
HTML_SKIP_HTML = (1 << 0)
HTML_ESCAPE = (1 << 1)
HTML_HARD_WRAP = (1 << 2)
HTML_USE_XHTML = (1 << 3)
# Autolink types
AUTOLINK_NONE = 1 # Used internally when it is not an autolink
AUTOLINK_NORMAL = 2 # Normal http/http/ftp/mailto/etc link
AUTOLINK_EMAIL = 3 # E-mail link without explit mailto:
ffi = cffi.FFI()
ffi.set_source(
'misaka._hoedown',
"""\
#include "hoedown/buffer.h"
#include "hoedown/document.h"
#include "hoedown/escape.h"
#include "hoedown/html.h"
#include "extra.h"
""",
sources=(
'misaka/hoedown/version.c',
'misaka/hoedown/stack.c',
'misaka/hoedown/html_smartypants.c',
'misaka/hoedown/html_blocks.c',
'misaka/hoedown/html.c',
'misaka/hoedown/escape.c',
'misaka/hoedown/document.c',
'misaka/hoedown/buffer.c',
'misaka/hoedown/autolink.c',
'misaka/extra.c',
),
include_dirs=('misaka',))
# NOTE: The constants are refined here, because CFFI
# doesn't parse the bitwise left-shift (<<).
ffi.cdef("""\
// --------------------------
// --- hoedown/document.h ---
// --------------------------
typedef enum hoedown_extensions {{
/* block-level extensions */
HOEDOWN_EXT_TABLES = {0},
HOEDOWN_EXT_FENCED_CODE = {1},
HOEDOWN_EXT_FOOTNOTES = {2},
HOEDOWN_EXT_AUTOLINK = {3},
HOEDOWN_EXT_STRIKETHROUGH = {4},
HOEDOWN_EXT_UNDERLINE = {5},
HOEDOWN_EXT_HIGHLIGHT = {6},
HOEDOWN_EXT_QUOTE = {7},
HOEDOWN_EXT_SUPERSCRIPT = {8},
HOEDOWN_EXT_MATH = {9},
HOEDOWN_EXT_NO_INTRA_EMPHASIS = {10},
HOEDOWN_EXT_SPACE_HEADERS = {11},
HOEDOWN_EXT_MATH_EXPLICIT = {12},
HOEDOWN_EXT_DISABLE_INDENTED_CODE = {13}
}} hoedown_extensions;
typedef enum hoedown_list_flags {{
HOEDOWN_LIST_ORDERED = {14},
HOEDOWN_LI_BLOCK = {15}
}} hoedown_list_flags;
typedef enum hoedown_table_flags {{
HOEDOWN_TABLE_ALIGN_LEFT = {16},
HOEDOWN_TABLE_ALIGN_RIGHT = {17},
HOEDOWN_TABLE_ALIGN_CENTER = {18},
HOEDOWN_TABLE_ALIGNMASK = {19},
HOEDOWN_TABLE_HEADER = {20}
}} hoedown_table_flags;
// ----------------------
// --- hoedown/html.h ---
// ----------------------
typedef enum hoedown_html_flags {{
HOEDOWN_HTML_SKIP_HTML = {21},
HOEDOWN_HTML_ESCAPE = {22},
HOEDOWN_HTML_HARD_WRAP = {23},
HOEDOWN_HTML_USE_XHTML = {24}
}} hoedown_html_flags;
""".format(
EXT_TABLES,
EXT_FENCED_CODE,
EXT_FOOTNOTES,
EXT_AUTOLINK,
EXT_STRIKETHROUGH,
EXT_UNDERLINE,
EXT_HIGHLIGHT,
EXT_QUOTE,
EXT_SUPERSCRIPT,
EXT_MATH,
EXT_NO_INTRA_EMPHASIS,
EXT_SPACE_HEADERS,
EXT_MATH_EXPLICIT,
EXT_DISABLE_INDENTED_CODE,
LIST_ORDERED,
LI_BLOCK,
TABLE_ALIGN_LEFT,
TABLE_ALIGN_RIGHT,
TABLE_ALIGN_CENTER,
TABLE_ALIGNMASK,
TABLE_HEADER,
HTML_SKIP_HTML,
HTML_ESCAPE,
HTML_HARD_WRAP,
HTML_USE_XHTML))
ffi.cdef("""\
// ------------------------
// --- hoedown/buffer.h ---
// ------------------------
typedef void *(*hoedown_realloc_callback)(void *, size_t);
typedef void (*hoedown_free_callback)(void *);
struct hoedown_buffer {
uint8_t *data; /* actual character data */
size_t size; /* size of the string */
size_t asize; /* allocated size (0 = volatile buffer) */
size_t unit; /* reallocation unit size (0 = read-only buffer) */
hoedown_realloc_callback data_realloc;
hoedown_free_callback data_free;
hoedown_free_callback buffer_free;
};
typedef struct hoedown_buffer hoedown_buffer;
void *hoedown_malloc(size_t size);
hoedown_buffer *hoedown_buffer_new(size_t unit);
void hoedown_buffer_grow(hoedown_buffer *buf, size_t neosz);
void hoedown_buffer_puts(hoedown_buffer *buf, const char *str);
void hoedown_buffer_free(hoedown_buffer *buf);
// --------------------------
// --- hoedown/document.h ---
// --------------------------
// NOTE: See earlier ff.cdef() for document.h's constants.
typedef enum hoedown_autolink_type {
HOEDOWN_AUTOLINK_NONE, /* used internally when it is not an autolink*/
HOEDOWN_AUTOLINK_NORMAL, /* normal http/http/ftp/mailto/etc link */
HOEDOWN_AUTOLINK_EMAIL /* e-mail link without explit mailto: */
} hoedown_autolink_type;
struct hoedown_document;
typedef struct hoedown_document hoedown_document;
struct hoedown_renderer_data {
void *opaque;
};
typedef struct hoedown_renderer_data hoedown_renderer_data;
/* hoedown_renderer - functions for rendering parsed data */
struct hoedown_renderer {
/* state object */
void *opaque;
/* block level callbacks - NULL skips the block */
void (*blockcode)(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_buffer *lang, const hoedown_renderer_data *data);
void (*blockquote)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
void (*header)(hoedown_buffer *ob, const hoedown_buffer *content, int level, const hoedown_renderer_data *data);
void (*hrule)(hoedown_buffer *ob, const hoedown_renderer_data *data);
void (*list)(hoedown_buffer *ob, const hoedown_buffer *content, hoedown_list_flags flags, const hoedown_renderer_data *data);
void (*listitem)(hoedown_buffer *ob, const hoedown_buffer *content, hoedown_list_flags flags, const hoedown_renderer_data *data);
void (*paragraph)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
void (*table)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
void (*table_header)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
void (*table_body)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
void (*table_row)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
void (*table_cell)(hoedown_buffer *ob, const hoedown_buffer *content, hoedown_table_flags flags, const hoedown_renderer_data *data);
void (*footnotes)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
void (*footnote_def)(hoedown_buffer *ob, const hoedown_buffer *content, unsigned int num, const hoedown_renderer_data *data);
void (*blockhtml)(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
/* span level callbacks - NULL or return 0 prints the span verbatim */
int (*autolink)(hoedown_buffer *ob, const hoedown_buffer *link, hoedown_autolink_type type, const hoedown_renderer_data *data);
int (*codespan)(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
int (*double_emphasis)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
int (*emphasis)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
int (*underline)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
int (*highlight)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
int (*quote)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
int (*image)(hoedown_buffer *ob, const hoedown_buffer *link, const hoedown_buffer *title, const hoedown_buffer *alt, const hoedown_renderer_data *data);
int (*linebreak)(hoedown_buffer *ob, const hoedown_renderer_data *data);
int (*link)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_buffer *link, const hoedown_buffer *title, const hoedown_renderer_data *data);
int (*triple_emphasis)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
int (*strikethrough)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
int (*superscript)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
int (*footnote_ref)(hoedown_buffer *ob, unsigned int num, const hoedown_renderer_data *data);
int (*math)(hoedown_buffer *ob, const hoedown_buffer *text, int displaymode, const hoedown_renderer_data *data);
int (*raw_html)(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
/* low level callbacks - NULL copies input directly into the output */
void (*entity)(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
void (*normal_text)(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
/* miscellaneous callbacks */
void (*doc_header)(hoedown_buffer *ob, int inline_render, const hoedown_renderer_data *data);
void (*doc_footer)(hoedown_buffer *ob, int inline_render, const hoedown_renderer_data *data);
};
typedef struct hoedown_renderer hoedown_renderer;
hoedown_document *hoedown_document_new(
const hoedown_renderer *renderer,
hoedown_extensions extensions,
size_t max_nesting
);
void hoedown_document_render(hoedown_document *doc, hoedown_buffer *ob, const uint8_t *data, size_t size);
void hoedown_document_free(hoedown_document *doc);
// ------------------------
// --- hoedown/escape.h ---
// ------------------------
void hoedown_escape_html(hoedown_buffer *ob, const uint8_t *data, size_t size, int secure);
// ----------------------
// --- hoedown/html.h ---
// ----------------------
// NOTE: See earlier ff.cdef() for html.h's constants.
typedef enum hoedown_html_tag {
HOEDOWN_HTML_TAG_NONE = 0,
HOEDOWN_HTML_TAG_OPEN,
HOEDOWN_HTML_TAG_CLOSE
} hoedown_html_tag;
hoedown_renderer *hoedown_html_renderer_new(
hoedown_html_flags render_flags,
int nesting_level
);
hoedown_renderer *hoedown_html_toc_renderer_new(
int nesting_level
);
void hoedown_html_renderer_free(hoedown_renderer *renderer);
void hoedown_html_smartypants(hoedown_buffer *ob, const uint8_t *data, size_t size);
// ---------------
// --- extra.h ---
// ---------------
void *misaka_get_renderer(const hoedown_renderer_data *data);
// ------------------------
// --- Python callbacks ---
// ------------------------
/* block level callbacks - NULL skips the block */
extern "Python" void _misaka_blockcode(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_buffer *lang, const hoedown_renderer_data *data);
extern "Python" void _misaka_blockquote(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" void _misaka_header(hoedown_buffer *ob, const hoedown_buffer *content, int level, const hoedown_renderer_data *data);
extern "Python" void _misaka_hrule(hoedown_buffer *ob, const hoedown_renderer_data *data);
extern "Python" void _misaka_list(hoedown_buffer *ob, const hoedown_buffer *content, hoedown_list_flags flags, const hoedown_renderer_data *data);
extern "Python" void _misaka_listitem(hoedown_buffer *ob, const hoedown_buffer *content, hoedown_list_flags flags, const hoedown_renderer_data *data);
extern "Python" void _misaka_paragraph(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" void _misaka_table(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" void _misaka_table_header(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" void _misaka_table_body(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" void _misaka_table_row(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" void _misaka_table_cell(hoedown_buffer *ob, const hoedown_buffer *content, hoedown_table_flags flags, const hoedown_renderer_data *data);
extern "Python" void _misaka_footnotes(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" void _misaka_footnote_def(hoedown_buffer *ob, const hoedown_buffer *content, unsigned int num, const hoedown_renderer_data *data);
extern "Python" void _misaka_blockhtml(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
/* span level callbacks - NULL or return 0 prints the span verbatim */
extern "Python" int _misaka_autolink(hoedown_buffer *ob, const hoedown_buffer *link, hoedown_autolink_type type, const hoedown_renderer_data *data);
extern "Python" int _misaka_codespan(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
extern "Python" int _misaka_double_emphasis(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" int _misaka_emphasis(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" int _misaka_underline(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" int _misaka_highlight(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" int _misaka_quote(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" int _misaka_image(hoedown_buffer *ob, const hoedown_buffer *link, const hoedown_buffer *title, const hoedown_buffer *alt, const hoedown_renderer_data *data);
extern "Python" int _misaka_linebreak(hoedown_buffer *ob, const hoedown_renderer_data *data);
extern "Python" int _misaka_link(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_buffer *link, const hoedown_buffer *title, const hoedown_renderer_data *data);
extern "Python" int _misaka_triple_emphasis(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" int _misaka_strikethrough(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" int _misaka_superscript(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
extern "Python" int _misaka_footnote_ref(hoedown_buffer *ob, unsigned int num, const hoedown_renderer_data *data);
extern "Python" int _misaka_math(hoedown_buffer *ob, const hoedown_buffer *text, int displaymode, const hoedown_renderer_data *data);
extern "Python" int _misaka_raw_html(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
/* low level callbacks - NULL copies input directly into the output */
extern "Python" void _misaka_entity(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
extern "Python" void _misaka_normal_text(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
/* miscellaneous callbacks */
extern "Python" void _misaka_doc_header(hoedown_buffer *ob, int inline_render, const hoedown_renderer_data *data);
extern "Python" void _misaka_doc_footer(hoedown_buffer *ob, int inline_render, const hoedown_renderer_data *data);
""")
if __name__ == '__main__':
ffi.compile()