Skip to content

Instantly share code, notes, and snippets.

@om-henners
Last active October 12, 2021 12:37
Show Gist options
  • Save om-henners/8c642c87b71daa3ea68222d40167edbc to your computer and use it in GitHub Desktop.
Save om-henners/8c642c87b71daa3ea68222d40167edbc to your computer and use it in GitHub Desktop.
Inkscape extension to dump source code text on a page, Lorem Ipsum style. Inspired by http://www.east5th.co/blog/2017/02/13/build-your-own-code-poster-with-elixir/ To install, drop these to files in your ~/.config/inkscape/extensions folder, restart inkscape and it's in the Extensions > Text menu.
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>Source Code Text</_name>
<id>org.henry.poster_text</id>
<dependency type="executable" location="extensions">poster_text.py</dependency>
<dependency type="executable" location="extensions">inkex.py</dependency>
<param name="tab" type="notebook">
<page name="Options" _gui-text="Options">
<param name="directory" type="string" _gui-text="Directory to search for code:">~/</param>
<param name="pattern" type="string" _gui-text="Filename pattern to match:">py</param>
<param name="wordsperpara" type="int" min="0" max="10000" _gui-text="Limit on paragraph length:">0</param>
<param name="numparas" type="int" min="0" max="1000" _gui-text="Limit number of paragraphs:">1</param>
</page>
<page name="Help" _gui-text="Help">
<_param name="title" type="description">Based on the "Lorem Ipsum" plugin, this plugin searches the base directory for code, and strings it all together by concatenating on whitespace. If a flowed text is selected, Source code is added to it; otherwise a new flowed text object, the size of the page, is created in a new layer.</_param>
</page>
</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu _name="Text"/>
</effects-menu>
</effect>
<script>
<command reldir="extensions" interpreter="python">poster_text.py</command>
</script>
</inkscape-extension>
#!/usr/bin/env python
'''
Copyright
'''
import os
import re
import random
import inkex
class MyEffect(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.OptionParser.add_option("--title")
self.OptionParser.add_option("-d", "--directory",
action="store", type="string",
dest="directory", default='~/',
help="Default directory")
self.OptionParser.add_option("-p", "--pattern",
action="store", type="string",
dest="pattern", default='py',
help="File extension pattern")
self.OptionParser.add_option("-w", "--wordsperpara",
action="store", type="int",
dest="wordsperpara", default=0,
help="Maximum words per paragraph")
self.OptionParser.add_option("-n", "--numparas",
action="store", type="int",
dest="numparas", default=1,
help="Number of paragraphs")
self.OptionParser.add_option("--tab",
action="store", type="string",
dest="tab",
help="The selected UI-tab when OK was pressed")
def text_generation(self):
'''
Get all the matching files. Then yield words one at a time.
This can take a while if there are a lot of files, but shouldn't be too bad.
'''
matcher = re.compile('.+\.{}$'.format(self.options.pattern))
matched_files = []
for root, _, names in os.walk(os.path.expanduser(self.options.directory)):
for name in names:
if matcher.match(name):
matched_files.append(os.path.join(root, name))
random.shuffle(matched_files)
for path in matched_files:
for word in open(path).read().split():
yield word
def add_text(self, node):
'''
Add the text to the node
'''
word_generator = self.text_generation()
for _ in range(self.options.numparas):
words = []
para = inkex.etree.SubElement(node, inkex.addNS('flowPara','svg'))
if self.options.wordsperpara:
for _, word in zip(range(self.options.wordsperpara), word_generator):
words.append(word_generator.next())
else:
words = word_generator
if words:
para.text = ' '.join(words)
inkex.etree.SubElement(node, inkex.addNS('flowPara','svg'))
else:
break
def effect(self):
found=0
for id, node in self.selected.iteritems():
if node.tag == inkex.addNS('flowRoot','svg'):
found+=1
if found==1:
self.addText(node)
if not found:
#inkex.debug('No "flowRoot" elements selected. Unable to add text.')
svg=self.document.getroot()
gattribs = {inkex.addNS('label','inkscape'):'lorem ipsum',inkex.addNS('groupmode','inkscape'):'layer'}
g=inkex.etree.SubElement(svg,inkex.addNS('g','svg'),gattribs)
flowRoot=inkex.etree.SubElement(g,inkex.addNS('flowRoot','svg'),{inkex.addNS('space','xml'):'preserve'})
flowRegion=inkex.etree.SubElement(flowRoot,inkex.addNS('flowRegion','svg'))
rattribs = {'x':'0','y':'0','width':svg.get('width'),'height':svg.get('height')}
rect=inkex.etree.SubElement(flowRegion,inkex.addNS('rect','svg'),rattribs)
self.add_text(flowRoot)
if __name__ == '__main__':
e = MyEffect()
e.affect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment