Permalink
Browse files

add hasRenderFlag method

  • Loading branch information...
1 parent cce30e8 commit e4f99339b38e26764ffca13000ed3ea13809195f @chobie committed Feb 19, 2012
View
50 php_sundown.c
@@ -33,6 +33,14 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_sundown__construct, 0, 0, 2)
ZEND_ARG_ARRAY_INFO(0, extensions, 0)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(arginfo_sundown_has_extension, 0, 0, 1)
+ ZEND_ARG_INFO(0, name)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_sundown_has_render_flag, 0, 0, 1)
+ ZEND_ARG_INFO(0, name)
+ZEND_END_ARG_INFO()
+
static void sundown__render(SundownRendererType render_type, INTERNAL_FUNCTION_PARAMETERS)
{
zval *object;
@@ -148,12 +156,54 @@ PHP_METHOD(sundown, toToc)
sundown__render(SUNDOWN_RENDER_TOC,INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
+
+/* {{{ proto string Sundown::hasExtension(string $ext_name)
+*/
+PHP_METHOD(sundown, 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(),"extensions",sizeof("extensions")-1, 0 TSRMLS_CC)) != IS_NULL) {
+ table = Z_ARRVAL_P(zend_read_property(sundown_class_entry, getThis(),"extensions",sizeof("extensions")-1, 0 TSRMLS_CC));
+ RETVAL_BOOL(php_sundown_has_ext(table, name));
+ }
+}
+
+/* {{{ proto string Sundown::hasRenderFlag(string $ext_name)
+*/
+PHP_METHOD(sundown, hasRenderFlag)
+{
+ 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(),"extensions",sizeof("extensions")-1, 0 TSRMLS_CC)) != IS_NULL) {
+ table = Z_ARRVAL_P(zend_read_property(sundown_class_entry, getThis(),"extensions",sizeof("extensions")-1, 0 TSRMLS_CC));
+ RETVAL_BOOL(php_sundown_has_ext(table, name));
+ }
+}
+
static zend_function_entry php_sundown_methods[] = {
PHP_ME(sundown, __construct, arginfo_sundown__construct, ZEND_ACC_PUBLIC)
PHP_ME(sundown, __destruct, NULL, ZEND_ACC_PUBLIC)
PHP_ME(sundown, toHtml, NULL, ZEND_ACC_PUBLIC)
PHP_ME(sundown, toToc, NULL, ZEND_ACC_PUBLIC)
PHP_ME(sundown, __toString, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(sundown, hasExtension,arginfo_sundown_has_extension,ZEND_ACC_PUBLIC)
+ PHP_ME(sundown, hasRenderFlag,arginfo_sundown_has_render_flag,ZEND_ACC_PUBLIC)
/* to_html and to_toc methods are compatible with Redcarpet */
PHP_MALIAS(sundown, to_html, toHtml, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(sundown, to_toc, toToc, NULL, ZEND_ACC_PUBLIC)
View
25 sundown_markdown.c
@@ -273,6 +273,10 @@ 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);
@@ -498,12 +502,33 @@ PHP_METHOD(sundown_markdown, hasExtension)
}
}
+/* {{{ proto string Sundown\Markdown::hasRenderFlag(string $ext_name)
+*/
+PHP_METHOD(sundown_markdown, hasRenderFlag)
+{
+ char *name;
+ int name_len = 0;
+ HashTable *table;
+ 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,
+ "s", &name, &name_len) == FAILURE) {
+ return;
+ }
+
+ if (Z_TYPE_P(zend_read_property(Z_OBJCE_P(object->render), object->render,"render_flags",sizeof("render_flags")-1, 0 TSRMLS_CC)) != IS_NULL) {
+ table = Z_ARRVAL_P(zend_read_property(Z_OBJCE_P(object->render), object->render,"render_flags",sizeof("render_flags")-1, 0 TSRMLS_CC));
+ RETVAL_BOOL(php_sundown_has_ext(table, name));
+ }
+}
+
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, 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}
};
View
124 tests/002-basic-enabled-extensions.phpt
@@ -0,0 +1,124 @@
+--TEST--
+Check for determine enabled extensions.
+--SKIPIF--
+<?php if (!extension_loaded("sundown")) print "skip"; ?>
+--FILE--
+<?php
+$markdown = new Sundown("hello world",array());
+$extensions = array("no_intraemphasis","tables","fenced_code_blocks","autolink","strikethrough","lax_html_blocks","space_after_header","superscript");
+
+$enabled = array();
+foreach ($extensions as $ext) {
+ $enabled[$ext] = true;
+}
+
+echo "default: all extensions are disabled\n";
+foreach ($extensions as $ext) {
+ var_dump($markdown->hasExtension($ext));
+}
+
+echo "enabled all:\n";
+$markdown = new Sundown("hello world",$enabled);
+foreach ($extensions as $ext) {
+ var_dump($markdown->hasExtension($ext));
+}
+
+foreach ($extensions as $ext) {
+ echo "only {$ext}:\n";
+ $markdown = new Sundown("hello world",array($ext => true));
+ foreach ($extensions as $ext2) {
+ var_dump($markdown->hasExtension($ext2));
+ }
+}
+
+--EXPECT--
+default: all extensions are disabled
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+enabled all:
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+only no_intraemphasis:
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+only tables:
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+only fenced_code_blocks:
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+only autolink:
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+only strikethrough:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+only lax_html_blocks:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+only space_after_header:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+only superscript:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
View
217 tests/002-basic-enabled-render-flags.phpt
@@ -0,0 +1,217 @@
+--TEST--
+Check for determine enabled extensions.
+--SKIPIF--
+<?php if (!extension_loaded("sundown")) print "skip"; ?>
+--FILE--
+<?php
+$r = new Sundown("http://chobie.org/");
+
+$flags = array("filter_html","no_images","no_links","no_styles","safe_links_only","with_toc_data","hard_wrap","xhtml");
+foreach ($flags as $flag) {
+ var_dump($r->hasRenderFlag($flag));
+}
+
+foreach ($flags as $flag) {
+ $r = new Sundown("http://chobie.org/", array($flag => true));
+ echo "test {$flag}:\n";
+ foreach ($flags as $f) {
+ var_dump($r->hasRenderFlag($f));
+ }
+}
+
+$extensions = array("no_intraemphasis","tables","fenced_code_blocks","autolink","strikethrough","lax_html_blocks","space_after_header","superscript");
+$exts = array();
+foreach ($extensions as $ext) {
+ $exts[$ext] = true;
+}
+
+$r = new Sundown("http://chobie.org/");
+echo "render flags and extensions should not conflict 1:\n";
+foreach ($flags as $flag) {
+ var_dump($r->hasRenderFlag($flag));
+}
+echo "render flags and extensions should not conflict 2:\n";
+$r = new Sundown("http://chobie.org/",$exts);
+foreach ($flags as $flag) {
+ var_dump($r->hasRenderFlag($flag));
+}
+
+foreach ($flags as $flag) {
+ $r = new Sundown("http://chobie.org/", array_merge($exts,array($flag => true)));
+ echo "test {$flag}:\n";
+ foreach ($flags as $f) {
+ var_dump($r->hasRenderFlag($f));
+ }
+}
+
+--EXPECT--
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test filter_html:
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_images:
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_links:
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_styles:
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test safe_links_only:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+test with_toc_data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+test hard_wrap:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+test xhtml:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+render flags and extensions should not conflict 1:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+render flags and extensions should not conflict 2:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test filter_html:
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_images:
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_links:
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_styles:
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test safe_links_only:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+test with_toc_data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+test hard_wrap:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+test xhtml:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
View
205 tests/003-advanced-enabled-render-flags.phpt
@@ -0,0 +1,205 @@
+--TEST--
+Check for determine enabled extensions.
+--SKIPIF--
+<?php if (!extension_loaded("sundown")) print "skip"; ?>
+--FILE--
+<?php
+$render = new Sundown\Render\HTML(array());
+$r = new Sundown\Markdown($render, array());
+
+$flags = array("filter_html","no_images","no_links","no_styles","safe_links_only","with_toc_data","hard_wrap","xhtml");
+foreach ($flags as $flag) {
+ var_dump($r->hasRenderFlag($flag));
+}
+
+foreach ($flags as $flag) {
+ $render = new Sundown\Render\HTML(array($flag=>true));
+ $r = new Sundown\Markdown($render, array());
+ echo "test {$flag}:\n";
+ foreach ($flags as $f) {
+ var_dump($r->hasRenderFlag($f));
+ }
+}
+
+$extensions = array("no_intraemphasis","tables","fenced_code_blocks","autolink","strikethrough","lax_html_blocks","space_after_header","superscript");
+$exts = array();
+foreach ($extensions as $ext) {
+ $exts[$ext] = true;
+}
+
+echo "render flags and extensions should not conflict 2:\n";
+$r = new Sundown\Markdown(new Sundown\Render\HTML(),$exts);
+foreach ($flags as $flag) {
+ var_dump($r->hasRenderFlag($flag));
+}
+
+foreach ($flags as $flag) {
+ $r = new Sundown\Markdown(new Sundown\Render\HTML(array($flag => true)),$exts);
+ echo "test {$flag}:\n";
+ foreach ($flags as $f) {
+ var_dump($r->hasRenderFlag($f));
+ }
+}
+
+--EXPECT--
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test filter_html:
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_images:
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_links:
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_styles:
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test safe_links_only:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+test with_toc_data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+test hard_wrap:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+test xhtml:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+render flags and extensions should not conflict 2:
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+test filter_html:
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_images:
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_links:
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test no_styles:
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+test safe_links_only:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+test with_toc_data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+bool(false)
+test hard_wrap:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)
+bool(false)
+test xhtml:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(true)

0 comments on commit e4f9933

Please sign in to comment.