[Idle-dev] [ idlefork-Bugs-837085 ] Print colored

SourceForge.net noreply at sourceforge.net
Sat Nov 8 18:20:21 EST 2003


Bugs item #837085, was opened at 2003-11-06 01:47
Message generated for change (Comment added) made by nobody
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=109579&aid=837085&group_id=9579

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Heiko Selber (drhok)
Assigned to: Nobody/Anonymous (nobody)
Summary: Print colored

Initial Comment:
It would be nice if the syntax highlighting could be 
printed.

Currently, the print function is in black and white only, 
even on color printers.


----------------------------------------------------------------------

Comment By: Nobody/Anonymous (nobody)
Date: 2003-11-08 15:20

Message:
Logged In: NO 

I wanted to print in color too. As far as I can tell, IDLE sends the contents of your editor window to 
notepad.exe (on windows) for printing; I don't know what it does on other platforms. Notepad doesn't do 
color but I found another way.

I was reading the "Python Cookbook" and there's a recipe in there for formatting python source in colorized 
HTML.

Jürgen Hermann has made a "colorizer" for the MoinMoin project
(MoinMoin urls:
http://moin.sourceforge.net/
http://twistedmatrix.com/users/jh.twistd/moin/moin.cgi/
)

You can find the colorizer here:
http://twistedmatrix.com/users/jh.twistd/python/moin.cgi/MoinMoinColorizer

If you want to get it to give you output the same as IDLE a few small modifications are needed:

In python, all name tokens are treated the same, 'if' is a name just like any identifier, so all "names" are 
colored the same. To get output like IDLE's, you want only identifiers occuring after "class" and "def" to be 
blue.


1. We need a new token type, so add the line

_DEForCLASS = token.NT_OFFSET + 3

after

_KEYWORD       = token.NT_OFFSET + 1
_TEXT          = token.NT_OFFSET + 2

(you can use any identifier you want, it doesn't have to be 
_DEForCLASS)

2. Next, edit the '_colors' dictionary to get more IDLE-like colors and add an entry for _DEForCLASS

_colors = {
    token.NUMBER:       '#000000', #black
    token.OP:              '#000000', #black
    token.STRING:        '#008000', #green
    tokenize.COMMENT: '#ff0000', #red
    token.NAME:           '#000000', #black
    token.ERRORTOKEN: '#FF8080',
    _KEYWORD:            '#ff8c00', #dark orange
    _TEXT:                  '#000000', #black
    _DEForCLASS:         '#0000ff', #blue
}

3. We need something to keep track of the previously scanned token for each current token so we can 
know to color a token blue if it follows a 'def' or 'class' token, so add the following to the __init__ function 
of the Parser class

        self.prev_tok = ''



4. Then in the if...elif statement with the comment "# map token type to a color group" in the __call__ 
function, add a branch to check if the previous token was "def" or "class"

        # map token type to a color group
        if token.LPAR <= toktype and toktype <= token.OP:
            toktype = token.OP
        elif toktype == token.NAME and keyword.iskeyword(toktext):
            toktype = _KEYWORD

#add this branch...
        elif toktype == token.NAME and self.prev_tok in ['def', 'class']:
            toktype = _NEWDEForCLASS


#in Python 2.3 you could just say self.prev_tok in 'def class'
#it might be slightly faster, but in practice it probably won't matter


5. Last but not least, somewhere before the end of the __call__ function put the following line, to remember 
the previous token for the elif from step 4.

        self.prev_tok = toktext


There ya go! This works great and is pretty simple to use. It's not as good as just clicking a menu option 
from within IDLE, but it will let you print your python source in color. Yay!

Many thanks to Jürgen Hermann for writing and making known this useful script. I hope this helps you out.

Sincerely,
~Simon Forman
calroc at mindspring.com



BTW, if you want the background to be white (in the browser, I don't think it affects printing) make the 
following changes in the format function

 <! sys.stdout.write('<body bgColor="#ffffff"><pre><font face="Lucida,Courier New">')
 !> sys.stdout.write('<pre><font face="Lucida,Courier New">')

and

 <! sys.stdout.write('</font></pre></body>')
 !> sys.stdout.write('</font></pre>')


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=109579&aid=837085&group_id=9579



More information about the IDLE-dev mailing list