Issues with creating a filter

51 views
Skip to first unread message

Artium Nihamkin

unread,
Aug 14, 2012, 8:53:58 PM8/14/12
to blogofil...@googlegroups.com
I use Blogofile version 0.7.1 that I installed with easy install.

I wanted to make my own filter, and used the following documentation page as a reference: http://docs.blogofile.com/en/latest/filters.html
Unfortunately, I encountered issues, some of which I managed to figure out, others are still open. I will appreciate getting help or helpful comments on these.

1. I could not find where the default filter chain is located. I did a search on all files in the _template folder but could not find anything. I suspect that blog.content contains content already filtered by markdown and syntax_highlight in some default way. If this is the case, I think it should be documented as it is a bit confusing.

2. For out of the box Blogofile, post.mako does not inherit from base.mako, so using the filter macro as advised in the earlier linked page produced weird python errors.

3. My filters is using a regex to substitute a special tag similar to the $$code tag. The problem is that sometimes the excerpt cut the tag in the middle and when the regex is run on the excerpt, no match is found. This will display part of the tag in the excerpt text. I checked with $$code, and it does not syntax highlight in the excerpt, but there is also no remaining part of the opening tag.

4. I discovered that my filter is run too many times, which may lead to performance issues, this is because the filtering happens in post.mako, and this template is included in many others (categories, chronological etc.). I wish there were a way to "activate" the filter where markdown and syntax highlighting filters are located. So when blog.content i used in a template, it is already filtered by my filter.

5. It is worth noting that there is also an option to extend the markdown engine itself (http://packages.python.org/Markdown/extensions/api.html). Have not tried it yet, but I believe it will not solve the excerpt cut in the middle issue.

Thank you,
Artium

Artium Nihamkin

unread,
Sep 24, 2012, 6:21:10 PM9/24/12
to blogofil...@googlegroups.com
Another unrelated question (previous question are still relevant)

6. How can I add a custom configuration variable in _config.py that will be accessible in the mako templates?

EJelome

unread,
Sep 24, 2012, 11:05:44 PM9/24/12
to blogofil...@googlegroups.com
Hi, I've managed to create a filter that minifies the HTML pages just now, just in case you find it useful, you can use, copy and/or improve it.

Here's how I did it:

1. Go to your _filters directory (same level with _config.py, if you don't have one, just create it). Then create a file named: html_minifier.py.

2. Paste the following code in the html_minifier.py (it kinda sucks since I'm a newbie myself):
import re

def run(content):
    """Returns a minified HTML."""

    ntr_spaces = re.compile(r'\n|\t|\r', re.IGNORECASE)
    dbl_spaces = re.compile(r'\s{2,}', re.IGNORECASE)
    content = ntr_spaces.sub('', content)
    content = dbl_spaces.sub(' ', content)

    return content.strip()

3. Go to _templates and create a file named: base.mako (if you don't have it, just create one) and paste the following code:
<%def name="filter(chain)" filter="trim">
    ## Definition of this function.

    <% return bf.filter.run_chain(chain, capture(caller.body)) %>
</%def>
${next.body()}

4. Open site.mako file and paste the following at the top of the file:
<%inherit file="base.mako" />

5. Now still in site.mako, wrap your templates like the following example:
<%self:filter chain="html_minifier">
    <html>
        <head>
            <title></title>
        </head>
        <body>
        </body>
    </html>
</%self:filter>

Voila! Now you have a HTML minifier. There are still room for improvements for that code (it didn't consider inline css and js) so use caution. But the point is making your own filter, so experiment and enjoy ^^,

Kind regards,
EJelome

Reply all
Reply to author
Forward
0 new messages