Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Mar 19th, 2007, 12:17 AM   #11
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
DaWei is suggesting that the line numbers and the actual code should be split into two columns so you could select only the code column and avoid the line numbers. I think...
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Mar 19th, 2007, 9:05 AM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Note that the images are deliberately degraded to reduce bandwidth demands on the forum.

__________________
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 19th, 2007, 1:32 PM   #13
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
I got ya now DaWei and here is the source that does what you're asking for[PHP]<html>
<head>
<title>Grim PHP Syntax Highlighter</title>
<style>
div.lineNumbers {
float: left;
text-align: left;
border-right: 1px #999999 solid;
margin-right: 8px;
}
</style>
</head>
<body>
<span style="font-family: Courier New; font-size: 10pt">
<?php
$filename = 'syntax_h.php';
ob_start();
highlight_file($filename);
$source = ob_get_contents();
ob_end_clean();
$source = explode(chr(13), $source);
$temp = substr($source[0], 0, 35);
$temp .= substr($source[0], 36);
$source[0] = substr($temp, 6);
$temp = substr($source[count($source) - 1], 0, strlen($source[count($source) - 1]) - 8);
$source[count($source) - 1] = $temp;
for($i = 1; $i < count($source); $i++){
$source[$i] = substr($source[$i], 6);
}
for($i = 0; $i < count($source) - 1; $i++){
$source[$i] .= '<br />';
}
$lineNum = array();
$spaces = strlen(count($source));
for($i = 0; $i < count($source); $i++){
array_push($lineNum, '<span style="color: #777777">' . ($i + 1) . '</span>&nbsp;');
for($j = 0; $j < $spaces - strlen($i + 1); $j++){
$lineNum[$i] = '&nbsp;' . $lineNum[$i];
}
$lineNum[$i] .= '<br />';
}
echo '<div class="lineNumbers">';
for($i = 0; $i < count($lineNum); $i++){
echo $lineNum[$i];
}
echo '</div>';
echo '<div class="sourceCode">';
for($i = 0; $i < count($source); $i++){
echo $source[$i];
}
echo '</div>';
?>
</span>
</body>
</html>[/PHP]Or at least it does it on my computer hehe ^_^. Only tested it on Mozilla FireFox and Internet Explorer though.
__________________
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 19th, 2007, 5:48 PM   #14
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Yeah this is a really usefull piece of code.
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Mar 19th, 2007, 7:59 PM   #15
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by grimpirate View Post
I got ya now DaWei and here is the source that does what you're asking for
I don't see why three quarters of the code is necessary. Why not something like:
php Syntax (Toggle Plain Text)
  1. <?php
  2. $filename = 'syntax_h.php';
  3.  
  4. ob_start();
  5. highlight_file($filename);
  6. $source = ob_get_contents();
  7. ob_end_clean();
  8.  
  9. $number_lines = count(explode("<br />", $source));
  10. ?>
  11.  
  12. <div class="lineNumbers">
  13. <code><?php print implode(range(1, $number_lines), "\n<br />") ?></code>
  14. </div>
  15. <div class="sourceCode">
  16. <?php print $source; ?>
  17. </div>
With some CSS to keep the line heights the same:
css Syntax (Toggle Plain Text)
  1. .lineNumbers, .sourceCode {
  2. line-height: 12pt;
  3. vertical-align: top;
  4. margin: 0px;
  5. padding: 0px;
  6. }
  7.  
  8. .lineNumbers {
  9. float: left;
  10. text-align: left;
  11. border-right: 1px #999999 solid;
  12. margin-right: 8px;
  13. padding-right: 8px;
  14. }
Arevos is offline   Reply With Quote
Old Mar 19th, 2007, 9:07 PM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Exploding on a string such as "<br /"> may not be effective. Many textual source code files never heard of "<br /"> or its variants, such as "<br/>". The idea is to reproduce logical lines in such a way that the resulting code is usable as reproduced, when copied intact. Since languages vary in this respect, it means reproducing the text in a way that will be acceptable to the target interpretation.
__________________
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 19th, 2007, 10:48 PM   #17
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Here is a shorter version of Arevos' code. It solves the issue of exploding on "<br />" brought up by DaWei.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $filename = 'syntax_h.php';
  3. $num_lines = count(file($filename));
  4. ?>
  5.  
  6. <div class="lineNumbers">
  7. <code><?php print implode(range(1, $num_lines), "\n<br />") ?></code>
  8. </div>
  9. <div class="sourceCode">
  10. <?php highlight_file($filename); ?>
  11. </div>
titaniumdecoy is offline   Reply With Quote
Old Mar 20th, 2007, 4:55 AM   #18
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
I'm don't quite get why "<br />"s would be a problem, assuming the functionality of highlight_file remains the same.

Titaniumdecoy's code is a lot neater, however. I'm rather jealous I didn't think of doing it that way, myself
Arevos is offline   Reply With Quote
Old Mar 20th, 2007, 7:33 PM   #19
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
That is interesting =D

so finally, it can be shortened to:
[php]<?php
$file = 'test.php';
echo '<code class="num">', implode(range(1, count(file($file))), '<br />'), '</code>';
highlight_file($file);
?>[/php]
css Syntax (Toggle Plain Text)
  1. .num {
  2. float: left;
  3. color: gray;
  4. text-align: right;
  5. margin-right: 6pt;
  6. padding-right: 6pt;
  7. border-right: 1px solid gray;}
Styx is offline   Reply With Quote
Old Mar 20th, 2007, 8:20 PM   #20
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Did you test that code, Styx? It doesn't work in my browser, as the code is shifted below the line numbers.

Last edited by titaniumdecoy; Mar 20th, 2007 at 8:38 PM.
titaniumdecoy 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
start Learning HTMl smita HTML / XHTML / CSS 0 Mar 15th, 2007 7:38 AM
GCC sprintf Source Code JawaKing00 C 2 Nov 30th, 2006 3:53 AM
Basic HTML Tutorial - Reuben Keeney ReubenK HTML / XHTML / CSS 14 Mar 26th, 2006 5:50 AM
Image processing:Stentiford and Holt Thinning Source code in C or C++ ladyscarlet99 C 0 Sep 9th, 2005 1:20 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:26 PM.

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