/library/Zend/Markup/Parser/Textile.php
PHP | 570 lines | 376 code | 66 blank | 128 comment | 70 complexity | 4f9c37542a3808fba993e2b79841e9f9 MD5 | raw file
Possible License(s): AGPL-1.0
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Markup
- * @subpackage Parser
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id: Textile.php 24594 2012-01-05 21:27:01Z matthew $
- */
- /**
- * @see Zend_Markup_TokenList
- */
- require_once 'Zend/Markup/TokenList.php';
- /**
- * @see Zend_Markup_Parser_ParserInterface
- */
- require_once 'Zend/Markup/Parser/ParserInterface.php';
- /**
- * @category Zend
- * @package Zend_Markup
- * @subpackage Parser
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Markup_Parser_Textile implements Zend_Markup_Parser_ParserInterface
- {
- const STATE_SCAN = 0;
- const STATE_NEW_PARAGRAPH = 1;
- const STATE_NEWLINE = 2;
- const MATCH_ATTR_CLASSID = '\((?<attr_class>[a-zA-Z0-9_]+)?(?:\#(?<attr_id>[a-zA-Z0-9_]+))?\)';
- const MATCH_ATTR_STYLE = "\{(?<attr_style>[^\}\n]+)\}";
- const MATCH_ATTR_LANG = '\[(?<attr_lang>[a-zA-Z_]+)\]';
- const MATCH_ATTR_ALIGN = '(?<attr_align>\<\>?|\>|=)';
- /**
- * Token tree
- *
- * @var Zend_Markup_TokenList
- */
- protected $_tree;
- /**
- * Current token
- *
- * @var Zend_Markup_Token
- */
- protected $_current;
- /**
- * Source to tokenize
- *
- * @var string
- */
- protected $_value = '';
- /**
- * Length of the value
- *
- * @var int
- */
- protected $_valueLen = 0;
- /**
- * Current pointer
- *
- * @var int
- */
- protected $_pointer = 0;
- /**
- * The buffer
- *
- * @var string
- */
- protected $_buffer = '';
- /**
- * Simple tag translation
- *
- * @var array
- */
- protected $_simpleTags = array(
- '*' => 'strong',
- '**' => 'bold',
- '_' => 'emphasized',
- '__' => 'italic',
- '??' => 'citation',
- '-' => 'deleted',
- '+' => 'insert',
- '^' => 'superscript',
- '~' => 'subscript',
- '%' => 'span',
- // these are a little more complicated
- '@' => 'code',
- '!' => 'img',
- );
- /**
- * Token array
- *
- * @var array
- */
- protected $_tokens = array();
- /**
- * Prepare the parsing of a Textile string, the real parsing is done in {@link _parse()}
- *
- * @param string $value
- *
- * @return array
- */
- public function parse($value)
- {
- if (!is_string($value)) {
- /**
- * @see Zend_Markup_Parser_Exception
- */
- require_once 'Zend/Markup/Parser/Exception.php';
- throw new Zend_Markup_Parser_Exception('Value to parse should be a string.');
- }
- if (empty($value)) {
- /**
- * @see Zend_Markup_Parser_Exception
- */
- require_once 'Zend/Markup/Parser/Exception.php';
- throw new Zend_Markup_Parser_Exception('Value to parse cannot be left empty.');
- }
- // first make we only have LF newlines, also trim the value
- $this->_value = str_replace(array("\r\n", "\r"), "\n", $value);
- $this->_value = trim($this->_value);
- // initialize variables and tokenize
- $this->_valueLen = iconv_strlen($this->_value, 'UTF-8');
- $this->_pointer = 0;
- $this->_buffer = '';
- $this->_temp = array();
- $this->_tokens = array();
- $this->_tokenize();
- // create the tree
- $this->_tree = new Zend_Markup_TokenList();
- $this->_current = new Zend_Markup_Token('', Zend_Markup_Token::TYPE_NONE, 'Zend_Markup_Root');
- $this->_tree->addChild($this->_current);
- $this->_createTree();
- return $this->_tree;
- }
- /**
- * Tokenize a textile string
- *
- * @return array
- */
- protected function _tokenize()
- {
- $state = self::STATE_NEW_PARAGRAPH;
- $attrsMatch = implode('|', array(
- self::MATCH_ATTR_CLASSID,
- self::MATCH_ATTR_STYLE,
- self::MATCH_ATTR_LANG,
- self::MATCH_ATTR_ALIGN
- ));
- $paragraph = '';
- while ($this->_pointer < $this->_valueLen) {
- switch ($state) {
- case self::STATE_SCAN:
- $matches = array(); //[^\n*_?+~%@!-]
- $acronym = '(?<acronym>[A-Z]{2,})\((?<title>[^\)]+)\)';
- $regex = '#\G(?<text>.*?)(?:'
- . "(?:(?<nl_paragraph>\n{2,})|(?<nl_break>\n))|"
- . '(?<tag>'
- . "(?<name>\*{1,2}|_{1,2}|\?{2}|\-|\+|\~|\^|%|@|!|$|{$acronym}"
- . '|":(?<url>[^\s]+)|")'
- . "(?:{$attrsMatch})*)"
- . ')#si';
- preg_match($regex, $this->_value, $matches, null, $this->_pointer);
- $this->_pointer += strlen($matches[0]);
- if (!empty($matches['text'])) {
- $this->_buffer .= $matches['text'];
- }
- // first add the buffer
- if (!empty($this->_buffer)) {
- $this->_tokens[] = array(
- 'tag' => $this->_buffer,
- 'type' => Zend_Markup_Token::TYPE_NONE
- );
- $this->_buffer = '';
- }
- if (!empty($matches['nl_paragraph'])) {
- $this->_temp = array(
- 'tag' => $matches['nl_paragraph'],
- 'name' => 'p',
- 'type' => Zend_Markup_Token::TYPE_TAG,
- 'attributes' => array()
- );
- $state = self::STATE_NEW_PARAGRAPH;
- } elseif (!empty($matches['nl_break'])) {
- $this->_tokens[] = array(
- 'tag' => $matches['nl_break'],
- 'name' => 'break',
- 'type' => Zend_Markup_Token::TYPE_TAG,
- 'attributes' => array()
- );
- $state = self::STATE_NEWLINE;
- } elseif (!empty($matches['tag'])) {
- if (isset($this->_simpleTags[$matches['name']])) {
- // now add the new token
- $this->_tokens[] = array(
- 'tag' => $matches['tag'],
- 'type' => Zend_Markup_Token::TYPE_TAG,
- 'name' => $this->_simpleTags[$matches['name']],
- 'attributes' => $this->_extractAttributes($matches)
- );
- } else {
-