Skip to main content

Ralsina.Me — Roberto Alsina's website

rst2pdf again

I did a lit­tle (very lit­tle) more work on rst2pdf 1

Bet­ter:

  • Head­­ers

  • Foot­ers

  • Foot­notes (as end­notes, re­al foot­notes are too much work be­­cause you have to re­flow the tex­t)

  • Ex­ter­­nal links (http, email)

  • Some styling im­prove­­ments (mean­ing: the out­­put is not so painful in the eye­­s)

  • Pa­per size sup­­port

  • Re­al sep­a­ra­­tors

Then there are some things you just get be­cause it's done us­ing re­port­lab:

  • True­­Type font em­bed­d­ing (check the ex­am­­ple be­low, and look at the monospaced font)

  • No hy­phen­a­tion (although there is a project on the web that claims to have done it, I should check it out)

Still bro­ken:

  • No way to put things like page num­bers or sec­­tion names in head­­er­s/­­foot­ers

  • The foot­notes are not linked to their con­­tents and vicev­er­sa

  • In fac­t, no in­­ter­­nal links work, in­­­clud­ing ti­­tle re­f­er­ences

  • Ta­bles are pret­­ty bro­ken

And here is the usu­al ver­sion of the rst de­mo show­ing im­prove­ments.

1

My tool to con­vert Rstruc­tured Text to PDF

Un post no-nerd | A non-nerd post

Un post no-n­erd, y bil­ingüe.

Es­to es ac­er­ca de mi vi­da per­son­al, na­da nerd, si no les in­tere­sa, al próx­i­mo ;-)

El sába­do a la noche mi hi­jo Juan Fran­cis­co tenía un po­quito de fiebre (38º). Lo ll­e­va­mos al hos­pi­tal, y le hicieron un análi­sis de san­gre que mostró que tenía ba­jos los glóbu­los blan­cos.

En re­sumen: quedó in­ter­na­do 3 días mien­tras los análi­sis descarta­ban to­do tipo de en­fer­medades muchísi­mo pe­o­res que lo que en re­al­i­dad tenía (Ex­an­tema vi­ral). El ya es­tá per­fec­to, es más, sal­vo por la erup­ción típi­ca de es­ta en­fer­medad no tu­vo ningún sín­toma de nada, ni siquiera fiebre, mien­tras es­tu­vo in­ter­na­do.

Al­go im­por­tan­te: el Hos­pi­tal Mater­no In­fan­til de San Isidro es ex­ce­lente. Doc­tores, en­fer­meras, las vol­un­tari­as, to­dos se des­viv­en por los chicos, y es un hos­pi­tal públi­co. Por suerte, mi cober­tu­ra médi­ca va a reem­bol­sar to­dos los cos­tos de la aten­ción, lo que va a ayu­dar al hos­pi­tal, pero el ser­vi­cio es buenísi­mo para to­dos los ve­ci­nos del mu­nici­pi­o.


This is about my per­son­al life, not nerdy, if you are not in­ter­est­ed, read the next en­try ;-)

Sat­ur­day night my son Juan Fran­cis­co had a lit­tle fever (100.4F). We took him to the hos­pi­tal, and blood tests showed a low white cell coun­t.

Short sto­ry: he stayed as a pa­tient 3 days while anal­y­sis dis­card­ed all sort of dis­eases much worse than what he ac­tu­al­ly had (vi­ral ex­an­them). He's all right now, in fact he had no symp­toms at all ex­cept for the typ­i­cal rash of this dis­ease (not even a fever any­more).

Some­thing im­por­tan­t: The Hos­pi­tal Mater­no In­fan­til de San Isidro is ex­cel­len­t. Doc­tors, nurs­es, vol­un­teer­s, ev­ery­one give ev­ery­thing for the kid­s, and it's a pub­lic (free) hos­pi­tal. For­tu­nate­ly, my in­sur­ance will pay all the costs of car­ing for him, which will help the hos­pi­tal, but the at­ten­tion is awe­some (and free) for any­one in the dis­tric­t.

Done with rst2pdf for now

It works much bet­ter than it did last night.

The main miss­ing/bro­ken things are:

  • Ta­bles

  • Links

  • Foot­notes/c­i­­ta­­tions

  • Head­­er­s/­­Foot­ers

  • Re­f­er­ences

  • Ta­ble HEad­­er­s/S­­pan­n­ing cells

  • Lists that don't start at 1

The rest seems to be in work­ing or­der and pro­duc­ing de­cent out­put al­ready. It can process the ReST de­mo and it does­n't look bad: check it out

I say, good for half a day of hack­ing, and will now stop, be­cause it's a week­end and I have a fam­i­ly :-)

But that does­n't mean you can't try it for your­self. Just run it thus:

python rst2pdf.py myrstfile.txt

And you will get (hopefully) a myrstfile.txt.pdf

Let me know how it work­s, and if it does­n't and it's not re­lat­ed to one of the things I men­tioned above as bro­ken, send me a test file!

Generating PDFs from Restructured text

This has al­ways been pos­si­ble, go­ing via La­TeX.

How­ev­er, La­TeX be­ing what it is, you ei­ther need to learn it, or you end up with rather plain-look­ing doc­u­ments.

While that's ok for a man­u­al, I want to use Re­struc­tured Text for ev­ery­thing in­volv­ing doc­u­ments.

So, I looked for an­oth­er so­lu­tion. Sad­ly, I could not make the ex­ist­ing rlpdf writ­er work, sooooo I de­cid­ed to write my own tool.

Since I in­tense­ly dis­like the Vis­i­tor pat­tern in­volved in writ­ing a reg­u­lar do­cu­tils writer, I adapt­ed my old rst2rst.py and end­ed with rst2pdf.py which just tra­vers­es the tree re­cur­sive­ly and writes the PDF us­ing Re­port­Lab.

And it took me about 3 hours to make it work:

  • For a lim­it­ed sub­­set of RST (no foot­notes, no links, no dec­o­ra­­tion)

  • For some sub­­set of ta­bles (no col/row span­n­ing)

  • With lim­it­ed "styling" (it's most­­ly there, but I need to write a lot of Re­­port­Lab styles.

How well does it work... rather well.

Here's a gen­er­at­ed PDF of The Re­struc­tured­Text Primer

Ig­nore aes­thet­ic­s, and con­sid­er func­tion, it's pret­ty good.

Templates that create templates...

... or why flex­i­bilty is good some­times.

Af­ter some dab­bling with Lurk­er, I de­cid­ed it's just too much trou­ble for the pro­to­type site and went look­ing for al­ter­na­tive mail­ing list archiver­s.

I found few, and de­cid­ed to try MHonArc.

Then I tried to make it fit the rest of the site. Ouch.

It's not that MHonArc does­n't pro­vide a way to cus­tom­ize the gen­er­at­ed HTM­L. It pro­vides one.

A ter­ri­bly con­vo­lut­ed one where in­stead of writ­ing a tem­plate for MHonArc to fill it has its tem­plate and it's filled from the da­ta and from a re­source file you write.

For ex­am­ple, I ex­pect­ed writ­ing a tem­plate like this (s­tupid ex­am­ple):

<h1> my archives </h1>
${toolbar}
${contents}

So that MHonArc would put a tool­bar and the con­tents where I tell it to.

In re­al­i­ty, MHonArc has a hard­cod­ed tem­plate like this:

IDXPGSSMARKUP
IDXPGBEGIN
    LISTBEGIN
        (AUTHORBEGIN |
         DAYBEGIN |
         SUBJECTBEGIN)?
        LITEMPLATE+
        (AUTHOREND |
         DAYEND |
         SUBJECTEND)?
     LISTEND
    DOC?
IDXPGEND

And you tell it what IDXP­GEND should be, or it us­es its de­fault val­ue. And there is one of these things, with sim­i­lar but dif­fer­ent vari­ables, for each kind of page it gen­er­ates.

So, you can cus­tom­ize your lay­out by putting things like this in a re­source file:

<IdxPgEnd>
$MY-FOOTER-LINK$
</body>
</html>
</IdxPgEnd>

Which is doable. But you end with stat­ic pages. How can you make that work well with a mod­ern site with­out du­pli­cat­ing the whole base lay­out? Not to men­tion all the niceties like hav­ing your user­name in the tool­bar and what­ev­er.

Gen­er­ate page tem­plates in­stead.

I cus­tom­ized the MHonArc lay­out to gen­er­ate neat Mako tem­plates that in­her­it the "re­al" base lay­out tem­plate.

I on­ly had to be care­ful to es­cape the $ signs as $$ if I want­ed them to sur­vive un­til Mako saw them.

Then cre­ate a view that pro­cess­es the tem­plates in­stead of serv­ing the stat­ic files and it works like a char­m.

A sim­ple, but neat hack­let, I think.


Contents © 2000-2020 Roberto Alsina