def test_wrap_subsequent_offset():
    assert lines.wrap(
        'foo bar baz',
        width=5,
        offset=0,
        indent=2,
    ) == 'foo\n  bar\n  baz'
def test_wrap_initial_offset():
    assert lines.wrap(
        'The hail in Wales falls mainly on the snails.',
        width=20,
        offset=12,
        indent=0,
    ) == 'The hail\nin Wales falls\nmainly on the\nsnails.'
예제 #3
0
def rst(text: str,
        width: int = 72,
        indent: int = 0,
        nl: bool = None,
        source_format: str = 'commonmark'):
    """Convert the given text to ReStructured Text.

    Args:
        text (str): The text to convert.
        width (int): The number of columns.
        indent (int): The number of columns to indent each line of text
            (except the first).
        nl (bool): Whether to append a trailing newline.
            Defaults to appending a newline if the result is more than
            one line long.
        source_format (str): The source format. This is ``commonmark`` by
            default, which is what is used by convention in protocol buffers.

    Returns:
        str: The same text, in RST format.
    """
    # Sanity check: If the text block does not appear to have any formatting,
    # do not convert it.
    # (This makes code generation significantly faster; calling out to pandoc
    # is by far the most expensive thing we do.)
    if not re.search(r'[|*`_[\]]', text):
        answer = wrap(
            text,
            indent=indent,
            offset=indent + 3,
            width=width - indent,
        )
    else:
        # Convert from CommonMark to ReStructured Text.
        answer = pypandoc.convert_text(
            text,
            'rst',
            format=source_format,
            extra_args=['--columns=%d' % (width - indent)],
        ).strip().replace('\n', f"\n{' ' * indent}")

    # Add a newline to the end of the document if any line breaks are
    # already present.
    #
    # This causes the closing """ to be on the subsequent line only when
    # appropriate.
    if nl or ('\n' in answer and nl is None):
        answer += '\n' + ' ' * indent

    # If the text ends in a double-quote, append a period.
    # This ensures that we do not get a parse error when this output is
    # followed by triple-quotes.
    if answer.endswith('"'):
        answer += '.'

    # Done; return the answer.
    return answer
def test_wrap_short_line_preserved():
    assert lines.wrap('foo\nbar\nbaz', width=80) == 'foo\nbar\nbaz'
def test_wrap_indent_short():
    assert lines.wrap('foo bar', width=30, indent=10) == 'foo bar'
def test_wrap_strips():
    assert lines.wrap('foo bar baz  ', width=80) == 'foo bar baz'
def test_wrap_simple():
    assert lines.wrap('foo bar baz', width=5) == 'foo\nbar\nbaz'
def test_wrap_empty_text():
    assert lines.wrap('', width=80) == ''
def test_wrap_noop():
    assert lines.wrap('foo bar baz', width=80) == 'foo bar baz'
def test_wrap_does_not_break_hyphenated_word():
    assert lines.wrap('do-not-break', width=5) == 'do-not-break'