tags:

views:

65

answers:

3

I try to make my evoque templates color-code a bit, but the html I get is already escaped with lt-gt's

I read there should be something like a quoted-no-more class but I haven't been able to find the evoque.quoted package

My aim is to not have escaped html coming out of the template, but 'real'.

from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from evoque.domain import Domain
import os


tmpl="""

$begin{code}
    ${codyfy(evoque(name=label), lang=label.split()[0][1:])}
$end{code}

$begin{c 0}
    int main(void){printf("hello world");return 0;}
$end{c 0}

$begin{python 0}
    print "hello world"
$end{python 0}

$evoque{#code, label="#c 0"}
$evoque{#code, label="#python 0"}
"""
td = Domain(os.path.abspath("."))
def codyfy(src,lang="python"):
    return highlight(src,get_lexer_by_name(lang, stripall=True),HtmlFormatter())

td.set_on_globals('codyfy',codyfy)
td.set_template("testtmpl", src=tmpl, from_string=True)
t = td.get_template("testtmpl")
print t.evoque()
+1  A: 

Have you tried it with raw=True? See:

I haven't used Qpy before, but perhaps this note will help:

Defining custom quoted-no-more classes

[...] It is also highly recommended to download and install the Qpy unicode templating utility that provides the qpy.xml Quoted-No-More class for automatic input escaping. [...]

ars
yes I have tried that. It just seems to print the whole tmpl string like this: $begin{code} ${codyfy(evoque(name=label), lang=label.split()[0][1:])} $end{code}
Do you mean in t.evoque()? I meant in a call to $evoque{...} from within the template where you can output the HTML -- it seems closest to what the documentation is talking about, i.e. calling another template from within a template. See the link for more detail. I haven't tried this though.
ars
yes - unfortunately i have tried that too: $evoque{#code, raw=True, label="#c 0"} yields:${codyfy(evoque(name=label), lang=label.split()[0][1:])}and ${codyfy(evoque(name=label, raw=True), lang=label.split()[0][1:])} yields:<div class="highlight"><pre><span ...
Please see the note on Qpy I just added to the answer.
ars
Thank You ars - shouldn't I just accept Your answer, so we can close the devil!
Glad it worked out! :)
ars
+1  A: 

Yeps - we got in parallel, ars and I. The answer is here - snip it into above:

from qpy import xml
def codyfy(src,lang="python"):
    return xml(highlight(src,get_lexer_by_name(lang, stripall=True),HtmlFormatter()))

xml() is apparently sort of a payload making subsequent escapers lay off.

A: 

qpy.xml() is the quoted-no-more class for XML (and HTML) provided by the Qpy package -- in both superfast C and alternative python versions. Evoque looks for this specific class when quoting="xml" (i.e. equivalent to: quoting=qpy.xml) is used on template loading or rendering.

But any custom quoted-no-more type may be specified as value to the quoting parameter. The evoque.quoted package provides a base quoted-no-more type, and some concrete examples, to make the definition of your custom quoted-no-more types easier. However, as noted, the evoque.quoted is not yet available -- it is mentioned in the changelog but for an as yet unreleased version of Evoque (see the changelog page).

If you pass the output of a whole bunch of templates thru codyfy(), you might also wish to consider specifying it as a filter, either on each template, or as default filter on a collection. An example of using filters is: http://evoque.gizmojo.org/howto/markdown/

Using filters would in fact be a better approach as it is more general -- you will:
a) not have to hard-wire the qpy.xml() call within codyfy(), and
b) be able to use the exact same codyfy() function as filter in all templates, even when these specify a quoting other than qpy.xml.

Mario Ruggier
Thank You very much Mario - I voted this up, as You can see, hopefully it will surpass my own answer in time.The reason I din't go for a filter - well - it has gone away somewhere in the mess of my mind. http://barskdata.org/articles/syntax-coloring-2.html was where I got at (when it's up...) - project is on hold for a while - hobby learning as it is.