-
Notifications
You must be signed in to change notification settings - Fork 16
/
sundown_markdown.c
747 lines (615 loc) · 21.6 KB
/
sundown_markdown.c
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2011 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Shuhei Tanuma <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_sundown.h"
extern zend_class_entry *sundown_render_base_class_entry;
static zend_class_entry *spl_ce_InvalidArgumentException;
zend_class_entry *sundown_markdown_class_entry;
static void rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "blockCode", 2, buf2str(text), buf2str(lang));
}
static void rndr_blockquote(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "blockQuote", 1, buf2str(text));
}
static void rndr_raw_block(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "blockHtml", 1, buf2str(text));
}
static void rndr_header(struct buf *ob, const struct buf *text, int level, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "header", 2, buf2str(text), buf2long(level));
}
static void rndr_hrule(struct buf *ob, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "hrule", 0);
}
static void rndr_list(struct buf *ob, const struct buf *text, int flags, void *opaque)
{
zval *flag;
MAKE_STD_ZVAL(flag);
ZVAL_LONG(flag, flags);
BLOCK_CALLBACK_EX(ob, "listBox", 2, buf2str(text), flag);
}
static void rndr_listitem(struct buf *ob, const struct buf *text, int flags, void *opaque)
{
zval *flag;
MAKE_STD_ZVAL(flag);
ZVAL_LONG(flag, flags);
BLOCK_CALLBACK_EX(ob, "listItem", 2, buf2str(text), flag);
}
static void rndr_paragraph(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "paragraph", 1, buf2str(text));
}
static void rndr_table(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "table", 2, buf2str(header), buf2str(body));
}
static void rndr_tablerow(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "tableRow", 1, buf2str(text));
}
static void rndr_tablecell(struct buf *ob, const struct buf *text, int align, void *opaque)
{
zval *php_align;
MAKE_STD_ZVAL(php_align);
switch (align) {
case MKD_TABLE_ALIGN_L:
ZVAL_STRING(php_align, "left", 1);
break;
case MKD_TABLE_ALIGN_R:
ZVAL_STRING(php_align, "right", 1);
break;
case MKD_TABLE_ALIGN_CENTER:
ZVAL_STRING(php_align, "center", 1);
break;
default:
ZVAL_NULL(php_align);
break;
}
BLOCK_CALLBACK_EX(ob, "tableCell", 2, buf2str(text), php_align);
}
/***
* SPAN LEVEL
*/
static int rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque)
{
SPAN_CALLBACK_EX(ob, "autolink", 2, buf2str(link), buf2long(type));
}
static int rndr_codespan(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK_EX(ob, "codespan", 1, buf2str(text));
}
static int rndr_double_emphasis(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK_EX(ob, "doubleEmphasis", 1, buf2str(text));
}
static int rndr_emphasis(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK_EX(ob, "emphasis", 1, buf2str(text));
}
static int rndr_image(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque)
{
SPAN_CALLBACK_EX(ob, "image", 3, buf2str(link), buf2str(title), buf2str(alt));
}
static int rndr_linebreak(struct buf *ob, void *opaque)
{
SPAN_CALLBACK_EX(ob, "linebreak", 0);
}
static int rndr_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque)
{
SPAN_CALLBACK_EX(ob, "link", 3, buf2str(link), buf2str(title), buf2str(content));
}
static int rndr_raw_html(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK_EX(ob, "rawHtml", 1, buf2str(text));
}
static int rndr_triple_emphasis(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK_EX(ob, "tripleEmphasis", 1, buf2str(text));
}
static int rndr_strikethrough(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK_EX(ob, "strikethrough", 1, buf2str(text));
}
static int rndr_superscript(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK_EX(ob, "superscript", 1, buf2str(text));
}
/**
* direct writes
*/
static void rndr_entity(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "entity", 1, buf2str(text));
}
static void rndr_normal_text(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "normalText", 1, buf2str(text));
}
static void rndr_doc_header(struct buf *ob, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "docHeader", 0);
}
static void rndr_doc_footer(struct buf *ob, void *opaque)
{
BLOCK_CALLBACK_EX(ob, "docFooter", 0);
}
static struct sd_callbacks php_sundown_callbacks = {
rndr_blockcode,
rndr_blockquote,
rndr_raw_block,
rndr_header,
rndr_hrule,
rndr_list,
rndr_listitem,
rndr_paragraph,
rndr_table,
rndr_tablerow,
rndr_tablecell,
rndr_autolink,
rndr_codespan,
rndr_double_emphasis,
rndr_emphasis,
rndr_image,
rndr_linebreak,
rndr_link,
rndr_raw_html,
rndr_triple_emphasis,
rndr_strikethrough,
rndr_superscript,
rndr_entity,
rndr_normal_text,
rndr_doc_header,
rndr_doc_footer,
};
static const char *php_sundown_method_names[] = {
"blockcode",
"blockquote",
"blockhtml",
"header",
"hrule",
"listbox",
"listitem",
"paragraph",
"table",
"tablerow",
"tablecell",
"autolink",
"codespan",
"doubleemphasis",
"emphasis",
"image",
"linebreak",
"link",
"rawhtml",
"tripleemphasis",
"strikethrough",
"superscript",
"entity",
"normaltext",
"docheader",
"docfooter"
};
static const size_t php_sundown_method_count = sizeof(php_sundown_method_names)/sizeof(char *);
static void php_sundown_get_render(zval *instance, zval **result TSRMLS_DC)
{
zval *render;
zend_call_method_with_0_params(&instance, Z_OBJCE_P(instance), NULL, "getrender", &render);
*result = render;
}
static void php_sundown_get_renderflags(zval *instance, unsigned int *result TSRMLS_DC)
{
unsigned int render_flags = 0;
zval *flags;
zend_call_method_with_0_params(&instance, Z_OBJCE_P(instance), NULL, "getrenderflags", &flags);
php_sundown__get_render_flags(Z_ARRVAL_P(flags), &render_flags);
*result = render_flags;
zval_ptr_dtor(&flags);
}
static void php_sundown_get_extensions(zval *instance, unsigned int *result TSRMLS_DC)
{
unsigned int enabled_extensions = 0;
zval *flags;
zend_call_method_with_0_params(&instance, Z_OBJCE_P(instance), NULL, "getextensions", &flags);
php_sundown__get_extensions(Z_ARRVAL_P(flags), &enabled_extensions);
*result = enabled_extensions;
zval_ptr_dtor(&flags);
}
static void php_sundown_markdown_preprocess(zval *instance, zval *render, const char *input, struct buf *buffer TSRMLS_DC)
{
zval preprocess, *ret, *params[1];
MAKE_STD_ZVAL(ret);
MAKE_STD_ZVAL(params[0]);
ZVAL_STRINGL(params[0], input, strlen(input), 1);
ZVAL_STRING(&preprocess, "preprocess", 1);
if (call_user_function(NULL, &render, &preprocess, ret, 1, params TSRMLS_CC) == FAILURE) {
}
if (ret != NULL && Z_TYPE_P(ret) == IS_STRING) {
buffer->data = emalloc(Z_STRLEN_P(ret));
memcpy(buffer->data, Z_STRVAL_P(ret), Z_STRLEN_P(ret));
buffer->size = Z_STRLEN_P(ret);
} else {
buffer->data = emalloc(strlen(input));
memcpy(buffer->data, input, strlen(input));
buffer->size = strlen(input);
}
zval_ptr_dtor(¶ms[0]);
zval_ptr_dtor(&ret);
zval_dtor(&preprocess);
}
static void php_sundown_markdown_postprocess(zval *instance, zval *render, struct buf *input, zval **result TSRMLS_DC)
{
zval postprocess, *tmp, *ret, *params[1];
MAKE_STD_ZVAL(ret);
MAKE_STD_ZVAL(params[0]);
MAKE_STD_ZVAL(tmp);
ZVAL_STRINGL(params[0], (char *)input->data, input->size, 1);
ZVAL_STRING(&postprocess, "postprocess", 1);
if (call_user_function(NULL, &render, &postprocess, ret, 1, params TSRMLS_CC) == FAILURE) {
}
if (ret != NULL && Z_TYPE_P(ret) == IS_STRING) {
ZVAL_ZVAL(tmp, ret, 1, 0);
} else {
ZVAL_STRINGL(tmp, (char*)input->data, input->size, 1);
}
zval_ptr_dtor(¶ms[0]);
zval_ptr_dtor(&ret);
zval_dtor(&postprocess);
*result = tmp;
}
static void php_sundown_markdown_inherit_functions(zval *render, int render_flags, struct php_sundown_renderopt_ex *opt, struct sd_callbacks *sundown_render TSRMLS_DC)
{
php_sundown_render_base_t *render_base;
zend_class_entry *ce;
void **source, **dest;
int i;
render_base = (php_sundown_render_base_t *) zend_object_store_get_object(render TSRMLS_CC);
render_base->html = opt->html;
ce = Z_OBJCE_P(render);
opt->self = render;
source = (void **)&php_sundown_callbacks;
dest = (void **)sundown_render;
for (i = 0; i < php_sundown_method_count; ++i) {
if (zend_hash_exists(&ce->function_table, php_sundown_method_names[i], strlen(php_sundown_method_names[i])+1)) {
dest[i] = source[i];
}
}
if (instanceof_function_ex(ce, sundown_render_html_class_entry, 0 TSRMLS_CC)) {
if (render_flags & HTML_SKIP_IMAGES) {
sundown_render->image = NULL;
}
if (render_flags & HTML_SKIP_LINKS) {
sundown_render->link = NULL;
sundown_render->autolink = NULL;
}
if (render_flags & HTML_SKIP_HTML || render_flags & HTML_ESCAPE) {
sundown_render->blockhtml = NULL;
}
}
}
void php_sundown_markdon_render(SundownRendererType render_type, INTERNAL_FUNCTION_PARAMETERS)
{
zval *render, *result;
struct buf input_buf, *output_buf;
struct sd_callbacks sundown_render;
struct php_sundown_renderopt_ex opt;
struct sd_markdown *markdown;
unsigned int enabled_extensions = 0, render_flags = 0;
char *buffer;
int buffer_len = 0;
int is_sundown_markdown = 0;
if (Z_OBJCE_P(getThis()) == sundown_markdown_class_entry) {
is_sundown_markdown = 1;
}
if (is_sundown_markdown) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"s", &buffer, &buffer_len) == FAILURE) {
return;
}
} else {
buffer = Z_STRVAL_P(zend_read_property(sundown_class_entry, getThis(), ZEND_STRS("data")-1, 0 TSRMLS_CC));
buffer_len = strlen(buffer);
memset(&input_buf, 0x0, sizeof(struct buf));
input_buf.data = (uint8_t *)buffer;
input_buf.size = strlen(buffer);
}
output_buf = bufnew(128);
bufgrow(output_buf, buffer_len * 1.2f);
if (is_sundown_markdown) {
php_sundown_get_render(getThis(), &render TSRMLS_CC);
php_sundown_get_renderflags(render, &render_flags TSRMLS_CC);
php_sundown_get_extensions(getThis(), &enabled_extensions TSRMLS_CC);
} else {
if (Z_TYPE_P(zend_read_property(sundown_class_entry, getThis(), ZEND_STRS("extensions")-1, 0 TSRMLS_CC)) != IS_NULL) {
php_sundown__get_flags(Z_ARRVAL_P(zend_read_property(sundown_class_entry, getThis(), ZEND_STRS("extensions")-1, 0 TSRMLS_CC)), &enabled_extensions, &render_flags);
}
}
switch (render_type) {
case SUNDOWN_RENDER_HTML:
sdhtml_renderer(&sundown_render, &opt.html, render_flags);
break;
case SUNDOWN_RENDER_TOC:
sdhtml_toc_renderer(&sundown_render, &opt.html);
break;
default:
RETURN_FALSE;
}
if (is_sundown_markdown) {
php_sundown_markdown_inherit_functions(render, render_flags, &opt, &sundown_render TSRMLS_CC);
php_sundown_markdown_preprocess(getThis(), render, buffer, &input_buf TSRMLS_CC);
} else {
opt.self = getThis();
}
if (is_sundown_markdown) {
markdown = sd_markdown_new(enabled_extensions, 16, &sundown_render, &opt);
if (SETJMP(SUNDOWN_G(jump)) == 0) {
sd_markdown_render(output_buf, input_buf.data, input_buf.size, markdown);
efree(input_buf.data);
sd_markdown_free(markdown);
} else {
efree(input_buf.data);
zval_ptr_dtor(&render);
sd_markdown_free(markdown);
return;
}
php_sundown_markdown_postprocess(getThis(), render, output_buf, &result TSRMLS_CC);
zval_ptr_dtor(&render);
RETURN_ZVAL(result, 0, 1);
} else {
markdown = sd_markdown_new(enabled_extensions, 16, &sundown_render, &opt);
sd_markdown_render(output_buf, input_buf.data, input_buf.size, markdown);
sd_markdown_free(markdown);
if (Z_BVAL_P(zend_read_property(sundown_class_entry, getThis(), ZEND_STRS("enable_pants")-1, 0 TSRMLS_CC))) {
struct buf *smart_buf = bufnew(128);
sdhtml_smartypants(smart_buf, output_buf->data, output_buf->size);
RETVAL_STRINGL((char*)smart_buf->data, smart_buf->size, 1);
bufrelease(smart_buf);
} else {
RETVAL_STRINGL((char*)output_buf->data, output_buf->size, 1);
}
}
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_sundown_markdown__construct, 0, 0, 2)
ZEND_ARG_INFO(0, render)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sundown_markdown_render, 0, 0, 1)
ZEND_ARG_INFO(0, body)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sundown_markdown_set_render, 0, 0, 1)
ZEND_ARG_INFO(0, render)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sundown_markdown_set_extensions, 0, 0, 1)
ZEND_ARG_INFO(0, extension)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sundown_markdown_has_extension, 0, 0, 1)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sundown_markdown_has_render_flag, 0, 0, 1)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
static void php_sundown_markdown_free_storage(php_sundown_markdown_t *obj TSRMLS_DC)
{
zend_object_std_dtor(&obj->zo TSRMLS_CC);
if (obj->render != NULL) {
zval_ptr_dtor(&obj->render);
}
efree(obj);
}
zend_object_value php_sundown_markdown_new(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
php_sundown_markdown_t *obj;
obj = ecalloc(1, sizeof(*obj));
zend_object_std_init( &obj->zo, ce TSRMLS_CC );
#if ZEND_MODULE_API_NO >= 20100525
object_properties_init(&(obj->zo), ce);
#else
{
zval *tmp;
zend_hash_copy(obj->zo.properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
}
#endif
retval.handle = zend_objects_store_put(obj,
(zend_objects_store_dtor_t)zend_objects_destroy_object,
(zend_objects_free_object_storage_t)php_sundown_markdown_free_storage,
NULL TSRMLS_CC);
retval.handlers = zend_get_std_object_handlers();
return retval;
}
/* {{{ proto string Sundown\Markdown::__construct(Sundown\Render\Base $render [, array $extensions])
*/
PHP_METHOD(sundown_markdown, __construct)
{
zval *render;
zval *extensions = NULL, *c_extensions = NULL;
zend_class_entry **ce;
php_sundown_markdown_t *object = (php_sundown_markdown_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
"z|a", &render, &extensions) == FAILURE) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, " Sundown\\Markdown::__construct() expects parameter 2 to be array");
return;
}
if (Z_TYPE_P(render) == IS_STRING) {
zval *obj, *retval;
if (zend_lookup_class(Z_STRVAL_P(render), strlen(Z_STRVAL_P(render)), &ce TSRMLS_CC) == FAILURE) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "class %s does not find.", Z_STRVAL_P(render));
return;
}
MAKE_STD_ZVAL(obj);
object_init_ex(obj, *ce);
zend_call_method_with_0_params(&obj, Z_OBJCE_P(obj), NULL, "__construct", &retval);
zval_ptr_dtor(&retval);
render = obj;
} else if (Z_TYPE_P(render) == IS_OBJECT) {
/* nothing to do */
Z_ADDREF_P(render);
} else {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "passed unexpected value. Sundown\\Markdown::__construct expects string or object");
return;
}
if (!instanceof_function_ex(Z_OBJCE_P(render), sundown_render_base_class_entry, 0 TSRMLS_CC)) {
Z_DELREF_P(render);
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "Render class must extend Sundown\\Render\\Base");
return;
}
object->render = render;
if (extensions == NULL) {
MAKE_STD_ZVAL(c_extensions);
array_init(c_extensions);
} else {
ALLOC_INIT_ZVAL(c_extensions);
ZVAL_ZVAL(c_extensions, extensions, 1, 0);
}
add_property_zval_ex(getThis(), ZEND_STRS("extensions"), c_extensions TSRMLS_CC);
}
/* }}} */
/* {{{ proto void Sundown\Markdown::__destruct()
cleanup variables */
PHP_METHOD(sundown_markdown, __destruct)
{
zval *extensions;
extensions = zend_read_property(sundown_markdown_class_entry, getThis(), ZEND_STRS("extensions")-1, 0 TSRMLS_CC);
zval_ptr_dtor(&extensions);
}
/* }}} */
/* {{{ proto string Sundown\Markdown::render(string $body)
*/
PHP_METHOD(sundown_markdown, render)
{
php_sundown_markdon_render(SUNDOWN_RENDER_HTML, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto string Sundown\Markdown::hasExtension(string $ext_name)
*/
PHP_METHOD(sundown_markdown, hasExtension)
{
char *name;
int name_len = 0;
HashTable *table;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"s", &name, &name_len) == FAILURE) {
return;
}
if (Z_TYPE_P(zend_read_property(sundown_class_entry, getThis(), ZEND_STRS("extensions")-1, 0 TSRMLS_CC)) != IS_NULL) {
table = Z_ARRVAL_P(zend_read_property(sundown_class_entry, getThis(), ZEND_STRS("extensions")-1, 0 TSRMLS_CC));
RETVAL_BOOL(php_sundown_has_ext(table, name));
}
}
/* }}} */
/* {{{ proto string Sundown\Markdown::hasRenderFlag(string $ext_name)
*/
PHP_METHOD(sundown_markdown, hasRenderFlag)
{
char *name;
int name_len = 0;
HashTable *table;
zval *render_flags, *render = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"s", &name, &name_len) == FAILURE) {
return;
}
zend_call_method_with_0_params(&this_ptr, Z_OBJCE_P(getThis()), NULL, "getrender", &render);
zend_call_method_with_0_params(&render, Z_OBJCE_P(render), NULL, "getrenderflags", &render_flags);
table = Z_ARRVAL_P(render_flags);
RETVAL_BOOL(php_sundown_has_ext(table, name));
zval_ptr_dtor(&render);
zval_ptr_dtor(&render_flags);
}
/* }}} */
/* {{{ proto void string Sundown\Markdown::setExtension(array $extensions)
*/
PHP_METHOD(sundown_markdown, setExtensions)
{
zval *tmp, *extensions = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"a", &extensions) == FAILURE) {
return;
}
tmp = zend_read_property(sundown_class_entry, getThis(), ZEND_STRS("extensions")-1, 0 TSRMLS_CC);
zval_ptr_dtor(&tmp);
add_property_zval_ex(getThis(), ZEND_STRS("extensions"), extensions TSRMLS_CC);
}
/* }}} */
/* {{{ proto array Sundown\Markdown::getExtensions()
*/
PHP_METHOD(sundown_markdown, getExtensions)
{
if (Z_TYPE_P(zend_read_property(sundown_class_entry, getThis(), ZEND_STRS("extensions")-1, 0 TSRMLS_CC)) != IS_NULL) {
zval *result;
result = zend_read_property(sundown_class_entry, getThis(), ZEND_STRS("extensions")-1, 0 TSRMLS_CC);
RETVAL_ZVAL(result, 1, 0);
}
}
/* }}} */
/* {{{ proto Sundown\Render\Base Sundown\Markdown::getRender()
*/
PHP_METHOD(sundown_markdown, getRender)
{
php_sundown_markdown_t *object = (php_sundown_markdown_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
RETVAL_ZVAL(object->render, 1, 0);
}
/* }}} */
/* {{{ proto void string Sundown\Markdown::setRender(Sundown\Render\Base $render)
*/
PHP_METHOD(sundown_markdown, setRender)
{
zval *render = NULL;
php_sundown_markdown_t *object = (php_sundown_markdown_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z", &render) == FAILURE) {
return;
}
if (!instanceof_function_ex(Z_OBJCE_P(render), sundown_render_base_class_entry, 0 TSRMLS_CC)) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "Render class must extend Sundown\\Render\\Base");
return;
}
zval_ptr_dtor(&object->render);
object->render = render;
Z_ADDREF_P(render);
}
/* }}} */
static zend_function_entry php_sundown_markdown_methods[] = {
PHP_ME(sundown_markdown, __construct, arginfo_sundown_markdown__construct, ZEND_ACC_PUBLIC)
PHP_ME(sundown_markdown, __destruct, NULL, ZEND_ACC_PUBLIC)
PHP_ME(sundown_markdown, render, arginfo_sundown_markdown_render, ZEND_ACC_PUBLIC)
PHP_ME(sundown_markdown, getRender, NULL, ZEND_ACC_PUBLIC)
PHP_ME(sundown_markdown, setRender, arginfo_sundown_markdown_set_render, ZEND_ACC_PUBLIC)
PHP_ME(sundown_markdown, getExtensions, NULL, ZEND_ACC_PUBLIC)
PHP_ME(sundown_markdown, setExtensions, arginfo_sundown_markdown_set_extensions, ZEND_ACC_PUBLIC)
PHP_ME(sundown_markdown, hasExtension, arginfo_sundown_markdown_has_extension, ZEND_ACC_PUBLIC)
PHP_ME(sundown_markdown, hasRenderFlag, arginfo_sundown_markdown_has_render_flag, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
void php_sundown_markdown_init(TSRMLS_D)
{
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, "Sundown", "Markdown", php_sundown_markdown_methods);
sundown_markdown_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
sundown_markdown_class_entry->create_object = php_sundown_markdown_new;
zend_declare_property_null(sundown_markdown_class_entry, ZEND_STRS("extensions")-1, ZEND_ACC_PUBLIC TSRMLS_CC);
if (!spl_ce_InvalidArgumentException) {
/* when I'm building this extension on windows box. I can't fix redefintion macro error. for now lookup the class */
zend_class_entry **pce;
if (zend_hash_find(CG(class_table), ZEND_STRS("invalidargumentexception")-1, (void **)&pce) == SUCCESS) {
spl_ce_InvalidArgumentException = *pce;
}
}
}