WikiExporter

From Inkscape Wiki
Revision as of 23:32, 12 February 2005 by LionKimbro (talk | contribs) (* docpath -> docbase)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

These are LionKimbro's notes on developing a wiki exporter.

Date: 2004-02-12
Inkscape version: 0.40
Target: Oddmuse

That is, a system so that you can load from and save to a wiki.

The wiki targetted by this exporter is Oddmuse. We have an upload script for it.

I imagine it should be possible to use the Wiki XML-RPC interfaces as well, in the future, though they are usually turned off.

odd_output.inx

This file goes in: /usr/share/inkscape/extensions

<inkscape-extension>
  <name>Oddwiki Output</name>
  <id>org.inkscape.output.odd</id>
  <dependency type="executable" location="extensions">svg2oddmuse.sh</dependency>
  <output>
     <extension>.odd</extension>
     <mimetype>image/x-svgz</mimetype>
     <filetypename>Oddmuse Wiki POST (*.odd)</filetypename>
     <filetypetooltip>Oddmuse Wiki proxy (SVG, PNG of Drawing)</filetypetooltip>
     <dataloss>FALSE</dataloss>
  </output>
  <script>
     <command reldir="extensions">svg2oddmuse.sh</command>
     <helper_extension>org.inkscape.output.svg.inkscape</helper_extension>
  </script>
</inkscape-extension>

What data am I receiving from Inkscape?

To see what you're getting from Inkscape, create the following script, and call it: svg2oddmuse.sh

#!/bin/bash
echo "hello, world" > /tmp/eraseme.txt
echo "invocation: $@"
echo
echo "$1:"
cat "$1"
echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - -"
env

It appears that no environment variables are being set. The first (and only) argument is the location of a file in /tmp that contains the Inkscape SVG data.

Now what?

The strategy is this:

  • create a directory, representing the wiki
  • place a file in the directory, "oddmuse.txt", representing necessary details about the wiki
  • when the user saves a file to the directory (.odd extension,) do the following:
    • push the SVG to the wiki
    • render a PNG, and push the PNG to the wiki
    • perhaps append the name of the wikipage containing the SVG to a page on the wiki

We don't have command-line access (bash argument, environment variable) to the filename or directory it was saved in. But we CAN get this data from the Inkscape SVG.

The <svg> element has attributes:

  • "sodipodi:docbase" -- path up to the save location
  • "sodpodi:docname" -- filaname, including extension (".odd", in this case)


Inkscape Export process

  • User hits "Save" in "Save As" dialog
  • Inkscape writes a file with a random name to /tmp
  • Inkscape calls export bash script, with /tmp file as only argument
  • Whatever bash outputs to /dev/stdout, Inkscape records to the file chosen by the user.

Inkscape SVG Attributes

Here's a Python script that outputs <svg> attributes:

svgattrs.py:

#!/usr/bin/python2.3
"""Expose Inkscape SVG attributes.

Read an Inkscape SVG from stdin.
Output attributes to the "<svg>" element to stdout.
"""

import sys
import xml.sax


class InkscapeSvgHandler(xml.sax.ContentHandler):

    """Print attributes to svg element."""

    def startElement(self, name, attrs):
        if name == "svg":
            for (k,v) in attrs.items():
                print k + " " + v


if __name__ == "__main__":
    parser = xml.sax.make_parser()
    parser.setContentHandler(InkscapeSvgHandler())
    parser.parse(sys.stdin)
    sys.exit(0)

You can use it like so:

DOCNAME=`cat "$1" | ./svgattrs.py | grep -e "^sodipodi:docname" | cut -d" " -f2`
DOCBASE=`cat "$1" | ./svgattrs.py | grep -e "^sodipodi:docbase" | cut -d" " -f2`