예제 #1
0
 def check_docutils_inliner(po, msgstr):
     inliner = Inliner()
     settings = AttrDict({'character_level_inline_markup': False, 'pep_references': None, 'rfc_references': None})
     inliner.init_customizations(settings)
     document = new_document(None)
     document.settings.syntax_highlight = 'long'
     stream = StringIO()
     reporter = Reporter(po.file, report_level=Reporter.WARNING_LEVEL,
                         halt_level=Reporter.SEVERE_LEVEL, stream=stream)
     memo = Struct(document=document, reporter=reporter, language=None, inliner=inliner)
     inliner.parse(msgstr, po.current_index, memo, None)
     return stream.getvalue()
예제 #2
0
    def _parse(self, line):
        """
        Parses a single line/string for inline rst statements, like strong, emphasis, literal, ...

        :param line: string to parse
        :return: nodes
        """
        inline_parser = Inliner()
        inline_parser.init_customizations(self.doc_settings)
        result, message = inline_parser.parse(line, 0, self.doc_memo, self.dummy_doc)
        if message:
            raise SphinxNeedLayoutException(message)
        return result
예제 #3
0
def sphinx_state(local_app):
    """
    Fixture which will provide a sphinx state for use in testing sphinx
    directives.

    Yields:
        :class:`docutils.parsers.rst.states.State`: A state for use in testing
            directive functionality.
    """
    # Get the environment and decorate it with what sphinx may need for the
    # parsing.
    env = local_app.env
    env.temp_data["docname"] = "test"  # A fake document name

    # Create a document and inliner object, to be perfectly honest not sure
    # exactly what these are or do, but needed to get the directive to run.
    document = new_document(__file__)
    document.settings.pep_references = 1
    document.settings.rfc_references = 1
    document.settings.env = env
    document.settings.tab_width = 4
    inliner = Inliner()
    inliner.init_customizations(document.settings)

    # Create a state machine so that we can get a state to pass back.
    statemachine = RSTStateMachine(state_classes=state_classes, initial_state="Body")
    statemachine.input_lines = StringList([""] * 40)
    state = statemachine.get_state()
    state.document = document
    state.memo = Struct(
        inliner=inliner,
        language=en,
        title_styles=[],
        reporter=document.reporter,
        document=document,
        section_level=0,
        section_bubble_up_kludge=False,
    )

    state.memo.reporter.get_source_and_line = statemachine.get_source_and_line

    # The environemnt isn't normally available on the state in sphinx, but it's
    # done here to make testing easier.
    state.env = env

    # Sphinx monkeypatches docutils when run. This is how it get's
    # monkeypatched so that the python directives and roles can be found
    with sphinx_domains(env):

        # Provide the state back to the test.
        yield state