- <?php
- /*
- * Lokorin.com
- * Copyright 2004-2006
- * Licensed under the GNU LGPL. See COPYING for full terms.
- */
- /**
- * A library for handelng the custom markup used on the site.
- * @author Andreas Launila
- * @version $Revision: 1.3 $
- * @package com.lokorin.lokorin.lib
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- */
-
- /**
- * For access to common constants and functions.
- */
- require_once('common.php');
- doInclude('lib_abbreviations');
-
- /**
- * Converts text with markup to the corresponding XHTML compliant code.
- * @param string $markupText The markup text that should be converted.
- * @return string XHTML compliant code corresponding to the markupped text.
- * @access public
- */
- function convertMarkupToHTML($markupText) {
- //HTML entieties
- $markupText = htmlspecialchars($markupText);
-
- $blockMarkup = array("img", "code", "php", "list", "list_num");
- //Linebreaks to paragraphs.
- //preg_match_all('/\[([^] ]+)[^]]*].*?\[\/\1]/sm', $markupText, $matches);
- //preg_match_all('/(([\s\n]*\[([^] ]+)[^]]*].*?\[\/\3][\s\n]*)*)([^\n]+)(\n\s?\n|$)/sm', $markupText, $matches);
- $matches = array();
- preg_match_all('/([\s\n]*\[([^] ]+)[^]]*].*?\[\/\2][\s\n]*)*([^[]*)/sm', $markupText, $matches);
- //print_r($matches);
- //Sort out all block markup.
- $nonBlockEntry = $markupText;
- foreach($matches[1] as $k => $match) {
- if(in_array($matches[2][$k], $blockMarkup)) {
- $nonBlockEntry = str_replace($match, "\n", $nonBlockEntry);
- }
- }
- //print_r($nonBlockEntry);
- preg_match_all('/([^\n]+)(\n\s?\n|\z)/sm', $nonBlockEntry, $matches);
- foreach($matches[0] as $k => $match) {
- $markupText = str_replace(trim($match), "<p>".$matches[1][$k]."</p>", $markupText);
- }
- //$markupText = preg_replace('/(([\s\n]*\[([^] ]+)[^]]*].*?\[\/\3][\s\n]*)*)([^\n]+)(\n\s?\n|$)/sm', "$1<p>$4</p>\n", $markupText);
-
- //Abbrevations
- $abbr = AbbreviationHandler::getInstance();
- $matches = array();
- preg_match_all('/\[([^ \]]+)[^\]]*\][^\[]+\[\/\1\]/', $markupText, $matches);
- $markupText = $abbr->handleAbbreviations($markupText);
- foreach($matches[0] as $match) {
- $markupText = str_replace($abbr->handleAbbreviations($match), $match, $markupText);
- }
-
- //Images ([img]...[/img])
- preg_match_all('/\[img( alt="(.*?)")?\]([^\[]*)\[\/img\]/',$markupText,$matches);
- //print_r($matches);
- for($i=0; is_array($matches) && ($i < sizeof($matches[3])); $i++) {
- if($matches[2][$i] != '') {
- $alt = $matches[2][$i];
- } else {
- $alt = $matches[3][$i];
- }
- doInclude('lib_images');
- $replacement = getImage($matches[3][$i], $alt);
- $markupText = str_replace($matches[0][$i],$replacement,$markupText);
- }
-
- //Code blocks ([code]...[/code])
- preg_match_all("/\[code\](.*?)\[\/code\]/s", $markupText, $matches);
- for($i=0; is_array($matches) && ($i < sizeof($matches[1])); $i++) {
- $replacement = '<pre>'.str_replace('<br />', '', $matches[1][$i]).'</pre>';
- $markupText = str_replace('[code]'.$matches[1][$i].'[/code]',$replacement,$markupText);
- }
-
- //PHP blocks ([php]...[/php])
- preg_match_all("/\[php\](.*?)\[\/php\]/s", $markupText, $matches);
- for($i=0; is_array($matches) && ($i < sizeof($matches[1])); $i++) {
- ob_start();
- highlight_string(str_replace('<br />', '', html_entity_decode($matches[1][$i])));
- $code = ob_get_contents();
- ob_end_clean();
- $code = str_replace('<br />', '', $code);
-
- //Make it XHTML strict valid
- $code = preg_replace('/<font color="(.*?)">/', '<span style="color:$1;">', $code);
- $code = str_replace('</font>', '</span>', $code);
- $markupText = str_replace('[php]'.$matches[1][$i].'[/php]', '<pre>'.$code.'</pre>', $markupText);//div class="code"
- }
-
- //Bold text ([b]...[/b])
- $markupText = preg_replace("/\[b\](.*?)\[\/b\]/", "<b>$1</b>", $markupText);
-
- //Italics ([i]...[/i])
- $markupText = preg_replace("/\[i\](.*?)\[\/i\]/", "<i>$1</i>", $markupText);
-
- //List [list]...[*]...[/*]...[/list]
- $markupText = preg_replace("/\[list\](<br \/>\s*)?(.*?)(<br \/>\s*)?\[\/list\]/s", "<ul>$2</ul>", $markupText);
- $markupText = preg_replace("/\[list_num\](<br \/>\s*)?(.*?)(<br \/>\s*)?\[\/list_num\]/s", "<ol>$2</ol>", $markupText);
- preg_match_all("/(<br \/>\s*)?\[\*\](.*?)\[\/\*\]/s", $markupText, $matches);
- foreach($matches[0] as $k => $match) {
- $text = $matches[2][$k];
- $markupText = str_replace($match,
- "<li>".$abbr->handleAbbreviations($text)."</li>", $markupText);
- }
-
- //Links [link]...[/link]
- $markupText = preg_replace("/\[link\](.*?)\[\/link\]/s", '<a href="$1">$1</a>', $markupText);
-
- //Links [link url="..."]...[/link]
- $markupText = preg_replace('/\[link url="http:\/\/(.*?)"\]([^\[]*)\[\/link\]/s', '<a href="http://$1">$2</a>', $markupText);
- $markupText = preg_replace('/\[link url="\/(.*?)"\]([^\[]*)\[\/link\]/s', '<a href="'.ROOT_PATH.'$1">$2</a>', $markupText);
-
- //Abbreviations [abbr="..."]...[/abbr]
- preg_match_all('/\[abbr="(.*?)"\]([^\[]*)\[\/abbr\]/s', $markupText, $matches);
- foreach($matches[0] as $k => $match) {
- $shortForm = $matches[2][$k];
- $expandedForm = $matches[1][$k];
- $markupText = str_replace($match,
- $abbr->formatAbbreviation($shortForm, $expandedForm),
- $markupText);
- }
-
- return $markupText;
- }
-
- ?>