tags:

views:

114

answers:

5

how do you add numbered lines to code examples and also giving a printing and view plain text options like in the code examples in the following link below:

http://www.sitepoint.com/blogs/2009/06/02/css-sizing-absolute-position/

+6  A: 

You should check out SyntaxHighlighter.

Andrew Hare
+1 for good ref
Jonathan Fingland
+1  A: 

the sitepoint article you give as an example uses javascript to create tables and then styles them with css.

this is imho a poor choice. it's putting non-tabular information into tables.

You could accomplish the same thing though using divs and spans

Jonathan Fingland
A: 

I looked at the source of the site you linked to. They are using Wordpress as their blogging software. There's a Wordpress plug-in that lets you do that. I use it at Dreaming In JavaScript. Looks like they're using the same one I am.

Nosredna
A: 

If you want to simply apply line-numbers to code, you can roll out a simple script like:

#!/usr/bin/perl

use strict;
use warnings;

my $file = $ARGV[0];
unless ( -f $file ) {
    print "File name has to be provided.\n";
    exit;
}

open my $fh, '+<', $file or die $!;
my @lines = <$fh>;
seek $fh, 0, 0;

my $line_number = 0;
for (@lines) {
    print $fh $line_number++, ": ", $_;
}

Just pass the file to be line-numbered as the first argument. Always keep a backup of the original file.

Alan Haggai Alavi
A: 

Personally, I'd prefer that you didn't add line numbers. They usually add nothing useful, and they make it more difficult to copy and paste the code.

If you're posting a really long snippet and referring to line numbers in your text, it might be better to break it up into chunks.

Something to consider, anyway.

Blorgbeard
The plug-in is great for blogs because you can refer to the code by line number, and there's a "view plain" button that gives you unadorned source in a new window.
Nosredna
Right, in a new window - still another step to go through. How often do you really need to refer to line numbers anyway? Like I said, break up the code so you don't need to. Or, there must be a way to get the line numbers visible but part of the text as such, so they don't get selected and copied.. I'm not an HTML/CSS guy though.
Blorgbeard