<s> import re <EOL> from definition_items import DefinitionItem <EOL> from line_functions import is_empty , get_indent , fix_backspace , NEW_LINE <EOL> underline_regex = re . compile ( r'<STR_LIT>' ) <EOL> class BaseDoc ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lines , headers = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _docstring = lines . splitlines ( ) <EOL> except AttributeError : <EOL> self . _docstring = lines <EOL> self . headers = { } if headers is None else headers <EOL> self . bookmarks = [ ] <EOL> def parse ( self ) : <EOL> """<STR_LIT>""" <EOL> self . index = <NUM_LIT:0> <EOL> self . seek_to_next_non_empty_line ( ) <EOL> while not self . eod : <EOL> header = self . is_section ( ) <EOL> if header : <EOL> self . _refactor ( header ) <EOL> else : <EOL> self . index += <NUM_LIT:1> <EOL> self . seek_to_next_non_empty_line ( ) <EOL> def _refactor ( self , header ) : <EOL> """<STR_LIT>""" <EOL> self . remove_lines ( self . index , <NUM_LIT:2> ) <EOL> self . remove_if_empty ( self . index ) <EOL> refactor_postfix = self . headers . get ( header , '<STR_LIT>' ) <EOL> method_name = '<STR_LIT>' . join ( ( '<STR_LIT>' , refactor_postfix ) ) <EOL> method = getattr ( self , method_name ) <EOL> lines = method ( header ) <EOL> self . insert_and_move ( lines , self . index ) <EOL> def _refactor_header ( self , header ) : <EOL> """<STR_LIT>""" <EOL> header = fix_backspace ( header ) <EOL> directive = '<STR_LIT>' . format ( header ) <EOL> lines = [ ] <EOL> lines += [ directive , NEW_LINE ] <EOL> return lines <EOL> def extract_items ( self , item_class = None ) : <EOL> """<STR_LIT>""" <EOL> item_type = DefinitionItem if ( item_class is None ) else item_class <EOL> is_item = item_type . is_definition <EOL> item_blocks = [ ] <EOL> while ( not self . eod ) and ( is_item ( self . peek ( ) ) or is_item ( self . peek ( <NUM_LIT:1> ) ) ) : <EOL> self . remove_if_empty ( self . index ) <EOL> item_blocks . append ( self . get_next_block ( ) ) <EOL> items = [ item_type . parse ( block ) for block in item_blocks ] <EOL> return items <EOL> def get_next_block ( self ) : <EOL> """<STR_LIT>""" <EOL> item_header = self . pop ( ) <EOL> sub_indent = get_indent ( item_header ) + '<STR_LIT:U+0020>' <EOL> block = [ item_header ] <EOL> while ( not self . eod ) : <EOL> peek_0 = self . peek ( ) <EOL> peek_1 = self . peek ( <NUM_LIT:1> ) <EOL> if ( is_empty ( peek_0 ) and ( not peek_1 . startswith ( sub_indent ) ) ) or ( ( not is_empty ( peek_0 ) ) and ( not peek_0 . startswith ( sub_indent ) ) ) : <EOL> break <EOL> else : <EOL> line = self . pop ( ) <EOL> block += [ line . rstrip ( ) ] <EOL> return block <EOL> def is_section ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . eod : <EOL> return False <EOL> header = self . peek ( ) <EOL> line2 = self . peek ( <NUM_LIT:1> ) <EOL> underline = underline_regex . match ( line2 ) <EOL> if underline is None : <EOL> return False <EOL> striped_header = header . rstrip ( ) <EOL> expected_underline1 = re . sub ( r'<STR_LIT>' , '<STR_LIT:->' , striped_header ) <EOL> expected_underline2 = re . sub ( r'<STR_LIT>' , '<STR_LIT:=>' , striped_header ) <EOL> if ( ( underline . group ( ) . rstrip ( ) == expected_underline1 ) or <EOL> ( underline . group ( ) . rstrip ( ) == expected_underline2 ) ) : <EOL> return header . strip ( ) <EOL> else : <EOL> return False <EOL> def insert_lines ( self , lines , index ) : <EOL> """<STR_LIT>""" <EOL> docstring = self . docstring <EOL> for line in reversed ( lines ) : <EOL> docstring . insert ( index , line ) <EOL> def insert_and_move ( self , lines , index ) : <EOL> """<STR_LIT>""" <EOL> self . insert_lines ( lines , index ) <EOL> self . index += len ( lines ) <EOL> def seek_to_next_non_empty_line ( self ) : <EOL> """<STR_LIT>""" <EOL> docstring = self . docstring <EOL> for line in docstring [ self . index : ] : <EOL> if not is_empty ( line ) : <EOL> break <EOL> self . index += <NUM_LIT:1> <EOL> def get_next_paragraph ( self ) : <EOL> """<STR_LIT>""" <EOL> lines = [ ] <EOL> while ( not self . eod ) and ( not is_empty ( self . peek ( ) ) ) : <EOL> line = self . pop ( ) <EOL> lines . append ( line ) <EOL> return lines <EOL> def read ( self ) : <EOL> """<STR_LIT>""" <EOL> index = self . index <EOL> line = self . _docstring [ index ] <EOL> self . index += <NUM_LIT:1> <EOL> return line <EOL> def remove_lines ( self , index , count = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> docstring = self . docstring <EOL> del docstring [ index : ( index + count ) ] <EOL> def remove_if_empty ( self , index = None ) : <EOL> """<STR_LIT>""" <EOL> if is_empty ( self . docstring [ index ] ) : <EOL> self . remove_lines ( index ) <EOL> def bookmark ( self ) : <EOL> """<STR_LIT>""" <EOL> self . bookmarks . append ( self . index ) <EOL> def goto_bookmark ( self , bookmark_index = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . index = self . bookmarks [ bookmark_index ] <EOL> return self . bookmarks . pop ( bookmark_index ) <EOL> def peek ( self , ahead = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> position = self . index + ahead <EOL> try : <EOL> line = self . docstring [ position ] <EOL> except IndexError : <EOL> line = '<STR_LIT>' <EOL> return line <EOL> def pop ( self , index = None ) : <EOL> """<STR_LIT>""" <EOL> index = self . index if ( index is None ) else index <EOL> return self . _docstring . pop ( index ) <EOL> @ property <EOL> def eod ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . index >= len ( self . docstring ) <EOL> @ property <EOL> def docstring ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _docstring </s>
