def align_pipe(width, inpipe=None, outpipe=None):
    '''
    read paragraphs from one pipe, 
    align them and puts to second pipe
    '''
    if inpipe is None:
        inpipe = sys.stdin
    if outpipe is None:
        outpipe = sys.stdout

    paragraph_lines = list()

    def flush_paragraph(paragraph_lines):
        '''
        align paragraph, put to output, 
        reset state
        '''
        if len(paragraph_lines) > 0:
            aligned_paragraph = align_paragraph(paragraph_lines, width)
            for line in aligned_paragraph:
                outpipe.write(line + '\n')
            paragraph_lines = list()

        return paragraph_lines

    for rawline in inpipe:
        line = rawline.splitlines()[0]

        # paragraph closed by empty line
        if line.strip() == '':
            paragraph_lines = flush_paragraph(paragraph_lines)
            outpipe.write('\n')
            continue

        # paragraph started with space
        if line[0].isspace():
            paragraph_lines = flush_paragraph(paragraph_lines)

        paragraph_lines.append(line)

    paragraph_lines = flush_paragraph(paragraph_lines)

    read paragraphs from one pipe, 
    align them and puts to second pipe
    
recipes/Python/577093_Align_text_filter/recipe-577093.py