<?php if (!defined('PmWiki')) exit();
/**
 * yumlme.php
 * 
 * Copyright 2009 Volker Eichhorn
 * This file is distributed under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Module to create image links to engine room's yUML online diagram service.  
 * Documentation for yUML syntax is available here: 
 * @see https://yuml.me/diagram/scruffy/class/draw
 *
 * (:yuml:)
 * [Customer]+1->*[Order]
 * [Order]++1-items >*[LineItem]
 * [Order]-0..1>[PaymentMethod]
 * (:yumlend:)
 * 
 * Gets converted into an image tag with this source:
 *
 * https://yuml.me/diagram/class/[Customer]+1->*[Order],\ 
 *              [Order]++1-items >*[LineItem], [Order]-0..1>[PaymentMethod] 
 *
 *
 * To use this module, simply place this file in the cookbook/ directory and
 * add the following line into config.php:
 *
 *    include_once("$FarmD/cookbook/yumlme.php");
 *
 * 
 * 
 * 🚨 🆕 🚧 ‼️ VOLKER EICHHORN ‼️ 🚧 🆕 🚨
 * 
 * I have fixed for new pmwiki version.. 
 * 
 * you can remove all my comments/copyright if you like, i don't care.
 * 
 * just let me know if you like it or not iz all i want! :D
 * 
 * 
 * - added https url to silence browser mixed content alerts
 * - added support for activity diagram type
 * - returns images as svg
 * - updated use of Markup() function
 * - added direction
 * 
 * @version 0.3
 * @copyright Copyright © 2018 by CIID Mike. All rights reserved.
 * @author CIID Mike
 */
 
// $RecipeInfo['YumlMe']['0.1'] = '2009-10-15';
$RecipeInfo['YumlMe']['0.3'] = '2020-01-03'; //CIID

function yumleize($str, $sargs)
 {
    $args = ParseArgs($sargs);
    $bscale    = strstr($sargs, 'scale');
    $bscruffy  = strstr($sargs, 'scruffy');
    $bstyle    = strstr($sargs, 'style');
    $bdir      = strstr($sargs, 'dir');
    $busecase  = strstr($sargs, 'usecase');
    $bactivity = strstr($sargs, 'activity');

    $url = "https://yuml.me/diagram/";

    $opts = [];
    
    if ($bstyle)
     {
         $opts[] = $args['style'];
     }
    if ($bdir)
     {
         $opts[] = 'dir:'.$args['dir'];
     }
    if ($bscale) 
     { 
         $opts[] = 'scale:'.$args['scale'];
     }
     
    $url.= implode(';', $opts);
    
    if ($bstyle || $bscale || $bdir) 
     { 
        $url .= '/'; 
     }
    if ($busecase) 
     { 
        $url .= 'usecase/'; 
     } 
    elseif ($bactivity) 
     { 
        $url .= 'activity/'; 
     } 
    else 
     { 
        $url .= 'class/'; 
     }
    
    $url .= trim(
        preg_replace('/(\<:vspace\>|[\\r\\n])+/imsx', ',', $str),
        ', '
     );
     
    return Keep('<img src="'.$url.'.svg" />');
}

Markup('yuml',
    'fulltext', 
    '/\\(:yuml(.*?):\\)(.*?)\\(:yumlend:\\)/sxi',
    function ($m)
     {
         return yumleize($m[2], $m[1]);
     }
 );