Extension talk:Flowchart

From MediaWiki.org
Jump to: navigation, search
The following discussion has been transferred from Meta-Wiki.
Any user names refer to users of that site, who are not necessarily users of MediaWiki.org (even if they share the same username).

My New Version


[edit] Example input

<flowchart>
Init:Main:Deinit
Start;Start;Start
Init Main begin
End;Loop;
Deinit Main exit
Main End
Deinit End
</flowchart>

[edit] The corresponding output

 Init      Main     Deinit   
  |         |         |   
Start     Start     Start   
  |         |         |   
  | begin   |         |   
  | ------> |         |   
  |         |         |   
 End      Loop        |   
  |         |         |   
  |         |   exit  |   
  |         | <------ |   
  |         |         |   
  |        End        |   
  |         |         |   
  |         |        End   

[edit] The code

<?php
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/Flowchart.php");

$wgExtensionFunctions[] = "wfFlowChartExtension";

function wfFlowChartExtension(){
        global $wgParser;
        # register the extension with the WikiText parser
        # the first parameter is the name of the new tag.
        # In this case it defines the tag <flowchart> ... </flowchart>
        # the second parameter is the callback function for
        # processing the text between the tags
        $wgParser->setHook( "flowchart", "renderFlowChart" );
}

# The callback function for converting the input text to HTML output
function renderFlowChart( $input ) {
# $argv is an array containing any arguments passed to the
# extension like <example argument="foo" bar>
        $lines=split("\n",$input);
        
        //remove empty lines at start (<flowchart>\n...)
        while(!preg_match("/:/i",$lines[0]))
                array_shift($lines);
        
        $colums=split(":",$lines[0]);
        $count=count($colums);
        $output .= "<CENTER><TABLE cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n<TR>";
        foreach($colums as $Name)
                $output .= "<TH>$Name</TH><td> </td>";
        $output .= "</TR>\n";
        array_shift($lines); #remove first line
        
        foreach($lines as $line)
                if ($line != "") { #ignoring empty lines
                        
                        //Insert empty column
                        $output .= "\n<TR>";
                        for ($i=0;$i<$count;$i++)
                                $output .= "<td>|</td><td> </td>";
                        $output.="</TR>\n<TR>";
                        
                        if (preg_match("/;/i",$line)){
                                $fields=split(";", $line);
                                if (count($fields)==$count)
                                        foreach($fields as $field){
                                                if ($field == "")
                                                        $field = "|";
                                                $output .= "<td>$field</td><td> </td>";
                                        }
                        }
                        else{
                                $fields=split(" ",$line);
                                $col1=array_search($fields[0],$colums);
                                if (count($fields)==2){
                                        for($i=0; $i<$col1;$i++)
                                                $output .= "<TD>|</TD><td> </td>";
                                        $output .= "<td>".$fields[1]."</td><td> </td>";
                                        $skip = $count - $col1 - 1;
                                        for ($i=0; $i<$skip; $i++)
                                                $output .= "<td>|</td><td> </td>";
                                        $output .= "</tr>";
                                }
                                else{
                                        $col2=array_search($fields[1],$colums);
                                        $mes=$fields[2];
                                        $dir= $col1<$col2 ? "------>" : "<------";
                                        
                                        $skip=min($col1,$col2);
                                        for($i=0;$i<$skip;$i++)
                                                $output.="<TD>|</TD><TD> </td>";
                                        $length=(abs($col1-$col2)*2)-1;
                                        $output.="<TD>|</TD><TD colspan=$length>$mes</TD>";
                                        $skip=$count-max($col1,$col2);
                                        for($i=0;$i<$skip;$i++)
                                                $output.="<TD>|</TD><td> </TD>";
                                        $output.="</TR>\n<TR>";
                                        
                                        $skip=min($col1,$col2);
                                        for($i=0;$i<$skip;$i++)
                                                $output.="<TD>|</TD><td> </TD>";
                                        $length=abs($col1-$col2)-1;
                                        $output.="<TD>|</TD><TD>$dir</TD>";
                                        for($i=0;$i<$length;$i++)
                                                $output.="<td> </td><TD>$dir</TD>";
                                        $skip=$count-max($col1,$col2);
                                        for($i=0;$i<$skip;$i++)
                                                $output.="<TD>|</TD><td> </TD>";
                                        $output.="</TR>\n";
                                }
                        }
                }
        $output .= "\n</TABLE></CENTER>";
        return $output;
}
?>
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox