Some of you have asked me if I could publish the php code of the code2css tool that I wrote some months ago for my need.
I'm a php noob, so if you find a way to improve the code, thanks to contact me ;)
Here is the php code:
function count_blank($line,$c) {
$sp=0;
$char = substr($line, $sp, 1);
while($sp<=(strlen($line)-1) && (true && $c == $char)) $char = substr($line, $sp++, 1);
return $sp;
}
function printCode($code)
{
$tab=array();
if (!is_array($code)) $code = explode("\n", $code);
$count_lines = count($code);
$i = 1;
$tab[0]= "<ol class=\"code\">";
foreach ($code as $line => $code_line) {
$sp = count_blank($code_line,"\t");
if($sp>=8) $sp=3;
if($sp==0) {
$sp = count_blank($code_line," ");
$r2 = "<li class=\"t".$sp."\">";
}
else $r2 = "<li class=\"t".$sp."\"><code>";
$sp!=0 ? $e="</code></li>" : $e="</li>";
$r2 .= stripslashes(htmlentities(trim(($code_line)))).$e;
$r .= $r2;
$tab[$i++] = $r2;
}
$tab[$i+1] = "</ol>";
echo "<ol class=\"code\">".$r.</ol>";
return $tab;
}
Don't forget that I wrote this code only for my need and I published the tool on my blog thinking it will be useful for others, so I don't tell that it is the best solution but my goal with this code is to have a clean css code without a ton of " " at the beginning of each line.
Now here is the css code:
ol { padding:0 0 0 2.5em }
.code {
padding:3px 5px 3px 35px;
border:1px solid #808080;
list-style-type: decimal-leading-zero;
font-family: Courier
}
.code code {
font-family:Monaco,"Courier New",Tahoma;
font-size: 1.2em
}
.code li {
margin:2px 0;
padding:0 5px;
background:#f7f7f5
}
.code li.t1 { padding-left:4ex }
.code li.t2 { padding-left:8ex }
.code li.t3 { padding-left:12ex }
.code li.t4 { padding-left:16ex }
.code li.t5 { padding-left:20ex }
.code li.t6 { padding-left:24ex }
.code li.t7 { padding-left:28ex }
.code li.t8 { padding-left:32ex }
Note: by writing this article, I noticed that I need to change the code a little in order to keep the backslashes in the php code. I will make an update soon. You just need to use the html code \ to replace the backslash character.
I'm a php noob, so if you find a way to improve the code, thanks to contact me ;)
Here is the php code:
function count_blank($line,$c) {
$sp=0;
$char = substr($line, $sp, 1);
while($sp<=(strlen($line)-1) && (true && $c == $char)) $char = substr($line, $sp++, 1);
return $sp;
}
function printCode($code)
{
$tab=array();
if (!is_array($code)) $code = explode("\n", $code);
$count_lines = count($code);
$i = 1;
$tab[0]= "<ol class=\"code\">";
foreach ($code as $line => $code_line) {
$sp = count_blank($code_line,"\t");
if($sp>=8) $sp=3;
if($sp==0) {
$sp = count_blank($code_line," ");
$r2 = "<li class=\"t".$sp."\">";
}
else $r2 = "<li class=\"t".$sp."\"><code>";
$sp!=0 ? $e="</code></li>" : $e="</li>";
$r2 .= stripslashes(htmlentities(trim(($code_line)))).$e;
$r .= $r2;
$tab[$i++] = $r2;
}
$tab[$i+1] = "</ol>";
echo "<ol class=\"code\">".$r.</ol>";
return $tab;
}
Don't forget that I wrote this code only for my need and I published the tool on my blog thinking it will be useful for others, so I don't tell that it is the best solution but my goal with this code is to have a clean css code without a ton of " " at the beginning of each line.
Now here is the css code:
ol { padding:0 0 0 2.5em }
.code {
padding:3px 5px 3px 35px;
border:1px solid #808080;
list-style-type: decimal-leading-zero;
font-family: Courier
}
.code code {
font-family:Monaco,"Courier New",Tahoma;
font-size: 1.2em
}
.code li {
margin:2px 0;
padding:0 5px;
background:#f7f7f5
}
.code li.t1 { padding-left:4ex }
.code li.t2 { padding-left:8ex }
.code li.t3 { padding-left:12ex }
.code li.t4 { padding-left:16ex }
.code li.t5 { padding-left:20ex }
.code li.t6 { padding-left:24ex }
.code li.t7 { padding-left:28ex }
.code li.t8 { padding-left:32ex }
Note: by writing this article, I noticed that I need to change the code a little in order to keep the backslashes in the php code. I will make an update soon. You just need to use the html code \ to replace the backslash character.




Comments