Yak Shavings for september 21, 2009
- yak shaving
-
(idiomatic) Any apparently useless activity which, by allowing you to overcome intermediate difficulties, allows you to solve a larger problem.
And boy, are my yaks hairy!
I started trying to do a rst2pdf stylesheet editor (see here).
One thing lead to another, and I have now at least three interesting mini-projects because of it.
Here's today's: abuse pygments to use it as a generic syntax highlighter in a Qt interface.
Why pygments? Because it's the only reStructured Text highlighter I found. That's probably because reSt is pretty damn hard to highlight!
AFAIK, this is the first time anyone has managed to use pygments for this, in an editable window. And there are good reasons for that:
It's pure python, so maybe you expect it to be too slow
It doesn't do partial or progressive lexing, so you need to lex the whole thing (again, maybe you expect it to be too slow)
It has a file-oriented API, it generates a file with all the formatting in it, and for this kind of thing we need to access stuff in the middle of the data.
So, of course, it turns out it works pretty well, as you can see in this video:
Lesson learned: computers are fast nowadays.
Here's the code for highlighter.py with extensive comments.
You can just run it and get the same demo you saw on the video (minus the typing ;-)