ソフトウェア/pukiwiki/uml.inc.php のバックアップ(No.2)

更新


公開メモ

UML を貼るためのプラグインです

このように書くと、

&uml(
[*] -> idle
idle: rready = 1
idle: rvalid = 0
idle --> reading : arvalid & \narready
reading: rready = 0
reading: rvalid = 0
reading -right--> sending : read done
sending: rready = 0
sending: rvalid = 1
sending --> idle : rready
);

こう表示されます。

&uml( [*] -> idle idle: rready = 1 idle: rvalid = 0 idle --> reading : arvalid & \narready reading: rready = 0 reading: rvalid = 0 reading -right--> sending : read done sending: rready = 0 sending: rvalid = 1 sending --> idle : rready );

PlantUML を利用させていただきました

http://ja.plantuml.com/

/usr/local/bin に plantuml.jar を置いてあります。

ソースファイル

plugin/uml.inc.php

LANG:php
<?php

function plantuml($source)
{
    $tmp = tempnam("/tmp", "plantuml");

    $source = <<<"EOS"
@startuml
skinparam defaultFontName arial,helvetica,MS Gothic
skinparam state {
  FontStyle bold
}
$source
@enduml
EOS;

    $handle = fopen($tmp, "w");
    fwrite($handle, preg_replace("/\\r?\\n/","\r\n",$source));
    fclose($handle);
    system("/usr/bin/java -Dfile.encoding=utf-8 -jar /usr/local/bin/plantuml.jar ".$tmp." -tsvg");
    unlink($tmp);

    $svg = file_get_contents($tmp.".svg");
    unlink($tmp.".svg");

    return preg_replace('/^<\\?xml.*?>/', '', $svg);
}

function plantuml_convert($source)
{
    $image_base = sha1($source);
    $image_dir = "uml_img/{$image_base[0]}/{$image_base[1]}";
    $image_svg = "{$image_dir}/{$image_base}.svg";

    if(!file_exists($image_svg)) {
        $svg = plantuml($source);
        
        if (mb_substr($svg,0,4)=="<svg") {
            if(!file_exists($image_dir)){
                mkdir($image_dir, 0770, true);
            }
            $fh = fopen($image_svg, "w");
            fwrite($fh, $svg);
            fclose($fh);
        }
    } else {
        $svg = file_get_contents($image_svg);
    }

    return $svg;
}

function plugin_uml_inline()
{
    $aryargs = func_get_args();
    $source = join(",", $aryargs);
    $source = rtrim($source, ",");  //remove extra comma at the end.
    $source = str_replace("<br>", "\n",$source);
    return plantuml_convert($source);
}

?>

pukiwiki.php の改造

複数行のパラメータを渡せるように、次のように改造しました。

LANG:php
//      $body  = convert_html(get_source($base));

        for($i=0;$i<count($lines);$i++)
            if (preg_match('/\&uml\(/', $lines[$i]) && $lines[$i][0]!=' ') 
                while(!preg_match('/\&uml\(.*?\)\;/', $lines[$i]) && ($i+1<count($lines)))
                    array_splice($lines, $i, 2, chop($lines[$i]) . '<br>' . $lines[$i+1] );

        $body  = convert_html($lines);

plantuml のソースコードを簡単に書くには

こちらが参考になりそうです。

Qiita - AtomとPlantUMLで爆速UMLモデリング
http://qiita.com/nakahashi/items/3d88655f055ca6a2617c

質問・コメント





Counter: 5952 (from 2010/06/03), today: 2, yesterday: 0