Added Source Code Text

This commit is contained in:
Mario Voigt 2020-08-23 16:17:17 +02:00
parent 19b123ec5c
commit 775b3effc0
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Source Code Text</name>
<id>fablabchemnitz.de.sourcecodetext</id>
<param name="directory" type="path" mode="folder" gui-text="Directory to search for code:">C:\Users\</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>
<label appearance="header">Help</label>
<label>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.</label>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu name="FabLab Chemnitz">
<submenu name="Shape/Pattern from Generator"/>
</submenu>
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">fablabchemnitz_sourcecodetext.py</command>
</script>
</inkscape-extension>

View File

@ -0,0 +1,72 @@
#!/usr/bin/env python3
import os
import re
import random
import inkex
from lxml import etree
class MyEffect(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument("--directory", default='~/', help="Default directory")
self.arg_parser.add_argument("--pattern", default='py', help="File extension pattern")
self.arg_parser.add_argument("--wordsperpara", type=int, default=0, help="Maximum words per paragraph")
self.arg_parser.add_argument("--numparas", type=int, default=1, help="Number of paragraphs")
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:
file = open(path)
for word in file.read().split():
file.close()
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 = etree.SubElement(node, inkex.addNS('flowPara','svg'))
if self.options.wordsperpara:
try:
for _, word in zip(range(self.options.wordsperpara), word_generator):
words.append(word_generator.next())
except:
pass
else:
words = word_generator
if words:
para.text = ' '.join(words)
etree.SubElement(node, inkex.addNS('flowPara','svg'))
else:
break
def effect(self):
found=0
for id, node in self.svg.selected.items():
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=etree.SubElement(svg,inkex.addNS('g','svg'),gattribs)
flowRoot=etree.SubElement(g,inkex.addNS('flowRoot','svg'),{inkex.addNS('space','xml'):'preserve'})
flowRegion=etree.SubElement(flowRoot,inkex.addNS('flowRegion','svg'))
rattribs = {'x':'0','y':'0','width':svg.get('width'),'height':svg.get('height')}
rect=etree.SubElement(flowRegion,inkex.addNS('rect','svg'),rattribs)
self.add_text(flowRoot)
MyEffect().run()