Skip to content

Commit

Permalink
add Sundown\Markdown::setRender(Sundown\Render\Base $render);
Browse files Browse the repository at this point in the history
  • Loading branch information
chobie committed May 4, 2012
1 parent 6b9fe9b commit 1e3bf31
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sundown_markdown.c
Expand Up @@ -269,6 +269,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_sundown_markdown_render, 0, 0, 1)
ZEND_ARG_INFO(0, body) ZEND_ARG_INFO(0, body)
ZEND_END_ARG_INFO() 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_BEGIN_ARG_INFO_EX(arginfo_sundown_markdown_set_extensions, 0, 0, 1)
ZEND_ARG_INFO(0, extension) ZEND_ARG_INFO(0, extension)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -590,11 +595,35 @@ PHP_METHOD(sundown_markdown, getRender)
} }
/* }}} */ /* }}} */


/* {{{ 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[] = { static zend_function_entry php_sundown_markdown_methods[] = {
PHP_ME(sundown_markdown, __construct, arginfo_sundown_markdown__construct, ZEND_ACC_PUBLIC) 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, __destruct, NULL, ZEND_ACC_PUBLIC)
PHP_ME(sundown_markdown, render, arginfo_sundown_markdown_render, 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, 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, getExtensions, NULL, ZEND_ACC_PUBLIC)
PHP_ME(sundown_markdown, setExtensions, arginfo_sundown_markdown_set_extensions, 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, hasExtension, arginfo_sundown_markdown_has_extension, ZEND_ACC_PUBLIC)
Expand Down
58 changes: 58 additions & 0 deletions tests/003-advanced-set-render.phpt
@@ -0,0 +1,58 @@
--TEST--
Check for Sundown\Markdown::setRender
--SKIPIF--
<?php if (!extension_loaded("sundown")) print "skip"; ?>
--FILE--
<?php
$render = new Sundown\Render\HTML_TOC();
$data = <<<EOH
# Hello World
lorem ipsum dolar sit amet
## the World, What a beautiful it is.
lorem ipsum dolar sit amet
EOH;

$md = new Sundown\Markdown($render);
echo "TOC_DATA\n";
echo $md->render($data);
echo "DATA\n";
$md->setRender(new Sundown\Render\HTML(array("with_toc_data"=>true)));
echo $md->render($data);
$render = new Sundown\Render\HTML(array("with_toc_data"=>true));
$md->setRender($render);
echo $md->render($data);
$md->setRender(new Sundown\Render\HTML(array("with_toc_data"=>true)));
echo "#check render was not destroyed\n";
if($render instanceof Sundown\Render\HTML) {
echo "OK";
} else {
echo "FAILURE";
}

--EXPECT--
TOC_DATA
<ul>
<li>
<a href="#toc_0">Hello World</a>
<ul>
<li>
<a href="#toc_1">the World, What a beautiful it is.</a>
</li>
</ul>
</li>
</ul>
DATA
<h1 id="toc_0">Hello World</h1>
<p>lorem ipsum dolar sit amet</p>
<h2 id="toc_1">the World, What a beautiful it is.</h2>
<p>lorem ipsum dolar sit amet</p>
<h1 id="toc_0">Hello World</h1>
<p>lorem ipsum dolar sit amet</p>
<h2 id="toc_1">the World, What a beautiful it is.</h2>
<p>lorem ipsum dolar sit amet</p>
#check render was not destroyed
OK

0 comments on commit 1e3bf31

Please sign in to comment.