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._rich_source = None
75 self._source = source
76
78 """gets the unquoted source string"""
79 return self._source
80 source = property(getsource, setsource)
81
83 """Sets the definition to the quoted value of target"""
84 self._rich_target = None
85 self.source = target
86
88 """gets the unquoted target string"""
89 return self.source
90 target = property(gettarget, settarget)
91
93 self.location.append(location)
94
97
98 -class TxtFile(base.TranslationStore):
99 """This class represents a text file, made up of txtunits"""
100 UnitClass = TxtUnit
101 - def __init__(self, inputfile=None, flavour=None, encoding="utf-8"):
109
111 """Read in text lines and create txtunits from the blocks of text"""
112 block = []
113 startline = 0
114 pretext = ""
115 posttext = ""
116 if not isinstance(lines, list):
117 lines = lines.split("\n")
118 for linenum in range(len(lines)):
119 line = lines[linenum].rstrip("\n").rstrip("\r")
120 for rule, prere, postre in self.flavour:
121 match = prere.match(line)
122 if match:
123 pretext, source = match.groups()
124 postmatch = postre.search(source)
125 if postmatch:
126 posttext = postmatch.group()
127 source = source[:postmatch.start()]
128 block.append(source)
129 isbreak = True
130 break
131 else:
132 isbreak = not line.strip()
133 if isbreak and block:
134 unit = self.addsourceunit("\n".join(block))
135 unit.addlocation("%s:%d" % (self.filename, startline + 1))
136 unit.pretext = pretext
137 unit.posttext = posttext
138 pretext = ""
139 posttext = ""
140 block = []
141 elif not isbreak:
142 if not block:
143 startline = linenum
144 block.append(line)
145 if block:
146 unit = self.addsourceunit("\n".join(block))
147 unit.addlocation("%s:%d" % (self.filename, startline + 1))
148
154
156 """Convert the units back to blocks"""
157 blocks = [str(unit) for unit in self.units]
158 string = "\n\n".join(blocks)
159 return string
160