Package lib :: Package text :: Module string
[hide private]
[frames] | no frames]

Source Code for Module lib.text.string

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2010-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module for building documentation.""" 
 24   
 25  # Some constants. 
 26  TITLE = 3 
 27  SECTION = 2 
 28  SUBSECTION = 1 
 29  PARAGRAPH = 0 
 30  LIST = 10 
 31   
 32   
33 -def strip_lead(text):
34 """Strip the leading whitespace from the given text. 35 36 @param text: The text to strip the leading whitespace from. 37 @type text: str 38 @return: The text with leading whitespace removed. 39 @rtype: str 40 """ 41 42 # Split by newline. 43 lines = text.split('\n') 44 45 # Find the minimum whitespace. 46 min_white = 1000 47 for line in lines: 48 # Empty lines. 49 if line.strip() == '': 50 continue 51 52 # Count the whitespace for the current line. 53 num_white = 0 54 for i in range(len(line)): 55 if line[i] != ' ': 56 break 57 num_white = num_white + 1 58 59 # The min value. 60 min_white = min(min_white, num_white) 61 62 # Strip the whitespace. 63 new_text = '' 64 for line in lines: 65 new_text = new_text + line[min_white:] + '\n' 66 67 # Return the new text. 68 return new_text
69 70
71 -def to_docstring(data):
72 """Convert the text to that of a docstring, dependent on the text level. 73 74 @param data: The lists of constants and text to convert into a properly formatted docstring. 75 @type data: list of lists of int and str 76 """ 77 78 # Init. 79 doc = '' 80 for i in range(len(data)): 81 # The level and text. 82 level, text = data[i] 83 84 # Title level. 85 if level == TITLE: 86 doc += text + '\n\n' 87 88 # Section level. 89 if level == SECTION: 90 doc += '\n\n' + text + '\n' + '='*len(text) + '\n\n' 91 92 # Subsection level. 93 if level == SUBSECTION: 94 doc += '\n\n' + text + '\n' + '-'*len(text) + '\n\n' 95 96 # Paragraph level. 97 elif level == PARAGRAPH: 98 # Starting newline. 99 if i and data[i-1][0] == PARAGRAPH: 100 doc += '\n' 101 102 # The text. 103 doc += text + '\n' 104 105 # List level. 106 elif level == LIST: 107 # Start of list. 108 if i and data[i-1][0] != LIST: 109 doc += '\n' 110 111 # The text. 112 doc += " - %s\n" % text 113 114 # End of list. 115 if i < len(data) and data[i+1][0] == PARAGRAPH: 116 doc += '\n' 117 118 # Return the docstring. 119 return doc
120