Ejemplo n.º 1
0
def entry_point(source, use_proxy=False):
    from docutils.core import publish_string
    if use_proxy:
        return publish_string(source, writer_name="codeplex",
                writer=Writer(translator=TranslatorWithProxy))
    return publish_string(source, writer_name="codeplex",
            writer=Writer(translator=CodePlexTranslator))
Ejemplo n.º 2
0
    def showDocs(self):
        """
        Display info from filter design file and docstring
        """

        if hasattr(fb.filObj,'info'):
            if self.chkRichText.isChecked():
                self.txtFiltInfoBox.setText(publish_string(
                    self.cleanDoc(fb.filObj.info), writer_name='html',
                    settings_overrides={'output_encoding': 'unicode'}))
            else:
                self.txtFiltInfoBox.setText(textwrap.dedent(fb.filObj.info))

        else:
            self.txtFiltInfoBox.setText("")


        if self.chkDocstring.isChecked() and hasattr(fb.filObj,'info_doc'):
            if self.chkRichText.isChecked():
                self.txtFiltInfoBox.append(
                '<hr /><b>Python module docstring:</b>\n')
                for doc in fb.filObj.info_doc:
                    self.txtFiltInfoBox.append(publish_string(
                     self.cleanDoc(doc), writer_name='html',
                        settings_overrides = {'output_encoding': 'unicode'}))
            else:
                self.txtFiltInfoBox.append('\nPython module docstring:\n')
                for doc in fb.filObj.info_doc:
                    self.txtFiltInfoBox.append(self.cleanDoc(doc))

#        self.txtFiltInfoBox.textCursor().setPosition(pos) # no effect
        self.txtFiltInfoBox.moveCursor(QtGui.QTextCursor.Start)
Ejemplo n.º 3
0
    def _show_doc(self):
        """
        Display info from filter design file and docstring
        """
        if hasattr(ff.fil_inst,'info'):
            if self.chkRichText.isChecked():
                self.txtFiltInfoBox.setText(publish_string(
                    self._clean_doc(ff.fil_inst.info), writer_name='html',
                    settings_overrides={'output_encoding': 'unicode'}))
            else:
                self.txtFiltInfoBox.setText(textwrap.dedent(ff.fil_inst.info))
        else:
            self.txtFiltInfoBox.setText("")

        if self.chkDocstring.isChecked() and hasattr(ff.fil_inst,'info_doc'):
            if self.chkRichText.isChecked():
                self.txtFiltInfoBox.append(
                '<hr /><b>Python module docstring:</b>\n')
                for doc in ff.fil_inst.info_doc:
                    self.txtFiltInfoBox.append(publish_string(
                     self._clean_doc(doc), writer_name='html',
                        settings_overrides = {'output_encoding': 'unicode'}))
            else:
                self.txtFiltInfoBox.append('\nPython module docstring:\n')
                for doc in ff.fil_inst.info_doc:
                    self.txtFiltInfoBox.append(self._clean_doc(doc))

        self.txtFiltInfoBox.moveCursor(QTextCursor.Start)
def parse_docstring(docstring, errors, **options):
  writer = _DocumentPseudoWriter()
  reader = _EpydocReader(errors) # Outputs errors to the list.
  publish_string(docstring, writer=writer, reader=reader,
                 settings_overrides={'report_level': 10000,
                                     'halt_level': 10000,
                                     'warning_stream': None})
  return MyParsedRstDocstring(writer.document)
Ejemplo n.º 5
0
 def do(self, source):
     writer = Writer()
     core.publish_string(source, writer=writer)
     meta = Meta()
     meta.title = ''.join(writer.title)
     meta.slug = None
     meta.labels = []
     meta.subtitle = ''.join(writer.subtitle)
     meta.footer = ''.join(writer.footer)
     return ''.join(writer.body), meta
Ejemplo n.º 6
0
def html_string(text):
    """
    Converts the strng from restructuredText to HTML and prepends
    the CSS string.
    """
    try:
        return __CSS + publish_string(
            text,
            writer_name="html",
            settings_overrides={'report_level': 'quiet'}, )
    except TypeError:
        return __CSS + publish_string(text, writer_name="html")
Ejemplo n.º 7
0
    def test_README_as_PyPi_landing_page(self):
        from docutils import core as dcore

        long_desc = subprocess.check_output(
                'python setup.py --long-description'.split(),
                cwd=proj_path)
        self.assertIsNotNone(long_desc, 'Long_desc is null!')

        with patch('sys.exit'):
            dcore.publish_string(long_desc, enable_exit_status=False,
                    settings_overrides={ # see `docutils.frontend` for more.
                            'halt_level': 2 # 2=WARN, 1=INFO
                    })
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
def main():
    benchmarks = {}
    # Run all of the performance comparisons.
    print tests

    modules = MODULE_LIST
    for testname, method in tests.iteritems():
        benchmarks[testname] = {}
        results = [None for module in modules]
        for i, module in enumerate(modules):
            print 'module', module
            # We pre-compile the pattern, because that's
            # what people do.
            current_re[0] = module.compile(method.pattern)

            try:
                results[i] = method(current_re[0], **method.data)
            except:
                results[i] = -1

            # Run a test.
            t = Timer("test(current_re[0],**test.data)",
                      setup_code % testname)
            try:
                benchmarks[testname][module.__name__] = (t.timeit(method.num_runs),
                                                         method.__doc__.strip(),
                                                         method.pattern,
                                                         method.num_runs)
            except:
                benchmarks[testname][module.__name__] = (-1,
                                                         method.__doc__.strip(),
                                                         method.pattern,
                                                         method.num_runs)
                

        for i in xrange(len(results)):
            print >> sys.stderr, 'result', modules[i], results[i]

        for i in range(len(results) - 1):
            if results[i] != results[i + 1]:
                print >> sys.stderr, modules[i], results[i]
                print >> sys.stderr, modules[i+1], results[i+1]
                #raise ValueError("re2 output is not the same as re output: %s" % testname)
                print >> sys.stderr, "re2 output is not the same as re output: %s" % testname

    txt = benchmarks_to_ReST(benchmarks, modules)

    from docutils.core import publish_string
    print publish_string(txt, writer_name='html')
    print txt
Ejemplo n.º 10
0
def convert_rest_to_wp(restFile):
    'convert ReST to WP html using docutils, rst2wp'
    from docutils.core import publish_string
    import rst2wp
    from xml.etree.ElementTree import XML
    ifile = file(restFile) # read our restructured text
    rest = ifile.read()
    ifile.close()
    xhtml = publish_string(rest, writer_name='xml')
    x = XML(xhtml) # parse the XML text
    title = x.find('title').text #extract its title
    writer = rst2wp.Writer()
    html = publish_string(rest, writer=writer) # convert to wordpress
    return title,html
Ejemplo n.º 11
0
def rst_file_to_html(file_path):
    with open(settings.PROJECT_PATH + "/" + file_path) as f:
        content = f.readlines()
    a_line = "".join(content)
    html = core.publish_string(source=a_line, writer_name="html")
    html = html[html.find("<body>") + 6 : html.find("</body>")].strip()
    return html
Ejemplo n.º 12
0
def test_patch(writer):
    """Verify that patching docutils works as expected."""
    TEST_SOURCE = """`
Hello `role`:norole:

.. nodirective::
"""
    rv = 0
    problems = []
    exc = None

    # patch and use lenient docutils
    try:
        try:
            patch_docutils()
        except Exception, exc:
            problems.append("error during library patching")
            raise

        try:
            out = publish_string(TEST_SOURCE,
                writer=writer, settings_spec=LenientSettingsSpecs)
        except Exception, exc:
            problems.append("error while running patched docutils")
            raise
Ejemplo n.º 13
0
def publish_xml_cmdline (in_path = None, in_string = None, out_path = None, settings_overrides=None ):
    try:
        import locale
        locale.setlocale(locale.LC_ALL, '')
    except:
        pass


    from docutils.core import  default_description, default_usage, publish_file, publish_string
    import docutils.core
    description = ('Generates Docutils-native XML from standalone '
               'reStructuredText sources.  ' + default_description)

    if not in_path and not in_string:
        raise TypeError('publish_xml_cmdlind() must have either "in_path" or "in_string" as parameters')


    if in_path:
        out_string = publish_file(source_path= in_path, destination_path=out_path, 
                    settings_overrides = settings_overrides, writer_name='xml')

    elif in_string:
        out_string = publish_string(source= in_string, destination_path=out_path, 
                    settings_overrides = settings_overrides, writer_name='xml')
    return out_string
Ejemplo n.º 14
0
def convert_rest_html(source, source_path, stylesheet=None, encoding='latin1'):
    from py.__.rest import directive
    """ return html latin1-encoded document for the given input. 
        source  a ReST-string
        sourcepath where to look for includes (basically)
        stylesheet path (to be used if any)
    """
    from docutils.core import publish_string
    directive.set_backend_and_register_directives("html")
    kwargs = {
        'stylesheet' : stylesheet, 
        'stylesheet_path': None,
        'traceback' : 1, 
        'embed_stylesheet': 0,
        'output_encoding' : encoding, 
        #'halt' : 0, # 'info',
        'halt_level' : 2, 
    }
    # docutils uses os.getcwd() :-(
    source_path = os.path.abspath(str(source_path))
    prevdir = os.getcwd()
    try:
        #os.chdir(os.path.dirname(source_path))
        return publish_string(source, source_path, writer_name='html',
                              settings_overrides=kwargs)
    finally:
        os.chdir(prevdir)
Ejemplo n.º 15
0
    def save(self, *args, **kwargs):
        # Update the Project's URIs
        docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})

        docutils_settings.update({"warning_stream": os.devnull})

        try:
            html_string = publish_string(source=smart_str(self.description), writer_name="html4css1", settings_overrides=docutils_settings)
            if html_string.strip():
                html = lxml.html.fromstring(html_string)

                for link in html.xpath("//a/@href"):
                    try:
                        if any(urlparse.urlparse(link)[:5]):
                            PackageURI.objects.get_or_create(package=self.package, uri=link)
                    except ValueError:
                        pass
        except Exception:
            # @@@ We Swallow Exceptions here, but it's the best way that I can think of atm.
            pass

        super(Release, self).save(*args, **kwargs)

        _current_show_install_command = self.show_install_command

        if self.classifiers.filter(trove="Framework :: Plone").exists():
            self.show_install_command = False
        else:
            self.show_install_command = True

        if _current_show_install_command != self.show_install_command:
            super(Release, self).save(*args, **kwargs)
Ejemplo n.º 16
0
    def __init__(self, dirpath, parent=None):
        self.parent = parent
        self.dirpath = dirpath
        self.dirname = os.path.split(self.dirpath)[-1]
        self.children = []
        self.path = self.parent and '%s%s/' % (self.parent.path, self.dirname) \
            or '/'

        self.root = self
        while self.root.parent:
            self.root = self.root.parent


        # Get reST as XML
        rstfilepath = os.path.join(self.dirpath, POST_NAME)
        rstfile = codecs.open(rstfilepath, encoding='utf-8')
        self.rst = unicode(rstfile.read())
        rstfile.close()
        xmlstring = publish_string(self.rst, writer_name='xml')
        self.xml = etree.parse(StringIO(xmlstring), Directory.parser)

        # Get metadata for this post
        self.title = self.xml.xpath('title/text()')[0]
        # todo: tags, date and other metadat that might be used in navigation

        # Add to the XML site structure
        attributes = {
            'path': self.path,
            'title': self.title,}
        self.element = etree.Element('directory', **attributes)
        if self.parent:
            parent.element.append(self.element)

        # Recurse
        self.find_children()
Ejemplo n.º 17
0
def rst2html(rst, theme=None, opts=None):
    r"""Render rst string and returns the HTML rendered string

    Quite straitghtforward at first view:

        >>> rst = '=====\nTitle\n=====\n\ntest\n====\ntext'
        >>> print rst2html(rst).strip() # doctest: +ELLIPSIS
        <h1...>Title</h1>
        <h2...>test</h2>
        <p>text</p>

    Notice how this is HTML part that must be included in a complete
    HTML document.

    """
    rst_opts = default_rst_opts.copy()
    if opts:
        rst_opts.update(opts)
    default_template_name = 'template.txt'
    rst_opts['template'] = os.path.join(os.path.dirname(__file__),
                                        default_template_name)

    out = publish_string(rst, writer_name='html', settings_overrides=rst_opts)

    return out
Ejemplo n.º 18
0
def format_post_from_string(body, initial_header_level=4):
    """Returns a tuple containing the title and an HTML string for the
    post body.
    """
    try:
        html = publish_string(
            body,
            writer_name='html',
            settings_overrides={'initial_header_level': initial_header_level,
                                'generator': False,
                                'traceback': True,
                                'syntax_highlight': 'short',
                                },
            )
        if not html:
            raise ValueError('No HTML produced by docutils')
    except Exception as err:
        raise RuntimeError('Could not convert input file to HTML: %s' % err)

    # Pull out the body of the HTML to make the blog post,
    # removing the H1 element with the title.
    d = PyQuery(html, parser='html')
    title = d('body').find('h1:first').html()
    d('body').find('h1:first').remove()
    content = d('body').html()
    return title, content
Ejemplo n.º 19
0
def process_rst (filename, body):
    "Parse 'body' as RST and convert it to HTML"
    output_file = StringIO.StringIO()
    settings = {
        'input_encoding': 'utf-8',
        'output_encoding': 'utf-8',
        # cloak email addresses to reduce spam
        'cloak_email_addresses': 1,
        # Forbid file inclusion
        'file_insertion_enabled': False,
        # remove reST comments from output HTML: 
        'strip_comments': True,
        }
    body = core.publish_string(
        reader_name='standalone',
        parser_name='restructuredtext',
        writer=WeblogWriter(),
        writer_name='html',
        source_path=filename,
        source=body,
        destination_path=filename,
        settings=None,
	settings_overrides=settings)

    # Return a Unicode string.
    body = unicode(body, 'utf-8')
    return body
Ejemplo n.º 20
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)
Ejemplo n.º 21
0
 def _get_desc(self, cr, uid, ids, field_name=None, arg=None, context=None):
     res = dict.fromkeys(ids, "")
     for module in self.browse(cr, uid, ids, context=context):
         path = get_module_resource(module.name, "static/description/index.html")
         if path:
             with tools.file_open(path, "rb") as desc_file:
                 doc = desc_file.read()
                 html = lxml.html.document_fromstring(doc)
                 for element, attribute, link, pos in html.iterlinks():
                     if (
                         element.get("src")
                         and not "//" in element.get("src")
                         and not "static/" in element.get("src")
                     ):
                         element.set("src", "/%s/static/description/%s" % (module.name, element.get("src")))
                 res[module.id] = html_sanitize(lxml.html.tostring(html))
         else:
             overrides = {
                 "embed_stylesheet": False,
                 "doctitle_xform": False,
                 "output_encoding": "unicode",
                 "xml_declaration": False,
             }
             output = publish_string(
                 source=module.description or "", settings_overrides=overrides, writer=MyWriter()
             )
             res[module.id] = html_sanitize(output)
     return res
 def _get_html_description(self, cr, uid, ids, name, arg, context=None):
     res = {}.fromkeys(ids, '')
     for product in self.browse(cr, uid, ids, context):
         overrides = dict(embed_stylesheet=False, doctitle_xform=False, output_encoding='unicode')
         output = publish_string(source=product.description, settings_overrides=overrides, writer=MyWriter())
         res[product.id] = output
     return res
Ejemplo n.º 23
0
 def _get_desc(self, cr, uid, ids, field_name=None, arg=None, context=None):
     res = dict.fromkeys(ids, '')
     for module in self.browse(cr, uid, ids, context=context):
         overrides = dict(embed_stylesheet=False, doctitle_xform=False, output_encoding='unicode')
         output = publish_string(source=module.description, settings_overrides=overrides, writer=MyWriter())
         res[module.id] = output
     return res
Ejemplo n.º 24
0
 def test_output(self):
     """Check that parsing input files provides the expected output files.
     """
     input_filenames = glob(os.path.join(self.fixtures_dir, '*-input.txt'))
     if not input_filenames:
         self.fail('No fixtures found')
     for input_filename in input_filenames:
         with open(input_filename) as input_file:
             input_str = input_file.read()
         real_output = publish_string(source=input_str,
                                      writer_name='rst2rst')
         output_filename = input_filename.replace('input.txt', 'output.txt')
         output_filename = os.path.join(self.fixtures_dir, output_filename)
         with open(output_filename) as output_file:
             theoric_output = output_file.read()
         if real_output != theoric_output:
             theoric_output = theoric_output.replace('\n', '\\n\n')
             real_output = real_output.replace('\n', '\\n\n')
             diff = []
             real_output_lines = real_output.splitlines(True)
             theoric_output_lines = theoric_output.splitlines(True)
             for line in unified_diff(real_output_lines,
                                      theoric_output_lines):
                 diff.append(line)
             diff = ''.join(diff)
             msg = "Content generated from %s differs from content at %s" \
                   "\nDiff:\n%s" % (
                       input_filename,
                       output_filename,
                       diff
                   )
             self.fail(msg)
Ejemplo n.º 25
0
def convert_rest_html(source, source_path, stylesheet=None, encoding="latin1"):
    """ return html latin1-encoded document for the given input. 
        source  a ReST-string
        sourcepath where to look for includes (basically)
        stylesheet path (to be used if any)
    """
    from docutils.core import publish_string

    kwargs = {
        "stylesheet": stylesheet,
        "stylesheet_path": None,
        "traceback": 1,
        "embed_stylesheet": 0,
        "output_encoding": encoding,
        #'halt' : 0, # 'info',
        "halt_level": 2,
    }
    # docutils uses os.getcwd() :-(
    source_path = os.path.abspath(str(source_path))
    prevdir = os.getcwd()
    try:
        # os.chdir(os.path.dirname(source_path))
        return publish_string(source, source_path, writer_name="html", settings_overrides=kwargs)
    finally:
        os.chdir(prevdir)
Ejemplo n.º 26
0
def rst2html(rst, header_level=3):
    """
    Converts ReStructured Text `rst` to a block of HTML for incorporation into
    another HTML file -- so it strips html/head/body tags.  Use higher
    `header_level` values if you already have <h1> in the destination HTML.
    """
    # Make a custom template for rst2thml that does not insert the
    # html/head/body tags.
    tmp = tempfile.mktemp()
    fout = open(tmp, 'w')
    fout.write(dedent(
        '''
        %(body_pre_docinfo)s
        %(body)s
        '''))
    fout.close()
    settings_overrides = {
        'template': tmp,
        'initial_header_level': header_level,
        'doctitle_xform': False,
        'strip_classes': 'simple',
    }
    return publish_string(
        rst,
        writer_name='html',
        settings_overrides=settings_overrides
    )
Ejemplo n.º 27
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)
Ejemplo n.º 28
0
def rst2html(rst, theme=None, opts=None, body_only=False):
    rst_opts = default_rst_opts.copy()
    rst_opts['warning_stream'] = StringIO()

    if body_only:
        out = publish_parts(rst, writer_name='html',
                            settings_overrides=rst_opts)['html_body']

        rst_opts['warning_stream'].seek(0)
        warnings = rst_opts['warning_stream'].read()
        return out, warnings

    if opts:
        rst_opts.update(opts)
    rst_opts['template'] = os.path.join(THEMES, 'template.txt')

    stylesheets = ['basic.css']
    if theme:
        stylesheets.append('%s/%s.css' % (theme, theme))
    rst_opts['stylesheet'] = ','.join([J(THEMES, p) for p in stylesheets])

    out = publish_string(rst, writer_name='html', settings_overrides=rst_opts)

    rst_opts['warning_stream'].seek(0)
    warnings = rst_opts['warning_stream'].read()
    return out, warnings
Ejemplo n.º 29
0
def make_message(rc, to, subject='', body='', attachments=()):
    """Creates an email following the approriate format. The body kwarg
    may be a string of restructured text.  Attachements is a list of filenames
    to attach.
    """
    msg = MIMEMultipart('alternative')
    plain = MIMEText(body, 'plain')
    msg.attach(plain)
    if publish_string is not None:
        html = publish_string(body, writer_name='html',
                              settings_overrides={'output_encoding': 'unicode'})
        html = MIMEText(html, 'html')
        msg.attach(html)
    if attachments:
        text = msg
        msg = MIMEMultipart('mixed')
        msg.attach(text)
        for attachment in attachments:
            _, ext = os.path.splitext(attachment)
            att = ATTACHERS[ext](attachment)
            att.add_header('content-disposition', 'attachment',
                           filename=os.path.basename(attachment))
            msg.attach(att)
    msg['Subject'] = subject
    msg['From'] = rc.email['from']
    msg['To'] = to
    return (to, msg.as_string())
Ejemplo n.º 30
0
def classes(format, destpath, classnames):
    for c in classnames:
        output_filename = os.path.join(destpath, c + '.' + format)
        module_name, classname = c.rsplit('.', 1)
        mod = importlib.import_module(module_name)
        klass = getattr(mod, classname)
    #try:
        with open(output_filename, 'w') as output:
            tmp = io.StringIO()
            tmp.write("#" * len(klass.__name__))
            tmp.write("\n")
            tmp.write(klass.__name__)
            tmp.write('\n')
            tmp.write("#" * len(klass.__name__))
            tmp.write("\n\n")

            tmp.write(str(napoleon.GoogleDocstring(klass.__doc__)))
            tmp.write('\n\n')

            builder = SchemaHelpBuilder(klass.schema, fmt=format)
            tmp.write(builder.rst)

            if format == 'html':
                output.write(publish_string(tmp.getvalue(), writer_name='html', settings_overrides={"stylesheet_path": "sondra/css/flasky.css"}).decode('utf-8'))
            elif format == 'rst':
                output.write(tmp.getvalue())
        logging.info("Wrote {0} to {1}".format(c, output_filename))
Ejemplo n.º 31
0
 def post(self):
     if publish_string is None:
         raise web.HTTPError(503, u'docutils not available')
     body = self.request.body.strip()
     source = body
     # template_path=os.path.join(os.path.dirname(__file__), u'templates', u'rst_template.html')
     defaults = {
         'file_insertion_enabled': 0,
         'raw_enabled': 0,
         '_disable_config': 1,
         'stylesheet_path': 0
         # 'template': template_path
     }
     try:
         html = publish_string(source,
                               writer_name='html',
                               settings_overrides=defaults)
     except:
         raise web.HTTPError(400, u'Invalid RST')
     print html
     self.set_header('Content-Type', 'text/html')
     self.finish(html)
Ejemplo n.º 32
0
 def _get_desc(self, cr, uid, ids, field_name=None, arg=None, context=None):
     res = dict.fromkeys(ids, '')
     for module in self.browse(cr, uid, ids, context=context):
         path = get_module_resource(module.name, 'static/description/index.html')
         if path:
             with tools.file_open(path, 'rb') as desc_file:
                 doc = desc_file.read()
                 html = lxml.html.document_fromstring(doc)
                 for element, attribute, link, pos in html.iterlinks():
                     if element.get('src') and not '//' in element.get('src') and not 'static/' in element.get('src'):
                         element.set('src', "/%s/static/description/%s" % (module.name, element.get('src')))
                 res[module.id] = lxml.html.tostring(html)
         else:
             overrides = {
                 'embed_stylesheet': False,
                 'doctitle_xform': False,
                 'output_encoding': 'unicode',
                 'xml_declaration': False,
             }
             output = publish_string(source=module.description or '', settings_overrides=overrides, writer=MyWriter())
             res[module.id] = output
     return res
Ejemplo n.º 33
0
def _render_rst_to_jinja_templating(course, page_path, content):
    cache_pages = syllabus.get_config()["caching"]["cache_pages"]
    toc = syllabus.get_toc(course)
    print_mode = session.get("print_mode", False)
    # look if we have a cached version of this content
    if cache_pages and toc.has_cached_content(content, print_mode):
        with open(safe_join(syllabus.get_pages_path(course), content.cached_path(print_mode)), "r") as f:
            rendered = f.read()
    else:
        # render the content
        with open(safe_join(syllabus.get_pages_path(course), page_path), "r") as f:
            rendered = publish_string(f.read(), writer_name='html', settings_overrides=default_rst_opts)
        if cache_pages:  # cache the content if needed
            if type(content) is Page:
                parent = toc.get_parent_of(content)
                os.makedirs(safe_join(toc.cached_path(print_mode), parent.path if parent is not None else ""),
                            exist_ok=True)
            else:
                os.makedirs(safe_join(toc.cached_path(print_mode), content.path), exist_ok=True)
            with open(safe_join(syllabus.get_pages_path(course), content.cached_path(print_mode)), "w") as cached_content:
                cached_content.write(rendered)
    return rendered
Ejemplo n.º 34
0
 def test_full_build_http_store(self):
     source = dedent("""
         .. smtimage:: foo
            :digest: 0123456789abcdef
            :align: center
         """)
     config = dedent("""
         [general]
         sumatra_record_store: http://smt.example.com/
         sumatra_project: MyProject
         sumatra_link_icon: icon_info.png
         """)
     with open("docutils.conf", "w") as fp:
         fp.write(config)
     output = publish_string(source, writer_name='pseudoxml')
     self.assertEqual(
         "\n" + output,
         dedent("""
                         <document source="<string>">
                             <reference refuri="http://smt.example.com/MyProject/foo/">
                                 <image align="center" alt="Data file generated by computation foo" digest="0123456789abcdef" uri="smt_images/bar.jpg">
                         """))
Ejemplo n.º 35
0
def main():

##     pub = core.Publisher(writer=OOwriter.Writer())
##     pub.set_reader('standalone', None, 'restructuredtext')
##     settings = pub.get_settings()
##     pub.source = io.FileInput(settings, source_path=sys.argv[1])
##     pub.destination = io.StringOutput(settings)
##     content = pub.publish()

    source = file(sys.argv[1]).read()
    content = core.publish_string(source, writer=OOwriter.Writer())

    xml_manifest_list = [
        ('content.xml', content),
        ('styles.xml', OOtext.styles)
        ]

    xml_entries = []
    for docname, _ in xml_manifest_list:
        xml_entries.append(OOtext.m_xml_format % docname)

    image_manifest_list = OOtext.pictures

    image_entries = []
    for name, format in image_manifest_list:
        image_entries.append(format)

    manifest = OOtext.manifest % ('\n '.join(image_entries),
                                  '\n '.join(xml_entries))
    xml_manifest_list.append(('META-INF/manifest.xml', manifest))

    zip = zipfile.ZipFile(sys.argv[2], "w")
    for docname, contents in xml_manifest_list:
        zinfo = zipfile.ZipInfo(docname)
        zip.writestr(zinfo, contents)
    for name, format in image_manifest_list:
        zip.write(name, 'Pictures/' + name)
    zip.close()
Ejemplo n.º 36
0
 def parse(self, input_data, reason):
     if not RstParser.extensions_registered:
         # need to do this only once...
         directives.register_directive('intro', IntroDirective)
         for extension in self.app.markup_extensions:
             if extension.is_block_level:
                 directives.register_directive(
                     extension.name,
                     make_extension_directive(self.app, extension))
             else:
                 roles.register_local_role(extension.name,
                                           make_extension_role(extension))
         RstParser.extensions_registered = True
     settings_overrides = {
         'file_insertion_enabled': False,
         'parsing_reason': reason,
     }
     rv = publish_string(source=input_data,
                         writer=ZemlWriter(),
                         settings_overrides=settings_overrides)
     if reason == 'comment':
         rv = sanitize(rv)
     return rv
Ejemplo n.º 37
0
def docs():
    """
    Prepare documentation.
    """
    import re
    from docutils.core import publish_string
    settings = dict(
        datestamp='%Y-%m-%d',
        generator=True,
        strict=True,
        toc_backlinks=None,
        cloak_email_addresses=True,
        initial_header_level=3,
        stylesheet_path='doc/style.css',
    )
    rst = open('readme.txt').read()
    html = publish_string(rst,
                          writer_name='html4css1',
                          settings_overrides=settings)
    html = re.sub('<col.*>\n', '', html)
    html = re.sub('</colgroup>', '', html)
    open('index.html', 'w').write(html)
    return
Ejemplo n.º 38
0
def rst_to_html( s ):
    """Convert a blob of reStructuredText to HTML"""
    log = logging.getLogger( "docutils" )

    if docutils_core is None:
        raise Exception("Attempted to use rst_to_html but docutils unavailable.")

    class FakeStream( object ):
        def write( self, str ):
            if len( str ) > 0 and not str.isspace():
                log.warning( str )

    settings_overrides = {
        "embed_stylesheet": False,
        "template": os.path.join(os.path.dirname(__file__), "docutils_template.txt"),
        "warning_stream": FakeStream(),
        "doctitle_xform": False,  # without option, very different rendering depending on
                                  # number of sections in help content.
    }

    return unicodify( docutils_core.publish_string( s,
                      writer=docutils_html4css1.Writer(),
                      settings_overrides=settings_overrides ) )
def format_docs(rst):
    # store full html output to html variable
    writer = Writer()
    html = publish_string(source=rst,
                          writer=writer,
                          writer_name='html',
                          settings_overrides={
                              'link': 'link',
                              'top': 'top'
                          })

    # disable system message in html, no in stderr
    parts = publish_parts(source=rst,
                          writer=writer,
                          writer_name='html',
                          settings_overrides={'report_level': 5})

    # store only html body
    body = parts['html_title'] + parts['body'] + parts['html_line'] + \
        parts['html_footnotes'] + parts['html_citations'] + \
        parts['html_hyperlinks']

    return body
Ejemplo n.º 40
0
    def gen_man_page(self, name, rst):
        from docutils.writers import manpage
        from docutils.core import publish_string
        from docutils.nodes import inline
        from docutils.parsers.rst import roles

        def issue(name,
                  rawtext,
                  text,
                  lineno,
                  inliner,
                  options={},
                  content=[]):
            return [inline(rawtext, '#' + text)], []

        roles.register_local_role('issue', issue)
        # We give the source_path so that docutils can find relative includes
        # as-if the document where located in the docs/ directory.
        man_page = publish_string(source=rst,
                                  source_path='docs/%s.rst' % name,
                                  writer=manpage.Writer())
        with open('docs/man/%s.1' % name, 'wb') as fd:
            fd.write(man_page)
Ejemplo n.º 41
0
Archivo: utils.py Proyecto: ctb/hubward
def reST_to_html(s):
    """
    Convert ReST-formatted string `s` into HTML.

    Output is intended for uploading to UCSC configuration pages, so this uses
    a whitelist approach for HTML tags.
    """
    html = publish_string(
        source=s,
        writer_name='html',
        settings=None,
        settings_overrides={'embed_stylesheet': False},
    )
    safe = bleach.ALLOWED_TAGS + [
        'p', 'img', 'pre', 'tt', 'a', 'h1', 'h2', 'h3', 'h4'
    ]

    attributes = {
        'img': ['alt', 'src'],
        'a': ['href'],
    }

    return bleach.clean(html, tags=safe, strip=True, attributes=attributes)
def code_to_html_string(
        # See code_str_.
        code_str,
        # A file-like object where warnings and errors will be written, or None to
        # send them to stderr.
        warning_stream=None,
        # See `options <options>`.
        **options):

    rest = code_to_rest_string(code_str, **options)
    # `docutils
    # <http://docutils.sourceforge.net/docs/user/tools.html#rst2html-py>`_
    # converts reST to HTML.
    html = core.publish_string(
        rest,
        writer_name="html",
        settings_overrides={
            # Include our custom css file: provide the path to the default css and
            # then to our css. The style sheet dirs must include docutils defaults.
            "stylesheet_path":
            ",".join(Writer.default_stylesheets + ["CodeChat.css"]),
            "stylesheet_dirs":
            Writer.default_stylesheet_dirs + html_static_path(),
            # Make sure to use Unicode everywhere.
            "output_encoding":
            "unicode",
            "input_encoding":
            "unicode",
            # Don't stop processing, no matter what.
            "halt_level":
            5,
            # Capture errors to a string and return it.
            "warning_stream":
            warning_stream,
        },
    )
    return html
Ejemplo n.º 43
0
def _gen_docutils_html(source, fpath, webpage_style, no_header, html_assets,
                       traceback, Parser, Reader):
    from docutils.core import publish_string
    from .docutils import HtmlTranslator, HtmlWriter

    # The encoding/decoding dance below happens because setting output_encoding
    # to "unicode" causes reST to generate a bad <meta> tag, and setting
    # input_encoding to "unicode" breaks the ‘.. include’ directive.

    html_assets.extend(HtmlTranslator.JS + HtmlTranslator.CSS)

    settings_overrides = {
        'traceback': traceback,
        'embed_stylesheet': False,
        'stylesheet_path': None,
        'stylesheet_dirs': [],
        'no_header': no_header,
        'webpage_style': webpage_style,
        'input_encoding': 'utf-8',
        'output_encoding': 'utf-8'
    }

    parser = Parser()
    return publish_string(source=source.encode("utf-8"),
                          source_path=fpath,
                          destination_path=None,
                          reader=Reader(parser),
                          reader_name=None,
                          parser=parser,
                          parser_name=None,
                          writer=HtmlWriter(),
                          writer_name=None,
                          settings=None,
                          settings_spec=None,
                          settings_overrides=settings_overrides,
                          config_section=None,
                          enable_exit_status=True).decode("utf-8")
Ejemplo n.º 44
0
def code_to_html_string(
        # See code_str_.
        code_str,
        # A file-like object where warnings and errors will be written, or None to
        # send them to stderr.
        warning_stream=None,
        # See `options <options>`.
        **options):

    rest = code_to_rest_string(code_str, **options)
    # `docutils
    # <http://docutils.sourceforge.net/docs/user/tools.html#rst2html-py>`_
    # converts reST to HTML.
    html = core.publish_string(
        rest,
        writer_name='html',
        settings_overrides={
            # Include our custom css file: provide the path to the default css and
            # then to our css. The style sheet dirs must include docutils defaults.
            'stylesheet_path':
            ','.join(Writer.default_stylesheets + ['CodeChat.css']),
            'stylesheet_dirs':
            Writer.default_stylesheet_dirs +
            [os.path.join(os.path.dirname(__file__), 'template')],
            # Make sure to use Unicode everywhere.
            'output_encoding':
            'unicode',
            'input_encoding':
            'unicode',
            # Don't stop processing, no matter what.
            'halt_level':
            5,
            # Capture errors to a string and return it.
            'warning_stream':
            warning_stream
        })
    return html
Ejemplo n.º 45
0
def save_table_to_file(table_data, errors, group_name, metric_type, file_extension):
    """
    Saves a group results table or overall results table to a given file type.

    @param table_data :: the results table
    @param errors :: whether to use observational errors
    @param group_name :: name of this group of problems (example 'NIST "lower difficulty"', or
                         'Neutron data')
    @param metric_type :: the test type of the table data (e.g. runtime, accuracy)
    @param file_extension :: the file type extension (e.g. html)
    """
    file_name = ('comparison_{weighted}_{version}_{metric_type}_{group_name}.'
                 .format(weighted=weighted_suffix_string(errors),
                         version=BENCHMARK_VERSION_STR, metric_type=metric_type, group_name=group_name))

    if file_extension == 'html':
        rst_content = '.. include:: ' + str(os.path.join(SCRIPT_DIR, 'color_definitions.txt'))
        rst_content += '\n' + table_data
        table_data = publish_string(rst_content, writer_name='html')

    with open(file_name + file_extension, 'w') as tbl_file:
        print(table_data, file=tbl_file)
    print('Saved {file_name}{extension} to {working_directory}'.
          format(file_name=file_name, extension=file_extension, working_directory=WORKING_DIR))
Ejemplo n.º 46
0
def upload(fname, space, parent_id):
    """Upload documentation for CMake module ``fname`` as child of page with
    id ``parent_id``."""
    pagename = path.splitext(path.basename(fname))[0]
    rst = '\n'.join(extract(fname))
    if not rst:
        return
    log.debug('=' * 79)
    log.info('Uploading %s', pagename)
    log.debug('=' * 79)
    log.debug('rST')
    log.debug('-' * 79)
    log.debug(rst)
    log.debug('-' * 79)
    log.debug('Confluence')
    log.debug('-' * 79)
    cwiki = publish_string(rst, writer=confluence.Writer())
    log.debug(cwiki)
    page = {
        'type': 'page',
        'title': pagename,
        'space': {
            'key': space
        },
        'body': {
            'storage': {
                'value': cwiki,
                'representation': 'wiki'
            }
        },
        'ancestors': [{
            'type': 'page',
            'id': parent_id
        }]
    }
    create_or_update(page, space)
Ejemplo n.º 47
0
def rst2latex(rst_string, no_preamble=True, allow_latex=True):
    """ Calls docutils' engine to convert a rst string to a LaTeX file.
    """
    overrides = {
        'output_encoding': 'utf-8',
        'initial_header_level': 3,
        'no_doc_title': True,
        'use_latex_citations': True,
        'use_latex_footnotes': True
    }
    if allow_latex:
        rst_string = u'''.. role:: math(raw)
                    :format: latex
                    \n\n''' + rst_string
    tex_string = docCore.publish_string(source=rst_string,
                                        writer_name='latex2e',
                                        settings_overrides=overrides)
    if no_preamble:
        extract_document = \
            re.compile(r'.*\\begin\{document\}(.*)\\end\{document\}',
            re.DOTALL)
        matches = extract_document.match(tex_string)
        tex_string = matches.groups()[0]
    return tex_string
Ejemplo n.º 48
0
def rst2htmlcode(rst_text, theme=None, settings={}):
    # register graphviz directive
    directives.register_directive('dot', docutils_graphviz.Graphviz)

    output = None
    try:
        overrides = {}
        mathjax = settings.get('mathjax')
        if mathjax:
            overrides['math_output'] = ' '.join(['MathJax', mathjax])
            del settings['mathjax']
        overrides.update(default_overrides)
        overrides.update(settings)
        overrides.update(get_theme_settings(theme))
        logger.debug(overrides)
        output = publish_string(
            rst_text,
            writer_name='html5',
            settings_overrides=overrides,
        )
    except Exception as err:
        logger.error(err)
        output = str(err)
    return output
Ejemplo n.º 49
0
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]

    if s == ns:
        return s

    ns = publish_string(
        ns, writer=html_fragment_writer).decode('utf-8').rstrip('\r\n')
    if '<p>' not in ns:
        return ''

    ns = ns.split('<p>', 1)[1]
    ns = ns[:ns.rfind('</p>')]
    return jinja2.Markup(ns)
Ejemplo n.º 50
0
 def _get_desc(self):
     for module in self:
         if not module.name:
             module.description_html = False
             continue
         path = modules.get_module_resource(module.name, 'static/description/index.html')
         if path:
             with tools.file_open(path, 'rb') as desc_file:
                 doc = desc_file.read()
                 html = lxml.html.document_fromstring(doc)
                 for element, attribute, link, pos in html.iterlinks():
                     if element.get('src') and not '//' in element.get('src') and not 'static/' in element.get('src'):
                         element.set('src', "/%s/static/description/%s" % (module.name, element.get('src')))
                 module.description_html = tools.html_sanitize(lxml.html.tostring(html))
         else:
             overrides = {
                 'embed_stylesheet': False,
                 'doctitle_xform': False,
                 'output_encoding': 'unicode',
                 'xml_declaration': False,
                 'file_insertion_enabled': False,
             }
             output = publish_string(source=module.description if not module.application and module.description else '', settings_overrides=overrides, writer=MyWriter())
             module.description_html = tools.html_sanitize(output)
Ejemplo n.º 51
0
def parse_rst():
    inpt = request.form["rst"]
    out = publish_string(inpt, writer_name='html')
    return out
Ejemplo n.º 52
0
from __future__ import absolute_import
from __future__ import print_function
import glob, os, sys

try:
    from docutils.core import publish_string
except:
    print('*** Install docutils to convert README.rst')
    sys.exit(0)

if not os.path.isfile('README.rst'):
    print('*** README.rst file not found in %s' % os.getcwd())
    sys.exit()

if (sys.version_info > (3,0)):
    html_string=publish_string(open('README.rst').read(),writer_name='html',
                settings_overrides={'output_encoding': 'unicode'})
else:
    html_string=publish_string(open('README.rst').read(),writer_name='html')

make_files = glob.glob("Makefile")
f_files = glob.glob("*.f") + glob.glob("*.f90")
py_files = glob.glob("*.py")
m_files = glob.glob("*.m")
ipynb_files = glob.glob("*.ipynb")
out_dirs = glob.glob("_out*")   # not currently listed on html page
plot_dirs = glob.glob("_plots") # for gallery -- want only main _plots
#plot_dirs = glob.glob("_plot*")   # might want to list other plot directories


make_text = "\n"
for f in make_files:
Ejemplo n.º 53
0
def execute(hdf, text, env):
    def do_trac(name, arguments, options, content, lineno,
                content_offset, block_text, state, state_machine):
        return trac(env, name, arguments, options, content, lineno,
                    content_offset, block_text, state, state_machine)

    def do_trac_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
        return trac_role(env, name, rawtext, text, lineno, inliner, options, content)

    # 1 required arg, 1 optional arg, spaces allowed in last arg
    do_trac.arguments = (1,1,1)
    do_trac.options = None
    do_trac.content = None
    rst.directives.register_directive('trac', do_trac)
    rst.roles.register_local_role('trac', do_trac_role)

    # The code_block could is taken from the leo plugin rst2
    def code_block(name,arguments,options,content,lineno,content_offset,block_text,state,state_machine):

        """Create a code-block directive for docutils.

        Usage: .. code-block:: language

        If the language can be syntax highlighted it will be."""


        
        from trac.WikiFormatter import Formatter
        
        language = arguments[0]

        code_processor = None
        if  Formatter.builtin_processors.has_key(language):
            code_processor = Formatter.builtin_processors[language]
        else:
            code_processor = Formatter.builtin_processors['default']


        html = code_processor(hdf, '\n'.join(content), env)        
        raw = 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' :
        rst.directives.unchanged # Return the text argument, unchanged
        }
    code_block.content = 1 # True if content is allowed.
    # Register the directive with docutils.
    rst.directives.register_directive('code-block',code_block)
    
    

    _inliner = rst.states.Inliner()
    _parser = rst.Parser(inliner = _inliner)

    html = publish_string(text, writer_name = 'html', parser = _parser,
                          settings_overrides = {'halt_level':6})
    return html[html.find('<body>')+6:html.find('</body>')].strip()
Ejemplo n.º 54
0
def rst2html(data):
    """Converts a reStructuredText into its HTML
    """
    if not data:
        return ''
    return core.publish_string(data, writer=_FragmentWriter())
Ejemplo n.º 55
0
        pass
    
    def visit_graphviz(self,node):
        self.body.append('Graphviz: ')

    def depart_graphviz(self,node):
        pass

    def visit_container(self,node):
        self.body.append('[stem]\n')
        self.body.append('++++')

    def depart_container(self,node):
        self.body.append('++++')

if __name__ == "__main__":
    """ To test the writer """
    from docutils.core import publish_string
    filename = sys.argv[-1]
    print('Converting: '+ filename)
    f_in = open(filename, 'rb')
    rtf = publish_string(f_in.read(), writer=AsciiDocWriter())
    f_in.close()

    filename = filename+'.adoc'

    f_out = open(filename, 'wb')
    f_out.write(rtf)
    print('Converted file: ' + filename)
    f_out.close()
Ejemplo n.º 56
0
def reST_to_html(s):
    return core.publish_string(s, writer=html_fragment_writer)
Ejemplo n.º 57
0
    if previous_end < len(text):
        yield ('text', text[previous_end:])


if __name__ == "__main__":

    import locale

    try:
        locale.setlocale(locale.LC_ALL, '')
    except:
        pass

    from docutils.core import publish_string
    docstring_html = publish_string(__doc__, writer_name='html')

    print(docstring_html)

    # Unit test output goes out stderr.
    import unittest
    sp = smartyPants

    class TestSmartypantsAllAttributes(unittest.TestCase):
        # the default attribute is "1", which means "all".

        def test_dates(self):
            self.assertEqual(sp("1440-80's"), "1440-80’s")
            self.assertEqual(sp("1440-'80s"), "1440-‘80s")
            self.assertEqual(sp("1440---'80s"), "1440–‘80s")
            self.assertEqual(sp("1960s"), "1960s")  # no effect.
Ejemplo n.º 58
0
def rst_to_html(string):
    result = core.publish_string(string, writer=NoHeaderHTMLWriter())
    return mark_safe(result.decode('utf-8'))
Ejemplo n.º 59
0
        section_node.append(line_node)

        return [section_node]


directives.register_directive('mydirective', MyCustomDirective)

if __name__ == '__main__':
    reStructuredText_source = """=====================
Custom directive test
=====================

Some text

.. mydirective:: MyArgument1 test test
   :option1: My value for option 1
   :option2: 42

  my directive's content block goes here
  multi line is ok

My directive should have run now.
"""

    # Since the directive was registered already, it should get used when
    # the parser runs when it encounters the ``.. mydirective::`` text.
    html_output = publish_string(source=reStructuredText_source,
                                 writer=html5_polyglot.Writer())

    #print(html_output.decode('utf8'))
Ejemplo n.º 60
0
def test_wordml( input_string ):
    overrides = { 'template' : '../template.xml' }
    parts = core.publish_string( input_string, writer_name='docutils_wordml', settings_overrides=overrides )
    print parts