I have something that works, but it is not as clean as I had hoped.
Problem:
Take existing html files and replace the <pre> decorated inline
code examples with pygments.
First idea for solution:
Somehow let mako know to send out some bits of each html file
to be decorated.
Flow:
1. A user chooses Articles from the web menu.
2. A user chooses to view mysample_article.
3. Routes hands the request to the articles controller.
4. That controller hands info to the articles.mako template.
5. That template inherits the base.mako template
-and- includes the chosen article in its body.
Inside that article are blocks of code to be decorated.
6. The whole thing is combined and served
I had hoped to update old articles with pygments by just replacing
<pre> tags like this
<code style="shell">
.. some shell commands ..
</code>
and somehow teaching mako to interpret that and process. But I got as
far as quoting the code bits in the html file as a string and sending
that out to be processed by way of a namespace and defined function.
This works, but there must be a cleaner way. I am new to this and open
to ideas. Does anybody have any suggestions?
Some details:
Here is a snipped tree.
==============================
templates/
|-- articles
| `-- mysample_article.html
|-- components
| `-- pygments.mako
`-- pages
|-- base.mako
`-- articles.mako
/articles/mysample_article.html
==============================
<%namespace name="mypygments" file="/components/pygments.mako"/>
<h1>Article title</h1>
<h2>Section title</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing.</p>
<% code="""
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
""" %>
${ mypygments.highlight("python", code) }
<p>Lorem ipsum dolor sit amet, consectetur adipisicing.</p>
<% code="""
$ cd /usr/local/lib/python/site-packages/
$ rm -r *
$ clear && exit
""" %>
${ mypygments.highlight("bash", code) }
<p>Lorem ipsum dolor sit amet, consectetur adipisicing.</p>
=----------------------------
/components/pygments.mako
==============================
<%!
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
%>
<%def name="highlight(lang, code)">
<%
if not lang:
lang = "python"
lexer = get_lexer_by_name(lang.strip('"'), stripall=True)
formatter = HtmlFormatter(linenos=True, cssclass="friendly")
result = highlight(code, lexer, formatter)
%>
<div id="highlight">${ result }</div>
</%def>
=----------------------------
--
michael
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---