def gather_elements(self, client, node, style): # Special cases: (Not sure this is right ;-) if isinstance(node.parent, docutils.nodes.document): # node.elements = [Paragraph(client.gen_pdftext(node), # client.styles['title'])] # The visible output is now done by the cover template node.elements = [] client.doc_title = node.rawsource client.doc_title_clean = node.astext().strip() elif isinstance(node.parent, docutils.nodes.topic): node.elements = [ Paragraph(client.gen_pdftext(node), client.styles['topic-title']) ] elif isinstance(node.parent, docutils.nodes.Admonition): node.elements = [ Paragraph(client.gen_pdftext(node), client.styles['admonition-title']) ] elif isinstance(node.parent, docutils.nodes.table): node.elements = [ Paragraph(client.gen_pdftext(node), client.styles['table-title']) ] elif isinstance(node.parent, docutils.nodes.sidebar): node.elements = [ Paragraph(client.gen_pdftext(node), client.styles['sidebar-title']) ] else: # Section/Subsection/etc. text = client.gen_pdftext(node) fch = node.children[0] if isinstance(fch, docutils.nodes.generated) and \ fch['classes'] == ['sectnum']: snum = fch.astext() else: snum = None key = node.get('refid') maxdepth = 6 # The parent ID is the refid + an ID to make it unique for Sphinx parent_id = (node.parent.get('ids', [None]) or [None])[0] + '-' + str(id(node)) node.elements = [ Heading(text, client.styles['heading%d' % min(client.depth, maxdepth)], level=client.depth - 1, parent_id=parent_id, node=node, section_header_depth=client.section_header_depth) ] if client.depth <= client.breaklevel: node.elements.insert(0, MyPageBreak(breakTo=client.breakside)) return node.elements
def parseRaw(data, node): """ Parse and process a simple DSL to handle creation of flowables. Supported (can add others on request): * PageBreak * Spacer width, height """ elements = [] lines = data.splitlines() for line in lines: lexer = shlex.shlex(line) lexer.whitespace += ',' tokens = list(lexer) if not tokens: continue # Empty line command = tokens[0] if command == 'PageBreak': if len(tokens) == 1: elements.append(MyPageBreak()) else: elements.append(MyPageBreak(tokens[1])) elif command == 'EvenPageBreak': if len(tokens) == 1: elements.append(MyPageBreak(breakTo='even')) else: elements.append(MyPageBreak(tokens[1], breakTo='even')) elif command == 'OddPageBreak': if len(tokens) == 1: elements.append(MyPageBreak(breakTo='odd')) else: elements.append(MyPageBreak(tokens[1], breakTo='odd')) # TODO: Where does CondPageBreak come from? # elif command == 'FrameBreak': # if len(tokens) == 1: # elements.append(CondPageBreak(99999)) # else: # elements.append(CondPageBreak(float(tokens[1]))) elif command == 'Spacer': elements.append( MySpacer(adjustUnits(tokens[1]), adjustUnits(tokens[2]))) elif command == 'Transition': elements.append(Transition(*tokens[1:])) elif command == 'SetPageCounter': elements.append(PageCounter(*tokens[1:])) else: log.error('Unknown command %s in raw pdf directive [%s]' % (command, nodeid(node))) return elements
def gather_elements(self, client, node, style): # This method is copied from the HandleTitle class # in rst2pdf.genelements. # Special cases: (Not sure this is right ;-) if isinstance(node.parent, docutils.nodes.document): #node.elements = [Paragraph(client.gen_pdftext(node), #client.styles['title'])] # The visible output is now done by the cover template node.elements = [] client.doc_title = node.rawsource client.doc_title_clean = node.astext().strip() elif isinstance(node.parent, docutils.nodes.topic): node.elements = [ Paragraph(client.gen_pdftext(node), client.styles['topic-title']) ] elif isinstance(node.parent, docutils.nodes.Admonition): node.elements = [ Paragraph(client.gen_pdftext(node), client.styles['admonition-title']) ] elif isinstance(node.parent, docutils.nodes.table): node.elements = [ Paragraph(client.gen_pdftext(node), client.styles['table-title']) ] elif isinstance(node.parent, docutils.nodes.sidebar): node.elements = [ Paragraph(client.gen_pdftext(node), client.styles['sidebar-title']) ] else: # Section/Subsection/etc. text = client.gen_pdftext(node) fch = node.children[0] if isinstance(fch, docutils.nodes.generated) and \ fch['classes'] == ['sectnum']: snum = fch.astext() else: snum = None key = node.get('refid') maxdepth = 4 if reportlab.Version > '2.1': maxdepth = 6 # The parent ID is the refid + an ID to make it unique for Sphinx parent_id = (node.parent.get('ids', [None]) or [None])[0] + u'-' + unicode(id(node)) if client.depth <= 3: depth = min(client.depth, maxdepth) bar = '%s' % depth height = self.heading_heights[depth] tfile = codecs.open('imagenes/titulo_%s.svg' % bar, 'r', 'utf-8') tdata = tfile.read() tfile.close() tfile = tempfile.NamedTemporaryFile(dir='.', delete=False, suffix='.svg') tfname = tfile.name tfile.write( tdata.replace('TITLEGOESHERE', text).encode('utf-8')) tfile.close() # Now tfname contains a SVG with the right title. # Make rst2pdf delete it later. client.to_unlink.append(tfname) e = FancyHeading( tfname, width=700, height=height, client=client, snum=snum, parent_id=parent_id, text=text, level=client.depth - 1, hstyle=client.styles['heading%d' % min(client.depth, maxdepth)]) node.elements = [e] else: node.elements = [ Heading( text, client.styles['heading%d' % min(client.depth, maxdepth)], level=client.depth - 1, parent_id=parent_id, node=node, ) ] if client.depth <= client.breaklevel: node.elements.insert(0, MyPageBreak(breakTo=client.breakside)) return node.elements