Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

printing source code green bar style

by BlndCat (Initiate)
on Sep 17, 2004 at 08:38 UTC ( #391709=perlquestion: print w/ replies, xml ) Need Help??
BlndCat has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know of a utility or perl script help me print out source code in a green bar style ala old dot matrix fan-fold striped paper (but on a laser printer)?

I find reading/parsing source code a lot faster if alternate lines were different shades.

I've considered writing something like this myself, outputting html and then printing that from a browser but html doesn't handle multiple spaces/tabs literally eg. if you have 2 or more spaces it just shows up as one. but if you use "pre" tags long lines won't wrap.

anyone got any pointers?

Comment on printing source code green bar style
Re: printing source code green bar style
by Roger (Parson) on Sep 17, 2004 at 09:10 UTC
    Why not preformat your source code to wrap at column 79 (which is very easy), and then wrap the formatted source code in between the HTML pre tags. You will need to translate the meta characters like '<', '>', etc. in your source code though.

Re: printing source code green bar style
by Aristotle (Chancellor) on Sep 17, 2004 at 09:10 UTC

    You could expand tabs to spaces (using Text::Tabs, f.ex) and then convert all spaces to &nbsp;/&#160; entities.

    I was pretty sure a2ps would have an option for this, but it doesn't appear to. Bummer…

    Makeshifts last the longest.

      I've been thinking along those lines but I am not sure how it would wrap after all the spaces have been changed to  

      another problem is that for a short line, the stripes won't run to the end but rather just to the length of text present on that line. maybe outputing to pdf while more complicated would be less of a hack. no idea of how to do it in perl though

      I've been searching the web and it seems strange that no-one has tackled this before. I do remember a html editor once had that option. maybe webedit?

        I don't think it's particularly strange given how trivial it is. Use CSS instead of a table and stick the lines in some block-level element. A stylesheet like this should be all you need:

        p.even, p.odd { margin: 0; padding: 0.2em; } p.odd { background-color: #ccc; }

        And then you stick the lines in

        <p class="even">#!/usr/bin/perl</p> <p class="odd">use&#160;strict;</p> <p class="even">use&#160;warnings;</p> <p class="odd"></p> <p class="even">BEGIN&#160;{</p> <p class="odd">&#160;&#160;&#160;&#160;$|&#160;=&#160;1;</p> <p class="even">}</p> ...

        Makeshifts last the longest.

Re: printing source code green bar style
by EdwardG (Vicar) on Sep 17, 2004 at 09:14 UTC

    The easiest way by far; get or make some pre-printed paper, that's how it was done1 and that's how you should do it. Alignment will be the worst problem you face.

    _____

    1Back when I was a mainframe monkey, one of my jobs on night shift was to print about fifty trillion pages of reports, and then manually split the piles into individual cost centres for distribution. I remember the green-bar-style paper very well, having cut many fingers on the edges and washed buckets of ink from my hands.

     

Re: printing source code green bar style
by hsmyers (Canon) on Sep 17, 2004 at 16:31 UTC
    Both HP printer control language and Postscript can easily handle what you have in mind. Since I've always had HP printers, I tend towards the first solution, but I've also set it up for Postscript when necessary. Like you I find it easier to look at code with 'green bars' than otherwise---although they were light gray, not green! I might even have a copy of something I wrote long ago called hprint or some such that has this as an option. I used it as a catch all for printer tricks that I wanted/needed...

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: printing source code green bar style
by beernuts (Pilgrim) on Sep 17, 2004 at 16:34 UTC
    Outputting html as you're considering, and combining Aristotle's suggestion with the following from A List Apart might get you in the neighborhood:

    Zebra Tables

    -beernuts
Re: printing source code green bar style
by thorkild (Initiate) on Sep 18, 2004 at 02:05 UTC

    a2ps (the swiss knife of pretty-printing) actually already has support for doing this. a2ps uses different styles for its printouts and one of those is named 'matrix'.
    so, by running:

    a2ps --prologue=matrix -o test.ps test.pl

    it will generate a suitable matrix style printout. The default is to alternate the background each 6. line, but this can be adjusted. (I tested with a2ps 4.13b. I had a postscript-problem with an earlier version before I asked apt to kindly update it.)

    I actually didn't find out how to adjust that value now, so I ended up patching matrix.pro (the prologue file). I changed the expand-line to:

    %Expand:  nline #{pro.matrix.cycle:-2} mod #{pro.matrix.gray:-1} ge {                                                

    -Thorkild

    PS: Yes, a2ps knows perl already

      First of all, thanks for all the helpful replies. Especially to hsmyers because I was starting to think that I was the only one who ever wanted to print-out code in such a way. And also to Thorkild. I've just had a look at a2ps and it seems that it might do the trick, especially with the patched matrix.pro file.

      I might still try to implement it using perl and outputting html etc. in the future but in the meantime, I'll use the right tool for the right job.

      thanks again!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://391709]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-07-25 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls