<?php

/*
   Converts Adl1-Markup to Adl2-Markup

   Copyright 2006 Nils Knappmeier (nk@knappi.org)

   Permission is hereby granted, free of charge, to any person obtaining 
   a copy of this software and associated documentation files (the 
   "Software"), to deal in the Software without restriction, including 
   without limitation the rights to use, copy, modify, merge, publish, 
   distribute, sublicense, and/or sell copies of the Software, and to 
   permit persons to whom the Software is furnished to do so, subject to 
   the following conditions:

   The above copyright notice and this permission notice shall be 
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
   SOFTWARE.

 
 */

function AdlUpgradeCode() {
   global $pagename;
   return FmtPageName("<form action='{\$PageUrl}' method='post'>
            <input type='hidden' value='$pagename' name='n'/>
            <input type='hidden' value='adlupgrade' name='action' />
            <input type='hidden' value='AdlUpgradeTool' name='author' />
            <input type='hidden' value='Upgrading Adl Markup to Version 2.x' name='csum' />
            <input type='submit' value='Click here to upgrade markup to AddDeleteLine2' name='ok' />
            </form>",$pagename);

}


Markup('adlu1','directives','/\(:form (.*):\)/e',"Keep(AdlUpgradeCode())");
Markup('adlu2','<adlu1','/\(:form (.*) target=(.*):\)/e',"Keep(AdlUpgradeCode())");
Markup('adlu3','directives','/\(:endform:\)/e',"Keep(AdlUpgradeCode())");
Markup('adlu4','directives','/\(:textfield +([^ ]*) *?:\)/e',"Keep(AdlUpgradeCode())");
Markup('adlu5','directives','/\(:submit (.*):\)/e',"Keep(AdlUpgradeCode())");
Markup('adlu6','[=','/\(:appendas "(.*)":\)/e',"Keep(AdlUpgradeCode())");
Markup('adlu7','directives','/\(:addhere (.*):\)/e',"Keep(AdlUpgradeCode())");
Markup('adlu8','directives','/\(:prependhere (.*):\)/e',"Keep(AdlUpgradeCode())");
Markup('adlu9','directives','/((.*)\(:delete:\)(.*))/e',"Keep(AdlUpgradeCode())");




function AdlConvertTemplate($template) {
    $temp = str_replace('(:delete:)', '(:adl delete:)',$template);
    return '(:adl template "'.$temp.'":)';
}

function AdlConvertDelete() {
    static $uid = 0;
    global $pagename;
    
    $uid++;
    $uidstr = time().'b'.$uid;
    return "(:adl delete $uidstr $pagename:)";
}



$HandleActions['adlupgrade'] = 'HandleAdlUpgrade';


function HandleAdlUpgrade($pagename) {
    global $HandleActions;

    $page = RetrieveAuthPage($pagename,"read");
    if (!$page) { Abort("?cannot edit $pagename"); }
    $text = $page['text'];
    

    
    /* (:adl delete:) inside templates */
    $text = preg_replace('/\(:appendas "(.*)":\)/e',
                "AdlConvertTemplate(PSS('$1'))",$text);
    /* Misc form markup */
    $text = preg_replace('/\(:form (.*) target=(.*):\)/',
                '(:adl form $1 target=$2:)',$text);
    $text = preg_replace('/\(:form (.*?):\)/',
                '(:adl form $1:)',$text); 
    $text = preg_replace('/\(:endform:\)/',
                '(:adl end:)', $text);
    $text = preg_replace('/\(:textfield +([^ ]*) *?:\)/',
                '(:input text $1:)',$text);
    $text = preg_replace('/\(:submit (.*):\)/',
                '(:input submit ok $1:)',$text);

    /* append and prepend markup */
    $text = preg_replace('/\(:addhere (.*):\)/',
                '(:adl append $1:)',$text);
    $text = preg_replace('/\(:prependhere (.*):\)/',
                '(:adl prepend $1:)',$text);

    /* delete statements outside templates */
    $text = preg_replace('/\(:delete:\)/e','AdlConvertDelete()',$text);

            
    $_POST['text']=get_magic_quotes_gpc()?addslashes($text):$text;
    $handle = $HandleActions['edit'];
    $_POST['post']='Save';
    return $handle($pagename);

    
}



# vi:shiftwidth=4:autoindent:softtabstop=4:expandtab:
?>