Getting twitter feed running for Blogofile

September 28, 2010 at 07:21 PM | categories: Website, Programming | View Comments


A small part to making it match

Wordpress has plugins, lots and lots of plugins. In my conversion from it to blogofile I wanted to make it match my old site as closely as I could. Having a twitter feed of my latest tweets was something I'd have to get on the site.

Fails

I though I might make a python api call and then pipe that into my sidebar. This worked. But it sucked because I'd have to rebuild the page every tweet. I don't mind static content for my posts and navigation and the like. That's just good caching I feel.

Tweets though are a constantly, or would be for anyone else but me, updating service.

In comes the JavaScript

So in looking for simple things to give me a twitter feed I found Remy Sharp's site and his directions on how to put a javascript based feed into a site.

It was simple as could be, and I had a base, but hard coded feed up an running in short order.

Making it better for blogofile users

Part of open source is giving back. Something I really want to do more of, and I feel am getting into the swing of it a bit more. But that's whatever, I found the useful and perfect plugin, and got it working in blogofile without much trouble.

I now started thinking about how others might like to use this and would not really want to hard code it too much. So I made it into a controller, and configurable by the _config.py file. I made it kinda simple, but a bit overkilled with the folder and an __init__.py, but I was aiming for it to be more work.

_controllers/tweets/__init__.py

import logging

from blogofile.cache import bf

config = {
    "name": "Twitter",
    "description": "Makes a sidebar widget for twitter",
    "priority": 70.0,
    }

def run():
    tweets = bf.config.controllers.tweets
    tweets.logger = logging.getLogger(config['name'])

After making it a controller then we're able to put in config vars and set them as we want. Which makes it nice for me later i want to change some things, since all configs are in this one file.

_configure.py

### Twitter Settings ###
controllers.tweets.enabled = True
tweets = controllers.tweets
tweets.username = "morganiangoose"
tweets.count = 3
tweets.enable_links = 'true'
tweets.ignore_replies = 'false'
tweets.template = ('<li><div class="item">%text% <a href="http://twitter.c'
        'om/%user_screen_name%/statuses/%id%/">%time%</a></div></li><br/>')

I then threw this, which is pretty much directly from remy's directions with mako var, into my sidebar template.

_templates/sidebar.mako

<div class="sidebar_item">
<h3>Twitter</h3>
    <script src="http://twitterjs.googlecode.com/svn/trunk/src/twitter.min.js"
    type="text/javascript"></script>
    <script type="text/javascript" charset="utf-8">
        getTwitters('tweet', {
            id: '${bf.config.tweets.username}',
            count: ${bf.config.tweets.count},
            enableLinks: ${bf.config.tweets.enable_links},
            ignoreReplies: ${bf.config.tweets.ignore_replies},
            clearContents: true,
            template: '${bf.config.tweets.template}',
            });
    </script>
    <div id="tweet">
    </div>
</div>
<br />

Now when I compile the site it'll just throw all this into the sidebar, which will load up my twitter feed as I described in the template. And I won't have to have a twitter trigger or the like for updating my blog, which is I feel the best of both worlds.







Switching to Blogofile

September 28, 2010 at 01:03 PM | categories: Website, Programming | View Comments


So wordpress is out

It had a nice run, and really did a lot of things I liked, highest among those being that it worked. But even with my vim plugin for posting to the site, I never really found myself happy with the whole setup. Then I stumbled upon blogofile via a fork I spied in my github feed. After inspecting the project and reading though the docs a bit I decided to give it a try.

Conversion process

It was criminal how simple this was. I added Disqus comments (should have done that ages ago) and imported all my old comments to the account. After that I followed the directions to switch from wordpress and used the two scripts they provided. After a few mysql hickups (dumped the db to a machine that had python-mysql), the scripts worked flawlessly.

I now had all of my posts, not a lot granted, in html blogofile form. Now I mainly switched so that I could write up my posts in rst which I like a lot, and use in other projects. So now I had some conversions to make by hand to get these posts into rst form.

After some hand editing

I go to publish the site, and it's not liking my code directives. I use these in other presentations and forgot that I'd made some changes to use the directive, which I go into more depth about in a previous post, but needed to now retrofit into this project.

The simple blog template had some helpers, but I had to pull the rst_template.py filter from the blogofile.com template and edit it to give me the highlighting control that I wanted.

rst_template.py changes

# Set to True if you want inline CSS styles instead of classes
INLINESTYLES = False
STYLE = "fruity"

from pygments.formatters import HtmlFormatter

# The default formatter
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES, style=STYLE)

# Add name -> formatter pairs for every variant you want to use
VARIANTS = {
        'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=False),
    }


from docutils import nodes
from docutils.parsers.rst import directives, Directive
from docutils.core import publish_parts, default_description

from pygments import highlight
from pygments.lexers import get_lexer_by_name, TextLexer


class Pygments(Directive):
    """ Source code execution.
    """
    required_arguments = 1
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = dict([(key, directives.flag) for key in VARIANTS])
    has_content = True

    def run(self):
        self.assert_has_content()
        try:
            lexer = get_lexer_by_name(self.arguments[0])
        except ValueError:
            # no lexer found - use the text one instead of an exception
            lexer = TextLexer()
        # take an arbitrary option if more than one is given
        formatter = self.options and VARIANTS[self.options.keys()[0]] or DEFAULT

        print >>open('css/pygments_fruity.css', 'w'), formatter.get_style_defs(
                '.highlight')
        parsed = highlight(u'\n'.join(self.content), lexer, formatter)
        return [nodes.raw('', parsed, format='html')]


config = {
    'name' : "reStructuredText",
    'description' : "Renders reStructuredText formatted text to HTML",
    'aliases' : ['rst']
}

def run(content):
    directives.register_directive('sourcecode', Pygments)
    directives.register_directive('code', Pygments)
    directives.register_directive('code', Pygments)

    description = ('Generates S5 (X)HTML slideshow documents from standalone '
            'reStructuredText sources.  ' + default_description)

    return publish_parts(content, writer_name='html')['html_body']

So I added a lot into my site, and put in the theme I wanted to use in the css/ folder so that the style would publish when I build the site.







« Previous Page -- Next Page »