Benutzer:Lateiner/Syntax corrector/Code
Zur Navigation springen
Zur Suche springen
<?
/**
* @author Lateiner <http://de.wikipedia.org/wiki/Benutzer_Diskussion:Lateiner>
* @version Rev 7
* @project Mediawiki Syntax corrector
* @copyright Lateiner 2008
*/
$time_start = microtime(true);//time measurement
$input=$_POST['input'];//get the Imput
$all_input_array=explode("\n",$input);//array of the Input. One value is one line
//variables initialization
$current_line_count=0;
$headline_count=0;
foreach ($all_input_array as $one_line) {//gets one line out of the array
$one_line=rtrim($one_line);//string cleaning
$line_input_array=str_split($one_line);//splits the line in an array of characters
if ($line_input_array[0]=='='){//if first array element is an = it is possible that it is a headline
if (array_pop($line_input_array)=='='){//if the last element is an = then it is a headline
//if it is a Headline
$line_input_array[]='=';//chargeback for array_pop
$headline_row_array[]=$current_line_count;//all lines numbers of headlines
foreach ($line_input_array as $one_char) {//counting how many = are in this line
if ($one_char=='='){
$this_headline_level_count++;
}
}
$old_level=intval($this_headline_level_count/2);//old level of the headline
$old_levels_array[]=$old_level;//all old levels of headlines
//now lets look for the new level of this headline
//if it is an headline level 1 or the fist headline then it is level 2
if ($old_level==1 or $headline_count==0){
$new_level=2;
}
//if this headline have the same old level then the former headline it
//is the same with the new level
elseif($old_level_former_headline==$old_level){
$new_level=$new_level_former_headline;
}
//if this headline have an higher level then the former headline the new
//level is the level of the former +1
elseif($old_level_former_headline<$old_level){
$new_level=$new_level_former_headline+1;
}
//if the headline level is lower then the former headline we must search
//for the new level. If the coult not found we use level 2
//im not sure at the moment if this is ther right way for this
elseif($old_level_former_headline>$old_level){
if ($key_sibling=array_search($old_level,$old_levels_array)){
$new_level=$new_levels_array[$key_sibling];
}
else{
$new_level=2;
}
}
//generate the new line
$old_line_string=$all_input_array[$current_line_count];//fetch the content of the headline line
$old_line_string=str_replace('=','',$old_line_string);//remove all =
for ($i = 1; $i <= $new_level; $i++) {//generate the new headline characters
$headline_char.='=';
}
//compose headline characters and the content
$new_line_string=$headline_char.' '.trim($old_line_string).' '.$headline_char;
//restore the new line in the array of lines
$all_input_array[$current_line_count]=$new_line_string;
//set the variables for the next run.
$new_levels_array[]=$new_level;
$new_level_former_headline=$new_level;
$old_level_former_headline=$old_level;
$headline_char='';
$new_level=0;
$this_headline_level_count=0;
$headline_count++;
}//end if it is a headline
}
$current_line_count++;
}//end of foreach
$output=implode("\n",$all_input_array);//generate one output string
$output=stripslashes(htmlspecialchars($output));//convert some characters, remove addet slashes
//makes the html Page
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Headline Syntax Corrector</title>
</head>
<body onload="document.getElementById(\'input\').select();">
<form method="post" action="index.php">
<textarea id="input" cols="150" rows="30" name="input" onchange="document.getElementById(\'Submit\').click();">'.$output.'</textarea><br />
<input type="Submit" id="Submit">
</form>
<p>';
$time_end = microtime(true);//time measurement
$time = $time_end - $time_start;
echo "Script runtime $time seconds\n";
echo'</p>
</body>
</html>';
?>