14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
19
17
# \subsection{\emph{rio} - simple text metaformat}
21
19
# \emph{r} stands for `restricted', `reproducible', or `rfc822-like'.
124
119
"""Append a name and value to the stanza."""
125
120
if not valid_tag(tag):
126
121
raise ValueError("invalid tag %r" % (tag,))
127
if isinstance(value, bytes):
128
value = value.decode('ascii')
129
elif isinstance(value, text_type):
122
if isinstance(value, str):
123
value = unicode(value)
124
elif isinstance(value, unicode):
126
## elif isinstance(value, (int, long)):
127
## value = str(value) # XXX: python2.4 without L-suffix
132
129
raise TypeError("invalid type for rio value: %r of type %s"
133
130
% (value, type(value)))
174
171
# max() complains if sequence is empty
177
for text_tag, text_value in self.items:
178
tag = text_tag.encode('ascii')
179
value = text_value.encode('utf-8')
181
result.append(tag + b': \n')
174
for tag, value in self.items:
176
result.append(tag + ': \n')
183
178
# don't want splitlines behaviour on empty lines
184
val_lines = value.split(b'\n')
185
result.append(tag + b': ' + val_lines[0] + b'\n')
179
val_lines = value.split('\n')
180
result.append(tag + ': ' + val_lines[0].encode('utf-8') + '\n')
186
181
for line in val_lines[1:]:
187
result.append(b'\t' + line + b'\n')
182
result.append('\t' + line.encode('utf-8') + '\n')
189
result.append(tag + b': ' + value + b'\n')
184
result.append(tag + ': ' + value.encode('utf-8') + '\n')
192
187
def to_string(self):
193
188
"""Return stanza as a single string"""
194
return b''.join(self.to_lines())
189
return ''.join(self.to_lines())
196
191
def to_unicode(self):
197
192
"""Return stanza as a single Unicode string.
304
299
max_rio_width = max_width - 4
306
301
for pline in stanza.to_lines():
307
for line in pline.split(b'\n')[:-1]:
308
line = re.sub(b'\\\\', b'\\\\\\\\', line)
302
for line in pline.split('\n')[:-1]:
303
line = re.sub('\\\\', '\\\\\\\\', line)
309
304
while len(line) > 0:
310
305
partline = line[:max_rio_width]
311
306
line = line[max_rio_width:]
312
if len(line) > 0 and line[:1] != [b' ']:
307
if len(line) > 0 and line[0] != [' ']:
314
break_index = partline.rfind(b' ', -20)
309
break_index = partline.rfind(' ', -20)
315
310
if break_index < 3:
316
break_index = partline.rfind(b'-', -20)
311
break_index = partline.rfind('-', -20)
318
313
if break_index < 3:
319
break_index = partline.rfind(b'/', -20)
314
break_index = partline.rfind('/', -20)
320
315
if break_index >= 3:
321
316
line = partline[break_index:] + line
322
317
partline = partline[:break_index]
323
318
if len(line) > 0:
325
partline = re.sub(b'\r', b'\\\\r', partline)
320
partline = re.sub('\r', '\\\\r', partline)
326
321
blank_line = False
327
322
if len(line) > 0:
329
elif re.search(b' $', partline):
324
elif re.search(' $', partline):
331
326
blank_line = True
332
lines.append(b'# ' + partline + b'\n')
327
lines.append('# ' + partline + '\n')
334
lines.append(b'# \n')
338
333
def _patch_stanza_iter(line_iter):
339
map = {b'\\\\': b'\\',
342
337
def mapget(match):
343
338
return map[match.group(0)]
346
341
for line in line_iter:
347
if line.startswith(b'# '):
342
if line.startswith('# '):
349
elif line.startswith(b'#'):
344
elif line.startswith('#'):
352
347
raise ValueError("bad line %r" % (line,))
353
348
if last_line is not None and len(line) > 2:
355
line = re.sub(b'\r', b'', line)
356
line = re.sub(b'\\\\(.|\n)', mapget, line)
350
line = re.sub('\r', '', line)
351
line = re.sub('\\\\(.|\n)', mapget, line)
357
352
if last_line is None:
360
355
last_line += line
361
if last_line[-1:] == b'\n':
356
if last_line[-1] == '\n':
364
359
if last_line is not None: