/
formatxml.py
179 lines (157 loc) · 6.22 KB
/
formatxml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
try:
# Please do not use 'from scribus import *' . If you must use a 'from import',
# Do so _after_ the 'import scribus' and only import the names you need, such
# as commonly used constants.
import scribus
except ImportError, err:
print "This Python script is written for the Scribus scripting interface."
print "It can only be run from within Scribus."
sys.exit(1)
import xml.sax
import xml.sax.handler
class XMLFormatter (xml.sax.handler.ContentHandler):
"""Formats xml in scribus document
Every text node including whitespace is placed in the document
Paragraph styles are applied through an attribute sla:style in the namespace "http://www.scribus.net/formatxml".
Character styles are applied through the attribute sla:override in the same namespace
Paragraph and character styles are inherited automatically"""
def __init__ (self):
self.styles = [None]
self.overrides = [None]
self.template = ""
self.document = ""
self.name = ""
self.in_namespace = False
self.prefix = None
self.first = True
def make_textframe (self):
margins = scribus.getPageMargins()
size = scribus.getPageSize()
w = size[0] - margins[1] - margins[3]
h = size[1] - margins[0] - margins[2]
return scribus.createText (margins[1], margins[0], w, h)
def startPrefixMapping(self, prefix, uri):
"""checks if current node is in proper namespace"""
if uri == "http://www.scribus.net/formatxml":
self.in_namespace = True
if prefix != "":
self.prefix = prefix + ":"
def endPrefixMapping(self, prefix):
"""signal that we are no longer in namespace"""
if self.in_namespace:
self.in_namespace = False
self.prefix = None
def startDocument(self):
"""Open document"""
scribus.openDoc(self.template)
self.name = self.make_textframe()
def add_style (self, stack, style):
if style == None:
style = stack[-1]
stack.append(style)
def startElementNS(self, name, qname, attrs):
"""record styles or overrides"""
attrs_names = attrs.getQNames()
if self.in_namespace:
if self.prefix + "style" in attrs_names:
style = attrs.getValueByQName(self.prefix + "style")
else:
style = None
self.add_style(self.styles, style)
if self.prefix + "override" in attrs_names:
override = attrs.getValueByQName(self.prefix + "override")
else:
override = None
self.add_style(self.overrides, override)
def endElementNS(self, name, qname):
self.styles.pop()
self.overrides.pop()
def shouldSetStyle (self):
if self.first:
return True
else:
return scribus.getAllText(self.name)[-1] == "\n"
def characters(self, content):
"""place text, apply style"""
start = scribus.getTextLength(self.name)
go_ahead = self.shouldSetStyle()
scribus.insertText (content, -1, self.name)
scribus.selectText(start, len(content), self.name)
if self.styles[-1] != None and go_ahead:
try:
scribus.setStyle(self.styles[-1], self.name)
except scribus.NotFoundError:
scribus.createParagraphStyle(self.styles[-1])
scribus.setStyle(self.styles[-1], self.name)
if self.overrides[-1] != None:
try:
scribus.setFont(self.overrides[-1], self.name)
except ValueError:
pass
self.first = False
def flow(self):
while(scribus.textOverflows(self.name) > 0 and scribus.getTextLines(self.name)):
current = self.name
scribus.newPage(-1)
scribus.gotoPage( scribus.pageCount() )
self.name = self.make_textframe()
scribus.linkTextFrames(current, self.name)
def endDocument(self):
"""Save and close document"""
self.flow()
scribus.saveDocAs(self.document)
scribus.closeDoc()
#end class XMLFormatter
class UserCanceled (Exception):
pass
#end class UserCanceled
def format(xml_file, scribus_template, document):
fmt = XMLFormatter ()
fmt.template = scribus_template
fmt.document = document
p = xml.sax.make_parser()
p.setFeature(xml.sax.handler.feature_namespaces, True)
p.setContentHandler(fmt)
p.parse(xml_file)
def getFile(caption, filter, defaultname, issave):
file = scribus.fileDialog(caption, filter, defaultname, issave)
if len(file) == 0:
raise UserCanceled("canceled")
else:
return file
def main():
"""Formats xml file using Scribus template"""
try:
xml_file = getFile("XML File", 'XML (*.xml)', "", False)
template = getFile("Scribus template", "Scribus document (*.sla)", "", False)
document = getFile("Save document", "Alles (*.*)", xml_file.replace(".xml", ".sla"), True)
format (xml_file, template, document)
except UserCanceled:
pass
except xml.sax.SAXParseException:
scribus.messageBox("XML Error", "XML Error: please check your document")
def main_wrapper():
"""The main_wrapper() function disables redrawing, sets a sensible generic
status bar message, and optionally sets up the progress bar. It then runs
the main() function. Once everything finishes it cleans up after the main()
function, making sure everything is sane before the script terminates."""
try:
scribus.statusMessage("Running script...")
scribus.progressReset()
main()
finally:
# Exit neatly even if the script terminated with an exception,
# so we leave the progress bar and status bar blank and make sure
# drawing is enabled.
if scribus.haveDoc():
scribus.setRedraw(True)
scribus.statusMessage("")
scribus.progressReset()
# This code detects if the script is being run as a script, or imported as a module.
# It only runs main() if being run as a script. This permits you to import your script
# and control it manually for debugging.
if __name__ == '__main__':
main_wrapper()