def check_rst_block(block): """ Check if every textBlock can be compiled as Rst. Change it to textBlock if so. >>> check_rst_block(["textBlock","foo"]) ['rstBlock', 'foo'] >>> check_rst_block(["textBlock","**foo"]) ['textBlock', '**foo'] """ publisher = docCore.Publisher(source_class=docIO.StringInput, destination_class=docIO.StringOutput) publisher.set_components('standalone', 'restructuredtext', 'pseudoxml') publisher.process_programmatic_settings(None, None, None) if block[0] == "textBlock": publisher.set_source(block[1], None) compiled_rst = publisher.reader.read(publisher.source, publisher.parser, publisher.settings) if compiled_rst.parse_messages: # FIXME: It would be nice to add the line number where the error # happened print >> sys.stderr, """Error reading rst on literate comment line falling back to plain text""" else: block[0] = "rstBlock" return block
def publish_doctree(source, source_path=None, source_class=io.StringInput, reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=None): """ A copy of docutils.core.publish_doctree(), that also returns other useful stuff. In this case, return - the document - the encoding detected and selected by docutils """ pub = core.Publisher(reader=reader, parser=parser, writer=None, settings=settings, source_class=source_class, destination_class=io.NullOutput) pub.set_components(reader_name, parser_name, 'null') pub.process_programmatic_settings(settings_spec, settings_overrides, config_section) pub.set_source(source, source_path) pub.set_destination(None, None) output = pub.publish(enable_exit_status=enable_exit_status) return pub.document, pub.source.successful_encoding
def rst2html(fpath, page): """``fpath`` is identified as a file containing ReStructured text. Read the file content, gather metadata from the content (if specified), translate content to HTML. And return a tuple of (metadata, content). Content is HTML text.""" from docutils import core, io, nodes setts = { 'syntax_highlight': 'short' } pub = core.Publisher( destination_class=io.StringOutput ) pub.set_components( 'standalone', 'restructuredtext', 'html' ) pub.process_programmatic_settings(None, setts, None) pub.set_source( source_path=fpath ) pub.publish() parts = pub.writer.parts metadata = {} for docinfo in pub.document.traverse(nodes.docinfo) : for element in docinfo.children : if element.tagname == 'field' : # Generic field name, value = element.children metadata[ name.astext().lower() ] = value.astext() else : # Standard fields metadata[ element.tagname.lower() ] = element.astext() content = parts.get('body') return metadata, content
def parts_from_doctree(document): reader = doctree.Reader(parser_name='null') pub = docutils_core.Publisher(reader, None, None, source=io.DocTreeInput(document), destination_class=io.StringOutput) pub.set_writer("html4css1") pub.process_programmatic_settings(None, None, None) pub.set_destination(None, None) pub.publish(enable_exit_status=False) return pub.writer.parts
def is_valid_rst(string): """ Check if the given string can be compiled to rst. """ publisher = docCore.Publisher(source_class=docIO.StringInput, destination_class=docIO.StringOutput) publisher.set_components('standalone', 'restructuredtext', 'pseudoxml') publisher.process_programmatic_settings(None, None, None) publisher.set_source(string, None) compiled_rst = publisher.reader.read(publisher.source, publisher.parser, publisher.settings) if compiled_rst.parse_messages: return False else: return True
def checkRstBlock(outputList): """ Scans the list of blocks and check if every commentBlock can be compiled as Rest.""" publisher = docCore.Publisher(source_class=docIO.StringInput, destination_class=docIO.StringOutput) publisher.set_components('standalone', 'restructuredtext', 'pseudoxml') publisher.process_programmatic_settings(None, None, None) for block in outputList: if block[0] == "commentBlock": publisher.set_source(block[1], None) compiledRst = publisher.reader.read(publisher.source, publisher.parser, publisher.settings) if compiledRst.parse_messages: print >> sys.stderr, """Error reading rst on literate comment line *to be added* falling back to plain text""" else: block[0] = "rstBlock" return outputList
def render(self, docName, docTree): """\ Render the named document tree as HTML with Kamaelia website specific directives. Returns string containing the entire HTML document. """ if not isinstance(docTree, nodes.document): root = core.publish_doctree('') root.append(docTree) docTree = root docTree.attributes['title'] = docName # do this first, before we turn the boxright nodes into "[ [boxright] ... ]" docTree.transformer.add_transform(squareBracketReplace_transform) docTree.transformer.apply_transforms() docTree.transformer.add_transform(boxright_transform) docTree.transformer.add_transform(crosslink_transform, priority=None, mappings=self.mappings) docTree.transformer.apply_transforms() reader = docutils.readers.doctree.Reader(parser_name='null') pub = core.Publisher(reader, None, None, source=docutils.io.DocTreeInput(docTree), destination_class=docutils.io.StringOutput) pub.set_writer("html") output = pub.publish(enable_exit_status=None) parts = pub.writer.parts doc = parts["html_title"] \ + parts["html_subtitle"] \ + parts["docinfo"] \ + parts["fragment"] wholedoc = self.headers(docTree) + doc + self.footers(docTree) return wholedoc
def publish(node,srcpath): description = ('Lino WebMan publisher. ' + core.default_description) # 20040922 parser = Parser(rfc2822=0, inliner=WebmanInliner(webmod)) # pub = core.Publisher(writer=WebmanWriter(webmod), parser=parser, # destination_class=io.StringOutput) pub = core.Publisher(writer=WebmanWriter(node), destination_class=io.StringOutput) pub.set_components('standalone', 'restructuredtext', None) webmod = node.getModule() pub.process_command_line(webmod.argv, description=description, **webmod.defaults) pub.set_source(None, srcpath) # node.getSourcePath()) cwd = os.getcwd() os.chdir(webmod.getLocalPath()) r = pub.publish() #enable_exit=enable_exit) os.chdir(cwd) return r
def rst2html(data, text): css = [ urllib.parse.quote('data:text/css;charset=UTF-8,' + resolvecss(data, e)) for e in aslist(data.options.cssPath or '') ] # todo: add the docutils default css as well... settings = dict( # input_encoding = 'UTF-8', output_encoding=data.options.encoding, embed_stylesheet=data.options.cssEmbed, stylesheet_path=css, doctitle_xform=False, sectsubtitle_xform=False, ) pub = core.Publisher(None, None, None, source_class=io.StringInput, destination_class=io.NullOutput) pub.set_components('standalone', 'restructuredtext', 'html') pub.process_programmatic_settings(None, settings, None) pub.set_source(text, None) pub.set_destination(None, None) pub.publish(enable_exit_status=False) doc = pub.document doc.walk(HtmlDoctreeFixer(doc)) doc = runFilters(data.options.filters, doc, data) html = core.publish_from_doctree(pub.document, writer_name='html', settings_overrides=settings) return html
from docutils import core, writers class HTML4Body(writers.get_writer_class("html"), object): def translate(self): super(HTML4Body, self).translate() self.output = ''.join(self.html_body) pub = core.Publisher() pub.writer = HTML4Body() pub.set_components('standalone', 'restructuredtext', 'htmlbody') pub.publish( None, '%prog [options] [<source> [<destination>]]', 'Reads from source (:stdin) and writes to destination (:stdout). ')
import sys import zipfile from cStringIO import StringIO from docutils import core, io import OOdirectives import OOtext import OOwriter pub = core.Publisher(writer=OOwriter.Writer()) pub.set_reader('standalone', None, 'restructuredtext') settings = pub.get_settings() pub.source = io.FileInput(settings, source_path=sys.argv[1]) pub.destination = io.StringOutput(settings) content = pub.publish() manifest_list = [ ('content.xml', content), ('styles.xml', OOtext.styles) ] manifest_entries = [] for docname, _ in manifest_list: manifest_entries.append(OOtext.manifest_format % docname) manifest = OOtext.manifest % '\n '.join(manifest_entries) manifest_list.append( ('META-INF/manifest.xml', manifest) ) zip = zipfile.ZipFile(sys.argv[2], "w") for docname, contents in manifest_list: zinfo = zipfile.ZipInfo(docname)
import EffMap class OO: pass class MIF: pass writer = MIF writer = OO if writer is OO: import OOtext import OOwriter as EffWriter elif writer is MIF: import MIFwriter as EffWriter else: raise "Bad writer" pub = core.Publisher(writer=EffWriter.Writer(EffMap.StyleMap)) inliner = EffParser.Inliner() pub.set_reader('standalone', EffParser.Parser(inliner=inliner), 'restructuredtext') settings = pub.get_settings() #settings.doctitle_xform = 0 settings.traceback = 1 pub.source = io.FileInput(source_path=sys.argv[1]) pub.destination = io.StringOutput(encoding='utf-8') content = pub.publish() if writer is OO: manifest_list = [ ('content.xml', content), ('styles.xml', OOtext.styles) ]