readme-parser.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * This is a slightly modified version of github.com/markjaquith/WordPress-Plugin-Readme-Parser
  4. * It uses Parsedown instead of the "Markdown Extra" parser.
  5. */
  6. Class PucReadmeParser {
  7. function __construct() {
  8. // This space intentially blank
  9. }
  10. function parse_readme( $file ) {
  11. $file_contents = @implode('', @file($file));
  12. return $this->parse_readme_contents( $file_contents );
  13. }
  14. function parse_readme_contents( $file_contents ) {
  15. $file_contents = str_replace(array("\r\n", "\r"), "\n", $file_contents);
  16. $file_contents = trim($file_contents);
  17. if ( 0 === strpos( $file_contents, "\xEF\xBB\xBF" ) )
  18. $file_contents = substr( $file_contents, 3 );
  19. // Markdown transformations
  20. $file_contents = preg_replace( "|^###([^#]+)#*?\s*?\n|im", '=$1='."\n", $file_contents );
  21. $file_contents = preg_replace( "|^##([^#]+)#*?\s*?\n|im", '==$1=='."\n", $file_contents );
  22. $file_contents = preg_replace( "|^#([^#]+)#*?\s*?\n|im", '===$1==='."\n", $file_contents );
  23. // === Plugin Name ===
  24. // Must be the very first thing.
  25. if ( !preg_match('|^===(.*)===|', $file_contents, $_name) )
  26. return array(); // require a name
  27. $name = trim($_name[1], '=');
  28. $name = $this->sanitize_text( $name );
  29. $file_contents = $this->chop_string( $file_contents, $_name[0] );
  30. // Requires at least: 1.5
  31. if ( preg_match('|Requires at least:(.*)|i', $file_contents, $_requires_at_least) )
  32. $requires_at_least = $this->sanitize_text($_requires_at_least[1]);
  33. else
  34. $requires_at_least = NULL;
  35. // Tested up to: 2.1
  36. if ( preg_match('|Tested up to:(.*)|i', $file_contents, $_tested_up_to) )
  37. $tested_up_to = $this->sanitize_text( $_tested_up_to[1] );
  38. else
  39. $tested_up_to = NULL;
  40. // Stable tag: 10.4-ride-the-fire-eagle-danger-day
  41. if ( preg_match('|Stable tag:(.*)|i', $file_contents, $_stable_tag) )
  42. $stable_tag = $this->sanitize_text( $_stable_tag[1] );
  43. else
  44. $stable_tag = NULL; // we assume trunk, but don't set it here to tell the difference between specified trunk and default trunk
  45. // Tags: some tag, another tag, we like tags
  46. if ( preg_match('|Tags:(.*)|i', $file_contents, $_tags) ) {
  47. $tags = preg_split('|,[\s]*?|', trim($_tags[1]));
  48. foreach ( array_keys($tags) as $t )
  49. $tags[$t] = $this->sanitize_text( $tags[$t] );
  50. } else {
  51. $tags = array();
  52. }
  53. // Contributors: markjaquith, mdawaffe, zefrank
  54. $contributors = array();
  55. if ( preg_match('|Contributors:(.*)|i', $file_contents, $_contributors) ) {
  56. $temp_contributors = preg_split('|,[\s]*|', trim($_contributors[1]));
  57. foreach ( array_keys($temp_contributors) as $c ) {
  58. $tmp_sanitized = $this->user_sanitize( $temp_contributors[$c] );
  59. if ( strlen(trim($tmp_sanitized)) > 0 )
  60. $contributors[$c] = $tmp_sanitized;
  61. unset($tmp_sanitized);
  62. }
  63. }
  64. // Donate Link: URL
  65. if ( preg_match('|Donate link:(.*)|i', $file_contents, $_donate_link) )
  66. $donate_link = esc_url( $_donate_link[1] );
  67. else
  68. $donate_link = NULL;
  69. // togs, conts, etc are optional and order shouldn't matter. So we chop them only after we've grabbed their values.
  70. foreach ( array('tags', 'contributors', 'requires_at_least', 'tested_up_to', 'stable_tag', 'donate_link') as $chop ) {
  71. if ( $$chop ) {
  72. $_chop = '_' . $chop;
  73. $file_contents = $this->chop_string( $file_contents, ${$_chop}[0] );
  74. }
  75. }
  76. $file_contents = trim($file_contents);
  77. // short-description fu
  78. if ( !preg_match('/(^(.*?))^[\s]*=+?[\s]*.+?[\s]*=+?/ms', $file_contents, $_short_description) )
  79. $_short_description = array( 1 => &$file_contents, 2 => &$file_contents );
  80. $short_desc_filtered = $this->sanitize_text( $_short_description[2] );
  81. $short_desc_length = strlen($short_desc_filtered);
  82. $short_description = substr($short_desc_filtered, 0, 150);
  83. if ( $short_desc_length > strlen($short_description) )
  84. $truncated = true;
  85. else
  86. $truncated = false;
  87. if ( $_short_description[1] )
  88. $file_contents = $this->chop_string( $file_contents, $_short_description[1] ); // yes, the [1] is intentional
  89. // == Section ==
  90. // Break into sections
  91. // $_sections[0] will be the title of the first section, $_sections[1] will be the content of the first section
  92. // the array alternates from there: title2, content2, title3, content3... and so forth
  93. $_sections = preg_split('/^[\s]*==[\s]*(.+?)[\s]*==/m', $file_contents, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
  94. $sections = array();
  95. for ( $i=1; $i <= count($_sections); $i +=2 ) {
  96. $_sections[$i] = preg_replace('/^[\s]*=[\s]+(.+?)[\s]+=/m', '<h4>$1</h4>', $_sections[$i]);
  97. $_sections[$i] = $this->filter_text( $_sections[$i], true );
  98. $title = $this->sanitize_text( $_sections[$i-1] );
  99. $sections[str_replace(' ', '_', strtolower($title))] = array('title' => $title, 'content' => $_sections[$i]);
  100. }
  101. // Special sections
  102. // This is where we nab our special sections, so we can enforce their order and treat them differently, if needed
  103. // upgrade_notice is not a section, but parse it like it is for now
  104. $final_sections = array();
  105. foreach ( array('description', 'installation', 'frequently_asked_questions', 'screenshots', 'changelog', 'change_log', 'upgrade_notice') as $special_section ) {
  106. if ( isset($sections[$special_section]) ) {
  107. $final_sections[$special_section] = $sections[$special_section]['content'];
  108. unset($sections[$special_section]);
  109. }
  110. }
  111. if ( isset($final_sections['change_log']) && empty($final_sections['changelog']) )
  112. $final_sections['changelog'] = $final_sections['change_log'];
  113. $final_screenshots = array();
  114. if ( isset($final_sections['screenshots']) ) {
  115. preg_match_all('|<li>(.*?)</li>|s', $final_sections['screenshots'], $screenshots, PREG_SET_ORDER);
  116. if ( $screenshots ) {
  117. foreach ( (array) $screenshots as $ss )
  118. $final_screenshots[] = $ss[1];
  119. }
  120. }
  121. // Parse the upgrade_notice section specially:
  122. // 1.0 => blah, 1.1 => fnord
  123. $upgrade_notice = array();
  124. if ( isset($final_sections['upgrade_notice']) ) {
  125. $split = preg_split( '#<h4>(.*?)</h4>#', $final_sections['upgrade_notice'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
  126. for ( $i = 0; $i < count( $split ); $i += 2 )
  127. $upgrade_notice[$this->sanitize_text( $split[$i] )] = substr( $this->sanitize_text( $split[$i + 1] ), 0, 300 );
  128. unset( $final_sections['upgrade_notice'] );
  129. }
  130. // No description?
  131. // No problem... we'll just fall back to the old style of description
  132. // We'll even let you use markup this time!
  133. $excerpt = false;
  134. if ( !isset($final_sections['description']) ) {
  135. $final_sections = array_merge(array('description' => $this->filter_text( $_short_description[2], true )), $final_sections);
  136. $excerpt = true;
  137. }
  138. // dump the non-special sections into $remaining_content
  139. // their order will be determined by their original order in the readme.txt
  140. $remaining_content = '';
  141. foreach ( $sections as $s_name => $s_data ) {
  142. $remaining_content .= "\n<h3>{$s_data['title']}</h3>\n{$s_data['content']}";
  143. }
  144. $remaining_content = trim($remaining_content);
  145. // All done!
  146. // $r['tags'] and $r['contributors'] are simple arrays
  147. // $r['sections'] is an array with named elements
  148. $r = array(
  149. 'name' => $name,
  150. 'tags' => $tags,
  151. 'requires_at_least' => $requires_at_least,
  152. 'tested_up_to' => $tested_up_to,
  153. 'stable_tag' => $stable_tag,
  154. 'contributors' => $contributors,
  155. 'donate_link' => $donate_link,
  156. 'short_description' => $short_description,
  157. 'screenshots' => $final_screenshots,
  158. 'is_excerpt' => $excerpt,
  159. 'is_truncated' => $truncated,
  160. 'sections' => $final_sections,
  161. 'remaining_content' => $remaining_content,
  162. 'upgrade_notice' => $upgrade_notice
  163. );
  164. return $r;
  165. }
  166. function chop_string( $string, $chop ) { // chop a "prefix" from a string: Agressive! uses strstr not 0 === strpos
  167. if ( $_string = strstr($string, $chop) ) {
  168. $_string = substr($_string, strlen($chop));
  169. return trim($_string);
  170. } else {
  171. return trim($string);
  172. }
  173. }
  174. function user_sanitize( $text, $strict = false ) { // whitelisted chars
  175. if ( function_exists('user_sanitize') ) // bbPress native
  176. return user_sanitize( $text, $strict );
  177. if ( $strict ) {
  178. $text = preg_replace('/[^a-z0-9-]/i', '', $text);
  179. $text = preg_replace('|-+|', '-', $text);
  180. } else {
  181. $text = preg_replace('/[^a-z0-9_-]/i', '', $text);
  182. }
  183. return $text;
  184. }
  185. function sanitize_text( $text ) { // not fancy
  186. $text = strip_tags($text);
  187. $text = esc_html($text);
  188. $text = trim($text);
  189. return $text;
  190. }
  191. function filter_text( $text, $markdown = false ) { // fancy, Markdown
  192. $text = trim($text);
  193. $text = call_user_func( array( __CLASS__, 'code_trick' ), $text, $markdown ); // A better parser than Markdown's for: backticks -> CODE
  194. if ( $markdown ) { // Parse markdown.
  195. if ( !class_exists('Parsedown', false) ) {
  196. require_once(dirname(__FILE__) . '/Parsedown.php');
  197. }
  198. $instance = Parsedown::instance();
  199. $text = $instance->text($text);
  200. }
  201. $allowed = array(
  202. 'a' => array(
  203. 'href' => array(),
  204. 'title' => array(),
  205. 'rel' => array()),
  206. 'blockquote' => array('cite' => array()),
  207. 'br' => array(),
  208. 'p' => array(),
  209. 'code' => array(),
  210. 'pre' => array(),
  211. 'em' => array(),
  212. 'strong' => array(),
  213. 'ul' => array(),
  214. 'ol' => array(),
  215. 'li' => array(),
  216. 'h3' => array(),
  217. 'h4' => array()
  218. );
  219. $text = balanceTags($text);
  220. $text = wp_kses( $text, $allowed );
  221. $text = trim($text);
  222. return $text;
  223. }
  224. function code_trick( $text, $markdown ) { // Don't use bbPress native function - it's incompatible with Markdown
  225. // If doing markdown, first take any user formatted code blocks and turn them into backticks so that
  226. // markdown will preserve things like underscores in code blocks
  227. if ( $markdown )
  228. $text = preg_replace_callback("!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", array( __CLASS__,'decodeit'), $text);
  229. $text = str_replace(array("\r\n", "\r"), "\n", $text);
  230. if ( !$markdown ) {
  231. // This gets the "inline" code blocks, but can't be used with Markdown.
  232. $text = preg_replace_callback("|(`)(.*?)`|", array( __CLASS__, 'encodeit'), $text);
  233. // This gets the "block level" code blocks and converts them to PRE CODE
  234. $text = preg_replace_callback("!(^|\n)`(.*?)`!s", array( __CLASS__, 'encodeit'), $text);
  235. } else {
  236. // Markdown can do inline code, we convert bbPress style block level code to Markdown style
  237. $text = preg_replace_callback("!(^|\n)([ \t]*?)`(.*?)`!s", array( __CLASS__, 'indent'), $text);
  238. }
  239. return $text;
  240. }
  241. function indent( $matches ) {
  242. $text = $matches[3];
  243. $text = preg_replace('|^|m', $matches[2] . ' ', $text);
  244. return $matches[1] . $text;
  245. }
  246. function encodeit( $matches ) {
  247. if ( function_exists('encodeit') ) // bbPress native
  248. return encodeit( $matches );
  249. $text = trim($matches[2]);
  250. $text = htmlspecialchars($text, ENT_QUOTES);
  251. $text = str_replace(array("\r\n", "\r"), "\n", $text);
  252. $text = preg_replace("|\n\n\n+|", "\n\n", $text);
  253. $text = str_replace('&amp;lt;', '&lt;', $text);
  254. $text = str_replace('&amp;gt;', '&gt;', $text);
  255. $text = "<code>$text</code>";
  256. if ( "`" != $matches[1] )
  257. $text = "<pre>$text</pre>";
  258. return $text;
  259. }
  260. function decodeit( $matches ) {
  261. if ( function_exists('decodeit') ) // bbPress native
  262. return decodeit( $matches );
  263. $text = $matches[2];
  264. $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES));
  265. $text = strtr($text, $trans_table);
  266. $text = str_replace('<br />', '', $text);
  267. $text = str_replace('&#38;', '&', $text);
  268. $text = str_replace('&#39;', "'", $text);
  269. if ( '<pre><code>' == $matches[1] )
  270. $text = "\n$text\n";
  271. return "`$text`";
  272. }
  273. } // end class
  274. Class Automattic_Readme extends PucReadmeParser {}