Based on the pretty printer in the wiki, I wrote my own.
If I understood you well, you want a b/w pretty printer
with reserved words bold. The pretty printer is capable
of doing both. An example is attached. Probably missing
is the functionality to control the typesetting of reserved
words in TeX. This is done in the lua code. I didn't mind
up to  now. Any feedback is welcome.

Erik

2010/3/23 R. Bastian <rbast...@free.fr>

> On Tue, 23 Mar 2010 13:24:09 +0100
> luigi scarso <luigi.sca...@gmail.com> scribit:
>
> > On Tue, Mar 23, 2010 at 1:17 PM, R. Bastian <rbast...@free.fr> wrote:
> > > Hello,
> > >
> > > is there a way to get Python code with reserved words highlighted ?
> > > Not colored as in
> > > http://wiki.contextgarden.net/Verbatim_with_LuaTeX
> > > but {\bf ...}
> > >
> > > I use context 2010.01.26 Mk IV or texexec
> > well, I'm working to a pretty-printer for Python with low priority....
>
> Attendo ;-)
>
> >
> > --
> > luigi
> >
> ___________________________________________________________________________________
> > If your question is of interest to others as well, please add an entry to
> the Wiki!
> >
> > maillist : ntg-context@ntg.nl /
> http://www.ntg.nl/mailman/listinfo/ntg-context
> > webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> > archive  : http://foundry.supelec.fr/projects/contextrev/
> > wiki     : http://contextgarden.net
> >
> ___________________________________________________________________________________
> >
>
>
> --
> René Bastian
> www.pythoneon.org
> www.musiques-rb.org
> http://www.soundsurvey.org.uk/
>
>
>
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to
> the Wiki!
>
> maillist : ntg-context@ntg.nl /
> http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
>
> ___________________________________________________________________________________
>

Attachment: sample.tex
Description: TeX document

if not modules then modules = { } end modules ['pret-py'] = {
    version   = 0.001,
    comment   = "companion to buff-ver.mkiv",
    author    = "Erik Margraf",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local utf = unicode.utf8

local utfcharacters, utfvalues = string.utfcharacters, string.utfvalues
local utfbyte, utffind = utf.byte, utf.find
local byte, sub, find, match = string.byte, string.sub, string.find, string.match
local texsprint, texwrite = tex.sprint, tex.write
local ctxcatcodes = tex.ctxcatcodes

local visualizer = buffers.newvisualizer("py")

visualizer.identifiers = { }

visualizer.identifiers.core = {
    "and", "as", "assert", "break", "class", "continue", 
    "def", "del", "elif", "else", "except","exec", "finally", "for", 
    "from", "global", "if", "import", "in", "is", "lambda", "not", 
    "or", "pass", "print", "raise", "return", "try", "while",
    "with", "yield"
}

visualizer.identifiers.base = {
    "False", "None", "True"
}

visualizer.identifiers.delimiters = {
    "@", "%", "^", "&", "*", "(", ")", "-", "+", 
    "=", "|", "\\", "/", "{", "}", "[", "]", ":", ";", "\"", 
    "\'", "<", ">", ",", ".", "?", " " , "#" 
}

local known_words = { }

for k,v in pairs(visualizer.identifiers) do
    for _,w in pairs(v) do
        known_words[w] = k
    end
end


visualizer.styles = {
    core = "\\bf\\ss  ",
    base = "\\sl ",
}

local styles = visualizer.styles

local declarations = {
    ["def"]=1, ["class"]=1
}


local colors = {
    "prettyone",
    "prettytwo",
    "prettythree",
    "prettyfour",
}

local change_state, finish_state = buffers.change_state, buffers.finish_state

local function flush_python_word(state, word)
    if word then
        local id = known_words[word]
        if id then
            state = change_state(1,state)
            if styles[id] then
                texsprint(ctxcatcodes,styles[id])
            end
            texwrite(word)
            state = finish_state(state)
            if declarations[word] then 
                state = change_state(3,state)
            end
        else
            texwrite(word)
            state = finish_state(state) -- ?
        end
    else
        state = finish_state(state)
    end
    return state
end

local in_py_long_string = false

function visualizer.flush_line(str,nested)

    local i, result , word = 1, { }, ""
    local state = 0
    local delimiters=visualizer.identifiers.delimiters
    local is_delimiter = false
    local in_string = false
    local string_end = ""
  
    buffers.currentcolors = colors

    while i <= #str do
        c=string.sub(str,i,i)
        if in_py_long_string then
            state = buffers.change_state(2,state)
            if c=="\'" and string.sub(str,i,i+2)=="\'\'\'" then
                texwrite(string.sub(str,i,i+2))
                i=i+2
                in_py_long_string=false
            elseif c=="\"" and string.sub(str,i,i+2)=="\"\"\"" then
                texwrite(string.sub(str,i,i+2))
                i=i+2
                in_py_long_string=false
            elseif c=="\\" then
                texwrite(string.sub(str,i,i+1))
                i=i+1
            elseif c=="\"" then
                texwrite(c)
            elseif c=="\'" then
                texwrite(c)
            elseif c==" " then
                texsprint(ctxcatcodes,"\\obs") 
            else
                texwrite(c)
            end
        elseif in_string then
            if c=="\\" then
                texwrite(string.sub(str,i,i+1))
                i=i+1
            elseif c=="\"" then
                texwrite(c)
                if c == string_end then
                    in_string=false
                    state = buffers.finish_state(state,result)
                end
            elseif c=="\'" then
                texwrite(c)
                if c == string_end then
                    in_string=false
                    state = buffers.finish_state(state,result)
                end
            elseif c==" " then
                texsprint(ctxcatcodes,"\\obs") 
            else
                texwrite(c)
            end
        else
            for _,v in ipairs(delimiters) do
                if  c==v then
                    is_delimiter = true
                end
            end
            if is_delimiter then
                if c==" " then
                    state = flush_python_word(state,word,result)
                    texsprint(ctxcatcodes,"\\obs") 
                elseif (c=="\'") and string.sub(str,i,i+2)=="\'\'\'" then
                    state = flush_python_word(state,word,result)
                    in_py_long_string = true
                    state = buffers.change_state(2,state,result)
                    i=i+1
                elseif (c=="\"") and string.sub(str,i,i+2)=="\"\"\"" then
                    state = flush_python_word(state,word,result)
                    in_py_long_string = true
                    state = buffers.change_state(2,state,result)
                    texwrite(string.sub(str,i,i+2))
                    i=i+2
                elseif (c=="#") then
                    state = buffers.change_state(4,state)
                    texwrite(string.sub(str,i,#str))
                    i=#str
                elseif (c=="\"") and not in_string then
                    state = flush_python_word(state,word,result)
                    in_string = true
                    string_end = "\""
                    state = buffers.change_state(2,state,result)
                    texwrite(c)
                elseif (c=="\'") and not in_string then
                    state = flush_python_word(state,word,result)
                    in_string = true
                    string_end = "\'"
                    state = buffers.change_state(2,state,result)
                    texwrite(c)
                elseif (c==".") and (#word==0 or string.find(string.sub(word,1,1),'%d')) then
                   word = word .. c
                    is_delimiter=false
                else
                    state = flush_python_word(state,word,result)
                    texwrite(c)
                end
                if is_delimiter then
                    word = ""
                    is_delimiter=false
                end
            else
              word = word .. c
            end
        end
        i=i+1
    end
    state = flush_python_word(state,word,result)
    buffers.flush_result(result,false)
end
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to