User:Silviolorusso/mipsum: Difference between revisions
No edit summary |
No edit summary |
||
Line 47: | Line 47: | ||
</script> | </script> | ||
</inkscape-extension> | </inkscape-extension> | ||
</source> | |||
mipsum.py is the actual extension. | |||
<source lang="python"> | |||
import os | |||
import random | |||
import sys, codecs | |||
from lxml import etree | |||
f = codecs.open(sys.argv[-1], encoding="utf-8") | |||
# open the file and parse into an etree file | |||
t = etree.parse(f) | |||
# modify the tree | |||
NS = {"svg": "http://www.w3.org/2000/svg"} | |||
# get the inkscape manual from flossmanuals.net | |||
lynxcmd = "lynx -dump -nolist http://en.flossmanuals.net/inkscape/_all/ | tr '\n' ' ' | sed 's/ */\ /g'" | |||
data = os.popen(lynxcmd).read() | |||
for flowPara in t.xpath("//svg:flowPara", namespaces=NS): | |||
if flowPara.text[0:6] == "mipsum" : | |||
# get the number of charachters to generate | |||
charnum = int(flowPara.text[6:]) | |||
# takes a random part of the text inside the manual | |||
charnumend = random.randint(0,239605) | |||
charnumbegin = charnumend - charnum | |||
flowPara.text = data[charnumbegin:charnumend] | |||
# output tree | |||
sys.stdout.write(etree.tostring(t, encoding="utf-8", xml_declaration=True)) | |||
</source> | </source> |
Revision as of 17:41, 11 December 2011
MIPSUM, an Inkscape "lorem ipsum" plugin for learning while practicing
WHAT?
A plugin that generates place-holder text randomly taken from the FLOSS Manual for Inkscape.
HOW?
1. Install the plugin putting the following files in your Inkscape extension folder ($HOME/.config/inkscape/extensions/ or /usr/share/inkscape/extensions):
file file
2. Create a textbox (or more) and write the word "mipsum" followed (without any space) by the number of characters you want to generate.
3. In your Inkscape, go to Extensions>Python>Mipsum. It will generate text taken from the manual for Inkscape.
4. Learn new tricks and techniques.
CODE
helloworld.inx describes your extensions to Inkscape.
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>Mipsum</_name>
<id>pzi.helloworld</id>
<dependency type="executable" location="extensions">mipsum.py</dependency>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu _name="Python"/>
</effects-menu>
</effect>
<script>
<command reldir="extensions" interpreter="python">mipsum.py</command>
</script>
</inkscape-extension>
mipsum.py is the actual extension.
import os
import random
import sys, codecs
from lxml import etree
f = codecs.open(sys.argv[-1], encoding="utf-8")
# open the file and parse into an etree file
t = etree.parse(f)
# modify the tree
NS = {"svg": "http://www.w3.org/2000/svg"}
# get the inkscape manual from flossmanuals.net
lynxcmd = "lynx -dump -nolist http://en.flossmanuals.net/inkscape/_all/ | tr '\n' ' ' | sed 's/ */\ /g'"
data = os.popen(lynxcmd).read()
for flowPara in t.xpath("//svg:flowPara", namespaces=NS):
if flowPara.text[0:6] == "mipsum" :
# get the number of charachters to generate
charnum = int(flowPara.text[6:])
# takes a random part of the text inside the manual
charnumend = random.randint(0,239605)
charnumbegin = charnumend - charnum
flowPara.text = data[charnumbegin:charnumend]
# output tree
sys.stdout.write(etree.tostring(t, encoding="utf-8", xml_declaration=True))