5 from wiki.constants import RE_TRIM_BEGIN, RE_TRIM_END
7 class GradedText(object):
18 'dramat_wierszowany_l',
19 'dramat_wierszowany_lp',
22 RDF = '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF'
24 def __init__(self, text):
28 if self._is_xml is None:
30 self._edoc = etree.fromstring(self._text)
31 except etree.XMLSyntaxError:
39 if self._is_wl is None:
42 self._is_wl = e.tag == self.ROOT and (
43 len(e) == 1 and e[0].tag in self.MASTERS or
44 len(e) == 2 and e[0].tag == self.RDF
45 and e[1].tag in self.MASTERS)
47 self._master = e[-1].tag
58 def _trim(text, trim_begin=True, trim_end=True):
60 Cut off everything before RE_TRIM_BEGIN and after RE_TRIM_END, so
61 that eg. one big XML file can be compiled from many small XML files.
64 text = RE_TRIM_BEGIN.split(text, maxsplit=1)[-1]
66 text = RE_TRIM_END.split(text, maxsplit=1)[0]
70 def compile_text(parts):
72 Compiles full text from an iterable of parts,
73 trimming where applicable.
78 for next_text in parts:
81 # trim the end, because there's more non-empty text
82 # don't trim beginning, if `text' is the first non-empty part
83 texts.append(_trim(text, trim_begin=trim_begin))
86 # don't trim the end, because there's no more text coming after `text'
87 # only trim beginning if it's not still the first non-empty
88 texts.append(_trim(text, trim_begin=trim_begin, trim_end=False))