Exemplo n.º 1
0
def find_bound_paragraph(c):
    '''
    Return the lines of a paragraph to be reformatted.
    This is a convenience method for the reformat-paragraph command.
    '''
    head, ins, tail = c.frame.body.getInsertLines()
    head_lines = g.splitLines(head)
    tail_lines = g.splitLines(tail)
    result = []
    insert_lines = g.splitLines(ins)
    para_lines = insert_lines + tail_lines
    # If the present line doesn't start a paragraph,
    # scan backward, adding trailing lines of head to ins.
    if insert_lines and not startsParagraph(insert_lines[0]):
        n = 0  # number of moved lines.
        for i, s in enumerate(reversed(head_lines)):
            if ends_paragraph(s) or single_line_paragraph(s):
                break
            elif startsParagraph(s):
                n += 1
                break
            else:
                n += 1
        if n > 0:
            para_lines = head_lines[-n:] + para_lines
            head_lines = head_lines[:-n]
    ended, started = False, False
    for i, s in enumerate(para_lines):
        if started:
            if ends_paragraph(s) or startsParagraph(s):
                ended = True
                break
            else:
                result.append(s)
        elif s.strip():
            result.append(s)
            started = True
            if ends_paragraph(s) or single_line_paragraph(s):
                i += 1
                ended = True
                break
        else:
            head_lines.append(s)
    if started:
        head = g.joinLines(head_lines)
        tail_lines = para_lines[i:] if ended else []
        tail = g.joinLines(tail_lines)
        return head, result, tail  # string, list, string
    else:
        return None, None, None
Exemplo n.º 2
0
def find_bound_paragraph(c):
    '''
    Return the lines of a paragraph to be reformatted.
    This is a convenience method for the reformat-paragraph command.
    '''
    head, ins, tail = c.frame.body.getInsertLines()
    head_lines = g.splitLines(head)
    tail_lines = g.splitLines(tail)
    result = []
    insert_lines = g.splitLines(ins)
    para_lines = insert_lines + tail_lines
    # If the present line doesn't start a paragraph,
    # scan backward, adding trailing lines of head to ins.
    if insert_lines and not startsParagraph(insert_lines[0]):
        n = 0 # number of moved lines.
        for i, s in enumerate(reversed(head_lines)):
            if ends_paragraph(s) or single_line_paragraph(s):
                break
            elif startsParagraph(s):
                n += 1
                break
            else: n += 1
        if n > 0:
            para_lines = head_lines[-n:] + para_lines
            head_lines = head_lines[: -n]
    ended, started = False, False
    for i, s in enumerate(para_lines):
        if started:
            if ends_paragraph(s) or startsParagraph(s):
                ended = True
                break
            else:
                result.append(s)
        elif s.strip():
            result.append(s)
            started = True
            if ends_paragraph(s) or single_line_paragraph(s):
                i += 1
                ended = True
                break
        else:
            head_lines.append(s)
    if started:
        head = g.joinLines(head_lines)
        tail_lines = para_lines[i:] if ended else []
        tail = g.joinLines(tail_lines)
        return head, result, tail # string, list, string
    else:
        return None, None, None