def gather_elements(self, client, node, style): # Either use the figure style or the class # selected by the user st_name = 'figure' if node.get('classes'): st_name = node.get('classes')[0] style = client.styles[st_name] cmd = getattr(style, 'commands', []) image = node.children[0] if len(node.children) > 1: caption = node.children[1] else: caption = None if len(node.children) > 2: legend = node.children[2:] else: legend = [] w = node.get('width', client.styles['figure'].colWidths[0]) cw = [w, ] sub_elems = client.gather_elements(node, style=None) t_style = TableStyle(cmd) table = DelayedTable([[e, ] for e in sub_elems], style=t_style, colWidths=cw) table.hAlign = node.get('align', 'CENTER').upper() return [MySpacer(0, style.spaceBefore), table, MySpacer(0, style.spaceAfter)]
def gather_elements(self, client, node, style): rows = [client.gen_elements(n) for n in node.children] t = [] for r in rows: if not r: continue t.append(r) t_style = TableStyle(client.styles['table'].commands) colWidths = client.styles['table'].colWidths return [DelayedTable(t, style=t_style, colWidths=colWidths)]
def gather_elements(self, client, node, style): # It seems a footnote contains a label and a series of elements ltext = client.gather_pdftext(node.children[0]) label = None ids = '' for i in node.get('ids', []): ids += '<a name="%s"/>' % (i) client.targets.extend(node.get('ids', [ltext])) if len(node['backrefs']) > 1 and client.footnote_backlinks: backrefs = [] i = 1 for r in node['backrefs']: backrefs.append('<a href="#%s" color="%s">%d</a>' % (r, client.styles.linkColor, i)) i += 1 backrefs = '(%s)' % ', '.join(backrefs) if ltext not in client.targets: label = Paragraph(ids + '%s' % (ltext + backrefs), client.styles["endnote"]) client.targets.append(ltext) elif len(node['backrefs']) == 1 and client.footnote_backlinks: if ltext not in client.targets: label = Paragraph( ids + '<a href="%s" color="%s">%s</a>' % (node['backrefs'][0], client.styles.linkColor, ltext), client.styles["endnote"]) client.targets.append(ltext) else: if ltext not in client.targets: label = Paragraph(ids + ltext, client.styles["endnote"]) client.targets.append(ltext) if not label: label = Paragraph(ids + ltext, client.styles["endnote"]) contents = client.gather_elements(node, client.styles["endnote"])[1:] if client.inline_footnotes: st = client.styles['endnote'] t_style = TableStyle(st.commands) colWidths = client.styles['endnote'].colWidths node.elements = [ MySpacer(0, st.spaceBefore), DelayedTable([[label, contents]], style=t_style, colWidths=colWidths), MySpacer(0, st.spaceAfter) ] if client.real_footnotes: client.mustMultiBuild = True for e in node.elements: e.isFootnote = True else: client.decoration['endnotes'].append([label, contents]) node.elements = [] return node.elements
def gather_elements(self, client, node, style): # Multiple authors. Create a two-column table. # Author references on the right. t_style = TableStyle(client.styles['field-list'].commands) colWidths = client.styles['field-list'].colWidths td = [[ Paragraph(client.text_for_label("authors", style) + ":", style=client.styles['fieldname']), client.gather_elements(node, style=style) ]] return [DelayedTable(td, style=t_style, colWidths=colWidths)]
def gather_elements(self, client, node, style): # Either use the figure style or the class # selected by the user st_name = 'figure' if node.get('classes'): st_name = node.get('classes')[0] style = client.styles[st_name] cmd = getattr(style, 'commands', []) image = node.children[0] if len(node.children) > 1: caption = node.children[1] else: caption = None if len(node.children) > 2: legend = node.children[2:] else: legend = [] w = node.get('width', client.styles['figure'].colWidths[0]) cw = [ w, ] sub_elems = client.gather_elements(node, style=None) t_style = TableStyle(cmd) table = DelayedTable([[ e, ] for e in sub_elems], style=t_style, colWidths=cw) table.hAlign = node.get('align', 'CENTER').upper() return [ MySpacer(0, style.spaceBefore), table, MySpacer(0, style.spaceAfter) ]
def gather_elements(self, client, node, style): # A field has two child elements, a field_name and a field_body. # We render as a two-column table, left-column is right-aligned, # bold, and much smaller fn = Paragraph(client.gather_pdftext(node.children[0]) + ":", style=client.styles['fieldname']) fb = client.gen_elements(node.children[1], style=client.styles['fieldvalue']) t_style = TableStyle(client.styles['field-list'].commands) return [ DelayedTable([[fn, fb]], style=t_style, colWidths=client.styles['field-list'].colWidths) ]
def gather_elements(self, client, node, style): optext = ', '.join([ client.gather_pdftext(child) for child in node.children[0].children ]) desc = client.gather_elements(node.children[1], style) t_style = TableStyle(client.styles['option-list'].commands) colWidths = client.styles['option-list'].colWidths node.elements = [ DelayedTable([[ client.PreformattedFit(optext, client.styles["literal"]), desc ]], style=t_style, colWidths=colWidths) ] return node.elements
def gather_elements(self, client, node, style): # This should work, but doesn't look good inside of # table cells (see Issue 173) # node.elements = [MyIndenter(left=client.styles['blockquote'].leftIndent)]\ # + client.gather_elements( node, style) + \ # [MyIndenter(left=-client.styles['blockquote'].leftIndent)] # Workaround for Issue 173 using tables leftIndent = client.styles['blockquote'].leftIndent rightIndent = client.styles['blockquote'].rightIndent spaceBefore = client.styles['blockquote'].spaceBefore spaceAfter = client.styles['blockquote'].spaceAfter s = copy(client.styles['blockquote']) s.leftIndent = style.leftIndent data = [['', client.gather_elements(node, s)]] if client.splittables: node.elements = [ MySpacer(0, spaceBefore), SplitTable(data, colWidths=[leftIndent, None], style=TableStyle([ ["TOPPADDING", [0, 0], [-1, -1], 0], ["LEFTPADDING", [0, 0], [-1, -1], 0], ["RIGHTPADDING", [0, 0], [-1, -1], rightIndent], ["BOTTOMPADDING", [0, 0], [-1, -1], 0], ])), MySpacer(0, spaceAfter) ] else: node.elements = [ MySpacer(0, spaceBefore), DelayedTable( data, colWidths=[leftIndent, None], style=TableStyle([ ["TOPPADDING", [0, 0], [-1, -1], 0], ["LEFTPADDING", [0, 0], [-1, -1], 0], ["RIGHTPADDING", [0, 0], [-1, -1], rightIndent], ["BOTTOMPADDING", [0, 0], [-1, -1], 0], ])), MySpacer(0, spaceAfter) ] return node.elements
def gather_elements(self, client, node, style): # Each child is a hlistcol and represents a column. # Each grandchild is a bullet list that's the contents # of the column # Represent it as a N-column, 1-row table, each cell containing # a list. cells = [[ client.gather_elements(child, style) for child in node.children ]] t_style = TableStyle(client.styles['hlist'].commands) cw = 100. / len(node.children) return [ DelayedTable(cells, colWidths=[ "%s%%" % cw, ] * len(cells), style=t_style) ]
def gather_elements(self, client, node, style): if node.children and isinstance(node.children[0], docutils.nodes.title): title = [] else: title = [ Paragraph(client.text_for_label(node.tagname, style), style=client.styles['%s-heading' % node.tagname]) ] rows = title + client.gather_elements(node, style=style) st = client.styles[node.tagname] if 'commands' in dir(st): t_style = TableStyle(st.commands) else: t_style = TableStyle() t_style.add("ROWBACKGROUNDS", [0, 0], [-1, -1], [st.backColor]) t_style.add("BOX", [0, 0], [-1, -1], st.borderWidth, st.borderColor) if client.splittables: node.elements = [ MySpacer(0, st.spaceBefore), SplitTable([['', rows]], style=t_style, colWidths=[0, None], padding=st.borderPadding), MySpacer(0, st.spaceAfter) ] else: padding, p1, p2, p3, p4 = tablepadding(padding=st.borderPadding) t_style.add(*p1) t_style.add(*p2) t_style.add(*p3) t_style.add(*p4) node.elements = [ MySpacer(0, st.spaceBefore), DelayedTable([['', rows]], style=t_style, colWidths=[0, None]), MySpacer(0, st.spaceAfter) ] return node.elements
def gather_elements(self, client, node, style): # I need to catch the classifiers here tt = [] dt = [] ids = [] for n in node.children: if isinstance(n, docutils.nodes.term): for i in n['ids']: # Used by sphinx glossary lists if i not in client.targets: ids.append('<a name="%s"/>' % i) client.targets.append(i) o, c = client.styleToTags("definition-list-term") tt.append(o + client.gather_pdftext(n) + c) elif isinstance(n, docutils.nodes.classifier): o, c = client.styleToTags("definition-list-classifier") tt.append(o + client.gather_pdftext(n) + c) else: dt.extend(client.gen_elements(n, style)) # FIXME: make this configurable from the stylesheet t_style = TableStyle(client.styles['definition'].commands) cw = getattr(client.styles['definition'], 'colWidths', []) if client.splittables: node.elements = [ Paragraph(''.join(ids) + ' : '.join(tt), client.styles['definition-list-term']), SplitTable([['', dt]], colWidths=cw, style=t_style) ] else: node.elements = [ Paragraph(''.join(ids) + ' : '.join(tt), client.styles['definition-list-term']), DelayedTable([['', dt]], colWidths=[10, None], style=t_style) ] return node.elements
def gather_elements(self, client, node, style): # Take the style from the parent "table" node # because sometimes it's not passed down. if node.parent['classes']: style = client.styles.combinedStyle(['table'] + node.parent['classes']) else: style = client.styles['table'] rows = [] colWidths = [] hasHead = False headRows = 0 for n in node.children: if isinstance(n, docutils.nodes.thead): hasHead = True for row in n.children: r = [] for cell in row.children: r.append(cell) rows.append(r) headRows = len(rows) elif isinstance(n, docutils.nodes.tbody): for row in n.children: r = [] for cell in row.children: r.append(cell) rows.append(r) elif isinstance(n, docutils.nodes.colspec): colWidths.append(int(n['colwidth'])) # colWidths are in no specific unit, really. Maybe ems. # Convert them to % colWidths = list(map(int, colWidths)) tot = sum(colWidths) colWidths = ["%s%%" % ((100.*w) / tot) for w in colWidths] if 'colWidths' in style.__dict__: colWidths[:len(style.colWidths)] = style.colWidths spans = client.filltable(rows) data = [] cellStyles = [] rowids = list(range(0, len(rows))) for row, i in zip(rows, rowids): r = [] j = 0 for cell in row: if isinstance(cell, str): r.append("") else: if i < headRows: st = client.styles['table-heading'] else: st = client.styles['table-body'] ell = client.gather_elements(cell, style=st) r.append(ell) j += 1 data.append(r) st = TableStyle(spans) if 'commands' in style.__dict__: for cmd in style.commands: st.add(*cmd) else: # Only use the commands from "table" if the # specified class has no commands. for cmd in client.styles['table'].commands: st.add(*cmd) if hasHead: for cmd in client.styles.tstyleHead(headRows): st.add(*cmd) rtr = client.repeat_table_rows t = DelayedTable(data, colWidths, st, rtr) if style.alignment == TA_LEFT: t.hAlign = 'LEFT' elif style.alignment == TA_CENTER: t.hAlign = 'CENTER' elif style.alignment == TA_RIGHT: t.hAlign = 'RIGHT' return [t]
def gather_elements(self, client, node, style): b, t = client.bullet_for_node(node) bStyle = copy(style) bStyle.alignment = 2 # FIXME: use different unicode bullets depending on b if b and b in "*+-": b = getattr(bStyle, 'bulletText', '\u2022') # The style has information about the bullet: # # bulletFontSize # bulletFont # This is so the baselines of the bullet and the text align extra_space = bStyle.bulletFontSize - bStyle.fontSize bStyle.fontSize = bStyle.bulletFontSize bStyle.fontName = bStyle.bulletFontName if t == 'bullet': item_st = client.styles['bullet-list-item'] else: item_st = client.styles['item-list-item'] el = client.gather_elements(node, item_st) # FIXME: this is really really not good code if not el: el = [Paragraph("<nobr>\xa0</nobr>", item_st)] idx = node.parent.children.index(node) if idx == 0: # The first item in the list, so doesn't need # separation (it's provided by the list itself) sb = 0 # It also doesn't need a first-line-indent fli = 0 else: # Not the first item, so need to separate from # previous item. Account for space provided by # the item's content, too. sb = item_st.spaceBefore - item_st.spaceAfter fli = item_st.firstLineIndent bStyle.spaceBefore = 0 t_style = TableStyle(style.commands) # The -3 here is to compensate for padding, 0 doesn't work :-( t_style._cmds.extend([ # ["GRID", [ 0, 0 ], [ -1, -1 ], .25, "black" ], ["BOTTOMPADDING", [0, 0], [-1, -1], -3] ]) if extra_space > 0: # The bullet is larger, move down the item text sb += extra_space sbb = 0 else: # The bullet is smaller, move down the bullet sbb = -extra_space # colWidths = map(client.styles.adjustUnits, # client.styles['item_list'].colWidths) colWidths = getattr(style, 'colWidths', []) while len(colWidths) < 2: colWidths.append( client.styles['item_list'].colWidths[len(colWidths)]) if client.splittables: node.elements = [ MySpacer(0, sb), SplitTable([[Paragraph(b, style=bStyle), el]], style=t_style, colWidths=colWidths) ] else: node.elements = [ MySpacer(0, sb), DelayedTable([[Paragraph(b, style=bStyle), el]], style=t_style, colWidths=colWidths) ] return node.elements
def gather_elements(self, client, node, style): # Take the style from the parent "table" node # because sometimes it's not passed down. if node.parent['classes']: style = client.styles.combinedStyle(['table'] + node.parent['classes']) else: style = client.styles['table'] rows = [] colWidths = [] hasHead = False headRows = 0 for n in node.children: if isinstance(n, docutils.nodes.thead): hasHead = True for row in n.children: r = [] for cell in row.children: r.append(cell) rows.append(r) headRows = len(rows) elif isinstance(n, docutils.nodes.tbody): for row in n.children: r = [] for cell in row.children: r.append(cell) rows.append(r) elif isinstance(n, docutils.nodes.colspec): colWidths.append(int(n['colwidth'])) # colWidths are in no specific unit, really. Maybe ems. # Convert them to % colWidths = list(map(int, colWidths)) tot = sum(colWidths) colWidths = ["%s%%" % ((100. * w) / tot) for w in colWidths] if 'colWidths' in style.__dict__: colWidths[:len(style.colWidths)] = style.colWidths spans = client.filltable(rows) data = [] cellStyles = [] rowids = list(range(0, len(rows))) for row, i in zip(rows, rowids): r = [] j = 0 for cell in row: if isinstance(cell, str): r.append("") else: if i < headRows: st = client.styles['table-heading'] else: st = client.styles['table-body'] ell = client.gather_elements(cell, style=st) r.append(ell) j += 1 data.append(r) st = TableStyle(spans) if 'commands' in style.__dict__: for cmd in style.commands: st.add(*cmd) else: # Only use the commands from "table" if the # specified class has no commands. for cmd in client.styles['table'].commands: st.add(*cmd) if hasHead: for cmd in client.styles.tstyleHead(headRows): st.add(*cmd) rtr = client.repeat_table_rows t = DelayedTable(data, colWidths, st, rtr) if style.alignment == TA_LEFT: t.hAlign = 'LEFT' elif style.alignment == TA_CENTER: t.hAlign = 'CENTER' elif style.alignment == TA_RIGHT: t.hAlign = 'RIGHT' return [t]