I am in the middle of that honeymoon you get starting a new app. Every new feature seems tobe just 50 lines of code away, there is no legacy code (in fact, you are creating that legacy code), and you learn new tricks all the time.
So, I did a new feature today.
A day or two ago, my editor started showing a yellow bar highlighting the current line.
But then I though... wouldn't it be more useful to have a similar bar following you in the PDF?
That way, when you are on a given line, you can immediately see where you are in the output. Neat, right?
Here is a video showing it:
Sadly it's not perfect, and probably never will be because of docutils limitations, but it's pretty nice!
(idiomatic) Any apparently useless activity which, by allowing you to overcome intermediate difficulties, allows you to solve a larger problem.
This yak is starting to look better.
For my second pile of yak shavings: turning QPlainTextEdit into a decent editing widget for programmers.
As work advanced in my rst2pdf editor (BTW: need a name!), it became obvious that the piece of the UI the user will use most is just a couple of plain text editors.
Qt comes with a widget for that, of course, called QPlainTextEdit. However, it's a very, very bad widget for programmers.
Here's the least I wanted:
Current line highlight
Error highlight when it makes sense (like, in the stylesheet)
One way to achieve this would be to dump QPlainTextEdit and use QSciScintilla which is the basis for the code editor in eric and (another version) in scite.
However, I experienced a bad bug in QSciScintilla, where I can't type accented characters! Without that, (decent) spanish is impossible, and the bug seems to be at least two years old, so... no go.
So, did I get those features? I say yes!
Here is the video (yes, I am getting addicted to making these, since qt-recordmydesktop makes them so easy ;-):
The basis for this is the Code Editor example that comes with Qt itself, plus a bit of my own handywork.
First, I ported Code Editor from C++ to Python, which was very simple and took a few minutes. That takes care of points 2 and 3.
Then, the syntax highlight was plugged in, which was point 1.
Replace self.ui.style.toPlainText with whatever your widget is called, of course:
defvalidateStyle(self):style=unicode(self.ui.style.toPlainText())#no point in validating an empty stringifnotstyle.strip():returnpos=Nonetry:json.loads(style)exceptValueError,e:s=str(e)printsifs=='No JSON object could be decoded':pos=0elifs.startswith('Expecting '):pos=int(s.split(' ')[-1][:-1])else:print'UNKNOWN ERROR'# This makes a red bar appear in the line# containing position posself.ui.style.highlightError(pos)
highlightError(pos) simply stores pos in the Code Editor, which will draw a red bar in that line, the same way it highlights the current line.
Inspired by a post by André Roberge I wanted to see if rst2pdf was too slow to be used for real-time previews in a restructured text editor.
It would also be very useful, for example, as a way to test stylesheet changes, making rst2pdf much more useful in general.
And after a couple of hours of gentle hacking, you know... it doesn't suck at all. I implemented the (still very primitive) PDF viewer using a python/poppler/Qt binding I found via google, the UI is PyQt.
Here's the video:
A note: the video was recorded using qt-recordmydesktop and that program is awesome. It was trivial to do.
I expect this will not be good enough when long documents are processed, but the rst2pdf manual (about 25 pages) renders in 5 seconds.