Jump to content

Toolserver:User:Luxo

From mediawiki.org

This page was moved from the Toolserver wiki.
Toolserver has been replaced by Toolforge. As such, the instructions here may no longer work, but may still be of historical interest.
Please help by updating examples, links, template links, etc. If a page is still relevant, move it to a normal title and leave a redirect.

Hi!

I'm

--Luxo 21:04, 6 July 2011 (UTC)



 Get all templates from wikitext and put it in a array
 {{Template|index=para1}}
       
       ↓ 
       ↓
       ↓
 
 Array
 (
     [0] => Array
         (
             [fulltemplate] => {{Template|index=para1}}
             [name] => Template
             [parameters] => Array
                 (
                     [index] => para1
                 )
  
         )
  
 )
function templateparser($wikitext,$marktemplates=true)
{
  //Parser for wikisyntax templates
  /*
    Copyright © Luxo 2011
    
    This function is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This function is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this function.  If not, see <http://www.gnu.org/licenses/>.
  */
  if(!is_string($wikitext)) {
    die("templateparser: no string attatched");
  }
  
  $wikitext = str_replace("\n", "¶", $wikitext);
  
  $templates = array();
  $reg = "/\{\{[^\{\{]*\}\}/Uu";
  while(preg_match($reg, $wikitext, $x) == 1){
    $templates[]['fulltemplate'] = $x[0];
    $replacer = count($templates) - 1;
    $replacer = ($marktemplates == true) ? "�".$replacer."�" : "";
    //exclude this template from wikitext
    $wikitext = str_replace($x[0], $replacer, $wikitext);
  }
  //Array with Templates generated. Read name of the template
  foreach($templates as $key => $template) {
    $reg = "/\{\{([^\}\|]*).*/u";
    $templates[$key]["name"] = trim(preg_replace($reg,"\\1",$template['fulltemplate']));
  }
  
  //Get all keys and content
  foreach($templates as $key => $template) {
    $templates[$key]["parameters"] = array();
    
    //links with [[goal|text]] must be changed
    $reg ="/(\[\[[^\{\}\[\]]*)\|([^\{\}\[\]]*\]\])/Uu";
    $t = preg_replace($reg, "\\1�\\2", $templates[$key]["fulltemplate"]);
    
    //split parameters
    $reg = "/\{\{".preg_quote($templates[$key]["name"])."|(.*)\}\}/Uu";
    $x = preg_replace($reg,"\\1",$t);
    $x = explode("|", $x);
    foreach($x as $keyX => $param) {
      if(trim($param) != "") {
        if(!stripos($param, "=")) {
          $templates[$key]["parameters"][$keyX] = $param;
        } else {
          $v = explode("=", $param, 2);
          $v = str_replace("¶", "\n", $v);
          $templates[$key]["parameters"][trim($v[0])] = str_replace("�","|",$v[1]);
        }
      }
    }
  }
  return $templates;
}