--- author: '' category: '' date: 2009/09/21 10:17 description: '' link: '' priority: '' slug: BB831 tags: programming, pyqt, python title: Yak Shavings for september 21, 2009 type: text updated: 2009/09/21 10:17 url_type: '' --- **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: .. raw:: html 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 ;-) .. _highlighter.py: //ralsina.me/static/highlighter.py .. _pygments: http://pygments.org .. _rst2pdf: http://rst2pdf.googlecode.com