Esempio n. 1
0
    def parse(doc: List[str], settings: Any) -> nodes.document:
        state_machine = RSTStateMachine(state_classes, 'Body')
        node = new_document('', settings)
        node.reporter = NullReporter()
        state_machine.run(doc, node)

        return node
Esempio n. 2
0
def extract_summary(doc, document):
    # type: (List[unicode], Any) -> unicode
    """Extract summary from docstring."""

    # Skip a blank lines at the top
    while doc and not doc[0].strip():
        doc.pop(0)

    # If there's a blank line, then we can assume the first sentence /
    # paragraph has ended, so anything after shouldn't be part of the
    # summary
    for i, piece in enumerate(doc):
        if not piece.strip():
            doc = doc[:i]
            break

    # Try to find the "first sentence", which may span multiple lines
    sentences = periods_re.split(" ".join(doc))  # type: ignore
    if len(sentences) == 1:
        summary = sentences[0].strip()
    else:
        summary = ''
        state_machine = RSTStateMachine(state_classes, 'Body')
        while sentences:
            summary += sentences.pop(0) + '.'
            node = new_document('', document.settings)
            node.reporter = NullReporter()
            state_machine.run([summary], node)
            if not node.traverse(nodes.system_message):
                # considered as that splitting by period does not break inline markups
                break

    return summary
Esempio n. 3
0
def extract_summary(doc, document):
    # type: (List[str], Any) -> str
    """Extract summary from docstring."""

    # Skip a blank lines at the top
    while doc and not doc[0].strip():
        doc.pop(0)

    # If there's a blank line, then we can assume the first sentence /
    # paragraph has ended, so anything after shouldn't be part of the
    # summary
    for i, piece in enumerate(doc):
        if not piece.strip():
            doc = doc[:i]
            break

    if doc == []:
        return ''

    # parse the docstring
    state_machine = RSTStateMachine(state_classes, 'Body')
    node = new_document('', document.settings)
    node.reporter = NullReporter()
    state_machine.run(doc, node)

    if not isinstance(node[0], nodes.paragraph):
        # document starts with non-paragraph: pick up the first line
        summary = doc[0].strip()
    else:
        # Try to find the "first sentence", which may span multiple lines
        sentences = periods_re.split(" ".join(doc))
        if len(sentences) == 1:
            summary = sentences[0].strip()
        else:
            summary = ''
            while sentences:
                summary += sentences.pop(0) + '.'
                node[:] = []
                state_machine.run([summary], node)
                if not node.traverse(nodes.system_message):
                    # considered as that splitting by period does not break inline markups
                    break

    # strip literal notation mark ``::`` from tail of summary
    summary = literal_re.sub('.', summary)

    return summary
Esempio n. 4
0
    def __init__(self, app):
        # After much digging through source code, this is how we emulate Sphinx's builtin reST parsing state
        # https://github.com/sphinx-doc/sphinx/blob/68cc0f7e94f360a2c62ebcb761f8096e04ebf07f/sphinx/io.py#L204
        # Here we're bypassing the RST Parser, and just doing the relevant code ops
        parser = app.registry.create_source_parser(app, "restructuredtext")
        # autosummary uses tab width 8; not set by publisher/env for some reason
        settings = dict(app.env.settings)
        if "tab_width" not in settings:
            settings["tab_width"] = 8
        p2 = Publisher()
        p2.process_programmatic_settings(None, settings, None)
        document = new_document('dummy_kissapi_source',
                                p2.settings)  # (source path, settings)
        document.reporter = NullReporter()
        state_machine = RSTStateMachine(
            state_classes, 'Body')  # (state machine classes, initial state)
        # needed to set various self.[attr] values of state_machine
        state_machine.run([""], document)  # (input lines, document)

        # the directive attrs that are needed
        self.state = state_machine.get_state()
        self.env = app.env
        self.lineno = 0