Source

emacs-blogger / filters.py

Full commit
'''Filter collection to handle the various text manipulation between formats
'''
import re
import utils.rst_directive
from BeautifulSoup import BeautifulSoup
from docutils.core import publish_string

def pipe(*a):
    '''takes functions to pipe in a single function
    '''
    def fun(arg):
        for function in a:
            arg = function(arg)
        return arg
    return fun

def rst2html(rst):
    '''transform rst in html with pygments for ..sourcecode directive
    '''
    return publish_string(rst, writer_name="html")

  
def extract_body(html):
    '''
    Extract the body part from the html string
    '''
    soup = BeautifulSoup(html)
    body = soup.findAll("body")[0]
    return "".join(str(element) for element in body.contents)

def degrade_sections(html):
    '''Degrade h1 to h2 , h2 to h3 etc...
    '''
    soup = BeautifulSoup(html)
    for i in range(1,6)[::-1]:
        headers = soup.findAll("h%d"%i)
        for head in headers:
            head.name = "h%d"%(i+1)
    return str(soup)

def remove_space_between_tags(html):
    return re.sub(">\s*<","><",html)