github.com/aclaygray/packer@v1.3.2/scripts/sort-md-list.py (about)

     1  #!/usr/bin/env python
     2  """
     3  sort-md-list.py sorts markdown lists
     4  
     5  Use this script to prepare sections of the changelog.
     6  
     7  this script expects a bulleted markdown list in stdout
     8  
     9  for example
    10  
    11  ./sort-md-list.py - <<EOF
    12  * builder/amazon: Only delete temporary key if we created one. [GH-4850]
    13  * core: Correctly reject config files which have junk after valid json.
    14          [GH-4906]
    15      * builder/azure: Replace calls to panic with error returns. [GH-4846]
    16  * communicator/winrm: Use KeepAlive to keep long-running connections open.  [GH-4952]
    17  EOF
    18  
    19  output:
    20  
    21  * builder/amazon: Only delete temporary key if we created one. [GH-4850]
    22  * builder/azure: Replace calls to panic with error returns. [GH-4846]
    23  * communicator/winrm: Use KeepAlive to keep long-running connections open.
    24      [GH-4952]
    25  * core: Correctly reject config files which have junk after valid json.
    26      [GH-4906]
    27  
    28  As you can see, the output is sorted and spaced appropriately.
    29  
    30  Limitations:
    31      * nested lists are not supported
    32      * must be passed a list to stdin, it does not process the changelog
    33      * whitespace within the list is elided
    34  """
    35  
    36  import fileinput
    37  import sys
    38  import textwrap
    39  
    40  
    41  if __name__ == '__main__':
    42      lines = []
    43      working = []
    44      for line in fileinput.input():
    45          line = line.strip()
    46          if line.startswith('*'):
    47              if len(working):
    48                  lines.append( " ".join(working))
    49              working = [line]
    50          else:
    51              working.append(line)
    52      if len(working):
    53          lines.append( " ".join(working))
    54  
    55      # take care of blank line at start of selection
    56      sys.stdin.seek(0)
    57      if sys.stdin.readlines()[0].strip() == "":
    58          print ""
    59  
    60      for line in sorted(lines, key=lambda s: s.lower()):
    61          if line.strip() == "":
    62              continue
    63          # print "-"*79
    64          wrapped = textwrap.wrap(line, 79)
    65          print wrapped[0]
    66          indented = " ".join([s.strip() for s in wrapped[1:]])
    67          for iline in textwrap.wrap(indented, 79-4):
    68              print "    " + iline
    69  
    70      # take care of blank line at end of selection
    71      sys.stdin.seek(0)
    72      if sys.stdin.readlines()[-1].strip() == "":
    73          print ""