Somehow this message didn't reach the list. I've now removed the attachment and put it on my server: http://a83-163-111-92.adsl.xs4all.nl/xmlgen
I hope that helps.
-------- Originele bericht --------
Op 13-01-11 10:05, Bas Wijnen schreef:
> Op 13-01-11 06:15, Jane Andreas schreef:
>> I would be more interested at this point in finding a 2d graphics
>> program that could generate objects based on equations/descriptions.
>
> It is quite well possible to write svg by hand, that may be more what I
> (and you?) like. Since this is text-based, it is easily scripted in any
> scripting language as well. So you can for example write equations in
> python to construct paths, and add circles, rectangles and colours by
> hand and copy them unchanged into the output.
However, writing xml by hand is annoying, because of all the quotes and
brackets you need to type. So I just wrote a converter which takes a
python-style (in the sense that it uses indentation) input and produces
xml. It knows xhtml and svg headers so you don't have to remember them.
For example, from:
>svg
g stroke=black fill=none
rect x=5 y=3 width=100 height=300 fill=yellow
rect x=50 y=30 width=10 height=30 stroke=blue
It creates:
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<g stroke="black" fill="none">
<rect x="5" y="3" width="100" height="300" fill="yellow"/>
<rect x="50" y="30" width="10" height="30" stroke="blue"/>
</g>
</svg>
I attached the converter (a python script). Use it for whatever you like
(as long as you follow the agpl3).
Thanks,
Bas
#!/usr/bin/env python # xmlgen: Convert python-style files into xml documents # Copyright 2011 Bas Wijnen <[email protected]> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. '''This program reads a file which is formatted with meaningful indentation (similar to python code). It turns it into an xml file. Example input: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" html head meta http-equiv="Content-Type" content="text/html; charset=utf-8" title 'Example page body p 'some text 'some more text !--included comment # not included comment This is transformed into: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Example page</title> </head> <body> <p>some text some more text<!--included comment--></p> </body> </html> In other words, tags are written with brackets; end tags are added. Lines starting with ' are used as text content, starting with ! are treated specially tags with text as direct child nodes or parents with direct child nodes preserve whitespace (meaning they do not add newlines or copy indentation). Two text nodes in a row get a newline between them. ''' import sys def close (): '''Close the top of stack and add it to its parent''' e = stack.pop () stack[-1]['children'].append (e) def parse_attrs (attrs): '''add quotes and escaping entities to an element''' v = attrs.split ()[0] for a in attrs.split ()[1:]: pos = a.index ('=') val = a[pos + 1:] if val[0] == '"' or val[0] == "'": v += ' ' + a else: v += ' ' + a[:pos] + '="' + val.replace ('&', '&').replace ('<', '<').replace ('>', '>').replace ('"', '"') + '"' return v stack = [{'indent': -2, 'istr': '', 'value': 'BUG', 'tag': True, 'children': []}] while True: line = sys.stdin.readline () if line == '': break l = line.lstrip () indent = len (line) - len (l) if l == '' or l[0] == '#': continue if l[-1] == '\n': l = l[:-1] if l[-1] == '\r': l = l[:-1] if l[0] == "'": l = l[1:] is_tag = False else: is_tag = True if l[0] == '>': # Create header for this filetype. # With a known header, allow children of the root element without indentation. if indent == 0: fakeindent = -1 else: fakeindent = indent x = l[1:].split () if x[0] == 'html': stack[-1]['children'].append ({'indent': fakeindent, 'istr': line[:indent], 'value': '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"', 'tag': True, 'children': []}) stack.append ({'indent': fakeindent, 'istr': line[:indent], 'value': 'html xmlns="http://www.w3.org/1999/xhtml" lang="' + x[1] + '" xml:lang="' + x[1] + '"', 'tag': True, 'children': []}) elif x[0] == 'svg': stack[-1]['children'].append ({'indent': fakeindent, 'istr': line[:indent], 'value': '!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"', 'tag': True, 'children': []}) stack.append ({'indent': fakeindent, 'istr': line[:indent], 'value': parse_attrs ('svg xmlns="http://www.w3.org/2000/svg" version="1.1"' + ' '.join (x[1:])), 'tag': True, 'children': []}) else: raise AssertionError ('unknown header %s requested' % x[0]) continue elif l[0] != '!' and l[0] != '?': l = parse_attrs (l) if indent > stack[-1]['indent']: assert stack[-1]['tag'] != False, 'cannot nest in text' stack.append ({'indent': indent, 'istr': line[:indent], 'value': l, 'tag': is_tag, 'children': []}) continue while indent < stack[-1]['indent']: close () assert indent == stack[-1]['indent'], 'invalid indentation' close () stack.append ({'indent': indent, 'istr': line[:indent], 'value': l, 'tag': is_tag, 'children': []}) # Clean up: close all open tags. while stack[-1]['indent'] >= -1: close () # Now write the tree. assert len (stack) == 1, 'Bug in generator: stack not empty at end of input' assert len (stack[0]['children']) > 0, 'No input elements' def write (tag, with_text): r = '' if tag['indent'] >= -1: r += '<' + tag['value'] if len (tag['children']) == 0: if tag['value'][0] == '?': r += '?' elif tag['value'][0] != '!': r += '/' elif tag['value'][:3] == '!--': r += '--' r += '>' return r r += '>' else: assert tag['tag'], 'Root elements must be tags' assert tag['value'][0] != '!', 'special tags may not have children' assert tag['value'][0] != '?', 'special tags may not have children' have_text = with_text for c in tag['children']: if not c['tag']: have_text = True break was_text = False for c in tag['children']: if c['tag']: if not have_text: if tag['indent'] >= -1 or c is not tag['children'][0]: r += '\n' r += c['istr'] r += write (c, have_text) was_text = False else: if was_text: r += '\n' r += c['value'] was_text = True if tag['indent'] >= -1: if not have_text: r += '\n' + tag['istr'] r += '</' + tag['value'].split ()[0] + '>' return r sys.stdout.write (write (stack[0], False) + '\n')
signature.asc
Description: OpenPGP digital signature
_______________________________________________ Qi Hardware Discussion List Mail to list (members only): [email protected] Subscribe or Unsubscribe: http://lists.en.qi-hardware.com/mailman/listinfo/discussion

