Beispiel #1
0
def rst2html(rst, path, context, request):
    settings = {
        'halt_level': 6,
        'input_encoding': 'UTF-8',
        'output_encoding': 'UTF-8',
        'initial_header_level': 1,
        'file_insertion_enabled': 1,
        'raw_enabled': 1,
        'writer_name': 'html',
        'language_code': 'zh_cn',
        'context': context,
        'request': request
    }

    parts = publish_parts(rst,
                          source_path=path,
                          writer=rst2html5.HTML5Writer(),
                          settings_overrides=settings)
    return parts['body']
Beispiel #2
0
def convert_rst2html(source,
                     templatedir,
                     markdown_template,
                     options=dict(),
                     compress=False):
    temp = docutils.core.publish_parts(unicode(source, 'utf-8'),
                                       writer_name='html5',
                                       writer=rst2html5.HTML5Writer())['body']
    temp = temp.replace('<table>', '<table class="table table-striped">')
    env = jinja2.Environment(loader=jinja2.DictLoader({'inputtemplate': temp}))
    options['content'] = env.get_template('inputtemplate').render(**options)

    parser = linkdown.htmlhelper.HeadlineParser()
    parser.feed(options['content'])
    options['title'] = parser.h1
    options['headlines'] = parser.headlines

    return convert_jinja2html(
        file(os.path.join(templatedir, markdown_template)).read(), templatedir,
        options, compress)
Beispiel #3
0
def rst2html(source,
             source_path=None,
             source_class=docutils.io.StringInput,
             destination_path=None,
             reader=None,
             parser=None,
             parser_name='restructuredtext',
             writer=None,
             writer_name='html5',
             settings=None,
             settings_spec=None,
             settings_overrides=None,
             config_section=None,
             enable_exit_status=None,
             logger=None,
             l_add_ln=0):
    """
    Set up & run a `Publisher`, and return a dictionary of document parts.
    Dictionary keys are the names of parts, and values are Unicode strings;
    encoding is up to the client.  For programmatic use with string I/O.

    For encoded string input, be sure to set the 'input_encoding' setting to
    the desired encoding.  Set it to 'unicode' for unencoded Unicode string
    input.  Here's how::

        publish_parts(..., settings_overrides={'input_encoding': 'unicode'})

    Parameters: see `publish_programmatically`.

    WARNING: `reader` should be None (or NikolaReader()) if you want Nikola to report
             reStructuredText syntax errors.
    """
    if reader is None:
        reader = NikolaReader()
        # For our custom logging, we have special needs and special settings we
        # specify here.
        # logger    a logger from Nikola
        # source   source filename (docutils gets a string)
        # add_ln   amount of metadata lines (see comment in compile_html above)
        reader.l_settings = {
            'logger': logger,
            'source': source_path,
            'add_ln': l_add_ln
        }

    if writer is None:
        writer = rst2html5.HTML5Writer()

    pub = docutils.core.Publisher(reader,
                                  parser,
                                  writer,
                                  settings=settings,
                                  source_class=source_class,
                                  destination_class=docutils.io.StringOutput)
    pub.set_components(None, parser_name, writer_name)
    pub.process_programmatic_settings(settings_spec, settings_overrides,
                                      config_section)
    pub.set_source(source, None)
    pub.settings._nikola_source_path = source_path
    pub.set_destination(None, destination_path)
    pub.publish(enable_exit_status=enable_exit_status)

    return pub.writer.parts[
        'body'], pub.document.reporter.max_level, pub.settings.record_dependencies
Beispiel #4
0
 def _rst_to_docutils(file: pathlib.Path) -> Dict[str, str]:
     return docutils.core.publish_parts(
         writer=rst2html5.HTML5Writer(),
         source=file.read_text(),
     )