def rst2html(rst): from docutils.core import publish_string from docutils.writers.html4css1 import Writer, HTMLTranslator class xia2HTMLTranslator(HTMLTranslator): def __init__(self, document): HTMLTranslator.__init__(self, document) def visit_table(self, node): self.context.append(self.compact_p) self.compact_p = True classes = ' '.join(['docutils', self.settings.table_style]).strip() self.body.append( self.starttag(node, 'table', CLASS=classes, border="0")) def write_colspecs(self): self.colspecs = [] xia2_root_dir = libtbx.env.find_in_repositories("xia2", optional=False) args = { 'stylesheet_path': os.path.join(xia2_root_dir, 'css', 'voidspace.css') } w = Writer() w.translator_class = xia2HTMLTranslator return publish_string(rst, writer=w, settings=None, settings_overrides=args)
def index(): flask.url_for('static', filename='github.css') flask.url_for('static', filename='semantic-1.2.0/semantic.css') flask.url_for('static', filename='semantic-1.2.0/semantic.js') flask.url_for('static', filename='qubes.css') input_filename = '/home/user/w/qubes-tools/NOTES.rst' # sys.argv.append('NOTES.rst') try: #text = codecs.open(sys.argv[1], 'r', 'utf-8').read() #with open(input_filename, "r", 'utf-8') as source: with open(input_filename, "r") as source: text = source.read().encode('utf-8') except IOError: # given filename could not be found return '' except IndexError: # no filename given text = sys.stdin.read() writer = Writer() writer.translator_class = rest2html.GitHubHTMLTranslator parts = docutils.core.publish_parts(text, writer=writer, settings_overrides=rest2html.SETTINGS) if 'html_body' in parts: print parts['html_body'] html = flask.Markup(parts['html_body']) # publish_parts() in python 2.x return dict values as Unicode type # in py3k Unicode is unavailable and values are of str type if isinstance(html, str): return flask.render_template('index.html', html=html) else: return flask.render_template('index.html', html=html.encode('utf-8')) return flask.render_template('index.html', html='')
def assemble_parts(self): # we will add 2 new parts to the writer: 'first_paragraph_as_text' and # 'images' Writer.assemble_parts(self) self.parts['first_paragraph_as_text'] = \ self.visitor.first_paragraph_as_text self.parts['images'] = self.visitor.images
def main(): # Create a writer to deal with the generic element we may have created. writer = Writer() writer.translator_class = MyTranslator description = ( 'Generates (X)HTML documents from standalone reStructuredText ' 'sources. Be forgiving against unknown elements. ' + default_description) # the parser processes the settings too late: we want to decide earlier if # we are running or testing. if ('--test-patch' in sys.argv and not ('-h' in sys.argv or '--help' in sys.argv)): return test_patch(writer) else: # Make docutils lenient. patch_docutils() overrides = { # If Pygments is missing, code-block directives are swallowed # with Docutils >= 0.9. 'syntax_highlight': 'none', # not available on Docutils < 0.8 so can't pass as an option 'math_output': 'HTML', } publish_cmdline(writer=writer, description=description, settings_spec=LenientSettingsSpecs, settings_overrides=overrides) return 0
def __init__(self): core.Publisher.__init__(self) self.set_reader('standalone', None, 'restructuredtext') writer = Writer() # we don't want HTML headers in our writer writer.translator_class = NoHeaderHTMLTranslator self.writer = writer # go with the defaults self.get_settings() self.settings.xml_declaration = "" # this is needed, but doesn't seem to do anything self.settings._destination = '' # use no stylesheet self.settings.stylesheet = '' self.settings.stylesheet_path = '' self.settings.embed_stylesheet = False # don't break if we get errors self.settings.halt_level = 6 # remember warnings self.warnings = ''
def rst2html(rst): from docutils.core import publish_string from docutils.writers.html4css1 import Writer, HTMLTranslator class xia2HTMLTranslator(HTMLTranslator): def __init__(self, document): HTMLTranslator.__init__(self, document) def visit_table(self, node): self.context.append(self.compact_p) self.compact_p = True classes = " ".join(["docutils", self.settings.table_style]).strip() self.body.append( self.starttag(node, "table", CLASS=classes, border="0")) def write_colspecs(self): self.colspecs = [] xia2_root_dir = os.path.dirname(xia2.__file__) args = { "stylesheet_path": os.path.join(xia2_root_dir, "css", "voidspace.css") } w = Writer() w.translator_class = xia2HTMLTranslator return publish_string(rst, writer=w, settings=None, settings_overrides=args)
def main(): """ Parses the given ReST file or the redirected string input and returns the HTML body. Usage: rest2html < README.rst rest2html README.rst """ try: text = codecs.open(sys.argv[1], 'r', 'utf-8').read() except IOError: # given filename could not be found return '' except IndexError: # no filename given text = sys.stdin.read() writer = Writer() writer.translator_class = GitHubHTMLTranslator parts = publish_parts(text, writer=writer, settings_overrides=SETTINGS) if 'html_body' in parts: html = parts['html_body'] # publish_parts() in python 2.x return dict values as Unicode type # in py3k Unicode is unavailable and values are of str type if isinstance(html, str): return html else: return html.encode('utf-8') return ''
def render(raw: str, stream: Optional[IO[str]] = None, **kwargs: Any) -> Optional[str]: if stream is None: # Use a io.StringIO as the warning stream to prevent warnings from # being printed to sys.stderr. stream = io.StringIO() settings = SETTINGS.copy() settings["warning_stream"] = stream writer = Writer() writer.translator_class = ReadMeHTMLTranslator try: parts = publish_parts(raw, writer=writer, settings_overrides=settings) except SystemMessage: rendered = None else: rendered = parts.get("docinfo", "") + parts.get("fragment", "") if rendered: return clean(rendered) else: # If the warnings stream is empty, docutils had none, so add ours. if not stream.tell(): stream.write("No content rendered from RST source.") return None
def rst2html(rst): from docutils.core import publish_string from docutils.writers.html4css1 import Writer,HTMLTranslator class xia2HTMLTranslator(HTMLTranslator): def __init__(self, document): HTMLTranslator.__init__(self, document) def visit_table(self, node): self.context.append(self.compact_p) self.compact_p = True classes = ' '.join(['docutils', self.settings.table_style]).strip() self.body.append( self.starttag(node, 'table', CLASS=classes, border="0")) def write_colspecs(self): self.colspecs = [] xia2_root_dir = libtbx.env.find_in_repositories("xia2", optional=False) args = { 'stylesheet_path': os.path.join(xia2_root_dir, 'css', 'voidspace.css') } w = Writer() w.translator_class = xia2HTMLTranslator return publish_string(rst, writer=w, settings=None, settings_overrides=args)
def publishPage(self, key): src = self.pages[key].rst pubpath = os.path.join(rootpath, "public", key) try: os.makedirs(os.path.split(pubpath)[0]) except: pass # Soft reset traveler.features.reset() writer = Writer() writer.translator_class = HTMLTranslatorForLegalResourceRegistry keylst = key.split("/") upset = [".."] * (len(keylst) - 1) css = os.path.sep.join(upset + ["screen.css"]) octicons = os.path.sep.join( upset + ["bower_components/octicons/octicons/octicons.css"]) options = { "stylesheet": octicons + "," + css, "stylesheet_path": None, "embed_stylesheet": False, "footnote_backlinks": True, "input_encoding": "utf-8" } src = src + '\n.. raw:: html\n\n </div>\n' html = publish_string(src, reader=None, reader_name='standalone', writer=writer, settings_overrides=options) if self.writePages: codecs.open(pubpath, "w+", "utf-8").write(html)
def render_rst(rst_filename): class HTMLFragmentTranslator(HTMLTranslator): def __init__(self, document): HTMLTranslator.__init__(self, document) self.head_prefix = ['', '', '', '', ''] self.body_prefix = [] self.body_suffix = [] self.stylesheet = [] def astext(self): return ''.join(self.body) html_fragment_writer = Writer() html_fragment_writer.translator_class = HTMLFragmentTranslator def rest_to_html(s): result = core.publish_string(s, writer=html_fragment_writer) result = result.decode('utf8') root = lxml.html.fromstring(result) nodes = root.xpath('//div[@class="document"]') assert len(nodes) == 1 return lxml.html.tostring(nodes[0], encoding=str) if not rst_filename: return u'' elif rst_filename.endswith('.rst'): fn = os.path.join(os.path.dirname(__file__), 'content', rst_filename) if not os.path.exists(fn): raise IOError('RST file {} not found'.format(fn)) with open(fn) as fp: rst_data = fp.read() return rest_to_html(rst_data) else: return rest_to_html(rst_filename)
def convert_string_RST(myString, use_mathjax=False): """ Converts a valid reStructuredText_ input string into rich HTML. :param str myString: the candidate reStructuredText_ input. :param bool use_mathjax: if ``True``, then use MathJax_ for math formulae. Default is ``False``. :returns: If the input string is valid reStructuredText_, returns the rich HTML as a :py:class:`string <str>`. Otherwise emits a :py:meth:`logging error message <logging.error>` and returns ``None``. :rtype: str .. seealso:: :py:meth:`check_valid_RST <nprstuff.npremail.check_valid_RST>`. """ if not check_valid_RST(myString): logging.error("Error, could not convert %s into RST." % myString) return None overrides = { 'input_encoding': 'unicode', 'doctitle_xform': True, 'initial_header_level': 1 } if use_mathjax: overrides['math_output'] = 'mathjax' htmlwriter = Writer() htmlwriter.translator_class = MyHTMLTranslator parts = core.publish_parts(source=myString, source_path=None, destination_path=None, writer_name='html', writer=htmlwriter, settings_overrides=overrides) html_body = parts['whole'] html = BeautifulSoup(html_body, 'lxml') return html.prettify()
def check_valid_RST(myString, use_mathjax=False): """ Checks to see whether the input string is valid reStructuredText_. :param str myString: the candidate reStructuredText_ input. :param bool use_mathjax: if ``True``, then use MathJax_ for math formulae. Default is ``False``. :returns: ``True`` if valid, otherwise ``False``. :rtype: bool .. seealso:: :py:meth:`convert_string_RST <nprstuff.npremail.convert_string_RST>`. .. _MathJax: https://www.mathjax.org """ overrides = { 'input_encoding': 'unicode', 'doctitle_xform': True, 'initial_header_level': 1 } if use_mathjax: overrides['math_output'] = 'mathjax' htmlwriter = Writer() htmlwriter.translator_class = MyHTMLTranslator parts = core.publish_parts(source=myString, source_path=None, destination_path=None, writer_name='html', writer=htmlwriter, settings_overrides=overrides) body = parts['body'] html = BeautifulSoup(body, 'lxml') error_messages = html.find_all('p', {'class': 'system-message-title'}) return len(error_messages) == 0
def publishPage(self,key): src = self.pages[key].rst pubpath = os.path.join(rootpath,"public",key) try: os.makedirs(os.path.split(pubpath)[0]) except: pass # Soft reset traveler.features.reset() writer = Writer() writer.translator_class = HTMLTranslatorForLegalResourceRegistry keylst = key.split("/") upset = [".."] * (len(keylst)-1) css = os.path.sep.join(upset + ["screen.css"]) octicons = os.path.sep.join(upset + ["bower_components/octicons/octicons/octicons.css"]) options = { "stylesheet": octicons + "," + css, "stylesheet_path": None, "embed_stylesheet": False, "footnote_backlinks": True, "input_encoding": "utf-8" } src = src + '\n.. raw:: html\n\n </div>\n' html = publish_string(src, reader=None, reader_name='standalone', writer=writer, settings_overrides=options) if self.writePages: codecs.open(pubpath, "w+", "utf-8").write(html)
def rst_to_html(rst_string, settings=None): """Convert a string written in reStructuredText to an HTML string. :param rst_string: A string that holds the contents of a reStructuredText document. :param settings: Optional. A dictionary which overrides the default settings. For a list of the possible keys and values, see http://docutils.sourceforge.net/docs/user/config.html. To log the values used, pass in ``'dump_settings': 'yes'``. """ writer = Writer() writer.translator_class = HTMLTranslator if settings is None: settings = { # Include a time/datestamp in the document footer. 'datestamp': '%Y-%m-%d %H:%M UTC', # Recognize and link to standalone PEP references (like "PEP 258"). 'pep_references': 1, # Do not report any system messages. 'report_level': 'none', # Recognize and link to standalone RFC references (like "RFC 822"). 'rfc_references': 1, } return core.publish_string(rst_string, writer=writer, settings_overrides=settings)
def getPublicCmsReSTWriter(): global _PublicCmsReSTWriter, _PublicReSTTranslator if not _PublicCmsReSTWriter: _PublicCmsReSTWriter = Writer() _PublicCmsReSTWriter.translator_class = _PublicReSTTranslator return _PublicCmsReSTWriter
def test_docutils(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") # from https://gist.github.com/mastbaum/2655700 import docutils.core from docutils.nodes import TextElement, Inline from docutils.parsers.rst import Directive, directives from docutils.writers.html4css1 import Writer, HTMLTranslator class foo(Inline, TextElement): pass class Foo(Directive): required_arguments = 1 optional_arguments = 0 has_content = True def run(self): thenode = foo(text=self.arguments[0]) return [thenode] class MyHTMLTranslator(HTMLTranslator): def __init__(self, document): HTMLTranslator.__init__(self, document) def visit_foo(self, node): self.body.append(self.starttag( node, 'span', '', style='background:red')) self.body.append("<!--foo-->") def depart_foo(self, node): self.body.append('<!--foo--></span>') directives.register_directive('foo', Foo) html_writer = Writer() html_writer.translator_class = MyHTMLTranslator rest_text = ''' this is a test ============== it is only a test .. foo:: whee '''.replace(" ", "") bhtml = docutils.core.publish_string( source=rest_text, writer=html_writer) typstr = str # unicode# html = typstr(bhtml, encoding="utf8") if "<!--foo-->" not in html: raise Exception(html)
def translate(self): # Build the document Writer.translate(self) # Build the contents contents = self.build_contents(self.document) contents_doc = self.document.copy() contents_doc.children = contents contents_visitor = self.translator_class(contents_doc) contents_doc.walkabout(contents_visitor) self.parts['toc'] = ''.join(contents_visitor.fragment)
def write(): """A simple command to get the notebook document. Now defaults to HTML.""" import docutils, docutils.io from docutils.writers.html4css1 import Writer out = docutils.io.FileOutput() out.encoding = 'unicode' w = Writer() w.write(document, out)
def convert_string_fragment(string): """ Converts a string of reST text into an html fragment (ie no <header>, <body>, etc tags). """ html_fragment_writer = Writer() html_fragment_writer.translator_class = HTMLFragmentTranslator return core.publish_string(_fix_indent(string), writer = html_fragment_writer, # Suppress output of warnings in html settings_overrides={'report_level':'5'})
def form_example(self, ctx): form = formal.Form() form.addField('restString', formal.String(required=True), widgetFactory=formal.ReSTTextArea) if docutilsAvailable: w = Writer() w.translator_class = CustomisedHTMLTranslator form.addField('customRestString', formal.String(required=True), formal.widgetFactory(formal.ReSTTextArea, restWriter=w)) form.addAction(self.submitted) return form
def convert_string_fragment(string): """ Converts a string of reST text into an html fragment (ie no <header>, <body>, etc tags). """ html_fragment_writer = Writer() html_fragment_writer.translator_class = HTMLFragmentTranslator return core.publish_string( _fix_indent(string), writer=html_fragment_writer, # Suppress output of warnings in html settings_overrides={'report_level': '5'})
def renderHtml(string): class NoHeaderHTMLTranslator(HTMLTranslator): def __init__(self, document): HTMLTranslator.__init__(self, document) self.head_prefix = ['', '', '', '', ''] self.body_prefix = [] self.body_suffix = [] self.stylesheet = [] w = Writer() w.translator_class = NoHeaderHTMLTranslator r = Reader() return core.publish_string(string, writer=w, reader=r)
def output(self): writer = Writer() output = StringOutput(encoding="utf8") mydoc = copy.deepcopy(self.document.source) mydoc.reporter = self.document.source.reporter mydoc.settings = \ OptionParser(components=(Writer,)).get_default_values() if config.footer: mydoc.append( docutils.nodes.footer(config.footer, docutils.nodes.Text(config.footer))) return writer.write(mydoc, output)
def form_example(self, ctx): form = formal.Form() form.addField('restString', formal.String(required=True), widgetFactory=formal.ReSTTextArea) if docutilsAvailable: w = Writer() w.translator_class = CustomisedHTMLTranslator form.addField( 'customRestString', formal.String(required=True), formal.widgetFactory(formal.ReSTTextArea, restWriter=w)) form.addAction(self.submitted) return form
def restructured_to_html(s): """ Convert RST string to HTML string. """ if not s: return s html_fragment_writer = Writer() html_fragment_writer.translator_class = HTMLFragmentTranslator #html = docutils.core.publish_string(s, writer=html_fragment_writer) parts = docutils.core.publish_parts(source=s, writer_name='html') return parts['body_pre_docinfo']+parts['fragment']
def test_docutils(self): # from https://gist.github.com/mastbaum/2655700 import docutils.core from docutils.nodes import TextElement, Inline from docutils.parsers.rst import Directive from docutils.writers.html4css1 import Writer, HTMLTranslator class foo(Inline, TextElement): pass class Foo(Directive): required_arguments = 1 optional_arguments = 0 has_content = True def run(self): thenode = foo(text=self.arguments[0]) return [thenode] class MyHTMLTranslator(HTMLTranslator): def __init__(self, document): HTMLTranslator.__init__(self, document) def visit_foo(self, node): self.body.append( self.starttag(node, 'span', '', style='background:red')) self.body.append("<!--foo-->") def depart_foo(self, node): self.body.append('<!--foo--></span>') directives.register_directive('foo', Foo) html_writer = Writer() html_writer.translator_class = MyHTMLTranslator rest_text = ''' this is a test ============== it is only a test .. foo:: whee '''.replace(" ", "") bhtml = docutils.core.publish_string(source=rest_text, writer=html_writer) typstr = str # unicode# html = typstr(bhtml, encoding="utf8") if "<!--foo-->" not in html: raise Exception(html)
def about(request, template="about.html"): """ Convert the README file into HTML. """ from docutils.core import publish_string from docutils.writers.html4css1 import Writer, HTMLTranslator writer = Writer() writer.translator_class = HTMLTranslator with open(join(settings.PROJECT_ROOT, "README.rst"), "r") as f: about = publish_string(f.read(), writer=writer) with open(join(settings.PROJECT_ROOT, "LICENSE"), "r") as f: license = publish_string(f.read(), writer=writer) context = {"about": about, "license": license} return render(request, template, context)
def rst2html(rst, path, context, request): lstrip_rst = rst.lstrip() if lstrip_rst and lstrip_rst[0] == '<': return rst settings = { 'halt_level': 6, 'input_encoding': 'UTF-8', 'output_encoding': 'UTF-8', 'initial_header_level': 2, 'file_insertion_enabled': 1, 'raw_enabled': 1, 'writer_name': 'html', 'language_code': 'zh_cn', 'context': context, 'request': request } # 表格生成的时候,会出现一个border=1,需要去除 rst = rst.replace('border="1"', '') html_content = docutils.core.publish_parts( rst, source_path=path, writer=Writer(), settings_overrides=settings, )['html_body'] return html_content
def render(self, request, text, settings_overrides={}): overrides = { 'halt_level': 6, 'input_encoding': 'unicode', 'output_encoding': 'unicode', 'initial_header_level': 3, } overrides.update(settings_overrides) writer = Writer() writer.translator_class = Translator html = docutils.core.publish_string( text, writer=writer, settings_overrides=overrides, ) return html
def _render_html(source): return core.publish_parts( source=source, writer=Writer(), writer_name='html', settings_overrides={'no_system_messages': True} )
def render_rest(content): # start by h2 and ignore invalid directives and so on # (most likely from Sphinx) settings = {'initial_header_level': 2, 'report_level': 'quiet'} return publish_parts(content, writer=Writer(), settings_overrides=settings).get('html_body')
def render(self): ''' Render the source to HTML ''' if self._v_formatted is None: warnings = self._v_warnings = Warnings() settings = { 'halt_level': 6, 'report_level': int(self.report_level), 'input_encoding': self.input_encoding, 'output_encoding': self.output_encoding, 'initial_header_level': 1, 'stylesheet': self.stylesheet, 'stylesheet_path': None, 'warning_stream': warnings, 'raw_enabled': 0, 'file_insertion_enabled': 0, } self._v_formatted = docutils.core.publish_string( self.source, writer=Writer(), settings_overrides=settings, ) return self._v_formatted
def rst2odt(rst): from docutils.core import publish_string from docutils.writers.odf_odt import Writer w = Writer() return publish_string(rst, writer=w)
def rst2latex(rst): from docutils.core import publish_string from docutils.writers.latex2e import Writer w = Writer() return publish_string(rst, writer=w)
def description_from_readme_rst( rst_text: str) -> Tuple[Optional[str], Iterable[UpstreamDatum]]: """Description from README.rst.""" if platform.python_implementation() == "PyPy": logger.debug( 'docutils does not appear to work on PyPy, skipping README.rst.') return None, {} try: from docutils.core import publish_parts except ModuleNotFoundError: logger.debug('docutils not available, not parsing README.rst') return None, {} from docutils.writers.html4css1 import Writer settings = {'initial_header_level': 2, 'report_level': 0} html_text = publish_parts(rst_text, writer=Writer(), settings_overrides=settings).get('html_body') try: from bs4 import BeautifulSoup, FeatureNotFound except ModuleNotFoundError: logger.debug('BeautifulSoup not available, not parsing README.rst') return None, {} try: soup = BeautifulSoup(html_text, 'lxml') except FeatureNotFound: logger.debug('lxml not available, not parsing README.rst') return None, {} return _description_from_basic_soup(list(soup.body.children)[0])
def parse_rst_parts(rst_input, system_message_re=SYSTEM_MESSAGE_RE, logger=logger): if not isinstance(rst_input, str): rst_input = '\n'.join(rst_input) rst_error = None parsing_error = None settings_overrides = { 'initial_header_level': '2', 'traceback': True, 'warning_stream': StringIO(), 'embed_stylesheet': False, 'dump_settings': False, 'halt_level': 3, } try: return publish_parts( settings_overrides=settings_overrides, writer=Writer(), source=rst_input, ) except SystemMessage as e: rst_error = e # parse docutils.utils.SystemMessage and re-raise SystemMessage # on parsing error try: result = system_message_re.search(rst_error.args[0]) result = result.groupdict() result['level'] = int(result['level']) result['line'] = int(result['line']) if result['short_description'][-1] == '.': result['short_description'] = result['short_description'][:-1] if 'long_description' not in result: result['long_description'] = '' except Exception as e: parsing_error = e logger.error('%s raised while analyzing %s', parsing_error, rst_error, exc_info=True) if parsing_error: raise rst_error raise reStructuredTextError(result['short_description'], **{ 'system_message': rst_error, **result, })
def render(self, settings_overrides={}): # default settings for the renderer overrides = { 'halt_level': 6, 'input_encoding': 'unicode', 'output_encoding': 'unicode', 'initial_header_level': 3, } overrides.update(settings_overrides) writer = Writer() writer.translator_class = HTMLTranslator html = docutils.core.publish_file( source_path=self.context, writer=writer, settings_overrides=overrides, ) return html
def render_page(pagename): """Render page with docutils""" path = os.path.abspath(os.path.join(DOC_DIR, pagename)) print "rendering: %s" % path raw = open(path, 'r').read() html_writer = Writer() html = publish_string(raw, writer=html_writer) return render_template('page.jinja', pagename=pagename, html=html)
def __init__(self): self.writer = Writer() self.writer.translator_class = GitHubHTMLTranslator roles.register_canonical_role('kbd', kbd) # Render source code in Sphinx doctest blocks directives.register_directive('doctest', DoctestDirective)
def render(raw): # Use a io.StringIO as the warning stream to prevent warnings from being # printed to sys.stderr. settings = SETTINGS.copy() settings["warning_stream"] = io.StringIO() writer = Writer() writer.translator_class = ReadMeHTMLTranslator try: parts = publish_parts(raw, writer=writer, settings_overrides=settings) except SystemMessage: rendered = None else: rendered = parts.get("fragment") return clean(rendered or raw), bool(rendered)
def render(self): settings_overrides = { 'halt_level': 6, 'input_encoding': 'utf8', 'output_encoding': 'utf8', 'initial_header_level': 2, # don't try to include the stylesheet (docutils gets hiccups) 'stylesheet_path': '', } writer = Writer() writer.translator_class = ZopeTranslator html = docutils.core.publish_string( self.content, writer=writer, settings_overrides=settings_overrides, ) return html
def render_rest(content, base_url): # start by h2 and ignore invalid directives and so on # (most likely from Sphinx) # # base_url is accepted for compatibility with other render_* # functions but not used #pylint: disable=unused-argument settings = {'initial_header_level': 2, 'report_level': 0} return publish_parts(content, writer=Writer(), settings_overrides=settings).get('html_body')
def render(self, text, **kwargs): settings_overrides = { "cloak_email_addresses": True, "file_insertion_enabled": False, "raw_enabled": False, "strip_comments": True, "doctitle_xform": False, "report_level": 5, "syntax_highlight": "none", "math_output": "latex", "input_encoding": "utf-8", "output_encoding": "utf-8", } writer = Writer() writer.translator_class = GitHubHTMLTranslator output = publish_parts(text, writer=writer, settings_overrides=settings_overrides) if "html_body" in output: return output["html_body"] return ""
def visit_perform(self, node): # Ideally, we should use sphinx but this is a simple temporary solution from docutils import core from docutils.writers.html4css1 import Writer w = Writer() try: res = core.publish_parts(node.rule_docstring, writer=w)['html_body'] self.body.append('<div class="">' + res + '</div>' ) node.children = [] except Exception as err: print(err) self.body.append('<div class=""> no docstring </div>' )
def render(raw, stream=None): if stream is None: # Use a io.StringIO as the warning stream to prevent warnings from # being printed to sys.stderr. stream = io.StringIO() settings = SETTINGS.copy() settings["warning_stream"] = stream writer = Writer() writer.translator_class = ReadMeHTMLTranslator try: parts = publish_parts(raw, writer=writer, settings_overrides=settings) except SystemMessage: rendered = None else: rendered = parts.get("fragment") if rendered: return clean(rendered) else: return None
def render(self): r"""See zope.app.interfaces.renderer.IHTMLRenderer Let's make sure that inputted unicode stays as unicode: >>> renderer = ReStructuredTextToHTMLRenderer(u'b\xc3h', None) >>> renderer.render() u'<p>b\xc3h</p>\n' """ settings_overrides = { 'halt_level': 6, 'input_encoding': 'unicode', 'output_encoding': 'unicode', 'initial_header_level': 3 } writer = Writer() writer.translator_class = ZopeTranslator html = docutils.core.publish_string( self.context, writer=writer, settings_overrides=settings_overrides, ) return html
def main(): # Create a writer to deal with the generic element we may have created. writer = Writer() writer.translator_class = MyTranslator description = ( 'Generates (X)HTML documents from standalone reStructuredText ' 'sources. Be forgiving against unknown elements. ' + default_description) # the parser processes the settings too late: we want to decide earlier if # we are running or testing. if ('--test-patch' in sys.argv and not ('-h' in sys.argv or '--help' in sys.argv)): return test_patch(writer) else: # Make docutils lenient. patch_docutils() publish_cmdline(writer=writer, description=description, settings_spec=LenientSettingsSpecs) return 0
def render(self, text, **kwargs): settings_overrides = { 'cloak_email_addresses': True, 'file_insertion_enabled': False, 'raw_enabled': False, 'strip_comments': True, 'doctitle_xform': False, 'report_level': 5, 'syntax_highlight': 'none', 'math_output': 'MathJax', 'input_encoding': 'utf-8', 'output_encoding': 'utf-8', 'stylesheet_dirs': [os.path.normpath(os.path.join(docutils_dir, Writer.default_stylesheet))], 'template': os.path.normpath(os.path.join(docutils_dir, Writer.default_template)) } writer = Writer() writer.translator_class = GitHubHTMLTranslator output = publish_parts( text, writer=writer, settings_overrides=settings_overrides ) if 'html_body' in output: return output['html_body'] return ''
def __init__(self, builder): Writer.__init__(self) self.builder = builder
def __init__(self, builder): # type: (StandaloneHTMLBuilder) -> None Writer.__init__(self) self.builder = builder
except: PKGINFO = False try: from docutils import core from docutils.writers.html4css1 import Writer,HTMLTranslator class NoHeaderHTMLTranslator(HTMLTranslator): def __init__(self, document): HTMLTranslator.__init__(self, document) self.head_prefix = ['','','','',''] self.body_prefix = [] self.body_suffix = [] self.stylesheet = [] _w = Writer() _w.translator_class = NoHeaderHTMLTranslator RESTIFY=True except: RESTIFY = False import math if sys.version_info > (3,): long = int class Size( long ): """ define a size class to allow custom formatting Implements a format specifier of S for the size class - which displays a human readable in b, kb, Mb etc """ def __format__(self, fmt): if fmt == "" or fmt[-1] != "S":
def __init__(self): Writer.__init__(self) self.translator_class = PelicanHTMLTranslator
from docutils import core from docutils.writers.html4css1 import Writer,HTMLTranslator class HTMLFragmentTranslator( HTMLTranslator ): def __init__( self, document ): HTMLTranslator.__init__( self, document ) self.head_prefix = ['','','','',''] self.body_prefix = [] self.body_suffix = [] self.stylesheet = [] def astext(self): return ''.join(self.body) html_fragment_writer = Writer() html_fragment_writer.translator_class = HTMLFragmentTranslator def reST_to_html( s ): return core.publish_string( s, writer = html_fragment_writer )
def __init__(self): Writer.__init__(self) self.translator_class = HTMLTranslatorForLegalCitem
def __init__(self, document): HTMLTranslator.__init__(self, document) self.head = "" self.head_prefix = ['', '', '', '', ''] self.body_prefix = [] self.body_suffix = [] self.stylesheet = [] def visit_document(self, node): pass def depart_document(self, node): self.fragment = self.body _w = Writer() _w.translator_class = CleanedHTMLTranslator def get_html_from_rst(rst): return core.publish_string(rst, writer=_w) def get_html_from_md(md): return misaka.html(md) _bbcode_markup = postmarkup.create() def get_html_from_bbcode(bbcode):
def render(raw): settings = { # Cloaking email addresses provides a small amount of additional # privacy protection for email addresses inside of a chunk of ReST. "cloak_email_addresses": True, # Prevent a lone top level heading from being promoted to document # title, and thus second level headings from being promoted to top # level. "doctitle_xform": True, # Set our initial header level "initial_header_level": 2, # Prevent local files from being included into the rendered output. # This is a security concern because people can insert files # that are part of the system, such as /etc/passwd. "file_insertion_enabled": False, # Halt rendering and throw an exception if there was any errors or # warnings from docutils. "halt_level": 2, # Output math blocks as LaTeX that can be interpreted by MathJax for # a prettier display of Math formulas. "math_output": "MathJax", # Disable raw html as enabling it is a security risk, we do not want # people to be able to include any old HTML in the final output. "raw_enabled": False, # Disable all system messages from being reported. "report_level": 5, # Use typographic quotes, and transform --, ---, and ... into their # typographic counterparts. "smart_quotes": True, # Strip all comments from the rendered output. "strip_comments": True, # Use the short form of syntax highlighting so that the generated # Pygments CSS can be used to style the output. "syntax_highlight": "short", # Use a io.StringIO as the warning stream to prevent warnings from # being printed to sys.stderr. "warning_stream": io.StringIO(), } writer = Writer() writer.translator_class = HTMLTranslator try: parts = publish_parts(raw, writer=writer, settings_overrides=settings, ) except SystemMessage as exc: raise ValueError(exc.message) rendered = parts.get("fragment") if rendered is None: raise ValueError("There was no rendered value") return rendered