/** * Parse the wiki-text and replace wiki markup with HTML markup. * * @param string $text * @param optional $editorSafeOnly If true, only templates that are safe for * working with in a WYSIWIG editor will be applied. * @return string * @access public * @since 7/14/08 */ public function applyTextTemplates($text, $editorSafeOnly = false) { $regexp = "/\n\n(<nowiki>)?\t\t# optional nowiki tag to prevent parsing.\n\n{{\t# The opening template tags\n\n\t\\s*\t\t# optional whitespace\n\t\n\t([a-z0-9_-]+)\t\t# template name\n\t\n\t\\s*\t\t# optional whitespace\n\n\t(?: |([^}]+) )?\t\t# A parameter list\n\n}}\t# The closing template tags\n\n(<\\/nowiki>)?\t# optional closing nowiki tag to prevent parsing.\n\n/xi"; $paramRegexp = "/\n\n\t\\s*\t\t\t\t\t# optional whitespace\n\t\n\t([a-z0-9_-]+)\t\t# param name\n\t\n\t\\s*\t\t\t\t\t# optional whitespace\n\t\n\t=\t\t\t\t\t# Equals\n\t\n\t\\s*\t\t\t\t\t# optional whitespace\n\n\t([^|]+)\t\t\t\t# param value\n\n/xi"; WikiResolver::mb_preg_match_all($regexp, $text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); // printpre($matches); // for each wiki template replace it with the HTML version $offsetDiff = 0; foreach ($matches as $match) { $offset = $match[0][1] + $offsetDiff; $wikiText = $match[0][0]; $templateName = strtolower($match[2][0]); if (isset($match[3])) { $paramString = trim($match[3][0]); } else { $paramString = ''; } // Ignore markup surrounded by nowiki tags if (!strlen($match[1][0]) && (!isset($match[4]) || !strlen($match[4][0]))) { try { $template = $this->getTextTemplate($templateName); if (!$editorSafeOnly || $template->isEditorSafe()) { // Build the parameter array $params = array(); preg_match_all($paramRegexp, $paramString, $paramMatches); foreach ($paramMatches[1] as $j => $paramName) { if (!isset($params[$paramName])) { $params[$paramName] = $paramMatches[2][$j]; } else { if (is_array($params[$paramName])) { $params[$paramName][] = $paramMatches[2][$j]; } else { $params[$paramName] = array($params[$paramName]); $params[$paramName][] = $paramMatches[2][$j]; } } } // Execute the template try { $output = $template->generate($params); // $output .= printpre(htmlentities(print_r($template->getHtmlMatches($output), true)), true); $offsetDiff = $offsetDiff + mb_strlen($output) - mb_strlen($wikiText); $text = substr_replace($text, $output, $offset, mb_strlen($wikiText)); } catch (Exception $e) { print $e->getMessage(); } } } catch (UnknownIdException $e) { if ($e->getCode() != 34563) { throw $e; } } } else { $output = '{{' . $templateName . $paramString . '}}'; $offsetDiff = $offsetDiff + mb_strlen($output) - mb_strlen($wikiText); $text = substr_replace($text, $output, $offset, mb_strlen($wikiText)); } } return $text; }