com.lokorin.lokorin.lib
[ class tree: com.lokorin.lokorin.lib ] [ index: com.lokorin.lokorin.lib ] [ all elements ]

Source for file lib_markup.php

Documentation is available at lib_markup.php

  1. <?php
  2. /*
  3. * Lokorin.com
  4. * Copyright 2004-2006
  5. * Licensed under the GNU LGPL. See COPYING for full terms.
  6. */
  7. /**
  8. * A library for handelng the custom markup used on the site.
  9. * @author Andreas Launila
  10. * @version $Revision: 1.3 $
  11. * @package com.lokorin.lokorin.lib
  12. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  13. */
  14.  
  15. /**
  16. * For access to common constants and functions.
  17. */
  18. require_once('common.php');
  19. doInclude('lib_abbreviations');
  20.  
  21. /**
  22. * Converts text with markup to the corresponding XHTML compliant code.
  23. * @param string $markupText The markup text that should be converted.
  24. * @return string XHTML compliant code corresponding to the markupped text.
  25. * @access public
  26. */
  27. function convertMarkupToHTML($markupText) {
  28. //HTML entieties
  29. $markupText = htmlspecialchars($markupText);
  30. $blockMarkup = array("img", "code", "php", "list", "list_num");
  31. //Linebreaks to paragraphs.
  32. //preg_match_all('/\[([^] ]+)[^]]*].*?\[\/\1]/sm', $markupText, $matches);
  33. //preg_match_all('/(([\s\n]*\[([^] ]+)[^]]*].*?\[\/\3][\s\n]*)*)([^\n]+)(\n\s?\n|$)/sm', $markupText, $matches);
  34. $matches = array();
  35. preg_match_all('/([\s\n]*\[([^] ]+)[^]]*].*?\[\/\2][\s\n]*)*([^[]*)/sm', $markupText, $matches);
  36. //print_r($matches);
  37. //Sort out all block markup.
  38. $nonBlockEntry = $markupText;
  39. foreach($matches[1] as $k => $match) {
  40. if(in_array($matches[2][$k], $blockMarkup)) {
  41. $nonBlockEntry = str_replace($match, "\n", $nonBlockEntry);
  42. }
  43. }
  44. //print_r($nonBlockEntry);
  45. preg_match_all('/([^\n]+)(\n\s?\n|\z)/sm', $nonBlockEntry, $matches);
  46. foreach($matches[0] as $k => $match) {
  47. $markupText = str_replace(trim($match), "<p>".$matches[1][$k]."</p>", $markupText);
  48. }
  49. //$markupText = preg_replace('/(([\s\n]*\[([^] ]+)[^]]*].*?\[\/\3][\s\n]*)*)([^\n]+)(\n\s?\n|$)/sm', "$1<p>$4</p>\n", $markupText);
  50. //Abbrevations
  51. $abbr = AbbreviationHandler::getInstance();
  52. $matches = array();
  53. preg_match_all('/\[([^ \]]+)[^\]]*\][^\[]+\[\/\1\]/', $markupText, $matches);
  54. $markupText = $abbr->handleAbbreviations($markupText);
  55. foreach($matches[0] as $match) {
  56. $markupText = str_replace($abbr->handleAbbreviations($match), $match, $markupText);
  57. }
  58. //Images ([img]...[/img])
  59. preg_match_all('/\[img( alt=&quot;(.*?)&quot;)?\]([^\[]*)\[\/img\]/',$markupText,$matches);
  60. //print_r($matches);
  61. for($i=0; is_array($matches) && ($i < sizeof($matches[3])); $i++) {
  62. if($matches[2][$i] != '') {
  63. $alt = $matches[2][$i];
  64. } else {
  65. $alt = $matches[3][$i];
  66. }
  67. doInclude('lib_images');
  68. $replacement = getImage($matches[3][$i], $alt);
  69. $markupText = str_replace($matches[0][$i],$replacement,$markupText);
  70. }
  71.  
  72. //Code blocks ([code]...[/code])
  73. preg_match_all("/\[code\](.*?)\[\/code\]/s", $markupText, $matches);
  74. for($i=0; is_array($matches) && ($i < sizeof($matches[1])); $i++) {
  75. $replacement = '<pre>'.str_replace('<br />', '', $matches[1][$i]).'</pre>';
  76. $markupText = str_replace('[code]'.$matches[1][$i].'[/code]',$replacement,$markupText);
  77. }
  78. //PHP blocks ([php]...[/php])
  79. preg_match_all("/\[php\](.*?)\[\/php\]/s", $markupText, $matches);
  80. for($i=0; is_array($matches) && ($i < sizeof($matches[1])); $i++) {
  81. ob_start();
  82. highlight_string(str_replace('<br />', '', html_entity_decode($matches[1][$i])));
  83. $code = ob_get_contents();
  84. ob_end_clean();
  85. $code = str_replace('<br />', '', $code);
  86. //Make it XHTML strict valid
  87. $code = preg_replace('/<font color="(.*?)">/', '<span style="color:$1;">', $code);
  88. $code = str_replace('</font>', '</span>', $code);
  89. $markupText = str_replace('[php]'.$matches[1][$i].'[/php]', '<pre>'.$code.'</pre>', $markupText);//div class="code"
  90. }
  91.  
  92. //Bold text ([b]...[/b])
  93. $markupText = preg_replace("/\[b\](.*?)\[\/b\]/", "<b>$1</b>", $markupText);
  94. //Italics ([i]...[/i])
  95. $markupText = preg_replace("/\[i\](.*?)\[\/i\]/", "<i>$1</i>", $markupText);
  96. //List [list]...[*]...[/*]...[/list]
  97. $markupText = preg_replace("/\[list\](<br \/>\s*)?(.*?)(<br \/>\s*)?\[\/list\]/s", "<ul>$2</ul>", $markupText);
  98. $markupText = preg_replace("/\[list_num\](<br \/>\s*)?(.*?)(<br \/>\s*)?\[\/list_num\]/s", "<ol>$2</ol>", $markupText);
  99. preg_match_all("/(<br \/>\s*)?\[\*\](.*?)\[\/\*\]/s", $markupText, $matches);
  100. foreach($matches[0] as $k => $match) {
  101. $text = $matches[2][$k];
  102. $markupText = str_replace($match,
  103. "<li>".$abbr->handleAbbreviations($text)."</li>", $markupText);
  104. }
  105.  
  106. //Links [link]...[/link]
  107. $markupText = preg_replace("/\[link\](.*?)\[\/link\]/s", '<a href="$1">$1</a>', $markupText);
  108.  
  109. //Links [link url="..."]...[/link]
  110. $markupText = preg_replace('/\[link url=&quot;http:\/\/(.*?)&quot;\]([^\[]*)\[\/link\]/s', '<a href="http://$1">$2</a>', $markupText);
  111. $markupText = preg_replace('/\[link url=&quot;\/(.*?)&quot;\]([^\[]*)\[\/link\]/s', '<a href="'.ROOT_PATH.'$1">$2</a>', $markupText);
  112. //Abbreviations [abbr="..."]...[/abbr]
  113. preg_match_all('/\[abbr=&quot;(.*?)&quot;\]([^\[]*)\[\/abbr\]/s', $markupText, $matches);
  114. foreach($matches[0] as $k => $match) {
  115. $shortForm = $matches[2][$k];
  116. $expandedForm = $matches[1][$k];
  117. $markupText = str_replace($match,
  118. $abbr->formatAbbreviation($shortForm, $expandedForm),
  119. $markupText);
  120. }
  121. return $markupText;
  122. }
  123.  
  124. ?>

Documentation generated on Sun, 16 Apr 2006 21:03:37 +0200 by phpDocumentor 1.3.0RC4