Skip to main content

Ralsina.Me — Roberto Alsina's website

Rawdog is flexible: using Mako templates

I am us­ing raw­dog for Plan­e­ta PyAr and I am very hap­py with it. One thing I re­al­ly did­n't like was the tem­plat­ing.

Why? It's ba­si­cal­ly un­doc­u­ment­ed, it looks ug­ly and it does­n't sup­port tem­plate in­her­i­tance, which in this case is very use­ful, be­cause I am ac­tu­al­ly do­ing two very sim­i­lar plan­et­s: 1 2.

So, since I saw a plug­in to use Vel­lum tem­plates, which means the tem­plat­ing is plug­gable, why not use my favourite tem­plat­ing li­brary (Mako) in­stead?

It turns out to be very easy to do!

Just put this in your plug­ins fold­er and start us­ing Mako tem­plates (yes, the plan­et's con­fig and stuff is in github. Why not?).

# -*- coding: utf-8 -*-
import rawdoglib.plugins

from mako.template import Template
from mako.lookup import TemplateLookup
from mako import exceptions

class MakoPlugin:
    def __init__(self):
        self.lookup = TemplateLookup(directories=['.'],\
                              output_encoding='utf-8',\
                              encoding_errors='replace')

    def fill_template(self,template, bits, result):
        self.lookup.put_string('actual',template)
        t=self.lookup.get_template('actual')
        try:
            r=t.render_unicode(**bits)
        except:
            r=exceptions.html_error_template().render()
        result.value=r
        return False

p = MakoPlugin()
rawdoglib.plugins.attach_hook("fill_template",\
                              p.fill_template)

Yup, 20 lines of code!

rst2qhc 0.2: the insomnia release

To­day I woke up at 6AM. I had noth­ing to do un­til 9, but I woke up and could­n't sleep again.

So, what can I do? I hacked rst2qhc a bit more :-D

Since this is a small pro­jec­t, it is quick­ly ap­proach­ing fea­ture com­plete sta­tus (there is re­al­ly on­ly so much this thing can do).

So, what's new?

  • Pass­ing op­­tions to rst2ht­m­l, so you can do things like set stylesheet­s.

  • Sup­­port for a man­i­fest file, so you can list what files should be em­bed­d­ed in the help­­file.

  • Fixed project sup­­port. Now you can op­­tion­al­­ly ask rst2qhc to cre­ate a qhcp file that will build your doc­u­­men­t.

  • Bet­ter, more com­­plete ex­am­­ple that dis­­­plays most fea­­tures.

I have on­ly one thing left in my TODO, which is call­ing the HTML writ­er via API in­stead of via sys­tem() be­cause that way there is no need to de­fine the key­word role in the doc­u­men­t, and I can add syn­tax high­light­ing via pyg­ments.

Since I now do have work to do, I will just re­lease 0.2 now ;-)

You can get it from the usu­al place: http://rst2qhc.­google­code.­com

And here's a sam­ple of the in­put/out­put:

rst2qhc

Now you, too can create Qt Help Files painlessly

I de­cid­ed to add a man­u­al for uRSSus. Since it's a Qt ap­p, I checked how to do one of those neat help files, like the ones that come with Qt app­s.

It turns out it's not so sim­ple to cre­ate one of those.

You need to cre­ate your help in one or more HTML files, then cre­ate a XML file that de­scribes what each file is, ref­er­ences for each sec­tion, and ref­er­ences for each key­word you want in the in­dex.

For any re­al-life-­size doc­u­men­t, that's go­ing to be in­cred­i­bly an­noy­ing.

So, I took my usu­al es­cape route when I don't want to do grunt work on doc­s: Do­cu­tils.

Specif­i­cal­ly, I wrote rst2qhc which takes one (or more) re­struc­tured text files, and cre­ates a nice and clean Qt Help Project file from them, in­clud­ing sec­tion ti­tles, ref­er­ences and key­word­s, which you mark on the text us­ing the 'key­word' role.

What does this mean? Let me be graph­i­cal:

rst2qhc2

The one on the right is nice to read. The one on the left is nice to write. The ar­row in the mid­dle is rst2qhc :-)

And here's how the gen­er­at­ed qhp file looks for a triv­ial out­line of a man­u­al (and the rea­son why I don't want to do this man­u­al­ly ;-):

<?xml version="1.0" encoding="UTF-8"?>
<QtHelpProject version="1.0">
    <namespace>urssus</namespace>
    <virtualFolder>doc</virtualFolder>
    <customFilter name="Unknown">
        <filterAttribute></filterAttribute>
    </customFilter>
    <filterSection>
        <filterAttribute></filterAttribute>
        <toc>
            <section title="The uRSSus Handbook" ref="manual.html">
                <section title="Introduction" ref="manual.html#introduction"/>
                <section title="Quick Start" ref="manual.html#quick-start"/>
                <section title="Configuration" ref="manual.html#configuration"/>
                <section title="Credits and License" ref="manual.html#credits-and-license"/>
            </section>
        </toc>
        <keywords>
                <keyword name="what can you" ref="manual.html#what-can-you-do-using-urssus"/>
                <keyword name="quick" ref="manual.html#quick-start"/>
                <keyword name="main window" ref="manual.html#the-main-window"/>
        </keywords>
        <files>
                <file>manual.html</file>
        </files>
    </filterSection>
</QtHelpProject>

Al­so, as a bonus, you can cre­ate PDF, La­TeX, HTML and ODT files from the same source (heck, you can cre­ate freak­ing man pages).

Planeta PyAr, or how to generate multiple planets with rawdog

I just fin­ished im­ple­ment­ing my first pub­lic plan­et, Plan­e­ta PyAr us­ing raw­dog. It con­tains the blogs of mem­bers of Python Ar­genti­na

To avoid the clas­si­cal ar­gu­ment about whether this is "the plan­et of peo­ple in PyAr" or "pos­ta about python from peo­ple in PyAr", I de­cid­ed to hack raw­dog in­to pro­duc­ing both.

I did­n't want to have two sep­a­rate raw­dog con­fig­u­ra­tions, so I start­ed look­ing at plug­ins.

The ba­sis was the se­lect­feeds plug­in and the rss plug­in.

Then I merged se­lect­feeds in­to rss to make things sim­pler.

Here is my hacked ver­sion which takes the se­lect­feed ar­gu­ment and us­es on­ly those feeds when cre­at­ing the HTM­L, FOAF, OPML and RSS files.

The bad side of this is that se­lect­feed re­quires you post the feed URLs all in one line!

With just a cou­ple dozen blogs in the plan­et, that means sev­er­al hun­dred char­ac­ter­s. And then when you de­cide to change one URL... ick.

So, I wrote a tiny script to take sep­a­rate feed files and cre­ate sep­a­rate con­fig files, merge-­con­fig.py.

Now, I cre­ate a feed­s-­full file with the non-python-spe­cif­ic feeds in raw­dog for­mat:

#############
# Feeds full
#############

# Anthony Lenton
feed 20m http://anthony.lenton.com.ar/feed/es/
# Rivendel
feed 20m http://www.ceportela.com.ar/?feed=rss2
# Facundo Batista
feed 20m http://feeds2.feedburner.com/com/wEER
:
:

And the same for a feed­s-python:

###################
# Feeds solo python
###################

# Anthony Lenton
feed 20m http://anthony.lenton.com.ar/?feed=rss2&cat=4&language=es
# Rivendel
# No tiene RSS solo python
# Facundo Batista
# No tiene RSS solo python
# Gabriel Patiño
feed 20m http://gepatino.blogspot.com/feeds/posts/default/-/Python
:
:
:

Put all the com­mon con­fig­u­ra­tions in con­fig-base.

And merge-­con­fig.py pro­duces a con­fig, a con­fig-­full and a con­fig-python.

Then run raw­dog like this:

cd ~/.rawdog ; LANG=es_ES rawdog -c config-full -w ; LANG=es_ES rawdog -c config-python -w

And that's it, two plan­ets for the cost of one and a half.I won­der why oth­er plan­ets don't pro­vide this.


Contents © 2000-2021 Roberto Alsina