Skip to main content

Ralsina.Me — Roberto Alsina's website

The best reStructuredText directive ever. Really.

It all start­ed qui­et­ly with an Is­sue in the Niko­la bug track­er:

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­some and I had nev­er heard of it be­fore. What it does is, it looks at a URL, and if it knows the site where it's point­ed at, it will then gath­er in­for­ma­tion from it. And us­ing that in­for­ma­tion it will cre­ate for you a HTML frag­ment so you can em­bed it in your own HTML page.

And how hard is it to turn that in­to a re­Struc­tured­Text di­rec­tive? 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 now, in Niko­la's github 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:

In order to use the media directive, you must install the "micawber" Python package.

In order to use the media directive, you must install the "micawber" Python package.

Mi­caw­ber sup­ports many sites, and more can be added. So, thanks coleifer and gus­tavo­di­az­jaimes for bring­ing 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