markdown.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Plugin Markdown.
  4. *
  5. * Shaare's descriptions are parsed with Markdown.
  6. */
  7. require_once 'Parsedown.php';
  8. /**
  9. * Parse linklist descriptions.
  10. *
  11. * @param array $data linklist data.
  12. *
  13. * @return mixed linklist data parsed in markdown (and converted to HTML).
  14. */
  15. function hook_markdown_render_linklist($data)
  16. {
  17. foreach ($data['links'] as &$value) {
  18. $value['description'] = process_markdown($value['description']);
  19. }
  20. return $data;
  21. }
  22. /**
  23. * Parse daily descriptions.
  24. *
  25. * @param array $data daily data.
  26. *
  27. * @return mixed daily data parsed in markdown (and converted to HTML).
  28. */
  29. function hook_markdown_render_daily($data)
  30. {
  31. // Manipulate columns data
  32. foreach ($data['cols'] as &$value) {
  33. foreach ($value as &$value2) {
  34. $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
  35. }
  36. }
  37. return $data;
  38. }
  39. /**
  40. * When link list is displayed, include markdown CSS.
  41. *
  42. * @param array $data includes data.
  43. *
  44. * @return mixed - includes data with markdown CSS file added.
  45. */
  46. function hook_markdown_render_includes($data)
  47. {
  48. if ($data['_PAGE_'] == Router::$PAGE_LINKLIST
  49. || $data['_PAGE_'] == Router::$PAGE_DAILY
  50. || $data['_PAGE_'] == Router::$PAGE_EDITLINK
  51. ) {
  52. $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/markdown/markdown.css';
  53. }
  54. return $data;
  55. }
  56. /**
  57. * Hook render_editlink.
  58. * Adds an help link to markdown syntax.
  59. *
  60. * @param array $data data passed to plugin
  61. *
  62. * @return array altered $data.
  63. */
  64. function hook_markdown_render_editlink($data)
  65. {
  66. // Load help HTML into a string
  67. $data['edit_link_plugin'][] = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html');
  68. return $data;
  69. }
  70. /**
  71. * Remove HTML links auto generated by Shaarli core system.
  72. * Keeps HREF attributes.
  73. *
  74. * @param string $description input description text.
  75. *
  76. * @return string $description without HTML links.
  77. */
  78. function reverse_text2clickable($description)
  79. {
  80. return preg_replace('!<a +href="([^ ]*)">[^ ]+</a>!m', '$1', $description);
  81. }
  82. /**
  83. * Remove <br> tag to let markdown handle it.
  84. *
  85. * @param string $description input description text.
  86. *
  87. * @return string $description without <br> tags.
  88. */
  89. function reverse_nl2br($description)
  90. {
  91. return preg_replace('!<br */?>!im', '', $description);
  92. }
  93. /**
  94. * Remove HTML spaces '&nbsp;' auto generated by Shaarli core system.
  95. *
  96. * @param string $description input description text.
  97. *
  98. * @return string $description without HTML links.
  99. */
  100. function reverse_space2nbsp($description)
  101. {
  102. return preg_replace('/(^| )&nbsp;/m', '$1 ', $description);
  103. }
  104. /**
  105. * Remove '&gt;' at start of line auto generated by Shaarli core system
  106. * to allow markdown blockquotes.
  107. *
  108. * @param string $description input description text.
  109. *
  110. * @return string $description without HTML links.
  111. */
  112. function reset_quote_tags($description)
  113. {
  114. return preg_replace('/^( *)&gt; /m', '$1> ', $description);
  115. }
  116. /**
  117. * Render shaare contents through Markdown parser.
  118. * 1. Remove HTML generated by Shaarli core.
  119. * 2. Generate markdown descriptions.
  120. * 3. Wrap description in 'markdown' CSS class.
  121. *
  122. * @param string $description input description text.
  123. *
  124. * @return string HTML processed $description.
  125. */
  126. function process_markdown($description)
  127. {
  128. $parsedown = new Parsedown();
  129. $processedDescription = $description;
  130. $processedDescription = reverse_text2clickable($processedDescription);
  131. $processedDescription = reverse_nl2br($processedDescription);
  132. $processedDescription = reverse_space2nbsp($processedDescription);
  133. $processedDescription = reset_quote_tags($processedDescription);
  134. $processedDescription = $parsedown
  135. ->setMarkupEscaped(false)
  136. ->setBreaksEnabled(true)
  137. ->text($processedDescription);
  138. $processedDescription = '<div class="markdown">'. $processedDescription . '</div>';
  139. return $processedDescription;
  140. }