And here is part 2!

After my disappointment with XQuery, I looked at a template engine for Python. While Python can do some basic templating itself, Cheetah is much more powerful. So here’s an example for a Cheetah template:

// Generated file, do not edit!

\#ifndef ${c.name.upper()}_H
\#define ${c.name.upper()}_H

class ${c.name}
{
    // properties
#for $a in $c.properties
    ${a.datatype} m_${a.name};
#end for

public:

#for $a in $c.properties

    ${a.datatype} ${a.name}() const {
        return m_${a.name};
    }

    void set${a.name.capitalize()}(${a.datatype} val) {
        m_${a.name} = val;
    }
#end for
};

\#endif

This template takes as parameter a Python object ‘c’ (a class) which has a name and a list of properties. Each property is another Python object with a name and a datatype. So given this
parameter c: { 'name': "MyClass", 'properties': [ {'name': "x", 'datatype': "double"}, {'name': "text", 'datatype': "QString"} ] } this template would generate this C++ header file:

// Generated file, do not edit!

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass
{
    // properties
    double m_x;
    QString m_text;

public:

    double x() const {
        return m_x;
    }

    void setX(double val) {
        m_x = val;
    }

    QString text() const {
        return m_text;
    }

    void setText(QString val) {
        m_text = val;
    }
};

#endif

I hope you agree that Cheetah is well suited for the task at hand. Now all is missing is a program that parses RelaxNG specification and produces the right parameters for these Cheetah templates. We have a Python script that does just that for the ongoing Google Summer of Code project of Parthasarathy. So if you want to know the nitty-gritty details, checkout “git clone git://git.scribus.net/gsoc12xml” and look in Scribus/Scripts/fromrelaxng for the Python scripts. The RelaxNG files, templates and generated file are in Scribus/scribus/relaxng.

So what’s next? The idea of the GSoC project is to define a new XML file format that is closely related to the classes and objects the Scribus code uses. Ideally the RelaxNG source will not only define the XML format, but also the classes used by Scribus and the object model which is available to Scribus Python scripts. On the other hand the XML file format should be clear and descriptive to allow external applications to parse and generate Scribus files. That’s why we invite any interested parties to discuss the future Scribus XML format on the scribus-dev mailing list. I’ll post a first RelaxNG specification to scribus-dev, soon. Until then,

/Andreas