Hatena::Groupsubtech

ういはるかぜの化学

Friday, November 10, 2006

Pukiwiki to Hiki コンバータ 19:20 Pukiwiki to Hiki コンバータ - ういはるかぜの化学 を含むブックマーク はてなブックマーク - Pukiwiki to Hiki コンバータ - ういはるかぜの化学

Pukiwiki の記法をそのまま Hiki にコンバートするかもしれないスクリプト。pukiwiki.php と同じところにおいて to_hiki.php?FrontPage とかやる。

あとでごにょる(かも知れない)。

<?php
#error_reporting(0);

require_once "pukiwiki.ini.php";
require_once "plugin.php";
require_once "make_link.php";
require_once "file.php";
require_once "func.php";
require_once "html.php";
require_once "convert_html.php";
require_once "init.php";

$title = urldecode(($_SERVER["QUERY_STRING"] != '') ? $_SERVER["QUERY_STRING"] : 'FrontPage');
$text =get_source($title);
$body =& new Body(0);
$body->parse($text);

header("Content-Type: text/html; charset=EUC-JP");
print "<title>".htmlspecialchars($title)." - Pukiwiki to Hiki</title>";
print "<pre>";
print "// Converted by PukiWiki2Hiki Script\n";
print htmlspecialchars(toHiki($body));
print "// ----\n";
print "</pre>";

#print_r($body);

function toHiki($e, $depth, $context)
{
    $depth++;
    $str = "";
    foreach ($e->elements as $child)
    {
        switch (get_class($child))
        {
            case 'heading':
                $str .= str_repeat("!", $child->level-1).toHiki($child, $depth)."\n";
                break;
            case 'inline':
                $str .= toHiki($child, $depth);
                if (
                    (is_a($e, 'listelement') && $e->head == 'li')
                ) {
                    $str .= "\n";
                }
                break;
            case 'dlist':
                $str .= toHiki($child, $depth) . "\n";
                break;
            case 'listelement':
                if ($child->head == 'dt') {
                    $str .= ":".toHiki($child, $depth) .":";
                } else if($child->head == 'li') {
                    // li
                    $str .= str_repeat((is_a($e, 'ulist')?"*":"#"), $child->level) . toHiki($child);
                } else {
                    // dd
                    $str .= toHiki($child, $depth) . "\n";
                }
                break;
            case 'paragraph':
                $str .= "\n".toHiki($child, $depth) . "\n\n";
                break;
            case 'pre':
                $str .= join("\n", $child->elements). "\n\n";
                break;
            case 'ulist':
            case 'olist':
                $str .= toHiki($child, $depth);
                break;
            case 'table':
                foreach ($child->elements as $row)
                {
                    $str .= "|";
                    foreach ($row as $cell)
                    {
                        $str .= "|".toHiki($cell, $depth)."|";
                    }
                    $str .= "|\n";
                }
                break;
            case 'tablecell':
                $str .= toHiki($child, $depth)."\n";
                break;
            default:
                if (is_string($child))
                {
                    // string
                    $str .= $child;
                } else {
                    $str .= toHiki($child, $depth);
                }
        }
    }

    $str = preg_replace('/<a href="([^"]+)">(.+?)<\/a>/', '[[$2|$1]]', $str);
    // wikiname
    $str = preg_replace('/<a href="[^"]+" title="[^"]+">(.+?)<\/a>/', '[[$1]]', $str);
    $str = preg_replace('/\[\[\?\|.*?\]\]/', '', $str);
    // del
    $str = preg_replace('/<del>(.+?)<\/del>/', '==$1==', $str);

    // strong
    $str = preg_replace('/<strong>(.+?)<\/strong>/', "''$1''", $str);

    // delete html
    $str = preg_replace('/<\/?[^>]+>/', '', $str);

    // #contents -> toc
    $str = preg_replace('/==#contents==/', '{{toc}}', $str);

    return $str;
}

?>

より新しいPukiwikiに対応してちょっと修正した。

<?php
#error_reporting(0);

define('DATA_HOME',     '');
define('LIB_DIR',       'lib/');

require_once "pukiwiki.ini.php";
require_once "lib/plugin.php";
require_once "lib/make_link.php";
require_once "lib/file.php";
require_once "lib/func.php";
require_once "lib/html.php";
require_once "lib/convert_html.php";
require_once "lib/init.php";

//$title = urldecode(($_SERVER["QUERY_STRING"] != '') ? $_SERVER["QUERY_STRING"] : 'FrontPage');
$title = $get['page'];
$debug = 0;
$text = get_source($title);
$body =& new Body(0);
$body->parse($text);

header("Content-Type: text/html; charset=EUC-JP");
print "<title>".htmlspecialchars($title)." - Pukiwiki to Hiki</title>";
print "<pre>";
print "// Page: ".htmlspecialchars($title)."\n";
print "// Converted by PukiWiki2Hiki Script\n";
print htmlspecialchars(toHiki($body));
print "// ----\n";
print "</pre>";

#print_r($body);

function toHiki($e, $depth = 0)
{
    $depth++;
    $str = "";
    $prev = null;
    foreach ($e->elements as $child)
    {
        if ($debug)
            $str .= (is_string($child) ? "(string)" : get_class($child))."\n";

        switch (strtolower(get_class($child)))
        {
            case 'heading':
                $str .= str_repeat("!", $child->level-1).toHiki($child, $depth)."\n";
                break;
            case 'inline':
                $str .= toHiki($child, $depth);
                if (
                    (is_a($e, 'listelement') && $e->head == 'li')
                ) {
                    $str .= "\n";
                }
                break;
            case 'dlist':
                $str .= toHiki($child, $depth) . "\n";
                break;
            case 'listelement':
                if ($child->head == 'dt') {
                    $str .= ":".toHiki($child, $depth) .":";
                } else if($child->head == 'li') {
                    // li
                    $str .= str_repeat((is_a($e, 'ulist')?"*":"#"), $child->level) . toHiki($child);
                } else {
                    // dd
                    $str .= toHiki($child, $depth) . "\n";
                }
                break;
            case 'paragraph':
                $str .= "\n".toHiki($child, $depth) . "\n\n";
                break;
            case 'pre':
                $str .= " ".join("\n ", $child->elements). "\n\n";
                break;
            case 'ulist':
            case 'olist':
                $str .= toHiki($child, $depth);
                break;
            case 'table':
                foreach ($child->elements as $row)
                {
                    $str .= "|";
                    foreach ($row as $cell)
                    {
                        $str .= "|".toHiki($cell, $depth)."|";
                    }
                    $str .= "|\n";
                }
                break;
            case 'tablecell':
                $str .= toHiki($child, $depth)."\n";
                break;
            default:
                if (is_string($child))
                {
                    // string
                    $str .= convertSpecialChars($child).
                    (strtolower(get_class($e->parent)) != 'listelement' && is_string($prev) ? "\n\n" : '');
                } else {
                    $str .= toHiki($child, $depth);
                }
        }
        $prev = $child;

        if ($debug)
            $str .= "\n/".(is_string($child) ? "(string)" : get_class($child))."\n";
    }

    // wikiname
    $str = preg_replace('/<a href="[^"]+" title="[^"]+">(.+?)<\/a>/', '[[$1]]', $str);
    $str = preg_replace('/\[\[\?\|.*?\]\]/', '', $str);
    // del
    $str = preg_replace('/<del>(.+?)<\/del>/', '==$1==', $str);

    // em/strong
    $str = preg_replace('/<em>(.+?)<\/em>/', "''$1''", $str);
    $str = preg_replace('/<strong>(.+?)<\/strong>/', "'''$1'''", $str);

    // delete html
    $str = preg_replace('/<a class="anchor_super"[^>]+>(.*?)<\/a>/', '', $str);
    $str = preg_replace(array('/<a href="\/redir\/([^"]+)"[^>]*>(.*?)<\/a>/', '/<a href="([^"]+)"[^>]*>(.*?)<\/a>/'), array('[[$2|$1]]', '[[$2|$1]]'), $str);
    $str = preg_replace('/<\/?[^>]+>/', '', $str);

    // quot/amp/dagger -> "/&/
    $str = preg_replace(array('/&quot;/', '/&amp;/', '/&dagger;/', '/&lt;/', '/&gt;/'), array('"', '&', '', '<', '>'), $str);

    // #contents -> toc
    $str = preg_replace('/==#contents==/', '{{toc}}', $str);

    return $str;
}

function convertSpecialChars($s) {
    return preg_replace(array(
        '/^#/'
    ), array(
        '.#'
    ), $s);
}
トラックバック - http://subtech.g.hatena.ne.jp/mayuki/20061110