milesj / decoda

A lightweight lexical string parser for BBCode styled markup.
MIT License
196 stars 52 forks source link

Some missing filter and parser #99

Closed goldsky closed 9 years ago

goldsky commented 9 years ago

Hi, I figured that the $code->defaults(); is missing table filter.

    /**
     * Apply default filters and hooks if none are set.
     *
     * @return \Decoda\Decoda
     */
    public function defaults() {
        $this->addFilter(new \Decoda\Filter\DefaultFilter());
        $this->addFilter(new \Decoda\Filter\EmailFilter());
        $this->addFilter(new \Decoda\Filter\ImageFilter());
        $this->addFilter(new \Decoda\Filter\UrlFilter());
        $this->addFilter(new \Decoda\Filter\TextFilter());
        $this->addFilter(new \Decoda\Filter\BlockFilter());
        $this->addFilter(new \Decoda\Filter\VideoFilter());
        $this->addFilter(new \Decoda\Filter\CodeFilter());
        $this->addFilter(new \Decoda\Filter\QuoteFilter());
        $this->addFilter(new \Decoda\Filter\ListFilter());

        $this->addHook(new \Decoda\Hook\CensorHook());
        $this->addHook(new \Decoda\Hook\ClickableHook());

        return $this;
    }

(I needed to add table filter manually)

Second question, the ListFilter() doesn't parse [ul] and [ol] as http://www.bbcode.org/examples/?id=12 (I just copy-pasted the olist and list to fix them).

Are these intentionally?

milesj commented 9 years ago

I believe it's [list] and [olist], but I don't see why we couldn't add the HTML equivalent names.

goldsky commented 9 years ago

I'm using http://www.sceditor.com/ for my editor, which needs [ul] and [ol] parser.

So, on my experiment, this fix fits my goal:

<?php
/**
 * @copyright   2006-2014, Miles Johnson - http://milesj.me
 * @license     https://github.com/milesj/decoda/blob/master/license.md
 * @link        http://milesj.me/code/php/decoda
 */

namespace Decoda\Filter;

use Decoda\Decoda;

/**
 * Provides tags for ordered and unordered lists.
 */
class ListFilter extends AbstractFilter {

    const LIST_TYPE = '/^[-a-z]+$/i';

    /**
     * Supported tags.
     *
     * @type array
     */
    protected $_tags = array(
        'olist' => array(
            'htmlTag' => 'ol',
            'displayType' => Decoda::TYPE_BLOCK,
            'allowedTypes' => Decoda::TYPE_BOTH,
            'lineBreaks' => Decoda::NL_REMOVE,
            'childrenWhitelist' => array('li', '*'),
            'onlyTags' => true,
            'attributes' => array(
                'default' => array(self::LIST_TYPE, 'type-{default}')
            ),
            'mapAttributes' => array(
                'default' => 'class'
            ),
            'htmlAttributes' => array(
                'class' => 'decoda-olist'
            )
        ),
        'ol' => array(
            'htmlTag' => 'ol',
            'displayType' => Decoda::TYPE_BLOCK,
            'allowedTypes' => Decoda::TYPE_BOTH,
            'lineBreaks' => Decoda::NL_REMOVE,
            'childrenWhitelist' => array('li', '*'),
            'onlyTags' => true,
            'attributes' => array(
                'default' => array(self::LIST_TYPE, 'type-{default}')
            ),
            'mapAttributes' => array(
                'default' => 'class'
            ),
            'htmlAttributes' => array(
                'class' => 'decoda-olist'
            )
        ),
        'list' => array(
            'htmlTag' => 'ul',
            'displayType' => Decoda::TYPE_BLOCK,
            'allowedTypes' => Decoda::TYPE_BOTH,
            'lineBreaks' => Decoda::NL_REMOVE,
            'childrenWhitelist' => array('li', '*'),
            'onlyTags' => true,
            'attributes' => array(
                'default' => array(self::LIST_TYPE, 'type-{default}')
            ),
            'mapAttributes' => array(
                'default' => 'class'
            ),
            'htmlAttributes' => array(
                'class' => 'decoda-list'
            )
        ),
        'ul' => array(
            'htmlTag' => 'ul',
            'displayType' => Decoda::TYPE_BLOCK,
            'allowedTypes' => Decoda::TYPE_BOTH,
            'lineBreaks' => Decoda::NL_REMOVE,
            'childrenWhitelist' => array('li', '*'),
            'onlyTags' => true,
            'attributes' => array(
                'default' => array(self::LIST_TYPE, 'type-{default}')
            ),
            'mapAttributes' => array(
                'default' => 'class'
            ),
            'htmlAttributes' => array(
                'class' => 'decoda-list'
            )
        ),
        'li' => array(
            'htmlTag' => 'li',
            'displayType' => Decoda::TYPE_BLOCK,
            'allowedTypes' => Decoda::TYPE_BOTH,
            'parent' => array('olist', 'list', 'ol', 'ul')
        ),
        '*' => array(
            'htmlTag' => 'li',
            'displayType' => Decoda::TYPE_BLOCK,
            'allowedTypes' => Decoda::TYPE_BOTH,
            'childrenBlacklist' => array('olist', 'list', 'ol', 'ul', 'li'),
            'parent' => array('olist', 'list', 'ol', 'ul')
        )
    );

}
milesj commented 9 years ago

Added.