Today we are going to dive into Pelican, and it's plugins :) We are going to make a plugin that will transform only a link into an HTML link with a description consisting of the page title.

S0-E23/E30 :)

Pelican Plugins

In this blog we have already created pelican-plugin, you can check pelican-plugin tag.

What type of signals pelican has to offer?

Well, that's the thing, they are not first-class documented things.

For making them a bit more available and easier to debug, I've created debug_pelican_signals repository at github that contains all signals which you can now connect to Pycharm or pdb and debug. The methods are visible at signalsdebugger.py of debug_pelican_signals repo.

Making a Pelican Link To Title

Now let's make a pelican link to title plugin that will convert all links into links with a title.

The plugin will be called 'Pelican Link to Title'. Its source code is available at github.

Code itself:

# -*- coding: utf-8 -*-
""" This is a main script for pelican_link_to_title """
from pelican import signals
from bs4 import BeautifulSoup
import urllib


def link_to_title_plugin(generator):
    "Link_to_Title plugin "
    article_ptags = {}
    for article in generator.articles:
        soup = BeautifulSoup(article._content, 'html.parser')
        p_tags = soup.find_all('ahref')
        for tag in p_tags:
            url_page = tag.string
            if url_page:
                if "http://" in url_page or "https://" in url_page:
                    tag.name = "a"
                    tag.string = read_page(url_page)
                    tag.attrs = {"href": url_page}
        article._content = str(soup).decode("utf-8")

def read_page(page):
    r = urllib.urlopen(page).read()
    soup = BeautifulSoup(r , "html.parser")
    return soup.find("title").string

def register():
    """ Registers Plugin """
    signals.article_generator_finalized.connect(link_to_title_plugin)

To use the plugin, all you need to do is add into your PLUGINS in pelicanconf a pelican-link-to-title and put in the article something like this:

<ahref>http://witkowskibartosz.com/blog</ahref>

and this "ahref" will be retransformed into:

<a href="http://witkowskibartosz.com/blog/">Anselmos Blog</a>

Which means, it goes into this page, takes the title of the page and puts into a-href string data.

Acknowledgements

Thanks!

That's it :) Comment, share or don't :)

If you have any suggestions what I should blog about in the next articles - please give me a hint :)

See you tomorrow! Cheers!



Comments

comments powered by Disqus