1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """This class implements the functionality for handling plain text files, or
23 similar wiki type files.
24
25 Supported formats are
26 - Plain text
27 - dokuwiki
28 - MediaWiki
29 """
30
31 from translate.storage import base
32 import re
33
34 dokuwiki = []
35 dokuwiki.append(("Dokuwiki heading", re.compile(r"( ?={2,6}[\s]*)(.+)"), re.compile("([\s]*={2,6}[\s]*)$")))
36 dokuwiki.append(("Dokuwiki bullet", re.compile(r"([\s]{2,}\*[\s]*)(.+)"), re.compile("[\s]+$")))
37 dokuwiki.append(("Dokuwiki numbered item", re.compile(r"([\s]{2,}-[\s]*)(.+)"), re.compile("[\s]+$")))
38
39 mediawiki = []
40 mediawiki.append(("MediaWiki heading", re.compile(r"(={2,5}[\s]*)(.+)"), re.compile("([\s]*={2,5}[\s]*)$")))
41 mediawiki.append(("MediaWiki bullet", re.compile(r"(\*+[\s]*)(.+)"), re.compile("[\s]+$")))
42 mediawiki.append(("MediaWiki numbered item", re.compile(r"(#+[\s]*)(.+)"), re.compile("[\s]+$")))
43
44 flavours = {
45 "dokuwiki": dokuwiki,
46 "mediawiki": mediawiki,
47 None: [],
48 "plain": []
49 }
50
51 -class TxtUnit(base.TranslationUnit):
52 """This class represents a block of text from a text file"""
53 - def __init__(self, source="", encoding="utf-8"):
54 """Construct the txtunit"""
55 self.encoding = encoding
56 super(TxtUnit, self).__init__(source)
57 self.source = source
58 self.pretext = ""
59 self.posttext = ""
60 self.location = []
61
63 """Convert a txt unit to a string"""
64 string = u"".join([self.pretext, self.source, self.posttext])
65 if isinstance(string, unicode):
66 return string.encode(self.encoding)
67 return string
68
69
71 """Sets the definition to the quoted value of source"""
72 if isinstance(source, str):
73 source = source.decode(self.encoding)
74 self._source = source
75
77 """gets the unquoted source string"""
78 return self._source
79 source = property(getsource, setsource)
80
82 """Sets the definition to the quoted value of target"""
83 self.source = target
84
86 """gets the unquoted target string"""
87 return self.source
88 target = property(gettarget, settarget)
89
91 self.location.append(location)
92
95
96 -class TxtFile(base.TranslationStore):
97 """This class represents a text file, made up of txtunits"""
98 UnitClass = TxtUnit
99 - def __init__(self, inputfile=None, flavour=None, encoding="utf-8"):
107
109 """Read in text lines and create txtunits from the blocks of text"""
110 block = []
111 startline = 0
112 pretext = ""
113 posttext = ""
114 if not isinstance(lines, list):
115 lines = lines.split("\n")
116 for linenum in range(len(lines)):
117 line = lines[linenum].rstrip("\n").rstrip("\r")
118 for rule, prere, postre in self.flavour:
119 match = prere.match(line)
120 if match:
121 pretext, source = match.groups()
122 postmatch = postre.search(source)
123 if postmatch:
124 posttext = postmatch.group()
125 source = source[:postmatch.start()]
126 block.append(source)
127 isbreak = True
128 break
129 else:
130 isbreak = not line.strip()
131 if isbreak and block:
132 unit = self.addsourceunit("\n".join(block))
133 unit.addlocation("%s:%d" % (self.filename, startline + 1))
134 unit.pretext = pretext
135 unit.posttext = posttext
136 pretext = ""
137 posttext = ""
138 block = []
139 elif not isbreak:
140 if not block:
141 startline = linenum
142 block.append(line)
143 if block:
144 unit = self.addsourceunit("\n".join(block))
145 unit.addlocation("%s:%d" % (self.filename, startline + 1))
146
152
154 """Convert the units back to blocks"""
155 blocks = [str(unit) for unit in self.units]
156 string = "\n\n".join(blocks)
157 return string
158