Difference between revisions of "InkSlide"
Terry brown (talk | contribs) m (→Text Input) |
m |
||
(11 intermediate revisions by one other user not shown) | |||
Line 8: | Line 8: | ||
<pre> | <pre> | ||
InkSlide: Features | |||
++++++++++++++++++ | |||
Features include wrapped top level text and | Features include wrapped top level text and | ||
- mulitple | |||
- levels | |||
- of wrapped bulleted lists with bullets and font | |||
information taken from the template file. | information taken from the template file. | ||
Slide specific content like this: | Slide specific content like this: | ||
Line 30: | Line 32: | ||
Here is an [[Media:template0.svg|example template]]. Use Save Link As to stop your browser displaying the file instead of downloading it. | Here is an [[Media:template0.svg|example template]]. Use Save Link As to stop your browser displaying the file instead of downloading it. | ||
=== How it works === | === How it works === | ||
Line 47: | Line 39: | ||
InkSlide saves the file, and uses Inkscape's command line ''--query-height'' and friends to find out how high the wrapped text pieces are. At it then repositions them properly, adds clones of the bullet markers, etc. Bullet markers have special @id attributes like ''bu_1'' for the first level of bulleted list. | InkSlide saves the file, and uses Inkscape's command line ''--query-height'' and friends to find out how high the wrapped text pieces are. At it then repositions them properly, adds clones of the bullet markers, etc. Bullet markers have special @id attributes like ''bu_1'' for the first level of bulleted list. | ||
Finally, InkSlide checks to see if there's an | Finally, InkSlide checks to see if there's an id tag for the slide, and if so if there's a corresponding Inkscape SVG file with content for this particular slide. This content has to occur in layers whose name starts with ''Instance'', and these layers have to be present in the template file, but you can have more than one, so they can be above and below the regular content. If such content is found, it's copied into the output slide. | ||
=== The Template File === | === The Template File === | ||
Line 55: | Line 47: | ||
Required contents: | Required contents: | ||
*A layer called '' | *A group or layer called ''gr_text'', into which text will be placed. | ||
*A flowed text frame with an @id of ''fr_title'' to define the font and position for the slide title. This should contain text like ''Title goes here Sly'' because ''Sly'' includes letters which ascend and descend from the baseline to the greatest extent. The placeholder text must be '''only''' one line because it's used to calculate line height. | *A flowed text frame with an @id of ''fr_title'' to define the font and position for the slide title. This should contain text like ''Title goes here Sly'' because ''Sly'' includes letters which ascend and descend from the baseline to the greatest extent. The placeholder text must be '''only''' one line because it's used to calculate line height. | ||
*A group | *A group or layer called ''gr_title'' into which the title will be placed. This makes it easier to do special effects on the title like shadows. | ||
*Flowed text frames with an @id of ''fr_tabN'' for each N in 0, 1, 2, 3. ''fr_tab0'' is used for top level non bulleted text. These should also include ''Sly'' or similar. The placeholder text must be '''only''' one line because it's used to calculate line height. | *Flowed text frames with an @id of ''fr_tabN'' for each N in 0, 1, 2, 3. ''fr_tab0'' is used for top level non bulleted text. These should also include ''Sly'' or similar. The placeholder text must be '''only''' one line because it's used to calculate line height. | ||
*Objects or groups withan @id of ''bu_N'' for each N in 1, 2, 3, for the three levels of bulleted list. | *Objects or groups withan @id of ''bu_N'' for each N in 1, 2, 3, for the three levels of bulleted list. | ||
Line 63: | Line 55: | ||
The fr_* elements define the font, font-size, color etc. for the different levels of text. They also define the position and wrap width. See the example template file for a possible arrangement. | The fr_* elements define the font, font-size, color etc. for the different levels of text. They also define the position and wrap width. See the example template file for a possible arrangement. | ||
"A group or layer called 'X'" indicates either a group with an '''@id''' | |||
of 'X' ''or'' a layer with a '''label''' (name) of 'X'. When both exist the group will take precedence. | |||
Optional contents: | Optional contents: | ||
*A background | *A background, which should be placed in a group or layer called `gr_bg` | ||
*Affiliation text and logos you want to appear on every slide | *A foreground, which should be placed in a group or layer called `gr_fg` | ||
*A blurred clone of the gr_title element, offset and behind it. | *Affiliation text and logos you want to appear on every slide, this could go in the background, or another named group or layer. | ||
*A blurred clone of the gr_title element, offset and behind it. It's easier to do this is `gr_title` is a group rather than a layer. | |||
*Layers whose name starts with ''Instance'' - these are the layers the user can edit directly in Inkscape for a particular slide. | *Layers whose name starts with ''Instance'' - these are the layers the user can edit directly in Inkscape for a particular slide. | ||
When you save the template file you should make sure that layers containing place holder text (the fr_* elements) and bullets are invisible | When you save the template file you should make sure that layers containing place holder text (the fr_* elements) and bullets are invisible. Likewise the Texts layer should be visible, as well as any Instance layers. | ||
=== Text Input === | |||
InkSlide uses [http://docutils.sourceforge.net/rst.html reStructuredText] for markup, although it renders only a subset of rst. | |||
<pre> | <pre> | ||
Inkslide: an example | |||
++++++++++++++++++++ | |||
This text will appear as a top level paragraph. | This text will appear as a top level paragraph. | ||
- Dash prefix indicates this text will appear as a bulleted item. | |||
Two slides will be produced for this input, one with everything up to the | - Indented dash prefix -> second level bulleted list. | ||
and one with that plus the remaining content. | |||
.. is:pause | |||
Two slides will be produced for this input, one with everything up to the | |||
".. is:pause", and one with that plus the remaining content. | |||
Incremental list display can be done this way. | Incremental list display can be done this way. | ||
The | The is:id tag below indicates that a file with a name like | ||
be inspected for extra content for this particular slide. | 'extra/is_intro.svg' should be inspected for extra content | ||
for this particular slide. | |||
.. is:id is_intro | |||
Inkslide: the next slide... | |||
+++++++++++++++++++++++++++ | |||
</pre> | </pre> | ||
Line 100: | Line 105: | ||
=== To do / status === | === To do / status === | ||
InkSlide is very usable, but is missing some features | InkSlide is very usable, but is missing some features. Remember you can | ||
use the slide specific editing feature to do any of these things and | |||
anything else you can do in Inkscape already, these will just be more | |||
convenient in the text input when they're done. | |||
*Inline text formatting (bold, color, etc.) for part of a piece of text | *Inline text formatting (bold, color, etc.) for part of a piece of text | ||
*Justification switching | *Justification switching | ||
*Verbatim code display | *Verbatim code display | ||
*Simple image inclusion | *<strike>Simple image inclusion</strike> | ||
*more helper tools to make PNG and PDF versions of slides | |||
*more helper | |||
*configuring / using a directory for edited slides | *configuring / using a directory for edited slides | ||
*command line flags for generating specific slides | *command line flags for generating specific slides | ||
Line 122: | Line 129: | ||
InkSlide was inspired by [http://member.wide.ad.jp/wg/mgp/ MagicPoint] | InkSlide was inspired by [http://member.wide.ad.jp/wg/mgp/ MagicPoint] | ||
Matt Harrison suggested using reStructured text as the markup language. | |||
=== Code === | === Code === | ||
<pre> | <pre> | ||
""" | |||
""" | |||
# inkslide.py $Id$ | # inkslide.py $Id$ | ||
# Author: Terry Brown | # Author: Terry Brown | ||
Line 135: | Line 147: | ||
import subprocess | import subprocess | ||
import os.path | import os.path | ||
from docutils.core import publish_string | |||
import sys # for debug | |||
class inkSlide(object): | |||
def __init__(self): | |||
self.NS = { 'i': 'http://www.inkscape.org/namespaces/inkscape', | |||
's': 'http://www.w3.org/2000/svg', | |||
'xlink' : 'http://www.w3.org/1999/xlink'} | |||
# number of defined tablevels, FIXME, could derive from template? | |||
self.tabLevels = 4 | |||
self._nextId = 0 | |||
self.gap = { | |||
'bIMAGE': 15, # gap before image | |||
'aIMAGE': 15, # gap after image | |||
'aTAB0': 10, # gap after top level text | |||
'aTAB1': 10, # gap after level 1 bullet | |||
'aTAB2': 10, # gap after level 2 bullet | |||
'aTAB3': 10, # gap after level 3 bullet | |||
'fVCENTER': 1, # flag - center vertically | |||
'dHEIGHT': 728, # dimension, bottom of page | |||
'fHCENTER': 1, # flag - center vertically | |||
'dWIDTH': 1024, # dimension, bottom of page | |||
} | |||
def | self.dimCache = {} | ||
self.is_reads, self.is_cache = 0, 0 | |||
def __del__(self): | |||
print '%d reads, %d cache hits' % (self.is_reads, self.is_cache) | |||
def nextId(self): | |||
"""return an unsed Id""" | |||
# FIXME, should inspect template doc. | |||
self._nextId += 1 | |||
return 'is'+str(self._nextId) | |||
def clearId(self, x, what='id', NS={}): | |||
"""recursively clear @id on element x and descendants""" | |||
if not NS: NS = self.NS | |||
if what in x.keys(): | |||
del x.attrib[what] | |||
for i in x.xpath('.//*[@%s]'%what, NS): | |||
del i.attrib[what] | |||
return x | |||
def getDim(self, fn, Id, what): | |||
"""return dimension of element in fn with @id Id, what is | |||
x, y, width, height | |||
""" | |||
hlist = [] | hsh = fn+Id+what | ||
if hsh in self.dimCache: | |||
self.is_cache += 1 | |||
return self.dimCache[hsh] | |||
cmd = ('inkscape', '--without-gui', '--query-id', Id, '--query-'+what, fn) | |||
proc = subprocess.Popen(cmd, stdout = subprocess.PIPE, | |||
stderr = subprocess.PIPE) | |||
# make new pipe for stderr to supress chatter from inkscape | |||
proc.wait() | |||
self.dimCache[hsh] = proc.stdout.read() | |||
if self.dimCache[hsh].strip() == '': | |||
print "Warning: '%s' not found" % Id | |||
self.is_reads += 1 | |||
return self.dimCache[hsh] | |||
def textReader(self, text): | |||
slideId = | """NOT USED iterate parts in input text""" | ||
if not text: return | |||
for line in text.split('\n'): | |||
if line.startswith('%slide '): | |||
yield {'type':'TITLE', 's':line[7:].strip()} | |||
continue | |||
if line.startswith('%pause'): | |||
yield {'type':'PAUSE'} | |||
continue | |||
if line.startswith('%id '): | |||
yield {'type':'ID', 's':line[4:].strip()} | |||
continue | |||
if not line.strip(): | |||
yield {'type':'BLANK'} | |||
continue | |||
tabs = len(line) - len(line.lstrip('\t')) | |||
yield {'type':'TAB', 'tabs':tabs, 's':line.strip()} | |||
def textReaderRst(self, text): | |||
xml = publish_string(text.replace('\t',' '), writer_name = 'xml') | |||
doc = ET.fromstring(xml) | |||
# yield {'type':'TITLE', 's':doc.xpath('/document/@title')[0]} | |||
nodes = 'paragraph', 'comment', 'title', 'image' | |||
for i in doc.xpath('//%s' % '|//'.join(nodes)): | |||
if i.tag == 'title': | |||
yield {'type':'TITLE', 's':i.text} | |||
if i.tag == 'paragraph': | |||
tabs = len(i.xpath('ancestor::list_item')) | |||
yield {'type':'TAB', 'tabs':tabs, 's':i.text, 'e':i} | |||
if i.tag == 'image': | |||
tabs = len(i.xpath('ancestor::list_item')) | |||
yield {'type':'IMAGE', 'uri':i.get('uri')} | |||
if i.tag == 'comment': | |||
comparts = i.text.split(None, 1) | |||
if len(comparts) > 1: | |||
ctag, cval = comparts | |||
elif len(comparts) == 1: | |||
ctag = comparts[0] | |||
else: | |||
continue # empty comment had no meaning to InkView | |||
if ctag == 'is:id': | |||
try: | |||
yield {'type':'ID', 's':cval} | |||
except: | |||
raise Exception('"id" comment requires a value') | |||
elif ctag == 'is:notitle': | |||
yield {'type':'DROP', 'what':('gr_title',)} | |||
elif ctag == 'is:notext': | |||
yield {'type':'DROP', 'what':('gr_text',)} | |||
elif ctag == 'is:nobackground': | |||
yield {'type':'DROP', 'what':('gr_bg',)} | |||
elif ctag == 'is:noforeground': | |||
yield {'type':'DROP', 'what':('gr_fg',)} | |||
elif ctag == 'is:blank': | |||
yield {'type':'DROP', 'what':('gr_title','gr_text')} | |||
elif ctag == 'is:empty': | |||
yield {'type':'DROP', 'what':('gr_bg','gr_title','gr_text','gr_fg')} | |||
elif ctag == 'is:no': | |||
try: | |||
yield {'type':'DROP', 'what':(cval,)} | |||
except: | |||
raise Exception('"no" comment requires a value') | |||
if ctag == 'is:dim': | |||
dim, val = cval.strip().split() | |||
yield {'type':'DIM', 'dim':dim, 'val':val} | |||
def groupOrLayer(self, g): | |||
dst = self.doc.xpath('//s:g[@id="%s"]'%g, self.NS) | |||
if not dst: dst = self.doc.xpath('//s:g[@i:label="%s"]'%g, self.NS) | |||
if not dst: return dst # let the caller test, don't raise exception here | |||
dst = dst[0] | |||
return dst | |||
def instanceFromTemplate(self, template, instance, text, pauseOk = True): | |||
slide = 0 | |||
hlist = [] | |||
self.doc = ET.parse(template) | |||
slideId = None | |||
for t in self.textReaderRst(text): | |||
if t['type'] == 'ID': | |||
slideId = t['s'] | |||
continue | |||
if t['type'] == 'TITLE': | |||
if hlist: | |||
self.process(instance, hlist, slideId, slide) | |||
slide = slide + 1 | |||
hlist = [t] | |||
self.doc = ET.parse(template) | |||
slideId = None | |||
else: | |||
hlist.append(t) | |||
print t['s'] | |||
dst = self.groupOrLayer('gr_title') | |||
src = self.doc.xpath('//s:flowRoot[@id="fr_title"]', self.NS)[0] | |||
cpy = self.clearId(copy.deepcopy(src)) | |||
Id = self.nextId() | |||
cpy.set('id', Id) | |||
hlist[-1]['id']=Id | |||
cpy.xpath('s:flowPara',self.NS)[0].text = t['s'] | |||
dst.append(cpy) | |||
continue | |||
if t['type'] == 'PAUSE': | |||
if pauseOk: | |||
self.process(instance, hlist, slideId, slide) | |||
slide = slide + 1 | |||
continue | |||
if t['type'] == 'BLANK': | |||
hlist.append(t) | hlist.append(t) | ||
continue | |||
if t['type'] == 'TAB': | |||
hlist.append(t) | |||
dst = self.groupOrLayer('gr_text') | |||
src = self.doc.xpath('//s:flowRoot[@id="fr_tab%d"]' % t['tabs'], self.NS)[0] | |||
cpy = self.clearId(copy.deepcopy(src)) | |||
Id = self.nextId() | |||
cpy.set('id', Id) | |||
hlist[-1]['id']=Id | |||
# cpy.xpath('s:flowPara',self.NS)[0].text = t['s'] | |||
self.flowParaFromElement( | |||
cpy.xpath('s:flowPara',self.NS)[0], t['e']) | |||
dst.append(cpy) | |||
continue | |||
if t['type'] == 'IMAGE': | |||
hlist.append(t) | |||
dst = self.groupOrLayer('gr_image') | |||
if not dst: | |||
dst = self.groupOrLayer('gr_text') | |||
img = ET.Element('image') | |||
img.set('{%s}href'%self.NS['xlink'], t['uri']) | |||
Id = self.nextId() | |||
img.set('id', Id) | |||
hlist[-1]['id']=Id | |||
dst.append(img) | |||
continue | |||
hlist.append(t) # whatever it was | |||
hlist | |||
self.process(instance, hlist, slideId, slide) | |||
def flowParaFromElement(self, fp, e): | |||
fp.text = e.xpath('string(.)') | |||
def process(self, instance, hlist, slideId, slide): | |||
instanceName = instance.replace('%s', '%04d'%slide) | |||
instance = 'tmp.svg' # reusing allows caching in getDim, but relies on nextId ensuring | |||
# that the changing parts have new ids | |||
self.doc.write(file(instance, 'w')) | |||
drop = [] | |||
bullDim = {} | |||
tabDim = {} | |||
for n in range(0,self.tabLevels): | |||
b = {} | |||
x = self.doc.xpath('//*[@id="bu_%d"]'%n, self.NS) | |||
if x: | |||
# self.clearId(x[0], what='transform') | |||
for i in 'x', 'y', 'width', 'height': | |||
b[i] = float(self.getDim(instance, 'bu_%d'%n, i)) | |||
bullDim[n] = b | |||
b = {} | |||
x = self.doc.xpath('//*[@id="fr_tab%d"]'%n, self.NS) | |||
if x: | |||
# clearId(x[0], what='transform') | |||
for i in 'x', 'y', 'width', 'height': | |||
b[i] = float(self.getDim(instance, 'fr_tab%d'%n, i)) | |||
x = x[0].xpath('.//s:rect',self.NS)[0] | |||
b['max-width'] = float(x.get('width')) | |||
b['max-height'] = float(x.get('height')) | |||
tabDim[n] = b | |||
delta = 0. | |||
firstVert = True | |||
placed_xy = [] | |||
placed_transform = [] | |||
for n, i in enumerate(hlist): | |||
this = hlist[n] | |||
prev = hlist[n-1] | |||
if this['type'] == 'DROP': drop += this['what'] | |||
if this['type'] == 'DIM': | |||
if this['dim'] not in self.gap or this['val'][0] not in '+-': | |||
self.gap[this['dim']] = float(this['val']) | |||
else: | |||
self.gap[this['dim']] += float(this['val']) | |||
zeroHeight = ['TITLE', 'DROP', 'DIM'] | |||
# don't do this - need to add height from prev to delta | |||
#if this['type'] in zeroHeight: | |||
# continue # already in right place, not a vertical element | |||
hgt = 0 | hgt = 0 | ||
firstVert = False | if prev['type'] == 'BLANK': | ||
hgt = 30 | |||
elif prev['type'] not in zeroHeight: | |||
hgt = float(self.getDim(instance, prev['id'], 'height')) | |||
# risky gambit to space bullets properly when some lines | |||
# have no ascenders etc. | |||
if prev['type'] == 'TAB' and prev['tabs'] > 0: | |||
lnh = tabDim[prev['tabs']]['height'] | |||
hgt = lnh * int(hgt/lnh+0.5) | |||
# TAB0, TAB1, TITLE, IMAGE, etc. | |||
# gap after previous | |||
hsh = 'a' + prev['type'] + str(prev.get('tabs','')) | |||
hgt += self.gap.get(hsh, 0) | |||
# gap before current | |||
hsh = 'b' + this['type'] + str(this.get('tabs','')) | |||
hgt += self.gap.get(hsh, 0) | |||
if firstVert: | |||
hgt = 0 | |||
firstVert = False | |||
delta += hgt | |||
if this['type'] == 'BLANK': continue | |||
if this['type'] == 'TAB': | |||
tabs = this['tabs'] | |||
r = self.doc.xpath('//s:flowRoot[@id="%s"]' % this['id'], self.NS)[0] | |||
self.clearId(r, what='transform') | |||
r = r.xpath('.//s:rect', self.NS)[0] | |||
r.set('x', str(tabDim[tabs]['x'])) | |||
y = tabDim[tabs]['y'] + delta | |||
#print '%s -> %s' % (r.get('y'), y) | |||
#print tabDim[tabs]['y'] | |||
r.set('y', str(y)) | |||
placed_xy.append([r, this['id']]) | |||
if (this['type'] == 'TAB' and this['tabs'] in bullDim | |||
and 'isDone' not in this): | |||
this['isDone'] = True | |||
dx = (tabDim[tabs]['x'] | |||
- bullDim[tabs]['x'] | |||
- 1.5*bullDim[tabs]['width'] | |||
) | |||
dy = (y | |||
+ tabDim[tabs]['height'] / 2. | |||
- bullDim[tabs]['y'] | |||
- bullDim[tabs]['height'] / 2. | |||
) | |||
# dst = doc0.xpath('//s:g[@i:label="Texts"]', NS)[0] | |||
dst = self.groupOrLayer('gr_text') | |||
#print y, tabDim[tabs]['height'], bullDim[tabs]['y'], bullDim[tabs]['height'], dy | |||
clone = ET.Element('use') | |||
clone.set('transform', 'translate(%f,%f)' % (dx,dy)) | |||
clone.set('{%s}href'%self.NS['xlink'], '#bu_%d'%tabs) | |||
placed_transform.append([clone, dx, dy]) | |||
dst.append(clone) | |||
if this['type'] == 'IMAGE': | |||
xpath = '//s:image[@id="%s"]' % this['id'] # doesn't work? | |||
xpath = '//*[@id="%s"]' % this['id'] | |||
i = self.doc.xpath(xpath, self.NS)[0] | |||
def instancePath(slideId | w = float(self.getDim(instance, this['id'], 'width')) | ||
x = tabDim[0]['x']+tabDim[0]['max-width']/2-w/2 | |||
i.set('x', str(x)) | |||
y = tabDim[0]['y'] + delta | |||
def writeComponents(inkscapeFile, slideId): | i.set('y', str(y)) | ||
placed_xy.append([i,this['id']]) | |||
if self.gap['fVCENTER']: | |||
if 'id' in this: | |||
delta += float(self.getDim(instance, this['id'], 'height')) | |||
recenter = (self.gap['dHEIGHT'] - delta - tabDim[0]['y']) * 0.45 | |||
for i in placed_transform: | |||
i[0].set('transform', 'translate(%f,%f)'%(i[1],i[2]+recenter)) | |||
i[2] += recenter # so it's not lost below | |||
for i in placed_xy: | |||
i[0].set('y', str(float(i[0].get('y'))+recenter)) | |||
if self.gap['fHCENTER']: | |||
maxX = 0 | |||
for i in placed_xy: | |||
x = float(self.getDim(instance, i[1], 'x')) | |||
x += float(self.getDim(instance, i[1], 'width')) | |||
if x > maxX: maxX = x | |||
recenter = (self.gap['dWIDTH'] - maxX - tabDim[0]['x']) * 0.5 | |||
for i in placed_transform: | |||
i[0].set('transform', 'translate(%f,%f)'%(i[1]+recenter,i[2])) | |||
i[1] += recenter # in case it's used again | |||
for i in placed_xy: | |||
if i[0].tag != 'image': # images already centered | |||
# print i[0].tag | |||
i[0].set('x', str(float(i[0].get('x'))+recenter)) | |||
# look for instance specific parts | |||
if slideId: | |||
f = self.instancePath(slideId) | |||
if os.path.isfile(f): | |||
comp = ET.parse(f) | |||
svg = self.doc.xpath('//s:svg', self.NS)[0] | |||
g = "{%s}g"%self.NS['s'] | |||
k = "{%s}label"%self.NS['i'] | |||
for n, i in enumerate(svg.getchildren()): | |||
if i.tag == g and i.get(k, '').startswith('Instance'): | |||
x = comp.xpath('//s:svg//s:g[@i:label="%s"]'%i.get(k), self.NS) | |||
if x: | |||
x = x[0] | |||
cpy = self.clearId(copy.deepcopy(x)) | |||
# cpy = self.textCopy(x) | |||
svg[n] = cpy | |||
defs = comp.xpath('//s:svg/s:defs/*', self.NS) | |||
dst = self.doc.xpath('//s:svg/s:defs', self.NS)[0] | |||
for d in defs: | |||
Id = d.get('id') | |||
current = self.doc.xpath('//s:svg/s:defs/*[@id="%s"]'%Id, self.NS) | |||
if not current: | |||
cpy = copy.deepcopy(d) | |||
dst.append(cpy) | |||
for i in drop: | |||
e = self.groupOrLayer(i) | |||
if e: | |||
e.xpath('..')[0].remove(e) | |||
# problem losing xlink ns? | |||
for i in self.doc.xpath('//*[@href]'): | |||
i.set('{%s}href'%self.NS['xlink'], i.get('href')) | |||
self.doc.write(file(instanceName, 'w')) | |||
return slideId | |||
def textCopy(self, ele): | |||
"""copy the element ele via xml->text->xml, as deepcopy seems to lose text""" | |||
txt = ET.tostring(ele) | |||
# print txt | |||
return ET.fromstring(txt) | |||
def instancePath(self, slideId): | |||
"""return path to component for slide""" | |||
# return os.path.join(slideId, component) | |||
return slideId+'.svg' | |||
def writeComponents(self, inkscapeFile, slideId): | |||
"""write components from file for slide""" | |||
if not slideId: return | |||
doc0 = ET.parse(inkscapeFile) | |||
hasComponents = False | |||
for i in doc0.xpath('//s:svg//s:g', self.NS): | |||
k = "{%s}label"%self.NS['i'] | |||
# print i.keys(),k | |||
if (k in i.keys() and i.get(k).startswith('Instance')): | |||
if len(i) > 0: | |||
hasComponents = True | |||
break | |||
# ET.ElementTree(i).write(f) | |||
f = self.instancePath(slideId) | |||
if hasComponents: | |||
if (os.path.abspath(os.path.realpath(inkscapeFile)) != | |||
os.path.abspath(os.path.realpath(f))): | |||
file(f, 'w').write(file(inkscapeFile).read()) | |||
else: | |||
if os.path.isfile(f): | |||
os.remove(f) | |||
if __name__ == '__main__': | if __name__ == '__main__': | ||
import sys | import sys | ||
instanceFromTemplate(sys.argv[1], 'slide%s.svg', file(sys.argv[2]).read()) | x = inkSlide() | ||
x.instanceFromTemplate(sys.argv[1], 'slide%s.svg', file(sys.argv[2]).read()) | |||
</pre> | </pre> |
Latest revision as of 00:19, 14 December 2010
InkSlide - quick and easy presentations using Inkscape
InkSlide produces slides like this:
from simple text input like this:
InkSlide: Features ++++++++++++++++++ Features include wrapped top level text and - mulitple - levels - of wrapped bulleted lists with bullets and font information taken from the template file. Slide specific content like this: which is updated when the template changes.
An Inkscape file is used as a template file to define the background, title position and font, fonts and positions for text at different levels of indentation, groups to be cloned and used as bullets, etc.
Content specific to a particular slide can also be created in Inkscape, this content merged with the template and text input to make the final slide, so changes to the template after a particular slide is edited in Inkscape are included.
Download
Copy the text below into a file called 'inkslide.py'. (How do you attach a non-image file on this wiki?)
Here is an example template. Use Save Link As to stop your browser displaying the file instead of downloading it.
How it works
First InkSlide parses the text input, something like the example at the top of this page. It looks for flowed text boxes in the template file with special @id attributes like fr_title for the title, and fr_tab2 for the second level of bulleted lists. Copies of these elements are made and placed in a layer called 'Texts'. At this point, the text is all piled up at the top of the page.
InkSlide saves the file, and uses Inkscape's command line --query-height and friends to find out how high the wrapped text pieces are. At it then repositions them properly, adds clones of the bullet markers, etc. Bullet markers have special @id attributes like bu_1 for the first level of bulleted list.
Finally, InkSlide checks to see if there's an id tag for the slide, and if so if there's a corresponding Inkscape SVG file with content for this particular slide. This content has to occur in layers whose name starts with Instance, and these layers have to be present in the template file, but you can have more than one, so they can be above and below the regular content. If such content is found, it's copied into the output slide.
The Template File
The template file is a regular Inkscape file. If this list seems hard, just use the example template and modify it to suit your needs.
Required contents:
- A group or layer called gr_text, into which text will be placed.
- A flowed text frame with an @id of fr_title to define the font and position for the slide title. This should contain text like Title goes here Sly because Sly includes letters which ascend and descend from the baseline to the greatest extent. The placeholder text must be only one line because it's used to calculate line height.
- A group or layer called gr_title into which the title will be placed. This makes it easier to do special effects on the title like shadows.
- Flowed text frames with an @id of fr_tabN for each N in 0, 1, 2, 3. fr_tab0 is used for top level non bulleted text. These should also include Sly or similar. The placeholder text must be only one line because it's used to calculate line height.
- Objects or groups withan @id of bu_N for each N in 1, 2, 3, for the three levels of bulleted list.
The fr_* elements define the font, font-size, color etc. for the different levels of text. They also define the position and wrap width. See the example template file for a possible arrangement.
"A group or layer called 'X'" indicates either a group with an @id of 'X' or a layer with a label (name) of 'X'. When both exist the group will take precedence.
Optional contents:
- A background, which should be placed in a group or layer called `gr_bg`
- A foreground, which should be placed in a group or layer called `gr_fg`
- Affiliation text and logos you want to appear on every slide, this could go in the background, or another named group or layer.
- A blurred clone of the gr_title element, offset and behind it. It's easier to do this is `gr_title` is a group rather than a layer.
- Layers whose name starts with Instance - these are the layers the user can edit directly in Inkscape for a particular slide.
When you save the template file you should make sure that layers containing place holder text (the fr_* elements) and bullets are invisible. Likewise the Texts layer should be visible, as well as any Instance layers.
Text Input
InkSlide uses reStructuredText for markup, although it renders only a subset of rst.
Inkslide: an example ++++++++++++++++++++ This text will appear as a top level paragraph. - Dash prefix indicates this text will appear as a bulleted item. - Indented dash prefix -> second level bulleted list. .. is:pause Two slides will be produced for this input, one with everything up to the ".. is:pause", and one with that plus the remaining content. Incremental list display can be done this way. The is:id tag below indicates that a file with a name like 'extra/is_intro.svg' should be inspected for extra content for this particular slide. .. is:id is_intro Inkslide: the next slide... +++++++++++++++++++++++++++
The GUI
A GUI for InkSlide is available as a plug-in for the Leo outline editor. This makes slide re-arranging easy, and adds some macro substitution capability. It also adds buttons for editing slide specific content in Inkscape.
FIXME: post plug-in on Leo wiki and link.
To do / status
InkSlide is very usable, but is missing some features. Remember you can use the slide specific editing feature to do any of these things and anything else you can do in Inkscape already, these will just be more convenient in the text input when they're done.
- Inline text formatting (bold, color, etc.) for part of a piece of text
- Justification switching
- Verbatim code display
Simple image inclusion- more helper tools to make PNG and PDF versions of slides
- configuring / using a directory for edited slides
- command line flags for generating specific slides
Currently InkSlide is slow, because it must invoke Inkscape once for every single dimension it needs to retrieve. I'll try and get a --query-all switch into 0.46.
Bugs
- There may be an issue with the way slide specific content is merged into the template producing conflicting @id attributes, but I haven't seen this happen yet.
Author / Credits
InkSlide was written by Terry Brown, terry-n-brown@yahoo.com - but use underscore, not '-'.
InkSlide was inspired by MagicPoint
Matt Harrison suggested using reStructured text as the markup language.
Code
""" """ # inkslide.py $Id$ # Author: Terry Brown # Created: Thu Oct 11 2007 import lxml.etree as ET import copy import subprocess import os.path from docutils.core import publish_string import sys # for debug class inkSlide(object): def __init__(self): self.NS = { 'i': 'http://www.inkscape.org/namespaces/inkscape', 's': 'http://www.w3.org/2000/svg', 'xlink' : 'http://www.w3.org/1999/xlink'} # number of defined tablevels, FIXME, could derive from template? self.tabLevels = 4 self._nextId = 0 self.gap = { 'bIMAGE': 15, # gap before image 'aIMAGE': 15, # gap after image 'aTAB0': 10, # gap after top level text 'aTAB1': 10, # gap after level 1 bullet 'aTAB2': 10, # gap after level 2 bullet 'aTAB3': 10, # gap after level 3 bullet 'fVCENTER': 1, # flag - center vertically 'dHEIGHT': 728, # dimension, bottom of page 'fHCENTER': 1, # flag - center vertically 'dWIDTH': 1024, # dimension, bottom of page } self.dimCache = {} self.is_reads, self.is_cache = 0, 0 def __del__(self): print '%d reads, %d cache hits' % (self.is_reads, self.is_cache) def nextId(self): """return an unsed Id""" # FIXME, should inspect template doc. self._nextId += 1 return 'is'+str(self._nextId) def clearId(self, x, what='id', NS={}): """recursively clear @id on element x and descendants""" if not NS: NS = self.NS if what in x.keys(): del x.attrib[what] for i in x.xpath('.//*[@%s]'%what, NS): del i.attrib[what] return x def getDim(self, fn, Id, what): """return dimension of element in fn with @id Id, what is x, y, width, height """ hsh = fn+Id+what if hsh in self.dimCache: self.is_cache += 1 return self.dimCache[hsh] cmd = ('inkscape', '--without-gui', '--query-id', Id, '--query-'+what, fn) proc = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE) # make new pipe for stderr to supress chatter from inkscape proc.wait() self.dimCache[hsh] = proc.stdout.read() if self.dimCache[hsh].strip() == '': print "Warning: '%s' not found" % Id self.is_reads += 1 return self.dimCache[hsh] def textReader(self, text): """NOT USED iterate parts in input text""" if not text: return for line in text.split('\n'): if line.startswith('%slide '): yield {'type':'TITLE', 's':line[7:].strip()} continue if line.startswith('%pause'): yield {'type':'PAUSE'} continue if line.startswith('%id '): yield {'type':'ID', 's':line[4:].strip()} continue if not line.strip(): yield {'type':'BLANK'} continue tabs = len(line) - len(line.lstrip('\t')) yield {'type':'TAB', 'tabs':tabs, 's':line.strip()} def textReaderRst(self, text): xml = publish_string(text.replace('\t',' '), writer_name = 'xml') doc = ET.fromstring(xml) # yield {'type':'TITLE', 's':doc.xpath('/document/@title')[0]} nodes = 'paragraph', 'comment', 'title', 'image' for i in doc.xpath('//%s' % '|//'.join(nodes)): if i.tag == 'title': yield {'type':'TITLE', 's':i.text} if i.tag == 'paragraph': tabs = len(i.xpath('ancestor::list_item')) yield {'type':'TAB', 'tabs':tabs, 's':i.text, 'e':i} if i.tag == 'image': tabs = len(i.xpath('ancestor::list_item')) yield {'type':'IMAGE', 'uri':i.get('uri')} if i.tag == 'comment': comparts = i.text.split(None, 1) if len(comparts) > 1: ctag, cval = comparts elif len(comparts) == 1: ctag = comparts[0] else: continue # empty comment had no meaning to InkView if ctag == 'is:id': try: yield {'type':'ID', 's':cval} except: raise Exception('"id" comment requires a value') elif ctag == 'is:notitle': yield {'type':'DROP', 'what':('gr_title',)} elif ctag == 'is:notext': yield {'type':'DROP', 'what':('gr_text',)} elif ctag == 'is:nobackground': yield {'type':'DROP', 'what':('gr_bg',)} elif ctag == 'is:noforeground': yield {'type':'DROP', 'what':('gr_fg',)} elif ctag == 'is:blank': yield {'type':'DROP', 'what':('gr_title','gr_text')} elif ctag == 'is:empty': yield {'type':'DROP', 'what':('gr_bg','gr_title','gr_text','gr_fg')} elif ctag == 'is:no': try: yield {'type':'DROP', 'what':(cval,)} except: raise Exception('"no" comment requires a value') if ctag == 'is:dim': dim, val = cval.strip().split() yield {'type':'DIM', 'dim':dim, 'val':val} def groupOrLayer(self, g): dst = self.doc.xpath('//s:g[@id="%s"]'%g, self.NS) if not dst: dst = self.doc.xpath('//s:g[@i:label="%s"]'%g, self.NS) if not dst: return dst # let the caller test, don't raise exception here dst = dst[0] return dst def instanceFromTemplate(self, template, instance, text, pauseOk = True): slide = 0 hlist = [] self.doc = ET.parse(template) slideId = None for t in self.textReaderRst(text): if t['type'] == 'ID': slideId = t['s'] continue if t['type'] == 'TITLE': if hlist: self.process(instance, hlist, slideId, slide) slide = slide + 1 hlist = [t] self.doc = ET.parse(template) slideId = None else: hlist.append(t) print t['s'] dst = self.groupOrLayer('gr_title') src = self.doc.xpath('//s:flowRoot[@id="fr_title"]', self.NS)[0] cpy = self.clearId(copy.deepcopy(src)) Id = self.nextId() cpy.set('id', Id) hlist[-1]['id']=Id cpy.xpath('s:flowPara',self.NS)[0].text = t['s'] dst.append(cpy) continue if t['type'] == 'PAUSE': if pauseOk: self.process(instance, hlist, slideId, slide) slide = slide + 1 continue if t['type'] == 'BLANK': hlist.append(t) continue if t['type'] == 'TAB': hlist.append(t) dst = self.groupOrLayer('gr_text') src = self.doc.xpath('//s:flowRoot[@id="fr_tab%d"]' % t['tabs'], self.NS)[0] cpy = self.clearId(copy.deepcopy(src)) Id = self.nextId() cpy.set('id', Id) hlist[-1]['id']=Id # cpy.xpath('s:flowPara',self.NS)[0].text = t['s'] self.flowParaFromElement( cpy.xpath('s:flowPara',self.NS)[0], t['e']) dst.append(cpy) continue if t['type'] == 'IMAGE': hlist.append(t) dst = self.groupOrLayer('gr_image') if not dst: dst = self.groupOrLayer('gr_text') img = ET.Element('image') img.set('{%s}href'%self.NS['xlink'], t['uri']) Id = self.nextId() img.set('id', Id) hlist[-1]['id']=Id dst.append(img) continue hlist.append(t) # whatever it was self.process(instance, hlist, slideId, slide) def flowParaFromElement(self, fp, e): fp.text = e.xpath('string(.)') def process(self, instance, hlist, slideId, slide): instanceName = instance.replace('%s', '%04d'%slide) instance = 'tmp.svg' # reusing allows caching in getDim, but relies on nextId ensuring # that the changing parts have new ids self.doc.write(file(instance, 'w')) drop = [] bullDim = {} tabDim = {} for n in range(0,self.tabLevels): b = {} x = self.doc.xpath('//*[@id="bu_%d"]'%n, self.NS) if x: # self.clearId(x[0], what='transform') for i in 'x', 'y', 'width', 'height': b[i] = float(self.getDim(instance, 'bu_%d'%n, i)) bullDim[n] = b b = {} x = self.doc.xpath('//*[@id="fr_tab%d"]'%n, self.NS) if x: # clearId(x[0], what='transform') for i in 'x', 'y', 'width', 'height': b[i] = float(self.getDim(instance, 'fr_tab%d'%n, i)) x = x[0].xpath('.//s:rect',self.NS)[0] b['max-width'] = float(x.get('width')) b['max-height'] = float(x.get('height')) tabDim[n] = b delta = 0. firstVert = True placed_xy = [] placed_transform = [] for n, i in enumerate(hlist): this = hlist[n] prev = hlist[n-1] if this['type'] == 'DROP': drop += this['what'] if this['type'] == 'DIM': if this['dim'] not in self.gap or this['val'][0] not in '+-': self.gap[this['dim']] = float(this['val']) else: self.gap[this['dim']] += float(this['val']) zeroHeight = ['TITLE', 'DROP', 'DIM'] # don't do this - need to add height from prev to delta #if this['type'] in zeroHeight: # continue # already in right place, not a vertical element hgt = 0 if prev['type'] == 'BLANK': hgt = 30 elif prev['type'] not in zeroHeight: hgt = float(self.getDim(instance, prev['id'], 'height')) # risky gambit to space bullets properly when some lines # have no ascenders etc. if prev['type'] == 'TAB' and prev['tabs'] > 0: lnh = tabDim[prev['tabs']]['height'] hgt = lnh * int(hgt/lnh+0.5) # TAB0, TAB1, TITLE, IMAGE, etc. # gap after previous hsh = 'a' + prev['type'] + str(prev.get('tabs','')) hgt += self.gap.get(hsh, 0) # gap before current hsh = 'b' + this['type'] + str(this.get('tabs','')) hgt += self.gap.get(hsh, 0) if firstVert: hgt = 0 firstVert = False delta += hgt if this['type'] == 'BLANK': continue if this['type'] == 'TAB': tabs = this['tabs'] r = self.doc.xpath('//s:flowRoot[@id="%s"]' % this['id'], self.NS)[0] self.clearId(r, what='transform') r = r.xpath('.//s:rect', self.NS)[0] r.set('x', str(tabDim[tabs]['x'])) y = tabDim[tabs]['y'] + delta #print '%s -> %s' % (r.get('y'), y) #print tabDim[tabs]['y'] r.set('y', str(y)) placed_xy.append([r, this['id']]) if (this['type'] == 'TAB' and this['tabs'] in bullDim and 'isDone' not in this): this['isDone'] = True dx = (tabDim[tabs]['x'] - bullDim[tabs]['x'] - 1.5*bullDim[tabs]['width'] ) dy = (y + tabDim[tabs]['height'] / 2. - bullDim[tabs]['y'] - bullDim[tabs]['height'] / 2. ) # dst = doc0.xpath('//s:g[@i:label="Texts"]', NS)[0] dst = self.groupOrLayer('gr_text') #print y, tabDim[tabs]['height'], bullDim[tabs]['y'], bullDim[tabs]['height'], dy clone = ET.Element('use') clone.set('transform', 'translate(%f,%f)' % (dx,dy)) clone.set('{%s}href'%self.NS['xlink'], '#bu_%d'%tabs) placed_transform.append([clone, dx, dy]) dst.append(clone) if this['type'] == 'IMAGE': xpath = '//s:image[@id="%s"]' % this['id'] # doesn't work? xpath = '//*[@id="%s"]' % this['id'] i = self.doc.xpath(xpath, self.NS)[0] w = float(self.getDim(instance, this['id'], 'width')) x = tabDim[0]['x']+tabDim[0]['max-width']/2-w/2 i.set('x', str(x)) y = tabDim[0]['y'] + delta i.set('y', str(y)) placed_xy.append([i,this['id']]) if self.gap['fVCENTER']: if 'id' in this: delta += float(self.getDim(instance, this['id'], 'height')) recenter = (self.gap['dHEIGHT'] - delta - tabDim[0]['y']) * 0.45 for i in placed_transform: i[0].set('transform', 'translate(%f,%f)'%(i[1],i[2]+recenter)) i[2] += recenter # so it's not lost below for i in placed_xy: i[0].set('y', str(float(i[0].get('y'))+recenter)) if self.gap['fHCENTER']: maxX = 0 for i in placed_xy: x = float(self.getDim(instance, i[1], 'x')) x += float(self.getDim(instance, i[1], 'width')) if x > maxX: maxX = x recenter = (self.gap['dWIDTH'] - maxX - tabDim[0]['x']) * 0.5 for i in placed_transform: i[0].set('transform', 'translate(%f,%f)'%(i[1]+recenter,i[2])) i[1] += recenter # in case it's used again for i in placed_xy: if i[0].tag != 'image': # images already centered # print i[0].tag i[0].set('x', str(float(i[0].get('x'))+recenter)) # look for instance specific parts if slideId: f = self.instancePath(slideId) if os.path.isfile(f): comp = ET.parse(f) svg = self.doc.xpath('//s:svg', self.NS)[0] g = "{%s}g"%self.NS['s'] k = "{%s}label"%self.NS['i'] for n, i in enumerate(svg.getchildren()): if i.tag == g and i.get(k, '').startswith('Instance'): x = comp.xpath('//s:svg//s:g[@i:label="%s"]'%i.get(k), self.NS) if x: x = x[0] cpy = self.clearId(copy.deepcopy(x)) # cpy = self.textCopy(x) svg[n] = cpy defs = comp.xpath('//s:svg/s:defs/*', self.NS) dst = self.doc.xpath('//s:svg/s:defs', self.NS)[0] for d in defs: Id = d.get('id') current = self.doc.xpath('//s:svg/s:defs/*[@id="%s"]'%Id, self.NS) if not current: cpy = copy.deepcopy(d) dst.append(cpy) for i in drop: e = self.groupOrLayer(i) if e: e.xpath('..')[0].remove(e) # problem losing xlink ns? for i in self.doc.xpath('//*[@href]'): i.set('{%s}href'%self.NS['xlink'], i.get('href')) self.doc.write(file(instanceName, 'w')) return slideId def textCopy(self, ele): """copy the element ele via xml->text->xml, as deepcopy seems to lose text""" txt = ET.tostring(ele) # print txt return ET.fromstring(txt) def instancePath(self, slideId): """return path to component for slide""" # return os.path.join(slideId, component) return slideId+'.svg' def writeComponents(self, inkscapeFile, slideId): """write components from file for slide""" if not slideId: return doc0 = ET.parse(inkscapeFile) hasComponents = False for i in doc0.xpath('//s:svg//s:g', self.NS): k = "{%s}label"%self.NS['i'] # print i.keys(),k if (k in i.keys() and i.get(k).startswith('Instance')): if len(i) > 0: hasComponents = True break # ET.ElementTree(i).write(f) f = self.instancePath(slideId) if hasComponents: if (os.path.abspath(os.path.realpath(inkscapeFile)) != os.path.abspath(os.path.realpath(f))): file(f, 'w').write(file(inkscapeFile).read()) else: if os.path.isfile(f): os.remove(f) if __name__ == '__main__': import sys x = inkSlide() x.instanceFromTemplate(sys.argv[1], 'slide%s.svg', file(sys.argv[2]).read())