Skip to content
This repository has been archived by the owner on Apr 16, 2019. It is now read-only.

Linkify filter #85

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 58 additions & 0 deletions blogofile/site_init/blog_filters/_filters/linkify.py
@@ -0,0 +1,58 @@
# Replace first keyword in post with its link, given a list of keywords and links in a file
import re
from BeautifulSoup import BeautifulSoup
import blogofile_bf as bf

config = {"name": "Linkify",
"description": "Linkify blog post based on a file with a list of link",
"author": "Paolo Corti",
}

def linkify(text, word_dic):
"""
take a html text and replace words that match a key in a dictionary with
the associated value, return the changed text.
Filter should be run after markup filter and syntax hightlight filter
"""

VALID_TAGS = ['p', 'li']

def translate(match):
key = match.group(0)
value = word_dic[key.lower()]
# only first keyword must be linkified
if key in linkified_words:
return key
else:
linkified_words.append(key)
return value

list_dict = map(re.escape, word_dic)
list_dict2 = ['\\b%s\\b' % x for x in list_dict]
re_str = '|'.join(list_dict2)
rc = re.compile(re_str, re.IGNORECASE)
soup = BeautifulSoup(text)
textNodes = soup.findAll(text=True)

linkified_words = []
for textNode in textNodes:
parent = textNode.findParent()
if parent.name in VALID_TAGS:
urlifiedtext = rc.sub(translate, textNode)
textNode.replaceWith(urlifiedtext)
return soup.renderContents()

def run(content):
"""
Read links and create dictionary, then linkify post content
"""
path_seo_links=bf.config.filters.linkify.seo_links
seo_links = open(path_seo_links, 'r').read().split("\n")
lst=[]
for l in seo_links:
if l.find(',')>0:
sl=l.split(",")
lst.append((sl[0].lower(), '<a href="%s">%s</a>' % (sl[1], sl[0])))
seo_dict=dict(lst)

return linkify(content, seo_dict)
3 changes: 3 additions & 0 deletions blogofile/site_init/simple_blog/_config.py
Expand Up @@ -47,6 +47,9 @@
## blog_timezone -- the timezone that you normally write your blog posts from
blog.timezone = "US/Eastern"

# links file for linkify
import os
filters.linkify.seo_links = "%s/_seolinks" % os.getcwd()

## Markdown extensions
## These are turned off by default, but turned on
Expand Down
@@ -0,0 +1,14 @@
---
categories: linkify, test
date: 2009/08/29 15:25:00
title: Post 10
filter: markdown, linkify
---
This is post #10 and it is testing the linkify filter.

The links under the following paragraphs are automatically rendered by using that filter:

Blogofile is a blog engine written in Python.
Python source code is on GitHub.


4 changes: 4 additions & 0 deletions blogofile/site_init/simple_blog/_seolinks
@@ -0,0 +1,4 @@
Python, http://www.python.org
Blogofile, http://www.blogofile.com/
GitHub, https://github.com/