+ def extract_content(self, book):
+ wld = WLDocument.from_file(book.xml_file.path)
+ root = wld.edoc.getroot()
+
+ # first we build a sequence of top-level items.
+ # book_id
+ # header_index - the 0-indexed position of header element.
+ # content
+ master = self.get_master(root)
+ header_docs = []
+ for header, position in zip(list(master), range(len(master))):
+ print("header %s @%d" % (header, position))
+ doc = self.create_book_doc(book)
+ doc.add(NumericField("header_index", Field.Store.YES, True).setIntValue(position))
+ doc.add(Field("header_type", header.tag, Field.Store.YES, Field.Index.NOT_ANALYZED))
+ content = u' '.join([t for t in header.itertext()])
+ doc.add(Field("content", content, Field.Store.NO, Field.Index.ANALYZED))
+ header_docs.append(doc)
+
+ def walker(node):
+ yield node, None
+ for child in list(node):
+ for b, e in walker(child):
+ yield b, e
+ yield None, node
+ return
+
+ # Then we create a document for each fragments
+ # fragment_anchor - the anchor
+ # themes - list of themes [not indexed]
+ fragment_docs = []
+ # will contain (framgent id -> { content: [], themes: [] }
+ fragments = {}
+ for start, end in walker(master):
+ print("%s %s" % (start, end))
+ if start is not None and start.tag == 'begin':
+ fid = start.attrib['id'][1:]
+ fragments[fid] = {'content': [], 'themes': []}
+ fragments[fid]['content'].append(start.tail)
+ elif start is not None and start.tag == 'motyw':
+ fid = start.attrib['id'][1:]
+ fragments[fid]['themes'].append(start.text)
+ fragments[fid]['content'].append(start.tail)
+ elif start is not None and start.tag == 'end':
+ fid = start.attrib['id'][1:]
+ frag = fragments[fid]
+ del fragments[fid]
+ print("Fragment %s complete, themes: %s contents: %s" % (fid, frag['themes'], frag['content']))
+
+ doc = self.create_book_doc(book)
+ doc.add(Field("fragment_anchor", fid, Field.Store.YES, Field.Index.NOT_ANALYZED))
+ doc.add(Field("content", u' '.join(filter(lambda s: s is not None, frag['content'])), Field.Store.NO, Field.Index.ANALYZED))
+ doc.add(Field("themes", u' '.join(frag['themes']), Field.Store.NO, Field.Index.ANALYZED))
+ fragment_docs.append(doc)
+ elif start is not None:
+ for frag in fragments.values():
+ frag['content'].append(start.text)
+ elif end is not None:
+ for frag in fragments.values():
+ frag['content'].append(end.tail)
+
+ return header_docs + fragment_docs