diff options
author | emkael <emkael@tlen.pl> | 2016-02-24 23:18:07 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-02-24 23:18:07 +0100 |
commit | 6f7fdef0f500cd4bb540affd3bc1482243f337c1 (patch) | |
tree | 4853eecd0769a903e6130c1896e1d070848150dd /lib/prado/framework/Web/UI/WebControls/TMarkdown.php | |
parent | 61f2ea48a4e11cb5fb941b3783e19c9e9ef38a45 (diff) |
* Prado 3.3.0
Diffstat (limited to 'lib/prado/framework/Web/UI/WebControls/TMarkdown.php')
-rw-r--r-- | lib/prado/framework/Web/UI/WebControls/TMarkdown.php | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/prado/framework/Web/UI/WebControls/TMarkdown.php b/lib/prado/framework/Web/UI/WebControls/TMarkdown.php new file mode 100644 index 0000000..7a99b1f --- /dev/null +++ b/lib/prado/framework/Web/UI/WebControls/TMarkdown.php @@ -0,0 +1,72 @@ +<?php +/** + * TMarkdown class file + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link https://github.com/pradosoft/prado + * @copyright Copyright © 2005-2015 The PRADO Group + * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT + * @package System.Web.UI.WebControls + */ + +/** + * Using TTextHighlighter and MarkdownParser classes + */ +Prado::using('System.Web.UI.WebControls.TTextHighlighter'); +Prado::using('System.3rdParty.Parsedown.Parsedown'); + +/** + * TMarkdown class + * + * TMarkdown is a control that produces HTML from code with markdown syntax. + * + * Markdown is a text-to-HTML conversion tool for web writers. Markdown allows + * you to write using an easy-to-read, easy-to-write plain text format, then + * convert it to structurally valid XHTML (or HTML). + * Further documentation regarding Markdown can be found at + * http://daringfireball.net/projects/markdown/ + * + * To use TMarkdown, simply enclose the content to be rendered within + * the body of TMarkdown in a template. + * + * See https://daringfireball.net/projects/markdown/basics for + * details on the Markdown syntax usage. + * + * TMarkdown also performs syntax highlighting for code blocks whose language + * is recognized by {@link TTextHighlighter}. + * The language of a code block must be specified in the first line of the block + * and enclosed within a pair of square brackets (e.g. [php]). + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @package System.Web.UI.WebControls + * @since 3.0.1 + */ +class TMarkdown extends TTextHighlighter +{ + /** + * Processes a text string. + * This method is required by the parent class. + * @param string text string to be processed + * @return string the processed text result + */ + public function processText($text) + { + $result = Parsedown::instance()->parse($text); + return preg_replace_callback( + '/<pre><code class="language-(\w+)">((.|\n)*?)<\\/code><\\/pre>/im', + array($this, 'highlightCode'), $result); + } + + /** + * Highlights source code using TTextHighlighter + * @param array matches of code blocks + * @return string highlighted code. + */ + protected function highlightCode($matches) + { + $text = html_entity_decode($matches[2],ENT_QUOTES,'UTF-8'); + $this->setLanguage($matches[1]); + return parent::processText($text); + } +} + |