Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Using rst2pdf in Different Ways

This was an idea by Di­nu Gher­man: you can use rs­t2­pdf as a flo­wa­ble ge­ne­ra­tor for re­por­tlab. Su­ppo­se you want to crea­te, in a re­por­tlab "s­to­r­y", a bun­ch of pa­ra­gra­phs, wi­th em­pha­sis, li­nks, etc, and perhaps a ta­ble.

Using res­truc­tu­red tex­t, it's so­me­thing like this:

This is a paragraph. It has a link: http://rst2pdf.ralsina.me and then some random text.

+-------------+---------------------------+
| A table     | With cells                |
|             |                           |
|             |                           |
|             |                           |
|             |                           |
+-------------+---------------------------+
| And inside                              |
| it some                                 |
| more text                               |
|                                         |
|                                         |
+-----------------------------------------+

* And a list
* Just to make it harder

  + with a nested item here

It is, of cour­se, per­fec­tly po­s­si­ble to ge­ne­ra­te a bun­ch of re­por­tlab (or ra­ther pla­ty­pus) flo­wa­bles to re­pre­sent all this. It wi­ll just mean so­me 75 li­nes of co­de. And if you chan­ge an­y­thin­g, then you ha­ve to edit co­de!

Or you can take ad­van­ta­ge of rs­t2­pdf and do this:

from docutils.core import publish_doctree
from rst2pdf.createpdf import RstToPdf
from reportlab.lib.units import cm
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Frame

rest_text = """
This is a paragraph. It has a link: http://rst2pdf.ralsina.me and then some random text.

+-------------+---------------------------+
| A table     | With cells                |
|             |                           |
|             |                           |
|             |                           |
|             |                           |
+-------------+---------------------------+
| And inside                              |
| it some                                 |
| more text                               |
|                                         |
|                                         |
+-----------------------------------------+

* And a list
* Just to make it harder

    + with a nested item here
"""
r2p = RstToPdf()
doctree = publish_doctree(rest_text)
story = r2p.gen_elements(doctree)
canv = Canvas("platypus-rest.pdf")
f = Frame(2 * cm, 2 * cm, 16 * cm, 18 * cm, showBoundary=True)
f.addFromList(story, canv)
canv.save()

This pro­du­ces this pdf. And of cour­se edi­ting it is ra­ther ea­sier than edi­ting co­de. Sin­ce you are not using rs­t2­pdf to do the fi­nal PDF ge­ne­ra­tio­n, you can use the­se flo­wa­bles in your own do­cu­men­ts.

The bad news

So­me things wi­ll not wo­rk, like hea­dings, sin­ce rs­t2­pdf crea­tes flo­wa­bles that do a ton of things like adding the­msel­ves on in­dexes and su­ch. If you want a hea­din­g-­like thing you can use cla­sses:

.. class:: heading1

This will look like a heading

This is a regular paragraph.

Other ran­dom res­truc­tu­red text fea­tu­res may or may not wo­rk, like footno­tes or ci­ta­tion­s.

The Password Is Password (follow the link to see what I mean)

I think this is a first in the world of stat­ic site gen­er­a­tors :-)

Now Niko­la sup­ports pass­word-pro­tect­ed pages. How does it work?

If you add a pass­word meta­da­ta field, then it will "Just Work". Like this:

I think this is a first in the world of stat­ic site gen­er­a­tors :-) And the re­sult is what you are see­ing now.

How is it im­ple­ment­ed? Niko­la will en­crypt the con­tent us­ing RC4, then wrap it in a <di­v>, and tack a form at the end that trig­gers Javascript code to de­crypt it, and show it.

Is it se­cure? Well, I am not a cryp­tog­ra­pher, so as­sume no. Specif­i­cal­ly, while RC4 is con­sid­ered se­cure, I am not dis­card­ing the be­gin­ning of they keystream, and the im­ple­men­ta­tions I am us­ing are not au­dit­ed.

So, don't use this for any­thing that could get you in trou­ble. Have fun!

UP­DATE Re­mem­ber when I asked "Is it se­cure?" well, re­al­ly, no it's not. It's not stupid in the sense that de­cod­ing what's writ­ten in the post will re­quire at least a mod­icum of ef­fort by who­ev­er is so in­ter­est­ed in read­ing what you are post­ing in your site, but peo­ple with cryp­to chops will crack it like a WEP-se­cured AP, mm­mmkay? It's al­so stupid­ly easy to brute­force this, so be smart about pass­word­s.

OTO­H, it's more se­cure than HTTP sim­ple au­th, since you can't sniff it (not that sim­ple auth is se­cure) and it can hide a piece of the page, which us­ing server-based auth can't.

I may do a more se­cure ver­sion even­tu­al­ly, but this is not it. There­fore, use for fun stuff, not to hide im­por­tan­t/il­le­gal stuff.

Nikola Internals Doc

Ya que Niko­la, mi ge­ne­ra­dor de si­tio­s/­blogs es­tá­ti­cos re­ci­be bas­tan­te có­di­go de otro­s, me pa­re­ció que ya era ho­ra de em­pe­zar a do­cu­men­tar co­mo fun­cio­na por aden­tro. Así que acá es­tá Niko­la in­ter­nals aun­que to­da­vía le fal­ta un her­vo­r.


Contents © 2000-2023 Roberto Alsina