Exemplo n.º 1
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)
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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 ''
Exemplo n.º 4
0
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='')
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
Arquivo: ReST.py Projeto: philn/alinea
    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 = ''
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
Arquivo: html.py Projeto: xia2/xia2
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)
Exemplo n.º 10
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)
Exemplo n.º 11
0
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()
Exemplo n.º 12
0
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
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
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)
Exemplo n.º 15
0
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
Exemplo n.º 16
0
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)
Exemplo n.º 17
0
def getPublicCmsReSTWriter():

    global _PublicCmsReSTWriter, _PublicReSTTranslator

    if not _PublicCmsReSTWriter:
        _PublicCmsReSTWriter = Writer()
        _PublicCmsReSTWriter.translator_class = _PublicReSTTranslator

    return _PublicCmsReSTWriter
Exemplo n.º 18
0
    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)
Exemplo n.º 19
0
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'})
Exemplo n.º 20
0
 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
Exemplo n.º 21
0
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'})
Exemplo n.º 22
0
 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
Exemplo n.º 23
0
    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)
Exemplo n.º 24
0
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']
Exemplo n.º 25
0
    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)
Exemplo n.º 26
0
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)
Exemplo n.º 27
0
 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
Exemplo n.º 28
0
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)
Exemplo n.º 29
0
 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
Exemplo n.º 30
0
    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
Exemplo n.º 31
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": "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 ""
Exemplo n.º 32
0
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:
        document = core.publish_string(rst_string,
                                       writer=writer,
                                       settings_overrides=SETTINGS)
    return str(document, "utf-8")
Exemplo n.º 33
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': '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 ''
Exemplo n.º 34
0
def render(raw, stream=None, **kwargs):
    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:
        return None
Exemplo n.º 35
0
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
Exemplo n.º 36
0
    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
Exemplo n.º 37
0
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
Exemplo n.º 38
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 ''
Exemplo n.º 39
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': 'short',
            'math_output': 'latex',
            '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 ''
Exemplo n.º 40
0
    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":
            if fmt[-1].tolower() in ['b','c','d','o','x','n','e','f','g','%']:
Exemplo n.º 41
0
import os.path

from django.conf import settings
from django.template.defaultfilters import slugify
from docutils.core import publish_string
from docutils.writers.html4css1 import Writer, HTMLTranslator

import mezzanine
from mezzanine.utils.importing import path_for_import

# Convert the overview documentation page from RST to HTML.
writer = Writer()
writer.translator_class = HTMLTranslator

try:
    path = os.path.join(path_for_import("mezzanine"), "..", "docs",
                        "overview.rst")
    with open(path, "r") as f:
        data = f.read()
    path = os.path.join(path_for_import("mezzanine"), "..", "README.rst")
    with open(path, "r") as f:
        data = data.replace(".. include:: ../README.rst", f.read())
except IOError:
    OVERVIEW = ""
else:
    OVERVIEW = publish_string(data, writer=writer)

# Grab the list items inside a blockquote after each title, and
# assign them to variables for the homepage.
project_context = {"version": mezzanine.__version__}
for section in ("Sites Using Mezzanine", "Quotes", "Features"):
Exemplo n.º 42
0
                 and atts['href'].startswith('mailto:')):
                atts['href'] = self.cloak_mailto(atts['href'])
                self.in_mailto = 1
            atts['class'] += ' external'
        else:
            assert node.has_key('refid'), \
                   'References must have "refuri" or "refid" attribute.'
            atts['href'] = self.document.settings.url + '#' + node['refid']
            atts['class'] += ' internal'
        if not isinstance(node.parent, nodes.TextElement):
            assert len(node) == 1 and isinstance(node[0], nodes.image)
            atts['class'] += ' image-reference'
        self.body.append(self.starttag(node, 'a', '', **atts))

g_writer = Writer()
g_writer.translator_class = SummaryTranslator

def init_publisher():
    from docutils.core import Publisher
    from docutils.io import StringOutput
    p = Publisher(destination_class=StringOutput,writer=g_writer)
    p.get_settings()
    p.set_components('standalone', 'restructuredtext', 'html')
    p.set_destination(None, None)
    return p

def load_articles(inputfile=sys.stdin):
    src_dir = 'src/'
    filename_reg = re.compile(r'^(\d+-\d+-\d+)_(.*)')
    for fullname in inputfile.readlines():
        fullname = fullname.strip()
Exemplo n.º 43
0
class my_Writer(Writer):
    def translate(self):
        self.visitor = visitor = self.translator_class(self.document)
        self.document.walkabout(visitor)
        for attr in self.visitor_attributes:
            #cur_attr = getattr(visitor, attr)
            #print(attr + '=' + str(cur_attr))
            setattr(self, attr, getattr(visitor, attr))
        #pdb.set_trace()
        self.output = self.apply_template()
        self.output = ''.join(self.body)
        #here is the template:
        #u'%(head_prefix)s\n%(head)s\n%(stylesheet)s\n%(body_prefix)s\n%(body_pre_docinfo)s\n%(docinfo)s\n%(body)s\n%(body_suffix)s\n'
        
_w = Writer()
_w.translator_class = MyHTMLTranslator#NoHeaderHTMLTranslator

_w2 = Writer()
_w2.translator_class = NoHeaderHTMLTranslator

_w0 = my_Writer()
_w0.translator_class = NoHeaderHTMLTranslator0




def reSTify(string):
    return core.publish_string(string,writer=_w)


Exemplo n.º 44
0
 def html(self, input_text):
     from docutils import core
     from docutils.writers.html4css1 import Writer
     writer = Writer()
     writer.translator_class = self.translator_class
     return core.publish_string(input_text, writer=writer)
Exemplo n.º 45
0

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 locale_(plugin_id, s):
    # Search the plugin that's rendering the template for the requested locale
    if plugin_id == 'prism':
        ns = prism.settings.PRISM_LOCALE[s]
    else:
        plugin = prism.get_plugin(plugin_id)

        if plugin is None:
            prism.output('Unknown plugin ID. Offender: %s' % plugin_id)
            return s

        ns = plugin.locale[s]
Exemplo n.º 46
0
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
Exemplo n.º 47
0
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


def rest_to_html(string):
    return core.publish_string(string, writer=_w)


def test1():
    test = """
Test example of reST__ document.

__ http://docutils.sf.net/rst.html

- item 1
- item 2
- item 3
Exemplo n.º 48
0
def rst_to_html(rst):
    html_fragment_writer = Writer()
    html_fragment_writer.translator_class = HTMLTranslator
    return docutils.core.publish_string(rst, writer=html_fragment_writer)
Exemplo n.º 49
0
def main():
    """Parses command-line arguments for an input file, then processes it using PyCite."""
    writer = Writer()
    writer.translator_class = HTMLTranslator
    reader = Reader()
    publish_cmdline(reader=reader, writer=writer)
Exemplo n.º 50
0
        # keyword arguments (attributes) are turned into html tag key/value
        # pairs, e.g. `{'style':'background:red'} => 'style="background:red"'`
        self.body.append(
            self.starttag(node, 'span', '', style='background:red'))

    def depart_foo(self, node):
        self.body.append('</span>')


# register the directive, telling docutils to apply `Foo` when the parser
# encounters a `foo`
directives.register_directive('foo', Foo)

# create a `Writer` and set phasers to `MyHTMLTranslator`
html_writer = Writer()
html_writer.translator_class = MyHTMLTranslator

rest_text = \
'''this is a test
==============

it is only a test

.. foo:: whee
'''

# use the writer to turn some ReST text into a string of HTML
html = docutils.core.publish_string(source=rest_text, writer=html_writer)
print html

# write a webpage (or serve it, or whatever)
Exemplo n.º 51
0
    from reST-formatted docstrings
    """
    def __init__(self, document):
        super().__init__(document)
        self.body_prefix = []
        self.body_suffix = []

    def visit_bullet_list(self, node):
        self.context.append((self.compact_simple, self.compact_p))
        self.compact_p = None
        self.compact_simple = self.is_compactable(node)
        self.body.append(self.starttag(node, 'ul', CLASS='docstring'))


DOCSTRING_WRITER = Writer()
DOCSTRING_WRITER.translator_class = NoHeaderHTMLTranslator


@register.filter
def safe_docstring_display(docstring):
    """
    Utility function to htmlize reST-formatted documentation in browsable
    api.
    """
    docstring = cleandoc(docstring)
    return publish_parts(docstring, writer=DOCSTRING_WRITER)['html_body']


@register.filter
def urlize_links_and_mails(text):
    """Utility function for decorating api links in browsable api.
Exemplo n.º 52
0
 def html(self, input_text):
     from docutils import core
     from docutils.writers.html4css1 import Writer
     writer = Writer()
     writer.translator_class = self.translator_class
     return core.publish_string(input_text, writer=writer)
Exemplo n.º 53
0
def rst_to_html(text):
    """Convert reStructured text to html."""
    w = Writer()
    w.translator_class = NoHeaderHTMLTranslator
    return core.publish_string(text, writer=w)
Exemplo n.º 54
0
def rst_to_html(text):
    """Convert reStructured text to html."""
    w = Writer()
    w.translator_class = NoHeaderHTMLTranslator
    return core.publish_string(text, writer=w)
Exemplo n.º 55
0
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 )
Exemplo n.º 56
0
def reSTify(string):
    _w = Writer()
    _w.translator_class = NoHeaderHTMLTranslator
    return core.publish_string(string, writer=_w)
Exemplo n.º 57
0
def gen_folder():
    project_root = os.path.normpath(os.path.join(__file__, '..', '..'))
    codebase = os.path.join(project_root, 'server', 'codebase')
    configs = (
        {
            'source': os.path.join(codebase, 'docpie'),
            'target': os.path.join(project_root, 'build', 'static', 'docpie'),
        },
        {
            'source': os.path.join(codebase, 'docpie.wiki'),
            'target': os.path.join(project_root, 'build', 'static',
                                   'docpie-wiki'),
        },
    )
    fenced_code_re = re.compile(
        r'(?P<indent>\s+)```(?P<lang>[\w\ \-_]*)(?P<content>.*?)\ +```',
        re.DOTALL)
    for config in configs:
        source_folder = config['source']
        target_folder = config['target']
        if not os.path.isdir(target_folder):
            os.makedirs(target_folder)

        _dirpath, _dirnames, filenames = next(os.walk(source_folder))
        for filename in filenames:
            print('processing {}'.format(filename))
            filebase, fileext = os.path.splitext(filename)
            fileext_lower = fileext.lower()
            if fileext_lower == '.md':
                filetype = 'md'
            elif fileext_lower == '.rst':
                filetype = 'rst'
            else:
                continue
            with open(os.path.join(source_folder, filename),
                      'r',
                      encoding='utf-8') as f:
                content_raw = f.read()

            if '```' in content_raw:
                #                 middle_parts = []
                #                 content_parts = content_raw.split('```')
                #                 # first_part = content_parts.pop(0)
                #                 last_part = content_parts.pop(-1)
                #                 for content, codepart in zip(content_parts[::2], content_parts[1::2]):
                #                     middle_parts.append(content)
                #
                #                     print(codepart)
                #                     code_parts = codepart.splitlines()
                #                     language = code_parts.pop(0)
                #                     code_rejoined = textwrap.dedent('\n'.join(code_parts)).replace('\n','<br />').rstrip()
                #
                #                     middle_parts.append("""
                # <div class="codehilite">
                #     <pre class="language-{lang}"><code>{content}</code></pre>
                # </div>
                # """.format(lang=language, content=code_rejoined)
                #                    )
                #               content = '\n'.join(middle_parts) + last_part
                content = fenced_code_re.sub(
                    lambda matchobj: """
{indent}<div class="codehilite"><pre class="language-{lang}"><code>{content}</code></pre></div>
                """.format(lang=matchobj.groupdict()['lang'],
                           indent=matchobj.groupdict()['indent'].replace(
                               '\n', ''),
                           content=html.escape(
                               textwrap.dedent(matchobj.groupdict()['content'])
                               .rstrip())[1:]).replace('\n', '<br />'),
                    content_raw)
                # print(content)
                # assert False
            else:
                content = content_raw

            if filetype == 'md':
                if filebase == 'Usage-Format':
                    # content = content.replace('\\<argument\\>', '&lt;argument&gt;')
                    content_body = content.split('\n\n', 1)[1].replace(
                        '\\<argument\\>', '&lt;argument&gt;')
                    content = '[TOC]\n\n' + content_body

                html_content = markdown.markdown(
                    content,
                    extensions=[
                        'markdown.extensions.fenced_code',
                        'markdown.extensions.footnotes',
                        'markdown.extensions.codehilite',
                        'markdown.extensions.toc',
                    ])
                # html_content = content
                # html = markdown2.markdown(content, extras=[
                #     'toc',
                #     'fenced-code-blocks',
                #     'footnotes',
                # ])
            elif filetype == 'rst':
                html_fragment_writer = Writer()
                html_fragment_writer.translator_class = HTMLFragmentTranslator
                html_content = core.publish_string(
                    content, writer=html_fragment_writer).decode('utf-8')
            if filebase in ('Home', '_Sidebar'):
                html_content = re.sub(
                    '\\[\\[(.*?)\\]\\]', lambda matchobj:
                    '<a href="/document/{link}">{linkname}</a>'.format(
                        link=matchobj.group(1).replace(' ', '-'),
                        linkname=matchobj.group(1)), html_content)

            soup = BeautifulSoup(html_content, 'html5lib')
            for link in soup.find_all('a'):
                href = link.get('href')
                if href and (href.startswith('http://')
                             or href.startswith('https://')):
                    url_obj = urlparse(href)
                    if url_obj.hostname in ('docpie.comes.today',
                                            'docpie.notexists.top'):
                        url = urlunparse(('', '', url_obj.path, url_obj.params,
                                          url_obj.query, url_obj.fragment))
                        link['href'] = url

            for pre in soup.find_all('pre'):
                # break
                inner_pre = pre.decode_contents()
                pre_class = pre.get('class') or []
                inner_break = inner_pre.replace('\n', '<br />')
                pre_soup = BeautifulSoup(
                    '<pre class="{classes}">{content}</pre>'.format(
                        classes=' '.join(pre_class), content=inner_break),
                    'html5lib')
                pre.replace_with(pre_soup.find('pre'))

            body = soup.body.decode_contents()

            target_filename = filebase + '.html'
            logger.info('saving %s', target_filename)
            with open(os.path.join(target_folder, target_filename),
                      'w',
                      encoding='utf-8') as t:
                t.write(body)
Exemplo n.º 58
0
import os.path

from docutils.core import publish_string
from docutils.writers.html4css1 import Writer,HTMLTranslator

import mezzanine
from mezzanine.utils.importing import path_for_import


# Convert the README file from RST to HTML.
writer = Writer()
writer.translator_class = HTMLTranslator
path = os.path.join(path_for_import("mezzanine"), "..", "README.rst")
try:
    with open(path, "r") as f:
        data = f.read()
except IOError:
    README = ""
else:
    README = publish_string(data, writer=writer)

# Grab the list items inside a blockquote after each title, and
# assign them to variables for the homepage.
project_context = {"version": mezzanine.__version__}
for section in ("Sites Using Mezzanine", "Quotes", "Features"):
    items = README.split("<h2>%s</h2>" % section)[1] \
                  .split("<li>", 1)[1].split("</li>\n</ul>")[0] \
                  .split("</li>\n<li>")
    project_context[section.split()[0].lower()] = items
Exemplo n.º 59
0
    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):
    return _bbcode_markup(bbcode)
Exemplo n.º 60
0
def reSTify(string):
    _w = Writer()
    _w.translator_class = NoHeaderHTMLTranslator
    return core.publish_string(string, writer=_w)