Module relax_string
[hide private]
[frames] | no frames]

Source Code for Module relax_string

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2010-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax 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 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax 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 relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Module for building documentation.""" 
 25   
 26  # Python module imports. 
 27  from string import split, strip 
 28   
 29   
 30  # Some constants. 
 31  TITLE = 3 
 32  SECTION = 2 
 33  SUBSECTION = 1 
 34  PARAGRAPH = 0 
 35  LIST = 10 
 36   
 37   
38 -def strip_lead(text):
39 """Strip the leading whitespace from the given text. 40 41 @param text: The text to strip the leading whitespace from. 42 @type text: str 43 @return: The text with leading whitespace removed. 44 @rtype: str 45 """ 46 47 # Split by newline. 48 lines = split(text, '\n') 49 50 # Find the minimum whitespace. 51 min_white = 1000 52 for line in lines: 53 # Empty lines. 54 if strip(line) == '': 55 continue 56 57 # Count the whitespace for the current line. 58 num_white = 0 59 for i in range(len(line)): 60 if line[i] != ' ': 61 break 62 num_white = num_white + 1 63 64 # The min value. 65 min_white = min(min_white, num_white) 66 67 # Strip the whitespace. 68 new_text = '' 69 for line in lines: 70 new_text = new_text + line[min_white:] + '\n' 71 72 # Return the new text. 73 return new_text
74 75
76 -def to_docstring(data):
77 """Convert the text to that of a docstring, dependent on the text level. 78 79 @param data: The lists of constants and text to convert into a properly formatted docstring. 80 @type data: list of lists of int and str 81 """ 82 83 # Init. 84 doc = '' 85 for i in range(len(data)): 86 # The level and text. 87 level, text = data[i] 88 89 # Title level. 90 if level == TITLE: 91 doc += text + '\n\n' 92 93 # Section level. 94 if level == SECTION: 95 doc += '\n\n' + text + '\n' + '='*len(text) + '\n\n' 96 97 # Subsection level. 98 if level == SUBSECTION: 99 doc += '\n\n' + text + '\n' + '-'*len(text) + '\n\n' 100 101 # Paragraph level. 102 elif level == PARAGRAPH: 103 # Starting newline. 104 if i and data[i-1][0] == PARAGRAPH: 105 doc += '\n' 106 107 # The text. 108 doc += text + '\n' 109 110 # List level. 111 elif level == LIST: 112 # Start of list. 113 if i and data[i-1][0] != LIST: 114 doc += '\n' 115 116 # The text. 117 doc += " - %s\n" % text 118 119 # End of list. 120 if i < len(data) and data[i+1][0] == PARAGRAPH: 121 doc += '\n' 122 123 # Return the docstring. 124 return doc
125