Posts about python (old posts, page 43)

2013-01-22 17:05

Nikola Can Parse That

Just for completeness' sake I have gone over Wikipedia's list of lightweight markup languages and added support in Nikola (a static site and blog generator) for a couple of them: BBCode and txt2tags, which brings the number of supported markup formats to 7 (the others are reStructured text, markdown, HTML, textile and Wiki).

Of the list of markup languages, the only missing ones with a Python implementation are AsciiDoc and Markdown Extra.

Why bother with this? It's not as if there was someone asking for BBCode support. However, one of the uses I want to support is archiving sites. Suppose you have a forum you want to shut down? I want you to be able to archive it (even if it's some work) and keep the data out there. A wiki? Same thing.

This is a first step in that direction.

2013-01-21 10:13

Make Me Talk

One of the things that have helped me be less shy and thus have helped me move forward in life is speaking in public. However, I have been speaking in public very little these last couple of years.

So, make me do it more. I am looking forward to speaking in places I haven't been. If it's not horribly far from Buenos Aires, I may not even ask you to pay for my trip and/or hotel, I will pay for it myself if the event is interesting or the place looks like a fun place to travel to.

I can speak about python, free software in general or other things you may suggest, and I can do it in spanish or in english.

2013-01-10 17:24

Adding Support for a Markup to Nikola

One of the goals for Nikola, my static site/blog generator is that it should be easy to extend. For example, today I added support for two markups: textile and CreoleWiki.

Since Nikola already supported HTML, reStructuredText and Markdown, adding a couple more is not very difficult. Here's how:

  1. Create a .plugin file like this one:
[Core]
Name = textile
Module = compile_textile

[Documentation]
Author = Roberto Alsina
Version = 0.1
Website = http://nikola.ralsina.me
Description = Compile Textile into HTML

Then you need to create a python module called (in this case) compile_textile.py

That file is boilerplate plus two methods, compile_html and create_post

The compile_html method takes two arguments, one file from which it reads the markup, and one to write HTML. Example:

def compile_html(self, source, dest):
    if textile is None:
        raise Exception('To build this site, you need to install the "textile" package.')
    try:
        os.makedirs(os.path.dirname(dest))
    except:
        pass
    with codecs.open(dest, "w+", "utf8") as out_file:
        with codecs.open(source, "r", "utf8") as in_file:
            data = in_file.read()
        output = textile(data, head_offset=1)
        out_file.write(output)

Make sure to use utf8 everyhere.

The create_post function is used to create a new, empty, post with some metadata in it. Example:

def create_post(self, path, onefile=False, title="", slug="", date="", tags=""):
    with codecs.open(path, "wb+", "utf8") as fd:
        if onefile:
            fd.write('<notextile>  <!--\n')
            fd.write('.. title: %s\n' % title)
            fd.write('.. slug: %s\n' % slug)
            fd.write('.. date: %s\n' % date)
            fd.write('.. tags: %s\n' % tags)
            fd.write('.. link: \n')
            fd.write('.. description: \n')
            fd.write('--></notextile>\n\n')
        fd.write("\nWrite your post here.")

The metadata has to be in the form ".. fieldname: fieldvalue" and usually needs to be wrapped in a comment so that it's not shown in the output.

The onefile parameter means you have to write that metadata in the post. If it's False, you don't.

In some rare cases (Creole, I am looking at you) comments are not supported and you should raise an exception if onefile is True.

And that's it, markup support is fairly easy to add as long as there is a python implementation of a function to convert markup into html.

2013-01-01 19:03

Client-rendered, one-URL, dynamic, static site.

My vacations end tomorrow. So, the time to spend hacking fun, personal, free stuff is going to be limited because of the time spent coding fun, free stuff for money. So, I decided to finish with a bit of whimsy.

I implemented a completely client-rendered, one-URL, dynamic blog. Which is actually totally static.

In fact, that blog is this blog, just with a twist. If you go to this URL you will see what's basically this very site, with comments and everything as usual. But if you click on "Previous Post" ... well, it stays in the same page, even though it displays a different post :-)

The magic is the new, experimental, dynamic task_mustache plugin for my static site generator, Nikola. which does the following:

  1. Renders post data as JSON files instead of HTML
  2. Creates a HTML file that is really a mustache.js template
  3. Creates a HTML file with some bits of Javascript that loads the template and the newest post's data.
  4. If you access that mustache.html with a fragment, it uses that to fetch JSON data and rewrite itself.

And that's it. It actually loads fast, and regenerates very fast, since it does much less than the real site. There are a bunch of things that will dump you out of the "dynamic" site, like tag links, and whatever, but it works surprisingly well (and if you want to theme it, it's just one template).

This is the first of a new kind of thing for Nikola, the "extra plugins". Basically, stuff that is too weird, specific or useless for the general distro, will go there, and to use those plugins, you have to create a plugins/ folder in your site and add it there manually.

Enjoy!

2012-12-27 22:31

Nikola Feature-by-request: tag cloud!

I asked for feature requests for Nikola my static site generator. I got some. One was from long-time user Kay Hayen, so I just couldn't say no.

He asked for tag clouds. Look:

Anything in here will be replaced on browsers that support the canvas element

Cute, isn't it? And it's real, actual tag data from this very site. With links!

How it's done:

Nikola generates a JSON file that has the cloud data (tag name, size, link), and that's processed by JQuery and tagCanvas and ... well, it works.

You have to add jquery.tagcanvas.min.js somewhere and embed a whole pile of raw HTML (see here for details) but hey, it works.

Hope you are happy, Kay, you made me code javascript, dude ;-)

It's probably rather "easy" to make it use any of these instead of tagCanvas if anyone is so inclined.

2012-12-26 20:22

Why Use Something Else?

I wrote a static site generator, called Nikola a while ago, and it's starting to get some adoption and contributions.

I think it's a fairly nice piece of code, to be honest, and it pretty much does everything I need. On the other hand, it's fun to code with.

So, if you are using another static site generator, or are researching into it, or just have opinions about it... what's missing? What should it have that it doesn't have now?

No feature too small, no idea too crazy, please share! I promise to make a good effort to implement any that seem like a good idea :-)

2012-12-25 19:34

New in Nikola Master: Interactive Console

Inspired by Django's interactive console, I added one to Nikola, my static site generator so that if you want to play with its internals, you can do it easily. Here's an example session:

$ nikola init pepe
A new site with some sample data has been created at pepe.
See README.txt in that folder for more information.
$ cd pepe
$ nikola console
Scanning posts
.
.
done!
You can now access your configuration as conf and your site engine as SITE
>>> [p.title('en') for p in SITE.timeline]
[u'Listings Demo', u'Nikola: it generates static', u'Welcome to Nikola',
u'A reStructuredText Primer', u'A reStructuredText Reference', u'Sample
Nikola Config File', u'The Nikola Handbook', u'Theming Nikola']

Have fun!

2012-12-20 12:53

Nikola Wordpress Importer Improvements

Thanks to having users trying to actually import data from wordpress, the wordpress importer plugin for Nikola, my static site generator got a couple of new features.

  • It will try to maintain your URLs via redirects

    If your post was in /2012/1/2/a-post/ and now it will be in /posts/a-post.html it will create a redirect from the former to the latter.

  • url_map for comment migration

    It will create a CSV file containing the old,new full URLs so that you can move your disqus threads to the right places.

Also, master works with docutils 0.10 which broke in the last release.

For these reasons, there will be a 5.0.1 release today or tomorrow.

PS: if you want to see a few sites using Nikola, now you can.

Contents © 2000-2018 Roberto Alsina