pjkersten / PlantUML

PlantUML plugin for MediaWiki
34 stars 17 forks source link

[Feature request] Support for @start(wbs|mindmap|...) tags #30

Open dvc opened 4 years ago

dvc commented 4 years ago

For example:

<uml ext="wbs">
...
</uml>

render to

@startwbs
...
@endwbs

empty or unsupported ext equals to "uml"

dvc commented 4 years ago
function wrap_formula($PlantUML_Source, $ext = null) {
    if ($ext === null) {
       $ext = 'uml';
    }
    $string  = "@start$ext\n";
    // Allow ditaa and graphviz first-line directives and accents (french, ..)
    // $string .= preg_replace("/^\r?\n/", "", utf8_decode($PlantUML_Source), 1) . "\n";
    $string.=$PlantUML_Source;
    $string .= "@end$ext";

    return $string;
}

function renderPlantUML($PlantUML_Source, $imgFile, $dirname, $filename_prefix, $ext = null) {
    global $plantumlJar, $plantumlImagetype;

    $PlantUML_document = wrap_formula($PlantUML_Source, $ext);

    // create temporary uml text file
    $umlFile = $dirname."/".$filename_prefix.".uml";
    $fp = fopen($umlFile,"wb");
    $w  = fputs($fp,mb_convert_encoding($PlantUML_document,'UTF-8'));
    fclose($fp);

    // Lauch PlantUML
    if ($plantumlImagetype == 'svg') {
        $typestr = ' -tsvg';
    } else {
        $typestr = '';
    }
    $command = "java -jar ".$plantumlJar.
               "{$typestr} -charset UTF-8 -o \"{$dirname}\" \"{$umlFile}\"";

    $status_code = exec($command);

    // Delete temporary uml text file
    // unlink($umlFile);

    // Only return existing path names.
    if (is_file($imgFile)) {
        return $imgFile;
    }

    return false;
}

function getImage($PlantUML_Source, $argv, $parser=null) {
    global $plantumlImagetype;
    global $usecloud;

    // Compute hash
    $title_hash = md5(getPageTitle($parser));
    $formula_hash = md5($PlantUML_Source);

    $filename_prefix = 'uml-'.$title_hash."-".$formula_hash;
    $dirname = getUploadDirectory();
    $full_path_prefix = $dirname."/".$filename_prefix;
    $result = array(
        'mapid' => $formula_hash, 'src' => false, 'map' => '', 'file' => ''
    );
    $imgFile = $dirname."/".$filename_prefix.".$plantumlImagetype";
    $mapFile = $dirname."/".$filename_prefix.".map";
    // Check cache. When found, reuse it. When not, generate image.
    // Honor the redraw tag as found in <uml redraw>
    if (is_file($imgFile) and is_file($mapFile) and !array_key_exists('redraw', $argv) ) {
        $result['file'] = $imgFile;
        $result['mapfile'] = $mapFile;
    } else {
        if ($usecloud) {
            $result['file'] = renderPlantUML_cloud($PlantUML_Source, $imgFile);
                    $result['mapfile'] = renderPlantUML_cloud_map($PlantUML_Source, $mapFile);
    } else {
        $ext = 'uml';
        if (isset($argv['ext'])) {
            $ext = $argv['ext'];
        }

            $result['file'] = renderPlantUML($PlantUML_Source, $imgFile, $dirname, $filename_prefix, $ext);
        }
    }
    // do we have a mapfile?
    if (isset($result['mapfile'])) {
        if (is_file($result['mapfile'])) {
                // map file is temporary data - read it and delete it.
                $fp = fopen($result['mapfile'],'r');
                $image_map_data = fread($fp, filesize($result['mapfile']));
                fclose($fp);
                // Replace generic ids with unique ids: first two ".." fields.
                $result['map'] = preg_replace('/"[^"]*"/', "\"{$result['mapid']}\"", $image_map_data, 2);
        }
    }
    if ($result['file']) {
        $result['src'] = getUploadPath()."/".basename($result['file']);
        if ((!$usecloud) && $plantumlImagetype == 'png') {
            $map_filename = $full_path_prefix.".cmapx";
            if (is_file($map_filename)) {
                // map file is temporary data - read it and delete it.
                $fp = fopen($map_filename,'r');
                $image_map_data = fread($fp, filesize($map_filename));
                fclose($fp);
                //unlink($map_filename);
                // Replace generic ids with unique ids: first two ".." fields.
                $result['map'] = preg_replace('/"[^"]*"/', "\"{$result['mapid']}\"", $image_map_data, 2);
            }
        }
    }
    return $result;
}