CMarkdown.php 3.4 KB

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