def add_xe(xe, t): text = xe.get('text', '') pt = xe.get('page-number-text', None) t.text = text or ' ' if pt: p = t.getparent().getparent() r = p.makeelement(expand('w:r')) p.append(r) t2 = r.makeelement(expand('w:t')) t2.set(expand('xml:space'), 'preserve') t2.text = ' [%s]' % pt r.append(t2) return xe['anchor'], t.getparent()
def get_applicable_xe_fields(index, xe_fields): iet = index.get('entry-type', None) xe_fields = [xe for xe in xe_fields if xe.get('entry-type', None) == iet] lr = index.get('letter-range', None) if lr is not None: sl, el = lr.parition('-')[0::2] sl, el = sl.strip(), el.strip() if sl and el: def inrange(text): return sl <= text[0] <= el xe_fields = [xe for xe in xe_fields if inrange(xe.get('text', ''))] bmark = index.get('bookmark', None) if bmark is None: return xe_fields attr = expand('w:name') bookmarks = { b for b in XPath('//w:bookmarkStart')(xe_fields[0]['start_elem']) if b.get(attr, None) == bmark } ancestors = XPath('ancestor::w:bookmarkStart') def contained(xe): # Check if the xe field is contained inside a bookmark with the # specified name return bool(set(ancestors(xe['start_elem'])) & bookmarks) return [xe for xe in xe_fields if contained(xe)]
def get_applicable_xe_fields(index, xe_fields): iet = index.get('entry-type', None) xe_fields = [xe for xe in xe_fields if xe.get('entry-type', None) == iet] lr = index.get('letter-range', None) if lr is not None: sl, el = lr.parition('-')[0::2] sl, el = sl.strip(), el.strip() if sl and el: def inrange(text): return sl <= text[0] <= el xe_fields = [xe for xe in xe_fields if inrange(xe.get('text', ''))] bmark = index.get('bookmark', None) if bmark is None: return xe_fields attr = expand('w:name') bookmarks = {b for b in XPath('//w:bookmarkStart')(xe_fields[0]['start_elem']) if b.get(attr, None) == bmark} ancestors = XPath('ancestor::w:bookmarkStart') def contained(xe): # Check if the xe field is contained inside a bookmark with the # specified name return bool(set(ancestors(xe['start_elem'])) & bookmarks) return [xe for xe in xe_fields if contained(xe)]
def add_xe(xe, t): text = xe.get('text', '') pt = xe.get('page-number-text', None) t.text = text or ' ' if False and pt: # We ignore the page numbering text as it breaks the merging code # below, which assumes every block ends with a link. I dont have the # time/motivation right now to fix the merging code. p = t.getparent().getparent() r = p.makeelement(expand('w:r')) p.append(r) t2 = r.makeelement(expand('w:t')) t2.set(expand('xml:space'), 'preserve') t2.text = ' [%s]' % pt r.append(t2) return xe['anchor'], t.getparent()
def make_block(style, parent, pos): p = parent.makeelement(expand('w:p')) parent.insert(pos, p) if style is not None: ppr = p.makeelement(expand('w:pPr')) p.append(ppr) ps = ppr.makeelement(expand('w:pStyle')) ppr.append(ps) ps.set(expand('w:val'), style) r = p.makeelement(expand('w:r')) p.append(r) t = r.makeelement(expand('w:t')) t.set(expand('xml:space'), 'preserve') r.append(t) return p, t