mit pygments ein Markup bauen...
Verfasst: Mittwoch 7. Februar 2007, 11:56
Hier http://www.python-forum.de/post-58201.html#58201 hatte ich ja die Idee, pygments zu nutzten um damit ein Markup aufzubauen...
Hab mal angefangen, klappt aber noch nicht so richtig:
Ausgaben:
Irgendwie verstehe ich noch nicht so ganz, wie man die tokens im Lexer zusammen baut.
Außerdem denke ich, mache ich zu viel im Lexer, oder? Also dort z.B. die Headline schon fertig zusammen bauen. Aber ich weiß nicht genau, wie ich die Informationen der Headline richtig in den Formatter übergeben kann.
Edit: http://www.ubuntuusers.de/paste/7359/
Hab mal angefangen, klappt aber noch nicht so richtig:
Code: Alles auswählen
import sys, cgi, re
from pygments import highlight
from pygments.lexer import RegexLexer#, bygroups, include, combined
from pygments.token import Token, Generic
from pygments.formatter import Formatter
Escaped = Token.Escaped
Text = Token.Text
class TextileLexer(RegexLexer):
name = 'Textile Lexer'
def headline_callback(lexer, match):
"""
<h1>Headlines</h1>
"""
headline = "<h%(no)s>%(txt)s</h%(no)s>\n" % {
"no": match.group(1),
"txt": match.group(2)
}
yield match.start(), Generic.Headline, headline
def escape_callback(lexer, match):
"""
cgi.escape()
"""
#~ print match.groups()
yield match.start(), Text, cgi.escape(match.group(2))
def small_callback(lexer, match):
"""
<small>txt</small>
"""
txt = "<small>%s</small>" % match.group(1)
yield match.start(), Text, txt
def strong_callback(lexer, match):
"""
<strong>txt</strong>
"""
txt = "<strong>%s</strong>" % match.group(1)
yield match.start(), Text, txt
tokens = {
'root': [
(r'\*(.*?)\*', strong_callback),
(r'\-\-(.*?)\-\-', small_callback),
(r'(==(.*?)==)(?usm)', escape_callback),
(r'h(\d)\. (.+)\n', headline_callback),
(r'.+', Text),
],
}
class TextileFormatter(Formatter):
def __init__(self, **options):
Formatter.__init__(self, **options)
def write(self, ttype, value, outfile):
if ttype != Text:
outfile.write("%s\n" % value)
return
value = value.strip()
blocks = re.split("\n{2,}", value)
for block in blocks:
block = block.replace("\n", "<br />\n")
outfile.write("<p>%s</p>\n\n" % block)
def format(self, tokensource, outfile):
lasttype = None
lastval = u''
for ttype, value in tokensource:
#~ outfile.write(">%s\n" % repr(ttype))
if ttype is lasttype:
lastval += value
else:
if lasttype:
self.write(lasttype, lastval, outfile)
lastval = value
lasttype = ttype
self.write(lasttype, lastval, outfile)
if __name__ == "__main__":
test_text = '''h1. Test
Blabla
blabla
blubblub
h2. headline 2
ein --kleines-- Wort, ein *fettes* Wort
--klein--
*fett*
Das wird ==<p>== Escaped:
==<a href="URL">txt</a>==
wow
==
Table: <table width="90%" border="0" align="center">
Link: <a href="URL">txt</a>
Input: <input type="submit" value="preview" />
==
und noch mehr
'''
lexer = TextileLexer()
formatter = TextileFormatter()
#~ for i, t, v in lexer.get_tokens_unprocessed(test_text):
#~ print "%4s %-22s %s" % (i, t, v)
highlight(test_text, lexer, formatter, sys.stdout)
Code: Alles auswählen
<h1>Test</h1>
<p>Blabla</p>
<p>blabla<br />
blubblub</p>
<h2>headline 2</h2>
<p>ein --kleines-- Wort, ein *fettes* Wort<br />
<small>klein</small><br />
<strong>fett</strong><br />
Das wird ==<p>== Escaped:<br />
<a href="URL">txt</a><br />
wow</p>
<p>Table: <table width="90%" border="0" align="center"><br />
Link: <a href="URL">txt</a><br />
Input: <input type="submit" value="preview" /></p>
<p>und noch mehr</p>
Außerdem denke ich, mache ich zu viel im Lexer, oder? Also dort z.B. die Headline schon fertig zusammen bauen. Aber ich weiß nicht genau, wie ich die Informationen der Headline richtig in den Formatter übergeben kann.
Edit: http://www.ubuntuusers.de/paste/7359/