def start_index(self, attr): attr = self.getAttributes(attr, _indexAttrMap) defn = ABag() if 'item' in attr: label = attr['item'] else: self._syntax_error('<index> needs at least an item attribute') if 'name' in attr: name = attr['name'] else: name = DEFAULT_INDEX_NAME format = attr.get('format', None) if format is not None and format not in ('123', 'I', 'i', 'ABC', 'abc'): raise ValueError( 'index tag format is %r not valid 123 I i ABC or abc' % offset) offset = attr.get('offset', None) if offset is not None: try: offset = int(offset) except: raise ValueError('index tag offset is %r not an int' % offset) defn.label = encode_label((label, format, offset)) defn.name = name defn.kind = 'index' self._push('index', cbDefn=defn) self.handle_data('') self._pop('index', )
def start_index(self,attr): attr=self.getAttributes(attr,_indexAttrMap) defn = ABag() if 'item' in attr: label = attr['item'] else: self._syntax_error('<index> needs at least an item attribute') if 'name' in attr: name = attr['name'] else: name = DEFAULT_INDEX_NAME format = attr.get('format',None) if format is not None and format not in ('123','I','i','ABC','abc'): raise ValueError('index tag format is %r not valid 123 I i ABC or abc' % offset) offset = attr.get('offset',None) if offset is not None: try: offset = int(offset) except: raise ValueError('index tag offset is %r not an int' % offset) defn.label = encode_label([label,format,offset]) defn.name = name defn.kind='index' self._push(cbDefn=defn) self.handle_data('') self._pop()
def start_index(self, attr): attr = self.getAttributes(attr, _indexAttrMap) defn = ABag() if "item" in attr: label = attr["item"] else: self._syntax_error("<index> needs at least an item attribute") if "name" in attr: name = attr["name"] else: name = DEFAULT_INDEX_NAME format = attr.get("format", None) if format is not None and format not in ("123", "I", "i", "ABC", "abc"): raise ValueError("index tag format is %r not valid 123 I i ABC or abc" % offset) offset = attr.get("offset", None) if offset is not None: try: offset = int(offset) except: raise ValueError("index tag offset is %r not an int" % offset) defn.label = encode_label((label, format, offset)) defn.name = name defn.kind = "index" self._push("index", cbDefn=defn) self.handle_data("") self._pop("index")
def _build(self, availWidth, availHeight): _tempEntries = self._getlastEntries() def getkey(seq): return [x.upper() for x in seq[0]] _tempEntries.sort(key=getkey) # was: _tempEntries.sort(lambda a,b: cmp([x.upper() for x in a[0]], [x.upper() for x in b[0]])) leveloffset = self.headers and 1 or 0 def drawIndexEntryEnd(canvas, kind, label): """Callback to draw dots and page numbers after each entry.""" if not isStrType(label): label = label.decoce("utf-8") style = self.getLevelStyle(leveloffset) pages = decode_label(label) drawPageNumbers(canvas, style, pages, availWidth, availHeight, self.dot) self.canv.drawIndexEntryEnd = drawIndexEntryEnd alpha = "" tableData = [] lastTexts = [] alphaStyle = self.getLevelStyle(0) for texts, pageNumbers in _tempEntries: texts = list(texts) # track when the first character changes; either output some extra # space, or the first letter on a row of its own. We cannot do # widow/orphan control, sadly. nalpha = texts[0][0].upper() if alpha != nalpha: alpha = nalpha if self.headers: header = alpha else: header = " " tableData.append([Spacer(1, alphaStyle.spaceBefore)]) tableData.append([Paragraph(header, alphaStyle)]) tableData.append([Spacer(1, alphaStyle.spaceAfter)]) i, diff = listdiff(lastTexts, texts) if diff: lastTexts = texts texts = texts[i:] label = encode_label(list(pageNumbers)) texts[-1] = '%s<onDraw name="drawIndexEntryEnd" label="%s"/>' % (texts[-1], label) for text in texts: # Platypus and RML differ on how parsed XML attributes are escaped. # e.g. <index item="M&S"/>. The only place this seems to bite us is in # the index entries so work around it here. text = escapeOnce(text) style = self.getLevelStyle(i + leveloffset) para = Paragraph(text, style) if style.spaceBefore: tableData.append([Spacer(1, style.spaceBefore)]) tableData.append([para]) i += 1 self._flowable = Table(tableData, colWidths=[availWidth], style=self.tableStyle)
def _build(self,availWidth,availHeight): _tempEntries = [(tuple(asUnicode(t) for t in texts),pageNumbers) for texts, pageNumbers in self._getlastEntries()] def getkey(seq): return [''.join((c for c in unicodedata.normalize('NFD', x.upper()) if unicodedata.category(c) != 'Mn')) for x in seq[0]] _tempEntries.sort(key=getkey) leveloffset = self.headers and 1 or 0 def drawIndexEntryEnd(canvas, kind, label): '''Callback to draw dots and page numbers after each entry.''' style = self.getLevelStyle(leveloffset) pages = [(p[1],k) for p,k in sorted(decode_label(label))] drawPageNumbers(canvas, style, pages, availWidth, availHeight, self.dot) self.canv.drawIndexEntryEnd = drawIndexEntryEnd alpha = '' tableData = [] lastTexts = [] alphaStyle = self.getLevelStyle(0) for texts, pageNumbers in _tempEntries: texts = list(texts) #track when the first character changes; either output some extra #space, or the first letter on a row of its own. We cannot do #widow/orphan control, sadly. nalpha = ''.join((c for c in unicodedata.normalize('NFD', texts[0][0].upper()) if unicodedata.category(c) != 'Mn')) if alpha != nalpha: alpha = nalpha if self.headers: header = alpha else: header = ' ' tableData.append([Spacer(1, alphaStyle.spaceBefore),]) tableData.append([Paragraph(header, alphaStyle),]) tableData.append([Spacer(1, alphaStyle.spaceAfter),]) i, diff = listdiff(lastTexts, texts) if diff: lastTexts = texts texts = texts[i:] label = encode_label(list(pageNumbers)) texts[-1] = '%s<onDraw name="drawIndexEntryEnd" label="%s"/>' % (texts[-1], label) for text in texts: #Platypus and RML differ on how parsed XML attributes are escaped. #e.g. <index item="M&S"/>. The only place this seems to bite us is in #the index entries so work around it here. text = escapeOnce(text) style = self.getLevelStyle(i+leveloffset) para = Paragraph(text, style) if style.spaceBefore: tableData.append([Spacer(1, style.spaceBefore),]) tableData.append([para,]) i += 1 self._flowable = Table(tableData, colWidths=[availWidth], style=self.tableStyle)
def testLabelCoding(self): a = (1,2,[3,4],"a"), [1,2,"4",b'4'] assert a == decode_label(encode_label(a))
def onDrawStr(self,value,*args): return onDrawStr(value,self,encode_label(args))
def onDrawText(self,*args): return '<onDraw name="%s" label="%s" />' % (self.name,encode_label(args))