![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
King of Portal
|
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], ' ') === 0){ $source[$i] = substr($source[$i], 6); $temp .= ' '; $count++; }elseif(strpos($source[$i], '<') === 0){ $source[$i] = substr($source[$i], 4); $temp .= '<'; $count++; }elseif(strpos($source[$i], '>') === 0){ $source[$i] = substr($source[$i], 4); $temp .= '>'; $count++; }elseif(strpos($source[$i], '&') === 0){ $source[$i] = substr($source[$i], 5); $temp .= '&'; $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>…</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 |
|
|
|
|
|
#12 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#13 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
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.) |
|
|
|
|
|
#14 |
|
King of Portal
|
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], ' ') === 0){ $source[$i] = substr($source[$i], 6); $temp .= ' '; $count++; }elseif(strpos($source[$i], '<') === 0){ $source[$i] = substr($source[$i], 4); $temp .= '<'; $count++; }elseif(strpos($source[$i], '>') === 0){ $source[$i] = substr($source[$i], 4); $temp .= '>'; $count++; }elseif(strpos($source[$i], '&') === 0){ $source[$i] = substr($source[$i], 5); $temp .= '&'; $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>…</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 |
|
|
|
|
|
#15 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#16 |
|
King of Portal
|
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 |
|
|
|
|
|
#17 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
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)
|
|
|
|
|
|
#18 | |
|
Expert Programmer
|
Quote:
|
|
|
|
|
|
|
#19 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#20 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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> </li>\n"; } } ?> </ol>[/php] |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |