Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

The best reStructuredText directive ever. Really.

It all started quie­tly wi­th an Is­sue in the Niko­la bug tra­cke­r:

gustavodiazjaimes opened this issue 3 hours ago
oembed by micawber - feature sugestion

instead of using specific code for embed object (Youtube, Vimeo, Soundcloud) use a generic solution like https://github.com/coleifer/micawber

So, I looked at mi­caw­ber. And it's awe­so­me and I had ne­ver heard of it be­fo­re. What it does is, it looks at a UR­L, and if it kno­ws the si­te whe­re it's pointed at, it wi­ll then ga­ther in­for­ma­tion from it. And using that in­for­ma­tion it wi­ll crea­te for you a HT­ML frag­ment so you can em­bed it in your own HT­ML pa­ge.

And how hard is it to turn that in­to a reS­truc­tu­re­dText di­rec­ti­ve? NOT HARD AT ALL.

import micawber

class Media(Directive):
    """ Restructured text extension for inserting any sort of media using micawber."""
    has_content = False
    required_arguments = 1

    def run(self):
        providers = micawber.bootstrap_basic()
        return [nodes.raw('', micawber.parse_text(self.arguments[0], providers), format='html')]

So no­w, in Niko­la's gi­thub mas­ter you can do this:

.. media:: http://www.flickr.com/photos/ralsina/7827374994/

-----

.. media:: http://www.youtube.com/watch?v=cEh5cLqxgpA

And get this:

DSCN0327

Mi­caw­ber su­ppor­ts many si­tes, and mo­re can be adde­d. So, thanks co­lei­fer and gus­ta­vo­dia­z­jai­mes for brin­ging this up :-)

dwhall256 / 2013-08-24 01:48:

I'm not a big fan of more dependencies, but this one is worth it!


Contents © 2000-2023 Roberto Alsina