wiki:SyntaxColoring

Syntax coloring

This page is an example of the library's ability to export syntax coloring in ODT.

First, a little bit of PHP:

<?php
/**
 * ODT Plugin: Exports to ODT
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 * @author     Aurelien Bompard <aurelien@bompard.org>
 */
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

class syntax_plugin_odt extends DokuWiki_Syntax_Plugin {

    /**
     * return some info
     */
    function getInfo(){
        return confToHash(dirname(__FILE__).'/info.txt');
    }

    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }

Now, a little bit of Python :

import tidy
from lxml import etree
from PIL import Image

#pylint#: disable-msg=C0301,C0111

INSTALL_PATH = "."

INCH_TO_CM = 2.54
CHARSET = "utf-8"

__version__ = 0.1

class HTMLFile(object):
    """
    This class contains the HTML document to convert to ODT. The HTML code
    will be run through Tidy to ensure that is is valid and well-formed
    XHTML.
    """

    def __init__(self, options):
        self.options = options
        self.html = ""

    def read(self):
        """
        Read the HTML file from :attr:`options`.input, run it through Tidy, and
        filter using the selected ID (if applicable).
        """
        in_file = open(self.options.input)
        self.html = in_file.read()
        in_file.close()
        self.cleanup()
        if self.options.htmlid:
            self.select_id()

Finally, a few lines of shell script :

# Requires: unzip, tidy, xsltproc

INSTALL_PATH="."

if [ $# -ne 3 ]; then
    echo "Usage: $0 <html-input-file> <odt-output-file> <odt-template-file>"
    exit 2
fi

htmlfile="$1"
odtfile="$2"
templatefile="$3"

if [ ! -d "$INSTALL_PATH/xsl" ]; then
    echo "Can't find the stylesheets in $INSTALL_PATH/xsl. Aborting" >&2
    exit 1
fi

# Unzip the template
tmpdir=`mktemp -d --tmpdir xhtml2odt-XXXX`
trap "rm -rf $tmpdir" EXIT
unzip -q -d $tmpdir/odt $templatefile

# Clean up the HTML
tidy -quiet -asxhtml -utf8 $htmlfile > $tmpdir/tidied.html

Et voilà !