Example #1
0
function do_format($formatter, $options)
{
    $mimes = array('text/plain' => 'html', 'text/xml' => 'text_xml');
    $mimetype = $options['mimetype'];
    $proc = !empty($options['proc']) ? $options['proc'] : '';
    if (!$mimetype) {
        $mimetype = 'text/plain';
    }
    $pi = $formatter->page->get_instructions($dummy);
    if (!$formatter->wordrule) {
        $formatter->set_wordrule($pi);
    }
    if ($pi['#format'] == 'xsltproc') {
        $options['title'] = _("It is a XML format !");
        do_invalid($formatter, $options);
        return;
    }
    if (!$formatter->page->exists()) {
        do_invalid($formatter, $options);
        return;
    } else {
        if (empty($proc) and array_key_exists($mimetype, $mimes)) {
            header("Content-type: " . $mimetype);
            print $formatter->processor_repl($mimes[$mimetype], $formatter->page->get_raw_body(), $options);
        } else {
            if (!empty($proc)) {
                #if (getProcessor($processor)) {
                #  do_invalid($formatter,$options);
                #  return;
                #}
                #header("Content-type: ".$mimetype);
                header("Content-type: text/plain");
                print $formatter->processor_repl($proc, $formatter->page->get_raw_body(), $options);
            } else {
                $processor = str_replace("/.", "__", $mimetype);
                header("Content-type: text/plain");
                if (getProcessor($processor)) {
                    print $formatter->processor_repl($processor, $formatter->page->get_raw_body(), $options);
                } else {
                    do_invalid($formatter, $options);
                    return;
                }
            }
        }
    }
    return;
}
Example #2
0
function processor_text_xml($formatter, $value, $options = array())
{
    global $DBInfo;
    if ($value[0] == '#' and $value[1] == '!') {
        list($line, $value) = explode("\n", $value, 2);
    }
    $lines = explode("\n", $value);
    $xml = new Formatter_xml();
    # have no contents
    if (!$lines) {
        return;
    }
    $text = "";
    $in_pre = 0;
    $in_li = 0;
    $li_open = 0;
    $in_table = 0;
    $indent_list[0] = 0;
    $indent_type[0] = "";
    $formatter->set_wordrule();
    $wordrule = "({{{([^}]+)}}})|" . "\\[\\[([A-Za-z0-9]+(\\(((?<!\\]\\]).)*\\))?)\\]\\]|";
    # macro
    if ($DBInfo->enable_latex) {
        # single line latex syntax
        $wordrule .= "\\\$\\s([^\\\$]+)\\\$(?:\\s|\$)|" . "\\\$\\\$\\s([^\\\$]+)\\\$\\\$(?:\\s|\$)|";
    }
    $wordrule .= $formatter->wordrule;
    $formatter->no_js = 1;
    $xml->use_cdata = $options['cdata'] ? 1 : 0;
    foreach ($lines as $line) {
        # empty line
        #if ($line=="") {
        if (!strlen($line)) {
            if ($in_pre) {
                $xml->pre_line .= "\n";
                continue;
            }
            if ($in_li) {
                $text .= "\n";
                continue;
            }
            if ($in_table) {
                $text .= $xml->_table(0) . "\n";
                $in_table = 0;
                continue;
            } else {
                if ($xml->in_p) {
                    $text .= "{$xml->padding}</para>\n";
                    $xml->in_p = "";
                } else {
                    if ($xml->in_p == '') {
                        $text .= "\n";
                    }
                }
                continue;
            }
        } else {
            if ($xml->in_p == '') {
                $text .= "{$xml->padding}<para>\n";
                $xml->in_p = $line;
            }
        }
        if ($line[0] == '#' and $line[1] == '#') {
            continue;
        }
        # comments
        if ($in_pre) {
            if (strpos($line, "}}}") === false) {
                $xml->pre_line .= $line . "\n";
                continue;
            } else {
                #$p=strrpos($line,"}}}");
                $p = strlen($line) - strpos(strrev($line), '}}}') - 1;
                if ($p > 2 and $line[$p - 3] == '\\') {
                    $xml->pre_line .= substr($line, 0, $p - 3) . substr($line, $p - 2) . "\n";
                    continue;
                }
                $xml->pre_line .= substr($line, 0, $p - 2);
                $line = substr($line, $p + 1);
                $in_pre = -1;
            }
            #} else if ($in_pre == 0 && preg_match("/{{{[^}]*$/",$line)) {
        } else {
            if (!(strpos($line, "{{{") === false) and preg_match("/{{{[^}]*\$/", $line)) {
                $p = strpos($line, "{{{");
                $processor = "";
                $in_pre = 1;
                # check processor
                if ($line[$p + 3] == "#" and $line[$p + 4] == "!") {
                    list($tag, $dummy) = explode(" ", substr($line, $p + 5), 2);
                    if (function_exists("processor_" . $tag)) {
                        $processor = $tag;
                    } else {
                        if ($pf = getProcessor($tag)) {
                            include_once "plugin/processor/{$pf}.php";
                            $processor = $pf;
                        }
                    }
                    if ($tag == 'docbook') {
                        $processor = $tag;
                    }
                } else {
                    if ($line[$p + 3] == ":") {
                        # new formatting rule for a quote block (pre block + wikilinks)
                        $line[$p + 3] = " ";
                        $in_quote = 1;
                    } else {
                        if ($line[$p + 3] == "!") {
                            $line[$p + 3] = " ";
                            $in_quote = 2;
                        }
                    }
                }
                $xml->pre_line = substr($line, $p + 3);
                if (trim($xml->pre_line)) {
                    $xml->pre_line .= "\n";
                }
                $line = substr($line, 0, $p);
            }
        }
        $line = preg_replace($xml->baserule, $xml->baserepl, $line);
        # bullet and indentation
        if ($in_pre != -1 && preg_match("/^(\\s*)/", $line, $match)) {
            #if (preg_match("/^(\s*)/",$line,$match)) {
            $open = "";
            $close = "";
            $indtype = "dd";
            $indlen = strlen($match[0]);
            if ($indlen > 0) {
                $line = substr($line, $indlen);
                #if (preg_match("/^(\*\s*)/",$line,$limatch)) {
                if ($line[0] == '*') {
                    $limatch[1] = '*';
                    $line = preg_replace("/^(\\*\\s?)/", "{$xml->padding}<listitem>", $line);
                    if ($indent_list[$in_li] == $indlen) {
                        $line = "</listitem>\n" . $line;
                    }
                    $numtype = "";
                    $indtype = "itemizedlist";
                } elseif (preg_match("/^((\\d+|[aAiI])\\.)(#\\d+)?\\s/", $line, $limatch)) {
                    $line = preg_replace("/^((\\d+|[aAiI])\\.(#\\d+)?)/", "{$xml->padding}<listitem>", $line);
                    if ($indent_list[$in_li] == $indlen) {
                        $line = "</listitem>\n" . $line;
                    }
                    $numtype = $limatch[2];
                    if ($limatch[3]) {
                        $numtype .= substr($limatch[3], 1);
                    }
                    $indtype = "orderedlist";
                } elseif (preg_match("/^([^:]+)::\\s/", $line, $limatch)) {
                    $line = preg_replace("/^[^:]+::\\s/", "<varlistentry><term>" . $limatch[1] . "</term>", $line);
                    if ($indent_list[$in_li] == $indlen) {
                        $line = "</para>\n" . $line;
                    }
                    $numtype = "";
                    $indtype = "dl";
                }
            }
            if ($indent_list[$in_li] < $indlen) {
                $in_li++;
                $indent_list[$in_li] = $indlen;
                # add list depth
                $indent_type[$in_li] = $indtype;
                # add list type
                $open .= $xml->_list(1, $indtype, $numtype);
            } else {
                if ($indent_list[$in_li] > $indlen) {
                    while ($in_li >= 0 && $indent_list[$in_li] > $indlen) {
                        if ($indent_type[$in_li] != 'dd' && $li_open == $in_li) {
                            $close .= "</listitem>\n";
                        }
                        $close .= $xml->_list(0, $indent_type[$in_li], "", $indent_type[$in_li - 1]);
                        unset($indent_list[$in_li]);
                        unset($indent_type[$in_li]);
                        $in_li--;
                    }
                }
            }
            if ($indent_list[$in_li] <= $indlen || $limatch) {
                $li_open = $in_li;
            } else {
                $li_open = 0;
            }
        }
        #if (!$in_pre && !$in_table && preg_match("/^\|\|.*\|\|$/",$line)) {
        if (!$in_pre && $line[0] == '|' && !$in_table && preg_match("/^\\|\\|.*\\|\\|\$/", $line)) {
            $count = preg_match_all("/\\|\\|/", $line, $match);
            $open .= $xml->_table(1, $count);
            $in_table = 1;
            #} elseif ($in_table && !preg_match("/^\|\|.*\|\|$/",$line)){
        } elseif ($in_table && $line[0] != '|' && !preg_match("/^\\|\\|.*\\|\\|\$/", $line)) {
            $close = $xml->_table(0) . $close;
            $in_table = 0;
        }
        if ($in_table) {
            $cells = preg_split('/((?:\\|\\|)+)/', $line, -1, PREG_SPLIT_DELIM_CAPTURE);
            $row = '';
            for ($i = 1, $sz = sizeof($cells); $i < $sz; $i += 2) {
                $cell = $cells[$i + 1];
                $row .= '<row><entry ' . $xml->_table_span($cells[$i]) . '>' . $cell . '</entry></row>' . "\n";
            }
            if (isset($row[0])) {
                $line = $row;
            }
        }
        $line = $close . $open . $line;
        $open = "";
        $close = "";
        # Headings
        if (preg_match("/(?<!=)(={1,5})\\s+(.*)\\s+(={1,5})\\s?\$/", $line, $m)) {
            $line = $xml->head_repl($m[1], $m[2], $m[3]);
        }
        # InterWiki, WikiName, {{{ }}}, !WikiName, ?single, ["extended wiki name"]
        # urls, [single bracket name], [urls text], [[macro]]
        $line = preg_replace_callback("/(" . $wordrule . ")/", array(&$xml, 'link_repl'), $line);
        if (!empty($xml->extrarule)) {
            $line = preg_replace($xml->extrarule, $xml->extrarepl, $line);
        }
        if (!empty($xml->smiley_rule)) {
            $line = preg_replace_callback($xml->smiley_rule, array(&$xml, 'smiley_repl'), $line);
        }
        $line = preg_replace("/&(?:\\s)/", "&amp;", $line);
        $xml->_set_padding($xml->level);
        if ($in_pre == -1) {
            $in_pre = 0;
            if ($processor) {
                $value = $xml->pre_line;
                if ($processor != 'docbook') {
                    if ($formatter->use_cdata) {
                        $out = call_user_func("processor_{$processor}", $formatter, $value, $options);
                        $line = "<programlisting><![CDATA[\n" . $out . "\n]]></programlisting>\n" . $line;
                    } else {
                        $pre = str_replace("&", "&amp;", $xml->pre_line);
                        $pre = str_replace("<", "&lt;", $pre);
                        $line = "<programlisting><![CDATA[\n" . $pre . "\n]]></programlisting>\n" . $line;
                    }
                } else {
                    list($tag, $pre) = explode("\n", $xml->pre_line, 2);
                    $line = $pre;
                }
            } else {
                if ($in_quote) {
                    # htmlfy '<'
                    if ($in_quote == 2) {
                        $line = $xml->pre_line;
                    } else {
                        $pre = str_replace("&", "&amp;", $xml->pre_line);
                        $pre = str_replace("<", "&lt;", $pre);
                        $pre = preg_replace($xml->baserule, $xml->baserepl, $pre);
                        $pre = preg_replace_callback("/(" . $wordrule . ")/", array(&$xml, 'link_repl'), $pre);
                        $line = "<blockquote>\n" . $pre . "</blockquote>\n" . $line;
                    }
                    $in_quote = 0;
                } else {
                    # htmlfy '<'
                    $pre = str_replace("&", "&amp;", $xml->pre_line);
                    $pre = str_replace("<", "&lt;", $pre);
                    $line = "<screen><![CDATA[\n" . $pre . "\n]]></screen>\n" . $line;
                }
            }
        }
        $text .= $xml->padding . $line . "\n";
    }
    # close all tags
    $close = "";
    # close pre,table
    if ($in_pre) {
        $close .= "</screen>\n";
    }
    if ($in_table) {
        $close .= "</table>\n";
    }
    # close indent
    while ($in_li >= 0 && $indent_list[$in_li] > 0) {
        if ($indent_type[$in_li] != 'dd' && $li_open >= $in_li) {
            $close .= "</listitem>\n";
        }
        #$close.=$in_li.":$li_open".$indent_type[$in_li];
        $close .= $xml->_list(0, $indent_type[$in_li]);
        unset($indent_list[$in_li]);
        unset($indent_type[$in_li]);
        $in_li--;
    }
    # close div
    if ($xml->in_p) {
        $close .= "</para>\n";
    }
    if ($xml->head_dep) {
        $odepth = $xml->head_dep;
        $dum = explode(".", $xml->head_num);
        $i = sizeof($dum) - 1;
        while (0 <= $odepth && $i >= 0) {
            $i--;
            #$close.="</sect$odepth>\n"; # </section>
            $close .= "</section>\n";
            # </section>
            $odepth--;
        }
    }
    $text .= $close;
    $pagename = $formatter->page->name;
    $header = <<<HEAD
<?xml version="1.0" encoding="{$DBInfo->charset}"?>
<!-- <?xml-stylesheet href="DocbookKoXsl" type="text/xml"?> -->
<!-- <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
                  "http://www.docbook.org/xml/4.1.2/docbookx.dtd"> -->

<article lang="ko">

<articleinfo>
<title>{$pagename}</title>
HEAD;
    $author = get_authorinfo($value);
    $author .= "</articleinfo>\n";
    $footer = "</article>\n";
    print $header . $author . $text . $footer;
}
Example #3
0
function macro_diff($formatter, $value, &$options = array())
{
    global $DBInfo;
    $option = '';
    $pi = $formatter->page->get_instructions();
    $formatter->pi = $pi;
    $processor_type = $pi['#format'];
    while ($DBInfo->default_markup != 'wiki') {
        // XXX
        $processor = $pi['#format'];
        if (!($f = function_exists("processor_" . $processor)) and !($c = class_exists('processor_' . $processor))) {
            $pf = getProcessor($processor);
            if (!$pf) {
                break;
            }
            include_once "plugin/processor/{$pf}.php";
            $processor = $pf;
            $name = 'processor_' . $pf;
            if (class_exists($name)) {
                $classname = 'processor_' . $processor;
                $myclass = new $classname($formatter, $options);
                $processor_type = $myclass->_type == 'wikimarkup' ? 'wiki' : $pi['#format'];
            }
        } else {
            if ($c = class_exists('processor_' . $processor)) {
                $classname = 'processor_' . $processor;
                $myclass = new $classname($formatter, $options);
                $processor_type = $myclass->_type == 'wikimarkup' ? 'wiki' : $pi['#format'];
            }
        }
        break;
    }
    //if (!in_array($pi['#format'],array('wiki','moni')) and !$options['type']) # is it not wiki format ?
    if ($processor_type != 'wiki' and !$options['type']) {
        # is it not wiki format ?
        $options['type'] = $DBInfo->diff_type;
    }
    # use default diff format
    if (empty($options['type']) and !empty($DBInfo->use_smartdiff)) {
        $options['type'] = 'smart';
    }
    if (!empty($options['type']) and function_exists($options['type'] . '_diff')) {
        $type = $options['type'] . '_diff';
    } else {
        $type = $DBInfo->diff_type . '_diff';
    }
    if (!empty($options['text'])) {
        $out = $options['text'];
        if (empty($options['raw'])) {
            $ret = call_user_func($type, $out, $options);
        } else {
            $ret = "<pre>{$out}</pre>\n";
        }
        return $ret;
    }
    $rev1 = !empty($options['rev']) ? $options['rev'] : '';
    // old
    $rev2 = !empty($options['rev2']) ? $options['rev2'] : '';
    // new
    // check revision number
    if (!empty($rev1) && !preg_match("/^[0-9a-f.]+\$/", $rev1) || !empty($rev2) && !preg_match("/^[0-9a-f.]+\$/", $rev2)) {
        return _("Invalid revision numbers");
    }
    if (!$rev1 and !$rev2) {
        $rev1 = $formatter->page->get_rev();
    } else {
        if (0 === strcmp($rev1, (int) $rev1)) {
            $rev1 = $formatter->page->get_rev($rev1);
            // date
        } else {
            if ($rev1 == $rev2) {
                $rev2 = '';
            }
        }
    }
    #if ($rev1) $option="-r$rev1 ";
    #if ($rev2) $option.="-r$rev2 ";
    if (!$rev1 && !$rev2) {
        $msg = _("No older revisions available");
        if (isset($options['retval'])) {
            $options['retval']['msg'] = $msg;
        }
        if (!empty($options['nomsg'])) {
            return '';
        }
        return "<h2>{$msg}</h2>";
    }
    if (!$DBInfo->version_class) {
        $msg = _("Version info is not available in this wiki");
        if (isset($options['retval'])) {
            $options['retval']['msg'] = $msg;
        }
        if (!empty($options['nomsg'])) {
            return '';
        }
        return "<h2>{$msg}</h2>";
    }
    $version = $DBInfo->lazyLoad('version', $DBInfo);
    if (isset($options['value'][0])) {
        $rev = trim($options['rev']);
        if (!preg_match('/^[0-9a-f.]+$/i', $rev)) {
            $rev = '';
        }
        $savetext = $options['value'];
        if (!empty($options['section'])) {
            if (!empty($rev)) {
                // get revision number
                $r = $formatter->page->get_rev($rev);
                $opts = array();
                if (!empty($r)) {
                    $opts['rev'] = $r;
                }
                // get raw text by selected revision
                $rawbody = $formatter->page->get_raw_body($opts);
            } else {
                $rawbody = $formatter->page->get_raw_body();
            }
            $sections = _get_sections($rawbody);
            if (isset($sections[$options['section']])) {
                if (substr($savetext, -1) != "\n") {
                    $savetext .= "\n";
                }
                $sections[$options['section']] = $savetext;
            }
        }
        // make a diff formatted text from given text
        $out = $formatter->get_diff(!empty($options['section']) ? implode('', $sections) : $savetext);
        // get diff
    } else {
        $out = $version->diff($formatter->page->name, $rev1, $rev2, $options);
    }
    $ret = '';
    if (!$out) {
        $msg = _("No difference found");
    } else {
        #$rev1=substr($rev1,0,5);
        #$rev2=substr($rev2,0,5);
        if ($rev1 == $rev2) {
            $msg = _("Difference between versions");
        } else {
            if ($rev1 and $rev2) {
                $msg = sprintf(_("Difference between r%s and r%s"), $rev1, $rev2);
            } else {
                if ($rev1 or $rev2) {
                    $msg = sprintf(_("Difference between r%s and the current"), $rev1 . $rev2);
                }
            }
        }
        if (empty($options['raw'])) {
            $ret = call_user_func($type, $out, $options);
            if (is_array($ret)) {
                // for smart_diff
                $dels = $ret[1];
                $ret = $ret[0];
                $rev = ($rev1 and $rev2) ? $rev2 : '';
                // get newest rev.
                if (!empty($rev)) {
                    $current = $formatter->page->get_raw_body(array('rev' => $rev));
                } else {
                    $current = $formatter->page->_get_raw_body();
                }
                $lines = explode("\n", $current);
                $nret = $ret;
                foreach ($ret as $k => $v) {
                    if ($v == "") {
                        continue;
                    }
                    $tmp = explode("\n", $v);
                    $tt = array_pop($tmp);
                    if ($tt != '') {
                        $tmp[] = $tt;
                    }
                    for ($kk = 0; $kk < sizeof($tmp); $kk++) {
                        $nret[$k + $kk] = $tmp[$kk];
                    }
                }
                foreach ($nret as $k => $v) {
                    $lines[$k] = $v;
                }
                # insert deleted lines
                if ($dels) {
                    foreach ($dels as $k => $v) {
                        $lines[$k] = $v . "\n" . $lines[$k];
                    }
                }
                $diffed = implode("\n", $lines);
                # change for headings
                $diffed = preg_replace("/^(|)(={1,5})\\s(.*)\\s\\2\\1\$/m", "\\2 \\1\\3\\1 \\2", $diffed);
                # change for lists
                $diffed = preg_replace("/(|)(\\s+)(\\*|\\d+\\.\\s)(.*)\\1/m", "\\2\\3\\1\\4\\1", $diffed);
                # fix <ins>{{{foobar</ins> to {{{<ins>foobar</ins>
                $diffed = preg_replace("/(|)({{{)(.*)\$/m", "\\2\\1\\3", $diffed);
                # fix <ins>foobar}}}</ins> to <ins>foobar</ins>}}}
                $diffed = preg_replace("/(|)(.*)(}}})(\\1)/m", "\\1\\2\\4\\3", $diffed);
                # change for hrs
                $diffed = preg_replace("/(|)(-{4,})\\1/m", "\\1\\2\n\\1", $diffed);
                # XXX FIXME
                # merge multiline diffs
                #$diffed=preg_replace("/\006([ ]*)\006$/m","\\1",$diffed);
                #$diffed=preg_replace("/\010([ ]*)\010$/m","\\1",$diffed);
                $diffed = preg_replace("/\n(?!\n)/m", "\n", $diffed);
                $diffed = preg_replace("/\n(?!\n)/m", "\n", $diffed);
                $options['nomsg'] = 0;
                $options['msg'] = $msg;
                $options['smart'] = 1;
                if (isset($options['retval'])) {
                    $options['retval']['msg'] = $msg;
                }
                #if (!in_array($pi['#format'],array('wiki','moni')))
                if ($processor_type != 'wiki') {
                    return '<pre class="code">' . $diffed . '</pre>';
                }
                ob_start();
                $formatter->send_page($diffed, $options);
                $out = ob_get_contents();
                ob_end_clean();
                #print "<pre>".str_replace(array("\010","\006"),array("+++","---"),$diffed)."</pre>";
                #print "<pre>".$diffed."</pre>";
                return $out;
            }
        } else {
            return $out;
        }
    }
    if (!empty($msg) && isset($options['retval'])) {
        $options['retval']['msg'] = $msg;
    }
    return $ret;
}
Example #4
0
 function processor_repl($processor, $value, $options = false)
 {
     $bra = '';
     $ket = '';
     if (!empty($this->wikimarkup) and empty($options['nomarkup'])) {
         if (!empty($options['type']) and $options['type'] == 'inline') {
             $markups = str_replace(array('=', '-', '&', '<'), array('==', '-=', '&amp;', '&lt;'), $value);
             $bra = "<span class='wikiMarkup' style='display:inline'><!-- wiki:\n" . $markups . "\n-->";
         } else {
             if (!empty($options['nowrap']) and !empty($this->pi['#format']) and $processor == $this->pi['#format']) {
                 $btag = '';
                 $etag = '';
             } else {
                 $btag = '{{{';
                 $etag = '}}}';
             }
             $notag = '';
             if ($value[0] != '#' and $value[1] != '!') {
                 $notag = "\n";
             }
             $markups = str_replace(array('=', '-', '&', '<'), array('==', '-=', '&amp;', '&lt;'), $value);
             $bra = "<span class='wikiMarkup'><!-- wiki:\n" . $btag . $notag . $markups . $etag . "\n-->";
         }
         $ket = '</span>';
     }
     $pf = $processor;
     if (!($f = function_exists('processor_' . $processor))) {
         $pf = getProcessor($processor);
     }
     if (empty($pf)) {
         $ret = call_user_func('processor_plain', $this, $value, $options);
         return $bra . $ret . $ket;
     }
     if (!$f and !($c = class_exists('processor_' . $pf))) {
         include_once "plugin/processor/{$pf}.php";
         $name = 'processor_' . $pf;
         if (!($f = function_exists($name)) and !($c = class_exists($name))) {
             $processor = 'plain';
             $f = true;
         }
     }
     if ($f) {
         if (!empty($this->use_smartdiff) and preg_match("/|/", $value)) {
             $pf = 'plain';
         }
         $ret = call_user_func_array("processor_{$pf}", array(&$this, $value, $options));
         if (!is_string($ret)) {
             return $ret;
         }
         return $bra . $ret . $ket;
     }
     $classname = 'processor_' . $pf;
     $myclass = new $classname($this, $options);
     $ret = call_user_func(array($myclass, 'process'), $value, $options);
     if (!empty($options['nowrap']) and !empty($myclass->_type) and $myclass->_type == 'wikimarkup') {
         return $ret;
     }
     return $bra . $ret . $ket;
 }