diff --git a/interlinks/__init__.py b/interlinks/__init__.py new file mode 100644 index 0000000..397894e --- /dev/null +++ b/interlinks/__init__.py @@ -0,0 +1 @@ +from .interlinks import * diff --git a/interlinks/interlinks.py b/interlinks/interlinks.py new file mode 100644 index 0000000..8855aae --- /dev/null +++ b/interlinks/interlinks.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- + +""" +Interlinks +========================= + +This plugin allows you to include "interwiki" or shortcuts links into the blog, as keyword>rest_of_url + +""" + +from bs4 import BeautifulSoup +from pelican import signals +import re + +interlinks = {} + +def getSettings (generator): + + global interlinks + + interlinks = {'this': generator.settings['SITEURL']+"/"} + if 'INTERLINKS' in generator.settings: + for key, value in generator.settings['INTERLINKS'].items(): + interlinks[key] = value + +def content_object_init(instance): + + if instance._content is not None: + content = instance._content + # use Python's built-in parser so no duplicated html & body tags appear, or use tag.unwrap() + text = BeautifulSoup(content, "html.parser") + + if 'a' in content: + for link in text.find_all(href=re.compile("(.+?)>")): + url = link.get('href') + m = re.search(r"(.+?)>", url).groups() + name = m[0] + if name in interlinks: + hi = url.replace(name+">",interlinks[name]) + link['href'] = hi + + instance._content = text.decode() + +def register(): + signals.generator_init.connect(getSettings) + signals.content_object_init.connect(content_object_init) \ No newline at end of file diff --git a/interlinks/readme.md b/interlinks/readme.md new file mode 100644 index 0000000..4339812 --- /dev/null +++ b/interlinks/readme.md @@ -0,0 +1,46 @@ +Interlinks +========================= + +This plugin allows you to include "interwiki" or shortcuts links into the blog, as keyword>rest-of-url + +Requirements +-------------------- +``interlinks`` requires BeautifulSoup + + pip install beautifulsoup4 + +Installation +-------------------- + +Install the plugin normally (plugins folder), then add interlinks in the settings.py: + + PLUGINS = ["interlinks"] + +Usage +------------------ + +The interlinks are specified in the settings.py file as (example): + + INTERLINKS = { + 'wikipedia_en': 'http://en.wikipedia.org/wiki/', + 'wikipedia_es': 'http://es.wikipedia.org/wiki/', + 'ddg': 'https://duckduckgo.com/?q=' + } + +There's also a default key, ``this``, that is mapped to the ``SITEURL`` variable. + +Then, in a blog post, you just create a normal link but adding the ``keyword>`` syntax in the url specification, followed by the rest of the url. + +Example +------------------- +(markdown syntax) + + [Normal boring link](http://www.example.com). But this is a [cool link](this>) that links to this site. + + Search in [Wikipedia](wikipedia_en>python), ([here](wikipedia_es>python) in spanish). Also can [search](ddg>python) it. + +All the above will be rendered as: + +

Normal boring link. But this is a cool link that links to this site.

+ +

Search in Wikipedia, (here in spanish). Also can search it.

\ No newline at end of file diff --git a/interlinks/test_data/testme.md b/interlinks/test_data/testme.md new file mode 100644 index 0000000..332deb6 --- /dev/null +++ b/interlinks/test_data/testme.md @@ -0,0 +1,9 @@ +Title: Testing +Date: 3000-07-09 +Slug: plugin-test + +Testeando un poco la cosa + +[Normal boring link](http://www.example.com). But this is a [cool link](this>) that links to this site. + +Search in [Wikipedia](wikipedia_en>python), ([here](wikipedia_es>python) in spanish). Also can [search](ddg>python) it.