def render(self): key = 'cia.apps.blog.%d' % self.id parts = cache.get(key) if not parts: # Convert the reST markup to a document tree document = publish_doctree(source=self.content) visitor = ImageTranslator(document) document.walkabout(visitor) # # Publish that document tree as HTML. We can't use any of # the simpler methods in docutils.core, since we need # access to writer.parts # reader = doctree.Reader(parser_name='null') pub = Publisher(reader, None, None, source=DocTreeInput(document), destination_class=StringOutput) pub.set_writer('html4css1') pub.process_programmatic_settings(None, { 'cloak_email_addresses': True, 'initial_header_level': 2, }, None) pub.publish() parts = pub.writer.parts cache.set(key, parts) return parts
def _docutils_rest_to(rest, writer): """ Uses docutils to convert a ReST string to HTML. Returns a tuple containg the HTML string and the list of warning nodes that were removed from the HTML. """ # Make sure any Sphinx polution of docutils has been removed. if Sphinx is not None: for key, value in docutils_roles.items(): if value.__module__.startswith('sphinx'): docutils_roles.pop(key) pub = Publisher(source_class=docutils.io.StringInput, destination_class=docutils.io.StringOutput) pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer(writer) pub.writer.default_stylesheet_path='' pub.get_settings() # Get the default settings pub.settings.halt_level = 6 # Don't halt on errors pub.settings.warning_stream = StringIO() pub.set_source(rest) pub.set_destination() pub.document = pub.reader.read(pub.source, pub.parser, pub.settings) pub.apply_transforms() # Walk the node structure of a docutils document and remove 'problematic' # and 'system_message' nodes. Save the system_message nodes. warning_nodes = [] for node in pub.document.traverse(docutils.nodes.problematic): node.parent.replace(node, node.children[0]) for node in pub.document.traverse(docutils.nodes.system_message): warning_nodes.append(node) node.parent.remove(node) return pub.writer.write(pub.document, pub.destination), warning_nodes
def render(self): key = 'cia.apps.blog.%d' % self.id parts = cache.get(key) if not parts: # Convert the reST markup to a document tree document = publish_doctree(source = self.content) visitor = ImageTranslator(document) document.walkabout(visitor) # # Publish that document tree as HTML. We can't use any of # the simpler methods in docutils.core, since we need # access to writer.parts # reader = doctree.Reader(parser_name='null') pub = Publisher(reader, None, None, source = DocTreeInput(document), destination_class = StringOutput) pub.set_writer('html4css1') pub.process_programmatic_settings(None, { 'cloak_email_addresses': True, 'initial_header_level': 2, }, None) pub.publish() parts = pub.writer.parts cache.set(key, parts) return parts
def convertWWW(src, language, options=None): if language == 'el': encoding = 'iso-8859-7' elif language == 'pl': encoding = 'iso-8859-2' elif language == 'ru': encoding = 'windows-1251' elif language == 'cs': encoding = 'iso-8859-2' else: encoding = 'iso-8859-15' arguments = [ '--no-generator', '--language=' + language, '--no-source-link', '--no-datestamp', '--input-encoding=' + encoding, '--output-encoding=' + encoding, '--target-suffix=' + 'php', src, '' ] if options: for option in options: arguments.insert(0, option) publisher = Publisher(destination_class=NullOutput) publisher.set_reader('standalone', None, 'restructuredtext') publisher.set_writer('html') publisher.publish(argv=arguments) return ''.join(publisher.writer.body_pre_docinfo + publisher.writer.body).encode(encoding)
def processHTML(src, depth): src = os.path.normpath(src) prefix = os.path.splitext(src)[0] suffix = os.path.splitext(src)[1][1:] if suffix != DEFAULTLANG: return dst = prefix + '.html' #.' + suffix dst_abs = os.path.normpath(os.path.join(TRGROOT, dst)) src_abs = os.path.normpath(os.path.join(SRCROOT, src)) dst_dir = os.path.dirname(dst_abs) makedir(dst_dir) if newer([src_abs], dst_abs): reportBuilding(src) arguments = [ '--no-generator', '--language=' + suffix, '--no-source-link', '--no-datestamp', '--output-encoding=iso-8859-15', '--target-suffix=html', '--stylesheet=' + '../' * depth + 'aros.css', '--link-stylesheet', src_abs, dst_abs ] publisher = Publisher() publisher.set_reader('standalone', None, 'restructuredtext') publisher.set_writer('html') publisher.publish(argv=arguments) else: reportSkipping(dst)
def render_rst(directory, name, meta_parser, template): # check if that file actually exists path = safe_join(directory, name + '.rst') if not os.path.exists(path): abort(404) # read file with codecs.open(path, encoding='utf-8') as fd: content = fd.read() if not template: # Strip out RST content = content.replace('.. meta::\n', '') content = content.replace('.. contents::\n\n', '') content = content.replace('.. raw:: html\n\n', '') content = content.replace('\n.. [', '\n[') content = content.replace(']_.', '].') content = content.replace(']_,', '],') content = content.replace(']_', '] ') # Change highlight formatter content = content.replace('{% highlight', "{% highlight formatter='textspec'") # Metatags for (metatag, label) in METATAG_LABELS.items(): content = content.replace(' :%s' % metatag, label) # render the post with Jinja2 to handle URLs etc. rendered_content = render_template_string(content) rendered_content = rendered_content.replace('</pre></div>', ' </pre></div>') if not template: # Send response r = make_response(rendered_content) r.mimetype = 'text/plain' return r # Render the ToC doctree = publish_doctree(source=rendered_content) bullet_list = doctree[1][1] doctree.clear() doctree.append(bullet_list) reader = Reader(parser_name='null') pub = Publisher(reader, None, None, source=io.DocTreeInput(doctree), destination_class=io.StringOutput) pub.set_writer('html') pub.publish() toc = pub.writer.parts['fragment'] # Remove the ToC from the main document rendered_content = rendered_content.replace('.. contents::\n', '') # publish the spec with docutils parts = publish_parts(source=rendered_content, source_path=directory, writer_name="html") meta = meta_parser(parts['meta']) if (directory == PROPOSAL_DIR): meta['num'] = int(name[:3]) return render_template(template, title=parts['title'], toc=toc, body=parts['fragment'], name=name, meta=meta)
def pypi_render(source): """ Copied (and slightly adapted) from pypi.description_tools """ ALLOWED_SCHEMES = '''file ftp gopher hdl http https imap mailto mms news nntp prospero rsync rtsp rtspu sftp shttp sip sips snews svn svn+ssh telnet wais irc'''.split() settings_overrides = { "raw_enabled": 0, # no raw HTML code "file_insertion_enabled": 0, # no file/URL access "halt_level": 2, # at warnings or errors, raise an exception "report_level": 5, # never report problems with the reST code } # capture publishing errors, they go to stderr old_stderr = sys.stderr sys.stderr = s = StringIO.StringIO() parts = None try: # Convert reStructuredText to HTML using Docutils. document = publish_doctree(source=source, settings_overrides=settings_overrides) for node in document.traverse(): if node.tagname == '#text': continue if node.hasattr('refuri'): uri = node['refuri'] elif node.hasattr('uri'): uri = node['uri'] else: continue o = urlparse.urlparse(uri) if o.scheme not in ALLOWED_SCHEMES: raise TransformError('link scheme not allowed') # now turn the transformed document into HTML reader = readers.doctree.Reader(parser_name='null') pub = Publisher(reader, source=io.DocTreeInput(document), destination_class=io.StringOutput) pub.set_writer('html') pub.process_programmatic_settings(None, settings_overrides, None) pub.set_destination(None, None) pub.publish() parts = pub.writer.parts except: pass sys.stderr = old_stderr # original text if publishing errors occur if parts is None or len(s.getvalue()) > 0: return None else: return parts['body']
def process_description(source, output_encoding='unicode'): """Given an source string, returns an HTML fragment as a string. The return value is the contents of the <body> tag. Parameters: - `source`: A multi-line text string; required. - `output_encoding`: The desired encoding of the output. If a Unicode string is desired, use the default value of "unicode" . """ # Dedent all lines of `source`. source = trim_docstring(source) settings_overrides = { 'raw_enabled': 0, # no raw HTML code 'file_insertion_enabled': 0, # no file/URL access 'halt_level': 2, # at warnings or errors, raise an exception 'report_level': 5, # never report problems with the reST code } parts = None # Convert reStructuredText to HTML using Docutils. document = publish_doctree(source=source, settings_overrides=settings_overrides) for node in document.traverse(): if node.tagname == '#text': continue if node.hasattr('refuri'): uri = node['refuri'] elif node.hasattr('uri'): uri = node['uri'] else: continue o = urlparse(uri) if o.scheme not in ALLOWED_SCHEMES: raise TransformError('link scheme not allowed: {0}'.format(uri)) # now turn the transformed document into HTML reader = readers.doctree.Reader(parser_name='null') pub = Publisher(reader, source=io.DocTreeInput(document), destination_class=io.StringOutput) pub.set_writer('html') pub.process_programmatic_settings(None, settings_overrides, None) pub.set_destination(None, None) pub.publish() parts = pub.writer.parts output = parts['body'] if output_encoding != 'unicode': output = output.encode(output_encoding) return output
def render_readme_like_pypi(source, output_encoding='unicode'): """ Render a ReST document just like PyPI does. """ # Dedent all lines of `source`. source = trim_docstring(source) settings_overrides = { 'raw_enabled': 0, # no raw HTML code 'file_insertion_enabled': 0, # no file/URL access 'halt_level': 2, # at warnings or errors, raise an exception 'report_level': 5, # never report problems with the reST code } parts = None # Convert reStructuredText to HTML using Docutils. document = publish_doctree(source=source, settings_overrides=settings_overrides) for node in document.traverse(): if node.tagname == '#text': continue if node.hasattr('refuri'): uri = node['refuri'] elif node.hasattr('uri'): uri = node['uri'] else: continue o = urlparse(uri) if o.scheme not in ALLOWED_SCHEMES: raise TransformError('link scheme not allowed: {0}'.format(uri)) # now turn the transformed document into HTML reader = readers.doctree.Reader(parser_name='null') pub = Publisher(reader, source=io.DocTreeInput(document), destination_class=io.StringOutput) pub.set_writer('html') pub.process_programmatic_settings(None, settings_overrides, None) pub.set_destination(None, None) pub.publish() parts = pub.writer.parts output = parts['body'] if output_encoding != 'unicode': output = output.encode(output_encoding) return output
def publish_doctree(source, logger): # type: (str, Any) -> Node pub = Publisher(reader=None, parser=None, writer=None, settings=None, source_class=io.StringInput, destination_class=io.NullOutput) pub.reader = Reader(None, 'restructuredtext') pub.reader.doc_logger = logger pub.set_writer('null') pub.parser = pub.reader.parser pub.process_programmatic_settings(None, None, None) pub.set_source(source, None) pub.set_destination(None, None) output = pub.publish(enable_exit_status=False) return pub.document
def pypi_rest2html(source, output_encoding="unicode"): """ >>> pypi_rest2html("test!") u'<p>test!</p>\n' """ settings_overrides = { "raw_enabled": 0, # no raw HTML code "file_insertion_enabled": 0, # no file/URL access "halt_level": 2, # at warnings or errors, raise an exception "report_level": 5, # never report problems with the reST code } # Convert reStructuredText to HTML using Docutils. document = publish_doctree(source=source, settings_overrides=settings_overrides) for node in document.traverse(): if node.tagname == "#text": continue if node.hasattr("refuri"): uri = node["refuri"] elif node.hasattr("uri"): uri = node["uri"] else: continue o = urlparse(uri) if o.scheme not in ALLOWED_SCHEMES: raise TransformError("link scheme not allowed") # now turn the transformed document into HTML reader = readers.doctree.Reader(parser_name="null") pub = Publisher(reader, source=io.DocTreeInput(document), destination_class=io.StringOutput) pub.set_writer("html") pub.process_programmatic_settings(None, settings_overrides, None) pub.set_destination(None, None) pub.publish() parts = pub.writer.parts output = parts["body"] if output_encoding != "unicode": output = output.encode(output_encoding) return output
def parts_from_doctree(document, destination_path=None, writer=Writer(), writer_name='html4css2', settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=None): """ A version of :func:`docutils.core.publish_from_doctree` that supplies the "parts" rather than a regular string representation. """ reader = Reader(parser_name='null') pub = Publisher(reader, None, writer, source=io.DocTreeInput(document), destination_class=io.StringOutput, settings=settings) if not writer and writer_name: pub.set_writer(writer_name) pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) pub.set_destination(None, destination_path) output = pub.publish() return pub.writer.parts
def parts_from_doctree(document, destination_path=None, writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=None): """ Set up & run a `Publisher` to render from an existing document tree data structure, for programmatic use with string I/O. Return the encoded string output. Note that document.settings is overridden; if you want to use the settings of the original `document`, pass settings=document.settings. Also, new document.transformer and document.reporter objects are generated. For encoded string output, be sure to set the 'output_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string output. Here's one way:: publish_from_doctree( ..., settings_overrides={'output_encoding': 'unicode'}) Parameters: `document` is a `docutils.nodes.document` object, an existing document tree. Other parameters: see `publish_programmatically`. """ reader = docutils.readers.doctree.Reader(parser_name='null') pub = Publisher(reader, None, writer, source=io.DocTreeInput(document), destination_class=io.StringOutput, settings=settings) if not writer and writer_name: pub.set_writer(writer_name) pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) pub.set_destination(None, destination_path) pub.publish(enable_exit_status=enable_exit_status) return pub.writer.parts
def convertRSTfilesToHTML(root_list): """This routine creates .html files from all .rst files in root_list, the list of files that have just been tangled.""" for root in root_list: base,fullname = g.os_path_split(root) name,ext = g.os_path_splitext(fullname) if ext == ".rst": file = g.os_path_join(base,name+".html") #@ << Convert root to corresponding .html file >> #@+node:EKR.20040502194930.3:<< Convert root to corresponding .html file >> # Leo will report the execption if docutils is not installed. from docutils.core import Publisher from docutils.io import FileInput,StringOutput,StringInput # Read .rst file into s. f = open(root,"r") s = f.read() f.close() # Restucture s into output. pub = Publisher() pub.source = StringInput(pub.settings,source=s) pub.destination = StringOutput(pub.settings,encoding="utf-8") pub.set_reader('standalone',None,'restructuredtext') pub.set_writer('html') output = pub.publish() # EKR: 3/7/03: convert output using the present encoding. dict = g.scanDirectives(self.c,p=root) encoding = dict.get("encoding",None) if encoding == None: encoding = g.app.config.default_derived_file_encoding output = g.toEncodedString(output,encoding,reportErrors=True) # Write the corresponding html file. f = open(file,"w") f.write(output) f.close()
def onIconDoubleClick(tag, keywords): c = keywords.get("c") p = keywords.get("p") # g.trace(c) if not c or not p: return applyConfiguration(c) config.tag = tag h = p.headString().strip() if g.match_word(h, 0, "@rst"): if len(h) > 5: fname = h[5:] ext = os.path.splitext(fname)[1].lower() if ext in ('.htm', '.html', '.tex'): #@ << write rST as HTML/LaTeX >> #@+node:ekr.20040331071319.4:<< write rST as HTML/LaTeX >> try: import docutils except: g.es('HTML/LaTeX generation requires docutils') return else: import docutils.parsers.rst from docutils.core import Publisher from docutils.io import StringOutput, StringInput, FileOutput, FileInput import StringIO # Set the writer and encoding for the converted file if ext in ('.html', '.htm'): writer = 'html' enc = "utf-8" else: writer = 'latex' enc = "iso-8859-1" syntax = False if writer == 'html': try: import SilverCity #@ << define code-block >> #@+node:ekr.20040331071319.5:<< define code-block >> def code_block(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """Create a code-block directive for docutils.""" # See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252170 language = arguments[0] module = getattr(SilverCity, language) generator = getattr(module, language + "HTMLGenerator") io = StringIO.StringIO() generator().generate_html(io, '\n'.join(content)) html = '<div class="code-block">\n%s\n</div>\n' % io.getvalue( ) raw = docutils.nodes.raw( '', html, format='html' ) #(self, rawsource='', text='', *children, **attributes): return [raw] # These are documented at http://docutils.sourceforge.net/spec/howto/rst-directives.html. code_block.arguments = ( 1, # Number of required arguments. 0, # Number of optional arguments. 0 ) # True if final argument may contain whitespace. # A mapping from option name to conversion function. code_block.options = { 'language': docutils.parsers.rst.directives. unchanged # Return the text argument, unchanged } code_block.content = 1 # True if content is allowed. # Register the directive with docutils. docutils.parsers.rst.directives.register_directive( 'code-block', code_block) config.do_replace_code_blocks = False #@nonl #@-node:ekr.20040331071319.5:<< define code-block >> #@nl syntax = True except ImportError: g.es( 'SilverCity not present so no syntax highlighting') #@ << define alternate code block implementation>> #@+node:bwmulder.20050326114320: << define alternate code block implementation>> # Don't know what to do here: Can someone make a suggestion? #import docutils.parsers.rst.directives.admonitions #import docutils.parsers.rst.directives.body # docutils.parsers.rst.directives._directives['code-block'] = docutils.parsers.rst.directives.body.block # docutils.parsers.rst.directives.register_directive('code-block', docutils.parsers.rst.directives.admonitions.admonition) #docutils.parsers.rst.directives._directives['code-block'] = docutils.parsers.rst.directives.body.pull_quote # g.es("Registered some alternate implementation for code-block directive") config.do_replace_code_blocks = config.rst2_replace_code_blocks #@-node:bwmulder.20050326114320: << define alternate code block implementation>> #@nl if config.rst2file: rstFileName = os.path.splitext(fname)[0] + ".txt" rstFile = file(rstFileName, "w") g.es("Using %s as rst file" % rstFileName) else: rstFile = StringIO.StringIO() config.current_file = fname writeTreeAsRst(rstFile, fname, p, c, syntax=syntax) if config.rst2file: rstFile.close() else: rstText = rstFile.getvalue() # This code snipped has been taken from code contributed by Paul Paterson 2002-12-05. pub = Publisher() if config.rst2file: pub.source = FileInput(source_path=rstFileName) pub.destination = FileOutput(destination_path=fname, encoding='unicode') else: pub.source = StringInput(source=rstText) pub.destination = StringOutput(pub.settings, encoding=enc) pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer(writer) output = pub.publish(argv=['']) if config.rst2file: pass else: convertedFile = file(fname, 'w') convertedFile.write(output) convertedFile.close() rstFile.close() writeFullFileName(fname) return http_support_main(tag, fname) #@-node:ekr.20040331071319.4:<< write rST as HTML/LaTeX >> #@nl else: #@ << write rST file >> #@+node:ekr.20040331071319.6:<< write rST file >> rstFile = file(fname, 'w') writeTreeAsRst(rstFile, fname, p, c) rstFile.close() writeFullFileName(fname) #@nonl #@-node:ekr.20040331071319.6:<< write rST file >> #@nl else: # if the headline only contains @rst then open the node and its parent in text editor # this works for me but needs to be generalized and should probably be a component # of the open_with plugin. if 0: c.openWith(("os.startfile", None, ".txt")) c.selectVnode(p.parent()) c.openWith(("os.startfile", None, ".tp"))
def create_toc(doctree, depth=9223372036854775807, writer_name='html', exclude_first_section=True, href_prefix=None, id_prefix='toc-ref-'): """ Create a Table of Contents (TOC) from the given doctree Returns: (docutils.core.Publisher instance, output string) `writer_name`: represents a reST writer name and determines the type of output returned. Example: pub = blazeutils.rst.rst2pub(toc_rst) pub, html_output = blazeutils.rst.create_toc(pub.document) # a full HTML document (probably not what you want most of the time) print html_output # just the TOC print pub.writer.parts['body'] """ # copy the doctree since Content alters some settings on the original # document doctree = doctree.deepcopy() # we want to be able to customize ids to avoid clashes if needed doctree.settings.auto_id_prefix = id_prefix details = { 'depth': depth, } # Assuming the document has one primary heading and then sub-sections, we # want to be able to give just the sub-sections startnode = None if exclude_first_section: nodes = doctree.traverse(docutils.nodes.section) if nodes: startnode = nodes[0] # use the Contents transform to build the TOC node structure from the # document c = Contents(doctree) # this startnode isn't really used as the start node, its only used for # to pull settings from c.startnode = BlankObject(details=details) # since this toc is detached from the rest of the document, we don't want # backlinks c.backlinks = 'none' # build the nodes toc_nodes = c.build_contents(startnode or doctree) # create a new document with the new nodes toc_doc = new_document(None) toc_doc += toc_nodes # fix fragements that reference the same page if href_prefix: prefix_refids(toc_doc, href_prefix) # setup a publisher and publish from the TOC document reader = docutils.readers.doctree.Reader(parser_name='null') pub = Publisher( reader, None, None, source=io.DocTreeInput(toc_doc), destination_class=io.StringOutput ) pub.set_writer(writer_name) final_settings_overrides = default_rst_opts.copy() pub.process_programmatic_settings( None, final_settings_overrides, None) output = pub.publish() return pub, output
usage = "%prog [options] [<source> [<destination>]]" description = ('Reads main document from standalone reStructuredText ' 'from <source> (default is stdin). ') pub = Publisher(Reader(), None, Writer(), settings=None, destination_class=io.NullOutput) ## Get settings for components from command-line pub.set_components(None, 'rst', None) pub.process_command_line(None, None, None, None, {}) ## Publish to doctree using reader/parser but without writing pub.set_writer('null') null_output = pub.publish( None, usage, description, None, None, config_section=None, enable_exit_status=1) document = pub.document def dump(document): # these indices are rebuild every time for a document from pprint import pformat print "Dumping vars for doc ", document['source'] print 'current_line', pformat(document.current_line) #print 'settings', pformat(document.settings) print 'reporter', pformat(document.reporter) print 'indirect_targets', pformat(document.indirect_targets) #print 'substitution_defs', pformat(document.substitution_defs) #print 'substitution_names', pformat(document.substitution_names)
def onIconDoubleClick(tag, keywords): v = keywords.get("p") or keywords.get("v") c = keywords.get("c") # g.trace(c) h = v.headString().strip() if g.match_word(h, 0, "@text"): fname = h[5:] ext = os.path.splitext(fname)[1].lower() if ext in ('.htm', '.html', '.tex'): #@ << write rST as HTML/LaTeX >> #@+node:edream.111803100242.4:<< write rST as HTML/LaTeX >> try: import docutils except ImportError: docutils = None g.es('HTML/LaTeX generation requires docutils') if docutils: import StringIO rstFile = StringIO.StringIO() writeTreeAsRst(rstFile, fname, v, c) rstText = rstFile.getvalue() # Set the writer and encoding for the converted file if ext in ('.html', '.htm'): writer = 'html' enc = "utf-8" else: writer = 'latex' enc = "iso-8859-1" #@ << convert rST to HTML/LaTeX >> #@+node:edream.111803100242.5:<< convert rST to HTML/LaTeX >> # this code snipped has been taken from code contributed by Paul Paterson 2002-12-05 from docutils.core import Publisher from docutils.io import StringOutput, StringInput pub = Publisher() # Initialize the publisher pub.source = StringInput(source=rstText) pub.destination = StringOutput(pub.settings, encoding=enc) pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer(writer) output = pub.publish(argv=['']) #@nonl #@-node:edream.111803100242.5:<< convert rST to HTML/LaTeX >> #@nl convertedFile = file(fname, 'w') convertedFile.write(output) convertedFile.close() rstFile.close() g.es('written: ' + str(fname)) #@nonl #@-node:edream.111803100242.4:<< write rST as HTML/LaTeX >> #@nl else: #@ << write rST file >> #@+node:edream.111803100242.6:<< write rST file >> rstFile = file(fname, 'w') writeTreeAsRst(rstFile, fname, v, c) rstFile.close() g.es('written: ' + str(fname))
SPELL_CHECK = False # Rasta Core Library try: from mainWindow import Ui_Rasta except ImportError: sys.exit(_('Please run "setup.py build" first.')) # Log Model for log view from model import LogTableModel TMPFILE = '/tmp/untitled.rst' # Global Publisher for Docutils PUB = Publisher(source_class=docutils.io.StringInput, destination_class=docutils.io.StringOutput) PUB.set_reader('standalone', None, 'restructuredtext') PUB.set_writer('html') PUB.get_settings() PUB.settings.halt_level = 7 PUB.settings.warning_stream = StringIO() def clear_log(log): ''' Removes not needed lines from log output ''' try: log = unicode(log) return re.findall("line\=\"(.*?)\"", log)[0], re.findall("\<paragraph\>(.*?)\<\/paragraph\>", log)[0] except: return 1, _('Rasta parse error: %s' % log)
properties = controls[control].properties.keys() properties.sort() l(" *Properties*\n%s" % ("\n\n".join([" %s" % prop for prop in properties]),)) methods = controls[control].methods.keys() methods.sort() l("\n *Methods*\n%s" % ("\n\n".join([" %s" % str(controls[control].methods[method]) for method in methods]),)) text = "\n".join(results) # << Convert to HTML >> import StringIO rstFile = StringIO.StringIO(text) from docutils.core import Publisher from docutils.io import StringOutput, StringInput pub = Publisher() # Initialize the publisher pub.source = StringInput(source=text) pub.destination = StringOutput(encoding="utf-8") pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer('html') output = pub.publish() print output # -- end -- << Convert to HTML >>
def onIconDoubleClick(tag,keywords): c = keywords.get("c") p = keywords.get("p") # g.trace(c) if not c or not p: return applyConfiguration(c) config.tag = tag h = p.headString().strip() if g.match_word(h,0,"@rst"): if len(h) > 5: fname = h[5:] ext = os.path.splitext(fname)[1].lower() if ext in ('.htm','.html','.tex'): #@ << write rST as HTML/LaTeX >> #@+node:ekr.20040331071319.4:<< write rST as HTML/LaTeX >> try: import docutils except: g.es('HTML/LaTeX generation requires docutils') return else: import docutils.parsers.rst from docutils.core import Publisher from docutils.io import StringOutput, StringInput, FileOutput,FileInput import StringIO # Set the writer and encoding for the converted file if ext in ('.html','.htm'): writer='html' ; enc="utf-8" else: writer='latex' ; enc="iso-8859-1" syntax = False if writer == 'html': try: import SilverCity #@ << define code-block >> #@+node:ekr.20040331071319.5:<< define code-block >> def code_block(name,arguments,options,content,lineno,content_offset,block_text,state,state_machine): """Create a code-block directive for docutils.""" # See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252170 language = arguments[0] module = getattr(SilverCity,language) generator = getattr(module,language+"HTMLGenerator") io = StringIO.StringIO() generator().generate_html(io, '\n'.join(content)) html = '<div class="code-block">\n%s\n</div>\n'%io.getvalue() raw = docutils.nodes.raw('',html, format='html') #(self, rawsource='', text='', *children, **attributes): return [raw] # These are documented at http://docutils.sourceforge.net/spec/howto/rst-directives.html. code_block.arguments = ( 1, # Number of required arguments. 0, # Number of optional arguments. 0) # True if final argument may contain whitespace. # A mapping from option name to conversion function. code_block.options = { 'language' : docutils.parsers.rst.directives.unchanged # Return the text argument, unchanged } code_block.content = 1 # True if content is allowed. # Register the directive with docutils. docutils.parsers.rst.directives.register_directive('code-block',code_block) config.do_replace_code_blocks = False #@nonl #@-node:ekr.20040331071319.5:<< define code-block >> #@nl syntax = True except ImportError: g.es('SilverCity not present so no syntax highlighting') #@ << define alternate code block implementation>> #@+node:bwmulder.20050326114320: << define alternate code block implementation>> # Don't know what to do here: Can someone make a suggestion? #import docutils.parsers.rst.directives.admonitions #import docutils.parsers.rst.directives.body # docutils.parsers.rst.directives._directives['code-block'] = docutils.parsers.rst.directives.body.block # docutils.parsers.rst.directives.register_directive('code-block', docutils.parsers.rst.directives.admonitions.admonition) #docutils.parsers.rst.directives._directives['code-block'] = docutils.parsers.rst.directives.body.pull_quote # g.es("Registered some alternate implementation for code-block directive") config.do_replace_code_blocks = config.rst2_replace_code_blocks #@-node:bwmulder.20050326114320: << define alternate code block implementation>> #@nl if config.rst2file: rstFileName = os.path.splitext(fname)[0] + ".txt" rstFile = file(rstFileName, "w") g.es("Using %s as rst file" % rstFileName) else: rstFile = StringIO.StringIO() config.current_file = fname writeTreeAsRst(rstFile,fname,p,c,syntax=syntax) if config.rst2file: rstFile.close() else: rstText = rstFile.getvalue() # This code snipped has been taken from code contributed by Paul Paterson 2002-12-05. pub = Publisher() if config.rst2file: pub.source = FileInput(source_path=rstFileName) pub.destination = FileOutput(destination_path=fname, encoding='unicode') else: pub.source = StringInput(source=rstText) pub.destination = StringOutput(pub.settings, encoding=enc) pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer(writer) output = pub.publish(argv=['']) if config.rst2file: pass else: convertedFile = file(fname,'w') convertedFile.write(output) convertedFile.close() rstFile.close() writeFullFileName(fname) return http_support_main(tag, fname) #@-node:ekr.20040331071319.4:<< write rST as HTML/LaTeX >> #@nl else: #@ << write rST file >> #@+node:ekr.20040331071319.6:<< write rST file >> rstFile = file(fname,'w') writeTreeAsRst(rstFile,fname,p,c) rstFile.close() writeFullFileName(fname) #@nonl #@-node:ekr.20040331071319.6:<< write rST file >> #@nl else: # if the headline only contains @rst then open the node and its parent in text editor # this works for me but needs to be generalized and should probably be a component # of the open_with plugin. if 0: c.openWith(("os.startfile", None, ".txt")) c.selectVnode(p.parent()) c.openWith(("os.startfile", None, ".tp"))
def onIconDoubleClick(tag,keywords): v = keywords.get("p") or keywords.get("v") c = keywords.get("c") # g.trace(c) h = v.headString().strip() if g.match_word(h,0,"@text"): fname = h[5:] ext = os.path.splitext(fname)[1].lower() if ext in ('.htm','.html','.tex'): #@ << write rST as HTML/LaTeX >> #@+node:edream.111803100242.4:<< write rST as HTML/LaTeX >> try: import docutils except ImportError: docutils = None g.es('HTML/LaTeX generation requires docutils') if docutils: import StringIO rstFile = StringIO.StringIO() writeTreeAsRst(rstFile, fname, v, c) rstText = rstFile.getvalue() # Set the writer and encoding for the converted file if ext in ('.html','.htm'): writer='html' enc="utf-8" else: writer='latex' enc="iso-8859-1" #@ << convert rST to HTML/LaTeX >> #@+node:edream.111803100242.5:<< convert rST to HTML/LaTeX >> # this code snipped has been taken from code contributed by Paul Paterson 2002-12-05 from docutils.core import Publisher from docutils.io import StringOutput, StringInput pub = Publisher() # Initialize the publisher pub.source = StringInput(source=rstText) pub.destination = StringOutput(pub.settings, encoding=enc) pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer(writer) output = pub.publish(argv=['']) #@nonl #@-node:edream.111803100242.5:<< convert rST to HTML/LaTeX >> #@nl convertedFile = file(fname,'w') convertedFile.write(output) convertedFile.close() rstFile.close() g.es('written: '+str(fname)) #@nonl #@-node:edream.111803100242.4:<< write rST as HTML/LaTeX >> #@nl else: #@ << write rST file >> #@+node:edream.111803100242.6:<< write rST file >> rstFile = file(fname,'w') writeTreeAsRst(rstFile, fname, v, c) rstFile.close() g.es('written: '+str(fname))