| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TMarkdown class file
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TMarkdown.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Web.UI.WebControls
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Using TTextHighlighter and MarkdownParser classes
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.Web.UI.WebControls.TTextHighlighter');
|
|
|
17 |
Prado::using('System.3rdParty.Markdown.MarkdownParser');
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* TMarkdown class
|
|
|
21 |
*
|
|
|
22 |
* TMarkdown is a control that produces HTML from code with markdown syntax.
|
|
|
23 |
*
|
|
|
24 |
* Markdown is a text-to-HTML conversion tool for web writers. Markdown allows
|
|
|
25 |
* you to write using an easy-to-read, easy-to-write plain text format, then
|
|
|
26 |
* convert it to structurally valid XHTML (or HTML).
|
|
|
27 |
* Further documentation regarding Markdown can be found at
|
|
|
28 |
* http://daringfireball.net/projects/markdown/
|
|
|
29 |
*
|
|
|
30 |
* To use TMarkdown, simply enclose the content to be rendered within
|
|
|
31 |
* the body of TMarkdown in a template.
|
|
|
32 |
*
|
|
|
33 |
* See http://www.pradosoft.com/demos/quickstart/?page=Markdown for
|
|
|
34 |
* details on the Markdown syntax usage.
|
|
|
35 |
*
|
|
|
36 |
* TMarkdown also performs syntax highlighting for code blocks whose language
|
|
|
37 |
* is recognized by {@link TTextHighlighter}.
|
|
|
38 |
* The language of a code block must be specified in the first line of the block
|
|
|
39 |
* and enclosed within a pair of square brackets (e.g. [php]).
|
|
|
40 |
*
|
|
|
41 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
42 |
* @version $Id: TMarkdown.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
43 |
* @package System.Web.UI.WebControls
|
|
|
44 |
* @since 3.0.1
|
|
|
45 |
*/
|
|
|
46 |
class TMarkdown extends TTextHighlighter
|
|
|
47 |
{
|
|
|
48 |
/**
|
|
|
49 |
* Processes a text string.
|
|
|
50 |
* This method is required by the parent class.
|
|
|
51 |
* @param string text string to be processed
|
|
|
52 |
* @return string the processed text result
|
|
|
53 |
*/
|
|
|
54 |
public function processText($text)
|
|
|
55 |
{
|
|
|
56 |
$renderer = new MarkdownParser;
|
|
|
57 |
$result = $renderer->parse($text);
|
|
|
58 |
return preg_replace_callback(
|
|
|
59 |
'/<pre><code>\[\s*(\w+)\s*\]\n+((.|\n)*?)\s*<\\/code><\\/pre>/im',
|
|
|
60 |
array($this, 'highlightCode'), $result);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Highlights source code using TTextHighlighter
|
|
|
65 |
* @param array matches of code blocks
|
|
|
66 |
* @return string highlighted code.
|
|
|
67 |
*/
|
|
|
68 |
protected function highlightCode($matches)
|
|
|
69 |
{
|
|
|
70 |
$text = html_entity_decode($matches[2],ENT_QUOTES,'UTF-8');
|
|
|
71 |
$this->setLanguage($matches[1]);
|
|
|
72 |
return parent::processText($text);
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
|