Ejemplo n.º 1
0
function ewiki_format($wiki_source, $params = array())
{
    global $ewiki_links, $ewiki_plugins, $ewiki_config;
    #-- state vars
    $params = @array_merge($ewiki_config["format_params"], $params);
    $s = array("in" => 0, "para" => "", "line" => "", "post" => "", "line_i" => 0, "lines" => array(), "list" => "", "tbl" => 0, "indent" => 0, "close" => array());
    #-- aliases
    $in =& $s["in"];
    $line =& $s["line"];
    $lines =& $s["lines"];
    $para =& $s["para"];
    $post =& $s["post"];
    $list =& $s["list"];
    #-- input and output arrays
    if ($wiki_source[0] == "<") {
        # also prepend an empty line
        $wiki_source = "\n" . $wiki_source;
        # for faster strpos() searchs
    }
    $iii = array(0 => array(0 => $wiki_source . "\n", 1 => 0xfff, 2 => "core"));
    $ooo = array();
    unset($wiki_source);
    #-- plugins
    $pf_tbl = @$ewiki_plugins["format_table"][0];
    $pf_line = @$ewiki_plugins["format_line"];
    #-- wikimarkup (wm)
    $htmlentities = $ewiki_config["htmlentities"];
    $wm_indent =& $ewiki_config["wm_indent"];
    $wm_table_defaults =& $ewiki_config["wm_table_defaults"];
    $wm_source =& $ewiki_config["wm_source"];
    $wm_list =& $ewiki_config["wm_list"];
    $wm_list_chars = implode("", array_keys($wm_list));
    $wm_style =& $ewiki_config["wm_style"];
    $wm_start_end =& $ewiki_config["wm_start_end"];
    #-- eleminate html
    $iii[0][0] = strtr($iii[0][0], $htmlentities);
    unset($htmlentities["&"]);
    #-- pre-processing plugins (working on wiki source)
    if ($pf_source = $ewiki_plugins["format_source"]) {
        foreach ($pf_source as $pf) {
            $pf($iii[0][0]);
        }
    }
    #-- simple markup
    $iii[0][0] = strtr($iii[0][0], $wm_source);
    #-- separate input into blocks ------------------------------------------
    foreach ($ewiki_config["format_block"] as $btype => $binfo) {
        #-- disabled block plugin?
        if ($binfo[2] && !$params[$binfo[2]]) {
            continue;
        }
        #-- traverse $iii[]
        $in = -1;
        while (++$in < count($iii)) {
            #-- search fragment delimeters
            if ($iii[$in][1] & 0x100) {
                while (($c =& $iii[$in][0]) && ($l = strpos($c, $binfo[0])) !== false && ($r = strpos($c, $binfo[1], $l))) {
                    $l_len = strlen($binfo[0]);
                    $r_len = strlen($binfo[1]);
                    $repl = array();
                    // pre-text
                    if ($l > 0 && trim($text = substr($c, 0, $l))) {
                        $repl[] = array($text, 0xffff, "core");
                    }
                    // the extracted part
                    if (trim($text = substr($c, $l + $l_len, $r - $l - $r_len))) {
                        $repl[] = array($text, $binfo[3], "{$btype}");
                    }
                    // rest
                    if ($r + $r_len < strlen($c) && trim($text = substr($c, $r + $r_len))) {
                        $repl[] = array($text, 0xffff, "core");
                    }
                    array_splice($iii, $in, 1, $repl);
                    $in += 1;
                }
            }
        }
    }
    #-- run format_block plugins
    $in = -1;
    while (++$in < count($iii)) {
        if (($btype = $iii[$in][2]) && ($pf_a = $ewiki_plugins["format_block"][$btype])) {
            $c =& $iii[$in][0];
            foreach ($pf_a as $pf) {
                # current buffer $c and pointer $in into $iii[] and state $s
                $pf($c, $in, $iii, $s, $btype);
            }
        }
    }
    #-- wiki markup ------------------------------------------------------
    $para = "";
    $in = -1;
    while (++$in < count($iii)) {
        #-- wikimarkup
        if ($iii[$in][1] & 0x1) {
            #-- input $lines buffer, and output buffer $ooo array
            $lines = explode("\n", $iii[$in][0]);
            $ooo[$in] = array(0 => "", 1 => $iii[$in][1]);
            $out =& $ooo[$in][0];
            $s["block"] = $iii[$in][2] != "core";
            # disables indentation & paragraphs
            #-- walk through wiki source lines
            $line_max = count($lines);
            foreach ($lines as $s["line_i"] => $line) {
                //echo "<pre>line={$s[line_i]}:".htmlspecialchars($line).":".htmlspecialchars($line[0])."</pre>";
                #-- empty lines separate paragraphs
                if (!strlen($line)) {
                    ewiki_format_close_para($ooo, $s);
                    ewiki_format_close_tags($ooo, $s);
                    if (!$s["block"]) {
                        $out .= "\n";
                    }
                }
                #-- horiz bar
                if (!strncmp($line, "----", 4)) {
                    $out .= "<hr noshade>\n";
                    continue;
                }
                #-- html comment
                #if (!strncmp($line, "&lt;!--", 7)) {
                #   $out .= "<!-- " . htmlentities(str_replace("--", "__", substr($line, 7))) . " -->\n";
                #   continue;
                #}
                $c0 = $line[0] or $c0 = "";
                #-- tables
                ### MOODLE CHANGE: TRIM
                if ($c0 == "|" && $line[strlen(trim($line)) - 1] == "|") {
                    if (!$s["tbl"]) {
                        ewiki_format_close_para($ooo, $s);
                        ewiki_format_close_tags($ooo, $s);
                        $s["list"] = "";
                    }
                    $line = substr($line, 1, -1);
                    if ($pf_tbl) {
                        $pf_tbl($line, $ooo, $s);
                    } else {
                        if (!$s["tbl"]) {
                            $out .= "<table " . $wm_table_defaults . ">\n";
                            $s["close"][] = "\n</table>";
                        }
                        $line = "<tr>\n<td>" . str_replace("|", "</td>\n<td>", $line) . "</td>\n</tr>";
                    }
                    $s["tbl"] = 1;
                    $para = false;
                } elseif ($s["tbl"]) {
                    $s["tbl"] = 0;
                }
                #-- headlines
                if ($c0 == "!" && ($excl = strspn($line, "!"))) {
                    if ($excl > 3) {
                        $excl = 3;
                    }
                    $line = substr($line, $excl);
                    $excl = 5 - $excl;
                    $line = "<h{$excl}>" . $line . "</h{$excl}>";
                    if ($para) {
                        ewiki_format_close_para($ooo, $s);
                    }
                    ewiki_format_close_tags($ooo, $s);
                    $para = false;
                }
                #-- whole-line markup
                # ???
                #-- indentation (space/tab markup)
                $n_indent = 0;
                if (!$list && !$s["block"] && ($n_indent = strspn($line, " "))) {
                    $n_indent = (int) ($n_indent / 2.65);
                    while ($n_indent > $s["indent"]) {
                        $out .= $wm_indent;
                        $s["indent"]++;
                    }
                }
                while ($n_indent < $s["indent"]) {
                    $out .= "";
                    $s["indent"]--;
                }
                #-- text style triggers
                foreach ($wm_style as $find => $replace) {
                    $find_len = strlen($find);
                    $loop = 20;
                    while ($loop-- && ($l = strpos($line, $find)) !== false && ($r = strpos($line, $find, $l + $find_len))) {
                        $line = substr($line, 0, $l) . $replace[0] . substr($line, $l + strlen($find), $r - $l - $find_len) . $replace[1] . substr($line, $r + $find_len);
                    }
                }
                #-- list markup
                if (isset($wm_list[$c0])) {
                    if (!$list) {
                        ewiki_format_close_para($ooo, $s);
                        ewiki_format_close_tags($ooo, $s);
                    }
                    $new_len = strspn($line, $wm_list_chars);
                    $new_list = substr($line, 0, $new_len);
                    $old_len = strlen($list);
                    $lchar = $new_list[$new_len - 1];
                    list($lopen, $ltag1, $ltag2) = $wm_list[$lchar];
                    #-- cut line
                    $line = substr($line, $new_len);
                    $lspace = "";
                    $linsert = "";
                    if ($ltag1) {
                        $linsert = "<{$ltag1}>" . strtok($line, $lchar) . "</{$ltag1}> ";
                        $line = strtok("");
                    }
                    #-- add another <li>st entry
                    if ($new_len == $old_len) {
                        $lspace = str_repeat("  ", $new_len);
                        $out .= "</{$ltag2}>\n" . $lspace . $linsert . "<{$ltag2}>";
                    } elseif ($new_len > $old_len) {
                        while ($new_len > ($old_len = strlen($list))) {
                            $lchar = $new_list[$old_len];
                            $list .= $lchar;
                            list($lopen, $ltag1, $ltag2) = $wm_list[$lchar];
                            $lclose = strtok($lopen, " ");
                            $lspace = str_repeat("  ", $new_len);
                            $out .= "\n{$lspace}<{$lopen}>\n" . "{$lspace}" . $linsert . "<{$ltag2}>";
                            $s["close"][] = "{$lspace}</{$lclose}>";
                            $s["close"][] = "{$lspace}</{$ltag2}>";
                        }
                    } else {
                        while ($new_len < ($old_len = strlen($list))) {
                            $remove = $old_len - $new_len;
                            ewiki_format_close_tags($ooo, $s, 2 * $remove);
                            $list = substr($list, 0, -$remove);
                        }
                        if ($new_len) {
                            $lspace = str_repeat("  ", $new_len);
                            $out .= "{$lspace}</{$ltag2}>\n" . $lspace . $linsert . "<{$ltag2}>";
                        }
                    }
                    $list = $new_list;
                    $para = false;
                } elseif ($list) {
                    if ($c0 == " ") {
                        $para = false;
                    } else {
                        ewiki_format_close_tags($ooo, $s);
                        $list = "";
                    }
                }
                #-- start-end markup
                foreach ($wm_start_end as $d) {
                    $len0 = strlen($d[0]);
                    $loop = 20;
                    while ($loop-- && ($l = strpos($line, $d[0])) !== false && ($r = strpos($line, $d[1], $l + $len0))) {
                        $len1 = strlen($d[1]);
                        $line = substr($line, 0, $l) . $d[2] . substr($line, $l + $len0, $r - $l - $len0) . $replace[1] . substr($line, $r + $len1);
                    }
                }
                #-- call wiki source formatting plugins that work on current line
                if ($pf_line) {
                    foreach ($pf_line as $pf) {
                        $pf($out, $line, $post);
                    }
                }
                #-- add formatted line to page-output
                $line .= $post;
                if ($para === false) {
                    $out .= $line;
                    $para = "";
                } else {
                    $para .= $line . "\n";
                }
            }
            #-- last block, or next not WikiSource?
            if (!isset($iii[$in + 1]) || !($iii[$in + 1][1] & 0x11)) {
                ewiki_format_close_para($ooo, $s);
                ewiki_format_close_tags($ooo, $s);
            }
        } else {
            $ooo[$in] = $iii[$in];
        }
        $iii[$in] = array();
    }
    #-- wiki linking ------------------------------------------------------
    $scan_src = "";
    for ($in = 0; $in < count($ooo); $in++) {
        if (EWIKI_HTML_CHARS && $ooo[$in][1] & 0x4) {
            # html character entities
            $ooo[$in][0] = str_replace("&amp;#", "&#", $ooo[$in][0]);
        }
        if ($ooo[$in][1] & 0x22) {
            #-- join together multiple WikiSource blocks
            while (isset($ooo[$in + 1]) && $ooo[$in][1] & 0x2 && $ooo[$in + 1][1] & 0x2) {
                $ooo[$in] = array(0 => $ooo[$in][0] . "\n" . $ooo[$in + 1][0], 1 => $ooo[$in][1] | $ooo[$in + 1][1]);
                array_splice($ooo, $in + 1, 1);
            }
        }
        $scan_src .= $ooo[$in][0];
    }
    #-- pre-scan
    if (1 + $params["scan_links"]) {
        ewiki_scan_wikiwords($scan_src, $ewiki_links);
    }
    if ($pf_linkprep = $ewiki_plugins["format_prepare_linking"]) {
        foreach ($pf_linkprep as $pf) {
            $pf($scan_src);
        }
    }
    $scan_src = NULL;
    #-- finally the link-detection-regex
    for ($in = 0; $in < count($ooo); $in++) {
        if ($ooo[$in][1] & 0x2) {
            ##### BEGIN MOODLE ADDITION #####
            # No WikiLinks in Editor
            #################################
            global $ewiki_use_editor, $ewiki_editor_content;
            if (!($ewiki_use_editor && $ewiki_editor_content)) {
                ##### END MOODLE ADDITION #####
                ewiki_render_wiki_links($ooo[$in][0]);
                ##### BEGIN MOODLE ADDITION #####
            }
            ##### END MOODLE ADDITION #####
        }
    }
    #-- fin: combine all blocks into html string ----------------------------
    $html = "";
    for ($in = 0; $in < count($ooo); $in++) {
        $html .= $ooo[$in][0] . "\n";
        $ooo[$in] = 0;
    }
    #-- call post processing plugins
    if ($pf_final = $ewiki_plugins["format_final"]) {
        foreach ($pf_final as $pf) {
            $pf($html);
        }
    }
    return $html;
}
Ejemplo n.º 2
0
function ewiki_navbar_format($wiki_source, $scan_links = 1, $currpage = EWIKI_PAGE_INDEX)
{
    global $ewiki_links, $ewiki_plugins;
    //echo("navbar function run");
    // pre-scan WikiLinks
    if ($scan_links) {
        ewiki_scan_wikiwords($wiki_source, $ewiki_links);
    }
    // formatted output
    $o = "\n";
    // plugins only format finals are respected
    $pf_final = @$ewiki_plugins["format_final"];
    $table_defaults = 'cellpadding="2" border="1" cellspacing="0"';
    $syn_htmlentities = array("&" => "&amp;", ">" => "&gt;", "<" => "&lt;", "%%%" => "<br />");
    $wm_list = array("-" => array('ul type="square"', "", "li"), "*" => array('ul type="circle"', "", "li"), "#" => array("ol", "", "li"), ":" => array("dl", "dt", "dd"), ";" => array("dl", "dt", "dd"));
    // eleminate html
    foreach ($syn_htmlentities as $find => $replace) {
        $wiki_source = str_replace($find, $replace, $wiki_source);
    }
    array_pop($syn_htmlentities);
    // strips "&amp;"
    // add a last empty line to get opened tables/lists closed correctly
    $wiki_source = trim($wiki_source) . "\n";
    #-- finally the link-detection-regex
    #   (impossible to do with the simple string functions)
    ewiki_render_wiki_links($wiki_source);
    $NavBar = array();
    //return($wiki_source);
    foreach (explode("\n", $wiki_source) as $line) {
        $line = rtrim($line);
        $lineout = "";
        #-- wiki list markup
        if (strlen($line) && isset($wm_list[@$line[0]])) {
            $n = 0;
            $li = "";
            #-- count depth of list
            #  	line has length		first character in the line is in wm_list
            while (strlen($line) && '*' == $line[0]) {
                $li .= '*';
                //add new list delim to list count
                $n++;
                //count depth
                $line = substr($line, 1);
                //remove first character
            }
            $line = ltrim($line);
            $regex = '#<a href=["\'](.*)' . preg_quote(EWIKI_SCRIPT) . '(.*?)["\'&?^](.*)#i';
            preg_match($regex, $line, $matches = array());
            $href = $matches[2];
            //echo($regex.$line."HREF'".$href."'");
            $NavBar[] = array($line, $n, 0, $href);
            //echo($line.$n.count($NavBar));
        }
    }
    //$NavBar now contains all elements in the navigation bar.
    //return("");
    $NavBar = $ewiki_plugins['select_navbar_buttons'][0]($NavBar, $currpage);
    reset($NavBar);
    $pre = '';
    $post = '';
    $barText = $ewiki_plugins['render_navbar_section'][0]($pre, $post, $NavBar);
    $barText = $pre . $barText . $post;
    //Cut out if we have no navigation bar
    if (!$barText) {
        return "";
    }
    #-- close last line
    $o .= $barText . "\n";
    #-- international characters
    if (EWIKI_HTML_CHARS) {
        $o = str_replace("&amp;#", "&#", $o);
    }
    #-- call post processing plugins
    if ($pf_final) {
        foreach ($pf_final as $pf) {
            $pf($o);
        }
    }
    return $o;
}