coleifer / micawber

a small library for extracting rich content from urls
http://micawber.readthedocs.org/
MIT License
632 stars 91 forks source link

Option to prevent converting inline links #29

Closed tiktuk closed 10 years ago

tiktuk commented 10 years ago

I have a Markdown RichText field in my Django app that I'm using micawber for converting video links into embedded videos. I only want micawber to convert links on their own line into embedded media however. I don't want it to convert my markdown links, the mardown converter will take care of those.

So far the text is first run through an oembed_no_urlize function as described in your documentation:

from micawber.contrib.mcdjango import extension

oembed_no_urlize = extension('oembed', urlize_all=False)

Inline YouTube links are still oEmbed converted though, so a Markdown link like

[5 minutter og ti sekunder](http://www.youtube.com/watch?v=chbOViRudAg&t=5m10s)

is converted into

<a href='a href="http://www.youtube.com/watch?v=chbOViRudAg&amp;t=5m10s" title="Joo Sae Hyuk Vs Chuang Chih Yuan: WTTC 2014: 1/4 Final AMAZING MATCH">Joo Sae Hyuk Vs Chuang Chih Yuan: WTTC 2014: 1/4 Final AMAZING MATCH</a'>5 minutter og ti sekunder</a>

when first converted by micawber and then markdown.

Is it possible to disable all inline conversion?

coleifer commented 10 years ago
        from micawber.contrib.mcdjango import extension

        def custom_handler(url, response_data):
            return url

        oembed_alt = extension(
            'oembed_alt',
            urlize_all=False,
            block_handler=custom_handler)

        text = '\n'.join((
            'this is the first line',
            'http://photo-test2',
            'this is the third line http://photo-test2',
            'http://photo-test2 this is the fourth line'))
        rendered = self.render('{{ text|oembed_alt }}', text=text)
        self.assertEqual(rendered.splitlines(), [
            'this is the first line',
            self.full_pairs['http://photo-test2'],
            'this is the third line http://photo-test2',
            'http://photo-test2 this is the fourth line',
        ])
tiktuk commented 10 years ago

Thanks, works perfectly :) .