Archive for the 'CMS' Category

04
Oct
09

PyML(Python Markup Language) template engine

I know it’s been a while since my last post. I’ve been busy with setting up and pulling down pepijndevos.nl(after I decided to go for another blog system), I’ve spend a good deal of time working on PyMouse and worked on some projects for myself and for clients.

I want to share one of those projects with you. I decided to make a CMS like Jekyll and Hyde(not the book!), since I liked neither of the previous systems. So like a lot of people who like reinventing wheels I started to look for a nice markup language for the user and a nice template language for the designer.

I choose for Markdown right away, end of story here…

Because of the little experience I have with Pylons I looked ad Genshi and Mako initially(can’t help the Chinese feeling these names give me). Personally the ugly tags in most template languages make me run away, so I threw Make out of the window, together with all the other ugly-tag-based languages.

I like the idea of working with valid XML attributes for templating, so I started of with Genshi initially, especially since speed isn’t a major point for a static blog!

Days later I came across this wiki page on the Python website: http://wiki.python.org/moin/Templating

Looking at Dirty I remembered my own attempts to write a template language implemented as Python objects(extending dict, for free functionality).

The problem with writing such a thing is described in the code below, I mailed the developer of Dirty about how he solved the problem — he didn’t.

>>> def div(*content, **attributes):
...     pass
...
>>> div("hello world!", id="test") # Doesn't html have the attributes first!? Ugly!
>>> def div(**attributes, *content): # Not allowed!
  File "", line 1
    def div(**attributes, *content):
                        ^
SyntaxError: invalid syntax
>>> def div(atributes, *content):
...     pass
...
>>> div({'id':'test'}, "hello world!") # Even uglier!

But then I came up with this slightly brilliant solution:

When you define __call__ on a class it allows you to call the class, no big deal, eh? Wrong! It allows you to write this:

html(xmlns="http://www.w3.org/1999/xhtml")(
    head( # No atributes
        title("Hello world")
    ),
    body(
        p(id="test")(
            "Hi", br(),
            "How are you?"
        ),
        img(src="test.img", alt="just a test") # No content
        ul(
            *[li(c) for c in xrange(10)] # Now that is cool!
        )
    )
)

In short PyML is a pure Python combination of a string and a dict looking like html when written and printed!

You can do all sorts of things with this that you normally do with dictionaries, strings and lists, like sorting and filtering them, change a p into a div or getting/setting attributes later with square brackets. You could define functions to return a snippet or you could include templates into others. If you’re finished with them, just print the object!

Oh, about the speed? Quite good for my hobby project! (Unreliable micro benchmark ahead!)

Render time Engine
0.03160 PyML
0.50618 Genshi
0.01813 Mako

You can get it here if you want! Or wait for my complete CMS of course…

19
Jun
09

My new plugin

website trafficI can tell for sure that you came from wordpress.org. Not because I checked you referrer or something, just because… Well, see for yourself, this is the traffic graph of the day I released my WordPress plugin.

I’m sorry to disappoint you, but there is not yet anything to find about the plugin on this blog. But it’s the only public blog I have. Keep an eye on the menu, as soon as I find the courage to do so I’ll write something about it.

01
Apr
09

First library for my CMS started!

I finaly started coding for my CMS. I’m not sure if I’m already up to the job, but I’ll find out soon enough. The first library is part of the template engine. I don’t want people to write crap HTML, so I wrote a library that can generate strict XHTML with python code that is as similar to html as possible, see below for an example.

The big question remains, what to do with user content, even valid XHTML theme break if someone pastes his Word HTML in the editor. I plan on using BeautifulSoup to parse the user code, strip off the ‘invalid’ tags like <i>, <u>, <iframe>, etc… and feed it to my XHTMLLib. The even cooler part is that you could use your old HTML theme, feed it to my lib and fill/replace the content. With the latest version this became even easyer because I added some sort of xpath support(absolute paths only, but with attribute selection).

I present to you, my first lib!

Link: http://www.box.net/shared/6x01eol1x5 Also available in the sidebar widget. You need xhtmlattr.py as well!

Example:

page = XHTMLLib().template() # generate a basic XHTML template
    page.xpath('/html/head/title')['child'] = "Hello world" # set the title

    page.function_factory(['div', 'img', 'fieldset', 'input', 'ul', 'li'], '', '') # register functions

    lists = [li(class_='xtra', child="Home"), li(class_="test", child="Links"), li(child="Contact"), li(class_="test", child="About"), li(child="Sitemap")] # create a list
    lists.sort() # sort elements

    page.xpath('/html/body')['child'] = [ # add to the body
        div(id_='header', child='test & more'), # Entities get converted
        img(src='image.jpg'),
        fieldset(class_='test', child=[
            input(type_='text', name='name'),
           input(type_='submit')
        ]),
        ul(child=lists) # Comppose pages of different object sets.
   ]

    page.xpath('/html/body/img').__setitem__('onclick', 'alert("!@#$^, stop clicking!")', True) # Voodoo magic to get evil js in, alsays use external js!
    page.xpath('/html/body/ul/li[class=test][4]')['style'] = 'color:red' # Some more 'advanced xpath' to get a specific list item

    print page.render();

result:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
	<head>
		<meta content="application/xhtml+xml;charset=utf-8" http-equiv="Content-Type"></meta>
		<title>Hello world</title>
	</head>
	<body>
		<div id="header">test &amp; more</div>
		<img src="image.jpg" onclick="<![CDATA[alert("!@#$^, stop clicking!")]]>"></img>
		<fieldset class="test">
			<input type="text" name="name"></input>
			<input type="submit"></input>
		</fieldset>
		<ul>
			<li>Contact</li>
			<li>Sitemap</li>
			<li class="test">About</li>
			<li class="xtra">Home</li>
			<li style="color:red" class="test">Links</li>
		</ul>
	</body>
</html>
26
Mar
09

PepSite CMS so far

What I did not yet do: Everything.

What I did do: the rest, being:

  • Started to learn python
  • Register at python-forum.com
  • Write a batch Thumbnail generator with PIL
  • Think about what I want and don’t want for my own website/CMS



My blog has moved!

My blog has permanently moved to a self hosted Wordpress at http://pepijndevos.nl

This blog will stay around for accidental search engine visitors.

Me

This is me

Blog Stats

  • 22,233 hits

@PepijnDeVos