| 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 |
of |
| 26 |
|
| 27 |
es |
| 28 |
ge |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
import re |
| 37 |
import libxml2 |
| 38 |
import os |
| 39 |
import md5 |
| 40 |
import sys |
| 41 |
|
| 42 |
class docbookXmlMode: |
| 43 |
"""Class for special handling of DocBook document types. |
| 44 |
|
| 45 |
It sets lang attribute on article elements, and adds translators |
| 46 |
to articleinfo/copyright.""" |
| 47 |
def __init__(self): |
| 48 |
self.lists = ['itemizedlist', 'orderedlist', 'variablelist', |
| 49 |
'segmentedlist', 'simplelist', 'calloutlist', 'varlistentry' ] |
| 50 |
self.objects = [ 'figure', 'textobject', 'imageobject', 'mediaobject', |
| 51 |
'screenshot' ] |
| 52 |
|
| 53 |
def getIgnoredTags(self): |
| 54 |
"Returns array of tags to be ignored." |
| 55 |
return self.objects + self.lists |
| 56 |
|
| 57 |
def getFinalTags(self): |
| 58 |
"Returns array of tags to be considered 'final'." |
| 59 |
return ['para', 'formalpara', 'simpara', |
| 60 |
'releaseinfo', 'revnumber', 'title', |
| 61 |
'date', 'term', 'programlisting'] + self.objects + self.lists |
| 62 |
|
| 63 |
def getSpacePreserveTags(self): |
| 64 |
"Returns array of tags in which spaces are to be preserved." |
| 65 |
return [ |
| 66 |
'classsynopsisinfo', |
| 67 |
'computeroutput', |
| 68 |
'funcsynopsisinfo', |
| 69 |
'literallayout', |
| 70 |
'programlisting', |
| 71 |
'screen', |
| 72 |
'synopsis', |
| 73 |
'userinput' |
| 74 |
] |
| 75 |
|
| 76 |
def getTreatedAttributes(self): |
| 77 |
"Returns array of tag attributes which content is to be translated" |
| 78 |
return [] |
| 79 |
|
| 80 |
def getStringForTranslators(self): |
| 81 |
"""Returns string which will be used to credit translators.""" |
| 82 |
return "translator-credits" |
| 83 |
|
| 84 |
def getCommentForTranslators(self): |
| 85 |
"""Returns a comment to be added next to string for crediting translators.""" |
| 86 |
return """Put one translator per line, in the form of NAME <EMAIL>, YEAR1, YEAR2.""" |
| 87 |
|
| 88 |
def _find_articleinfo(self, node): |
| 89 |
if node.name == 'articleinfo' or node.name == 'bookinfo' or node.name == 'info': |
| 90 |
return node |
| 91 |
child = node.children |
| 92 |
while child: |
| 93 |
ret = self._find_articleinfo(child) |
| 94 |
if ret: |
| 95 |
return ret |
| 96 |
child = child.next |
| 97 |
return None |
| 98 |
|
| 99 |
def _find_authorgroup(self, node): |
| 100 |
if node.name == 'authorgroup': |
| 101 |
return node |
| 102 |
child = node.children |
| 103 |
while child: |
| 104 |
ret = self._find_authorgroup(child) |
| 105 |
if ret: |
| 106 |
return ret |
| 107 |
child = child.next |
| 108 |
return None |
| 109 |
|
| 110 |
def _find_lastcopyright(self, node): |
| 111 |
if not node.children: |
| 112 |
return None |
| 113 |
last = node.lastChild() |
| 114 |
tmp = last |
| 115 |
while tmp: |
| 116 |
if tmp.name == "copyright": |
| 117 |
last = tmp |
| 118 |
break |
| 119 |
tmp = tmp.prev |
| 120 |
return last |
| 121 |
|
| 122 |
def _find_lastothercredit(self, node): |
| 123 |
if not node.children: |
| 124 |
return None |
| 125 |
last = node.lastChild() |
| 126 |
tmp = last |
| 127 |
while tmp: |
| 128 |
if tmp.name == "othercredit": |
| 129 |
last = tmp |
| 130 |
break |
| 131 |
tmp = tmp.prev |
| 132 |
return last |
| 133 |
|
| 134 |
def _md5_for_file(self, filename): |
| 135 |
hash = md5.new() |
| 136 |
input = open(filename, "rb") |
| 137 |
read = input.read(4096) |
| 138 |
while read: |
| 139 |
hash.update(read) |
| 140 |
read = input.read(4096) |
| 141 |
input.close() |
| 142 |
return hash.hexdigest() |
| 143 |
|
| 144 |
def _output_images(self, node, msg): |
| 145 |
if node and node.type=='element' and node.name=='imagedata': |
| 146 |
|
| 147 |
attr = node.prop("fileref") |
| 148 |
if attr: |
| 149 |
dir = os.path.dirname(msg.filename) |
| 150 |
fullpath = os.path.join(dir, attr) |
| 151 |
if os.path.exists(fullpath): |
| 152 |
hash = self._md5_for_file(fullpath) |
| 153 |
else: |
| 154 |
hash = "THIS FILE DOESN'T EXIST" |
| 155 |
print >>sys.stderr, "Warning: image file '%s' not found." % fullpath |
| 156 |
|
| 157 |
msg.outputMessage("@@image: '%s'; md5=%s" % (attr, hash), node.lineNo(), |
| 158 |
"When image changes, this message will be marked fuzzy or untranslated for you.\n"+ |
| 159 |
"It doesn't matter what you translate it to: it's not used at all.") |
| 160 |
elif node and node.children: |
| 161 |
child = node.children |
| 162 |
while child: |
| 163 |
self._output_images(child,msg) |
| 164 |
child = child.next |
| 165 |
|
| 166 |
|
| 167 |
def preProcessXml(self, doc, msg): |
| 168 |
"""Add additional messages of interest here.""" |
| 169 |
root = doc.getRootElement() |
| 170 |
self._output_images(root,msg) |
| 171 |
|
| 172 |
def postProcessXmlTranslation(self, doc, language, translators): |
| 173 |
"""Sets a language and translators in "doc" tree. |
| 174 |
|
| 175 |
"translators" is a string consisted of "Name <email>, years" pairs |
| 176 |
of each translator, separated by newlines.""" |
| 177 |
|
| 178 |
root = doc.getRootElement() |
| 179 |
|
| 180 |
while root and root.name != 'article' and root.name != 'book': |
| 181 |
root = root.next |
| 182 |
if root and (root.name == 'article' or root.name == 'book'): |
| 183 |
root.setProp('lang', language) |
| 184 |
else: |
| 185 |
return |
| 186 |
|
| 187 |
if translators == self.getStringForTranslators(): |
| 188 |
return |
| 189 |
elif translators: |
| 190 |
self._append_copyright(root, translators) |
| 191 |
self._append_othercredit(root, translators) |
| 192 |
|
| 193 |
def _append_copyright(self, root, translators): |
| 194 |
|
| 195 |
ai = self._find_articleinfo(root) |
| 196 |
if not ai: |
| 197 |
return |
| 198 |
|
| 199 |
|
| 200 |
lines = translators.split("\n") |
| 201 |
for line in lines: |
| 202 |
line = line.strip() |
| 203 |
match = re.match(r"^([^<,]+)\s*(?:<([^>,]+)>)?,\s*(.*)$", line) |
| 204 |
if match: |
| 205 |
last = self._find_lastcopyright(ai) |
| 206 |
copy = libxml2.newNode("copyright") |
| 207 |
if last: |
| 208 |
copy = last.addNextSibling(copy) |
| 209 |
else: |
| 210 |
ai.addChild(copy) |
| 211 |
if match.group(3): |
| 212 |
copy.newChild(None, "year", match.group(3).encode('utf-8')) |
| 213 |
if match.group(1) and match.group(2): |
| 214 |
holder = match.group(1).strip()+" (%s)" % match.group(2).strip() |
| 215 |
elif match.group(1): |
| 216 |
holder = match.group(1) |
| 217 |
elif match.group(2): |
| 218 |
holder = match.group(2) |
| 219 |
else: |
| 220 |
holder = "???" |
| 221 |
copy.newChild(None, "holder", holder.encode('utf-8')) |
| 222 |
|
| 223 |
def _append_othercredit(self, root, translators): |
| 224 |
|
| 225 |
ai = self._find_authorgroup(root) |
| 226 |
if not ai: |
| 227 |
return |
| 228 |
|
| 229 |
|
| 230 |
lines = translators.split("\n") |
| 231 |
for line in lines: |
| 232 |
line = line.strip() |
| 233 |
match = re.match(r"^([^<,]+)\s*(?:<([^>,]+)>)?,\s*(.*)$", line) |
| 234 |
if match and (match.group(1) or match.group(2)): |
| 235 |
last = self._find_lastothercredit(ai) |
| 236 |
copy = libxml2.newNode("othercredit") |
| 237 |
copy.setProp("class", "translator") |
| 238 |
if last: |
| 239 |
copy = last.addNextSibling(copy) |
| 240 |
else: |
| 241 |
ai.addChild(copy) |
| 242 |
|
| 243 |
if match.group(1): |
| 244 |
name = match.group(1).strip() |
| 245 |
if len(name) < 1: |
| 246 |
copy.newChild(None, "firstname", "???") |
| 247 |
elif name.find(" ") == -1: |
| 248 |
copy.newChild(None, "firstname", name.encode('utf-8')) |
| 249 |
else: |
| 250 |
firstname = name[:name.find(" ")] |
| 251 |
surname = name[name.find(" "):].strip() |
| 252 |
copy.newChild(None, "firstname", firstname.encode('utf-8')) |
| 253 |
copy.newChild(None, "surname", surname.encode('utf-8')) |
| 254 |
if match.group(2): |
| 255 |
copy.newChild(None, "email", match.group(2).encode('utf-8')) |
| 256 |
|
| 257 |
|
| 258 |
if __name__ == '__main__': |
| 259 |
test = docbookXmlMode() |
| 260 |
print "Ignored tags : " + repr(test.getIgnoredTags()) |
| 261 |
print "Final tags : " + repr(test.getFinalTags()) |
| 262 |
print "Space-preserve tags: " + repr(test.getSpacePreserveTags()) |
| 263 |
|
| 264 |
print "Credits from string: '%s'" % test.getStringForTranslators() |
| 265 |
print "Explanation for credits:\n\t'%s'" % test.getCommentForTranslators() |
| 266 |
|