Skip to main content

Ralsina.Me — Roberto Alsina's website

Yak Shavings for september 21, 2009

yak shaving

(id­iomat­ic) Any ap­par­ent­ly use­less ac­tiv­i­ty which, by al­low­ing you to over­come in­ter­me­di­ate dif­fi­cul­ties, al­lows you to solve a larg­er prob­lem.

And boy, are my yaks hairy!

I start­ed try­ing to do a rst2pdf stylesheet ed­i­tor (see here).

One thing lead to an­oth­er, and I have now at least three in­ter­est­ing mini-pro­jects be­cause of it.

Here's to­day's: abuse pyg­ments to use it as a gener­ic syn­tax high­lighter in a Qt in­ter­face.

Why pyg­ments? Be­cause it's the on­ly re­Struc­tured Text high­lighter I found. That's prob­a­bly be­cause reSt is pret­ty damn hard to high­light!

AFAIK, this is the first time any­one has man­aged to use pyg­ments for this, in an ed­itable win­dow. And there are good rea­sons for that:

  • It's pure python, so maybe you ex­pect it to be too slow

  • It does­n't do par­­tial or pro­­gres­­sive lex­ing, so you need to lex the whole thing (a­­gain, maybe you ex­pect it to be too slow)

  • It has a file-ori­en­t­ed API, it gen­er­ates a file with all the for­­mat­t­ing in it, and for this kind of thing we need to ac­cess stuff in the mid­­dle of the da­­ta.

So, of course, it turns out it works pret­ty well, as you can see in this video:

Les­son learned: com­put­ers are fast nowa­days.

Here's the code for high­lighter.py with ex­ten­sive com­ments.

You can just run it and get the same de­mo you saw on the video (mi­nus the typ­ing ;-)


Contents © 2000-2024 Roberto Alsina