mercurial/minirst.py
changeset 10444 e99e0e077bc4
parent 10443 62d484a81dfe
child 10447 e957cc7cbd14
equal deleted inserted replaced
10443:62d484a81dfe 10444:e99e0e077bc4
   190     """Prune unwanted containers.
   190     """Prune unwanted containers.
   191 
   191 
   192     The blocks must have a 'type' field, i.e., they should have been
   192     The blocks must have a 'type' field, i.e., they should have been
   193     run through findliteralblocks first.
   193     run through findliteralblocks first.
   194     """
   194     """
       
   195     pruned = []
   195     i = 0
   196     i = 0
   196     while i + 1 < len(blocks):
   197     while i + 1 < len(blocks):
   197         # Searching for a block that looks like this:
   198         # Searching for a block that looks like this:
   198         #
   199         #
   199         # +-------+---------------------------+
   200         # +-------+---------------------------+
   205             blocks[i]['lines'][0].startswith('.. container::')):
   206             blocks[i]['lines'][0].startswith('.. container::')):
   206             indent = blocks[i]['indent']
   207             indent = blocks[i]['indent']
   207             adjustment = blocks[i + 1]['indent'] - indent
   208             adjustment = blocks[i + 1]['indent'] - indent
   208             containertype = blocks[i]['lines'][0][15:]
   209             containertype = blocks[i]['lines'][0][15:]
   209             prune = containertype not in keep
   210             prune = containertype not in keep
       
   211             if prune:
       
   212                 pruned.append(containertype)
   210 
   213 
   211             # Always delete "..container:: type" block
   214             # Always delete "..container:: type" block
   212             del blocks[i]
   215             del blocks[i]
   213             j = i
   216             j = i
   214             while j < len(blocks) and blocks[j]['indent'] > indent:
   217             while j < len(blocks) and blocks[j]['indent'] > indent:
   217                     i -= 1 # adjust outer index
   220                     i -= 1 # adjust outer index
   218                 else:
   221                 else:
   219                     blocks[j]['indent'] -= adjustment
   222                     blocks[j]['indent'] -= adjustment
   220                     j += 1
   223                     j += 1
   221         i += 1
   224         i += 1
   222     return blocks
   225     return blocks, pruned
   223 
   226 
   224 
   227 
   225 def findsections(blocks):
   228 def findsections(blocks):
   226     """Finds sections.
   229     """Finds sections.
   227 
   230 
   315     return textwrap.fill(text, width=width,
   318     return textwrap.fill(text, width=width,
   316                          initial_indent=initindent,
   319                          initial_indent=initindent,
   317                          subsequent_indent=subindent)
   320                          subsequent_indent=subindent)
   318 
   321 
   319 
   322 
   320 def format(text, width, indent=0, keep=[]):
   323 def format(text, width, indent=0, keep=None):
   321     """Parse and format the text according to width."""
   324     """Parse and format the text according to width."""
   322     blocks = findblocks(text)
   325     blocks = findblocks(text)
   323     for b in blocks:
   326     for b in blocks:
   324         b['indent'] += indent
   327         b['indent'] += indent
   325     blocks = findliteralblocks(blocks)
   328     blocks = findliteralblocks(blocks)
   326     blocks = prunecontainers(blocks, keep)
   329     blocks, pruned = prunecontainers(blocks, keep or [])
   327     blocks = inlineliterals(blocks)
   330     blocks = inlineliterals(blocks)
   328     blocks = splitparagraphs(blocks)
   331     blocks = splitparagraphs(blocks)
   329     blocks = updatefieldlists(blocks)
   332     blocks = updatefieldlists(blocks)
   330     blocks = findsections(blocks)
   333     blocks = findsections(blocks)
   331     blocks = addmargins(blocks)
   334     blocks = addmargins(blocks)
   332     return '\n'.join(formatblock(b, width) for b in blocks)
   335     text = '\n'.join(formatblock(b, width) for b in blocks)
       
   336     if keep is None:
       
   337         return text
       
   338     else:
       
   339         return text, pruned
   333 
   340 
   334 
   341 
   335 if __name__ == "__main__":
   342 if __name__ == "__main__":
   336     from pprint import pprint
   343     from pprint import pprint
   337 
   344