summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TMarkdown.php
blob: fc4b9bdb4fcc3632b394f0dee3d52150db61d44c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
 * TMarkdown class file
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2011 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Web.UI.WebControls
 */

/**
 * Using TTextHighlighter and MarkdownParser classes
 */
Prado::using('System.Web.UI.WebControls.TTextHighlighter');
Prado::using('System.3rdParty.Markdown.MarkdownParser');

/**
 * 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 http://www.pradosoft.com/demos/quickstart/?page=Markdown 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>
 * @version $Id$
 * @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)
	{
		$renderer = new MarkdownParser;
		$result = $renderer->parse($text);
		return preg_replace_callback(
				'/<pre><code>\[\s*(\w+)\s*\]\n+((.|\n)*?)\s*<\\/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);
	}
}