1: <?php
2: /**
3: * CMarkdown class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CMarkdown converts the captured content from markdown syntax to HTML code.
13: *
14: * CMarkdown can be used as either a widget or a filter. It is a wrapper of {@link CMarkdownParser}.
15: * CMarkdown adds an additional option {@link purifyOutput} which can be set true
16: * so that the converted HTML code is purified before being displayed.
17: *
18: * For details about the markdown syntax, please check the following:
19: * <ul>
20: * <li>{@link http://daringfireball.net/projects/markdown/syntax official markdown syntax}</li>
21: * <li>{@link http://michelf.com/projects/php-markdown/extra/ markdown extra syntax}</li>
22: * <li>{@link CMarkdownParser markdown with syntax highlighting}</li>
23: * </ul>
24: *
25: * @property CMarkdownParser $markdownParser The parser instance.
26: *
27: * @author Qiang Xue <qiang.xue@gmail.com>
28: * @package system.web.widgets
29: * @since 1.0
30: */
31: class CMarkdown extends COutputProcessor
32: {
33: /**
34: * @var mixed the CSS file used for the widget. Defaults to null, meaning
35: * using the default CSS file included together with the widget.
36: * If false, no CSS file will be used. Otherwise, the specified CSS file
37: * will be included when using this widget.
38: */
39: public $cssFile;
40: /**
41: * @var boolean whether to use {@link CHtmlPurifier} to purify the generated HTML code. Defaults to false.
42: */
43: public $purifyOutput=false;
44:
45: private $_parser;
46:
47: /**
48: * Processes the captured output.
49: * This method converts the content in markdown syntax to HTML code.
50: * If {@link purifyOutput} is true, the HTML code will also be purified.
51: * @param string $output the captured output to be processed
52: * @see convert
53: */
54: public function processOutput($output)
55: {
56: $output=$this->transform($output);
57: if($this->purifyOutput)
58: {
59: $purifier=new CHtmlPurifier;
60: $output=$purifier->purify($output);
61: }
62: parent::processOutput($output);
63: }
64:
65: /**
66: * Converts the content in markdown syntax to HTML code.
67: * This method uses {@link CMarkdownParser} to do the conversion.
68: * @param string $output the content to be converted
69: * @return string the converted content
70: */
71: public function transform($output)
72: {
73: $this->registerClientScript();
74: return $this->getMarkdownParser()->transform($output);
75: }
76:
77: /**
78: * Registers the needed CSS and JavaScript.
79: */
80: public function registerClientScript()
81: {
82: if($this->cssFile!==false)
83: self::registerCssFile($this->cssFile);
84: }
85:
86: /**
87: * Registers the needed CSS file.
88: * @param string $url the CSS URL. If null, a default CSS URL will be used.
89: */
90: public static function registerCssFile($url=null)
91: {
92: CTextHighlighter::registerCssFile($url);
93: }
94:
95: /**
96: * Returns the markdown parser instance.
97: * This method calls {@link createMarkdownParser} to create the parser instance.
98: * Call this method multipe times will only return the same instance.
99: * @return CMarkdownParser the parser instance
100: */
101: public function getMarkdownParser()
102: {
103: if($this->_parser===null)
104: $this->_parser=$this->createMarkdownParser();
105: return $this->_parser;
106: }
107:
108: /**
109: * Creates a markdown parser.
110: * By default, this method creates a {@link CMarkdownParser} instance.
111: * @return CMarkdownParser the markdown parser.
112: */
113: protected function createMarkdownParser()
114: {
115: return new CMarkdownParser;
116: }
117: }
118: