Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 20th, 2007, 1:41 AM   #11
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 395
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
The only way I could address the problem in IE was to rewrite the code to insert line breaks at a given number of characters, and thus wordwrap the source code to that specific number. If the window gets resized smaller than that character count then obviously some bad stuff happens but it's all I could figure out:[PHP]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Grim PHP Syntax Highlighter</title>
<style type="text/css">
body {
font-family: Courier New;
font-size: 10pt;
}
div.lineNumbers {
float: left;
text-align: right;
margin-right: 8px;
padding-right: 8px;
padding-left: 8px;
border-right: 1px #999999 solid;
color: #777777;
}
span.v {
color: #0000BB;
}
span.k {
color: #007700;
}
span.s {
color: #DD0000;
}
span.c {
color: #FF8000;
}
</style>
</head>
<body>
<?php
define('SPLIT', 112);
$filename = 'syntax_h.php';
ob_start();
highlight_file($filename);
$source = ob_get_contents();
ob_end_clean();
$source = str_replace('<span style="color: #0000BB">', '<span class="v">', $source);
$source = str_replace('<span style="color: #007700">', '<span class="k">', $source);
$source = str_replace('<span style="color: #DD0000">', '<span class="s">', $source);
$source = str_replace('<span style="color: #FF8000">', '<span class="c">', $source);
$source = explode(chr(13), $source);
$source = implode('', $source);
$source = explode(chr(10), $source);
$source = implode('', $source);
$source = explode('<br />', $source);
$source[0] = substr($source[0], 35);
$source[count($source) - 1] = substr($source[count($source) - 1], 0, strlen($source[count($source) - 1]) - 14);
for($i = 0; $i < count($source) - 1; $i++){
$count = 0;
$temp = '';
while(strlen($source[$i]) > 0){
if(strpos($source[$i], '<span class="v">') === 0){
$source[$i] = substr($source[$i], 16);
$temp .= '<span class="v">';
}elseif(strpos($source[$i], '<span class="k">') === 0){
$source[$i] = substr($source[$i], 16);
$temp .= '<span class="k">';
}elseif(strpos($source[$i], '<span class="s">') === 0){
$source[$i] = substr($source[$i], 16);
$temp .= '<span class="s">';
}elseif(strpos($source[$i], '<span class="c">') === 0){
$source[$i] = substr($source[$i], 16);
$temp .= '<span class="c">';
}elseif(strpos($source[$i], '</span>') === 0){
$source[$i] = substr($source[$i], 7);
$temp .= '</span>';
}elseif(strpos($source[$i], '&nbsp;') === 0){
$source[$i] = substr($source[$i], 6);
$temp .= '&nbsp;';
$count++;
}elseif(strpos($source[$i], '&lt;') === 0){
$source[$i] = substr($source[$i], 4);
$temp .= '&lt;';
$count++;
}elseif(strpos($source[$i], '&gt;') === 0){
$source[$i] = substr($source[$i], 4);
$temp .= '&gt;';
$count++;
}elseif(strpos($source[$i], '&amp;') === 0){
$source[$i] = substr($source[$i], 5);
$temp .= '&amp;';
$count++;
}else{
$temp .= substr($source[$i], 0, 1);
$source[$i] = substr($source[$i], 1);
$count++;
}
if($count == SPLIT){
$temp .= '<br />' . "\n";
$count = 0;
}
}
if($count != 0){
$temp .= '<br />' . "\n";
}
$source[$i] = $temp;
}
echo '<div class="lineNumbers">' . "\n";
for($i = 0; $i < count($source); $i++){
echo $i + 1 . '<br />' . "\n";
for($j = 1; $j < substr_count($source[$i], '<br />'); $j++){
echo '<strong>&hellip;</strong><br />' . "\n";
}
}
echo "\n" . '</div>' . "\n";
echo '<div class="sourceCode">' . "\n";
for($i = 0; $i < count($source); $i++){
echo $source[$i];
}
echo "\n" . '</div>' . "\n";
?>
</body>
</html>[/PHP]The constant SPLIT controls the amount of characters you wish to display on a line.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Mar 20th, 2007, 1:54 AM   #12
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 836
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Your code removes empty lines (which IMO is a bad thing, as it makes the result less readable) but does not adjust the number of lines accordingly. A file I tested ended at line 103 (although there were actually 118 lines in the file, including blank lines); your code went on to print 118 line numbers.
titaniumdecoy is offline   Reply With Quote
Old Mar 20th, 2007, 6:41 AM   #13
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
heh, all things considered, you could conceivably just reduce it to:

[php]<style type="text/css">
.num {
float: left;
color: #777777;
text-align: right;
margin-right: 8px;
padding-right: 8px;
border-right: 1px #999999 solid;}

body {
font-family: Courier New;
font-size: 10pt;}
</style>
<html><body>
<?php
$file = basename($_SERVER['PHP_SELF']);

$lines = file($file);

echo '<div class="num">';
for ($i = 1; $i <= count($lines); $i++)
{
echo "$i<br />";
}
echo '</div>';
highlight_file($file);
?>
</body></html>[/php]
Once you put the numbers off to the left and separate it like that, you don't need to split the file anymore. Just make sure the line numbers line up (with a common font and size) and that's it. (This ignores line wrapping of course.)
Styx is offline   Reply With Quote
Old Mar 20th, 2007, 2:12 PM   #14
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 395
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Yes you're correct titaniumdecoy by replacing the chr(13) and chr(10) arbitrarily I have in fact neglected the whitespace added to the code. Which is important because when an error is generated in PHP the line numbers given take that into account. I believe that has been fixed with this version of the script.[PHP]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Grim PHP Syntax Highlighter</title>
<style type="text/css">
body {
font-family: Courier New;
font-size: 10pt;
}
div.lineNumbers {
float: left;
text-align: right;
margin-right: 8px;
padding-right: 8px;
padding-left: 8px;
border-right: 1px #999999 solid;
color: #777777;
}
span.v {
color: #0000BB;
}
span.k {
color: #007700;
}
span.s {
color: #DD0000;
}
span.c {
color: #FF8000;
}
</style>
</head>
<body>
<?php

define('SPLIT', 112);
$filename = basename($_SERVER['PHP_SELF']);

ob_start();
highlight_file($filename);
$source = ob_get_contents();
ob_end_clean();

$source = str_replace('<span style="color: #0000BB">', '<span class="v">', $source);
$source = str_replace('<span style="color: #007700">', '<span class="k">', $source);
$source = str_replace('<span style="color: #DD0000">', '<span class="s">', $source);
$source = str_replace('<span style="color: #FF8000">', '<span class="c">', $source);

$source = explode('<br />', $source);

$source[0] = substr($source[0], 36);
$source[count($source) - 1] = substr($source[count($source) - 1], 0, strlen($source[count($source) - 1]) - 15);

for($i = 0; $i < count($source) - 1; $i++){

$count = 0;
$temp = '';

$source[$i] = str_replace(chr(13), '', $source[$i]);
$source[$i] = str_replace(chr(10), '', $source[$i]);

if(strlen($source[$i]) == 0){
$count++;
}

while(strlen($source[$i]) > 0){
if(strpos($source[$i], '<span class="v">') === 0){
$source[$i] = substr($source[$i], 16);
$temp .= '<span class="v">';
}elseif(strpos($source[$i], '<span class="k">') === 0){
$source[$i] = substr($source[$i], 16);
$temp .= '<span class="k">';
}elseif(strpos($source[$i], '<span class="s">') === 0){
$source[$i] = substr($source[$i], 16);
$temp .= '<span class="s">';
}elseif(strpos($source[$i], '<span class="c">') === 0){
$source[$i] = substr($source[$i], 16);
$temp .= '<span class="c">';
}elseif(strpos($source[$i], '</span>') === 0){
$source[$i] = substr($source[$i], 7);
$temp .= '</span>';
}elseif(strpos($source[$i], '&nbsp;') === 0){
$source[$i] = substr($source[$i], 6);
$temp .= '&nbsp;';
$count++;
}elseif(strpos($source[$i], '&lt;') === 0){
$source[$i] = substr($source[$i], 4);
$temp .= '&lt;';
$count++;
}elseif(strpos($source[$i], '&gt;') === 0){
$source[$i] = substr($source[$i], 4);
$temp .= '&gt;';
$count++;
}elseif(strpos($source[$i], '&amp;') === 0){
$source[$i] = substr($source[$i], 5);
$temp .= '&amp;';
$count++;
}else{
$temp .= substr($source[$i], 0, 1);
$source[$i] = substr($source[$i], 1);
$count++;
}

if($count == SPLIT){
$temp .= '<br />';
$count = 0;
}
}

if($count != 0){
$temp .= '<br />';
}

$source[$i] = $temp;
}

echo '<div class="lineNumbers">' . "\n";
for($i = 0; $i < count($source); $i++){
echo $i + 1 . '<br />' . "\n";
for($j = 1; $j < substr_count($source[$i], '<br />'); $j++){
echo '<strong>&hellip;</strong><br />' . "\n";
}
}
echo '</div>' . "\n";
echo '<div class="sourceCode">' . "\n";
for($i = 0; $i < count($source); $i++){
echo $source[$i] . "\n";
}
echo '</div>' . "\n";

?>
</body>
</html>[/PHP]
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Mar 20th, 2007, 3:22 PM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I haven't messed with this, but I would think that if one pulled the lines into an array using the PHP functions, then one would put the onus of determining what is a line on the language (PHP, in this instance) of choice. The language will then deal with differences in platform and line-end definitions by virtue of the fact that it has to deal with these in its various implementations, anyway.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 20th, 2007, 6:43 PM   #16
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 395
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
I agree DaWei, I'm working under the assumption that PHP will determine what is and what isn't a line break and will structure the output of highlight_file accordingly.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Mar 20th, 2007, 6:49 PM   #17
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
What is the advantage of splitting out the source like that instead of with count(file()) and highlight_file() next to each other, besides the extras? (line wrapping, specific line highlighting, etc)
Styx is offline   Reply With Quote
Old Mar 20th, 2007, 7:01 PM   #18
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 836
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by Styx View Post
What is the advantage of splitting out the source like that instead of with count(file()) and highlight_file() next to each other, besides the extras? (line wrapping, specific line highlighting, etc)
It sounds to me like you are describing the result of this thread.
titaniumdecoy is offline   Reply With Quote
Old Mar 20th, 2007, 7:14 PM   #19
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Absolutely. This thread, presented as a definitive/useful solution, was obviously premature.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 20th, 2007, 10:23 PM   #20
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
I was wondering if it was possible to use the standard HTML ordered list to do the line numbers. It's long and pointless, but I enjoyed doing it. The problem was closing all tags in one line and re-opening them on the next.

[php]<?php
$file = __FILE__; // replace with whatever

function prepend_open_tags ($line, $open_tags)
{
foreach (array_reverse($open_tags) as $tag)
{
$line = "<$tag>$line";
}

return $line;
}

function find_open_tags ($line)
{
$open_tags = array();
for ($i = 0; $i < strlen($line); $i++)
{
if ($line{$i} == '<' && $line{$i + 1} == '/')
{
$open_tags = array_slice($open_tags, 0, count($open_tags) - 1);
}
else if ($line{$i} == '<')
{
$tag = '';
while ($line{$i + 1} != '>')
{
$i++;
$tag .= $line{$i};
}

if ($tag{strlen($tag) - 1} != '/')
{
$open_tags[] = $tag;
}
}
}

return $open_tags;
}

function close_tags ($line, $open_tags)
{
foreach (array_reverse($open_tags) as $tag)
{
$tag = explode(' ', $tag);
$tag = $tag[0];
$line .= "</$tag>";
}

return $line;
}

$content = explode('<br />', highlight_file($file, true));
?>
<ol style="font-family: monospace;">
<?php
$open_tags = array();
foreach ($content as $line)
{
if (strlen($line) > 0)
{
$line = prepend_open_tags($line, $open_tags);
$open_tags = find_open_tags($line);
$line = close_tags($line, $open_tags);

echo "<li>" . $line . "</li>\n";
}
else
{
echo "<li>&nbsp;</li>\n";
}
}
?>
</ol>[/php]
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error in Mysql... ktsirig PHP 1 Feb 3rd, 2007 1:30 PM
Header file internal errors kruptof Coder's Corner Lounge 2 Jan 14th, 2007 1:12 PM
C# corruption!!! Kilo C++ 32 May 21st, 2006 8:44 PM
Masm rsnd Assembly 4 May 20th, 2006 9:05 PM
Cobra Sane Python 18 Aug 22nd, 2005 5:19 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC