1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from copy import deepcopy
8 from catalogue.constants import TRIM_BEGIN, TRIM_END, MASTERS
10 RE_TRIM_BEGIN = re.compile("^<!--%s-->$" % TRIM_BEGIN, re.M)
11 RE_TRIM_END = re.compile("^<!--%s-->$" % TRIM_END, re.M)
14 class ParseError(BaseException):
18 def _trim(text, trim_begin=True, trim_end=True):
20 Cut off everything before RE_TRIM_BEGIN and after RE_TRIM_END, so
21 that eg. one big XML file can be compiled from many small XML files.
24 text = RE_TRIM_BEGIN.split(text, maxsplit=1)[-1]
26 text = RE_TRIM_END.split(text, maxsplit=1)[0]
30 def compile_text(parts):
32 Compiles full text from an iterable of parts,
33 trimming where applicable.
38 for next_text in parts:
42 # trim the end, because there's more non-empty text
43 # don't trim beginning, if `text' is the first non-empty part
44 texts.append(_trim(text, trim_begin=trim_begin))
47 # don't trim the end, because there's no more text coming after `text'
48 # only trim beginning if it's not still the first non-empty
49 texts.append(_trim(text, trim_begin=trim_begin, trim_end=False))
53 def add_trim_begin(text):
54 trim_tag = etree.Comment(TRIM_BEGIN)
55 e = etree.fromstring(text)
56 for master in e[::-1]:
57 if master.tag in MASTERS:
59 if master.tag not in MASTERS:
60 raise ParseError('No master tag found!')
62 master.insert(0, trim_tag)
63 trim_tag.tail = '\n\n\n' + (master.text or '')
65 return str(etree.tostring(e, encoding="utf-8"), 'utf-8')
68 def add_trim_end(text):
69 trim_tag = etree.Comment(TRIM_END)
70 e = etree.fromstring(text)
71 for master in e[::-1]:
72 if master.tag in MASTERS:
74 if master.tag not in MASTERS:
75 raise ParseError('No master tag found!')
77 master.append(trim_tag)
79 prev = trim_tag.getprevious()
81 prev.tail = (prev.tail or '') + '\n\n\n'
83 master.text = (master.text or '') + '\n\n\n'
84 return str(etree.tostring(e, encoding="utf-8"), 'utf-8')
88 """Splits text into chapters.
90 All this stuff really must go somewhere else.
93 src = etree.fromstring(text)
96 splitter = u'naglowek_rozdzial'
97 parts = src.findall('.//naglowek_rozdzial')
100 copied = deepcopy(src)
104 # find the chapter's title
105 name_elem = deepcopy(element)
106 for tag in 'extra', 'motyw', 'pa', 'pe', 'pr', 'pt', 'uwaga':
107 for a in name_elem.findall('.//' + tag):
110 name = etree.tostring(name_elem, method='text', encoding='utf-8').strip()
112 # in the original, remove everything from the start of the last chapter
113 parent = element.getparent()
114 del parent[parent.index(element):]
115 element, parent = parent, parent.getparent()
116 while parent is not None:
117 del parent[parent.index(element) + 1:]
118 element, parent = parent, parent.getparent()
120 # in the copy, remove everything before the last chapter
121 element = copied.findall('.//naglowek_rozdzial')[-1]
122 parent = element.getparent()
123 while parent is not None:
125 while parent[0] is not element:
127 element, parent = parent, parent.getparent()
129 str(etree.tostring(copied, encoding='utf-8'), 'utf-8')
132 parts = src.findall('.//naglowek_rozdzial')
134 chunks[:0] = [[u'początek',
135 str(etree.tostring(src, encoding='utf-8'), 'utf-8')
138 for ch in chunks[1:]:
139 ch[1] = add_trim_begin(ch[1])
140 for ch in chunks[:-1]:
141 ch[1] = add_trim_end(ch[1])