chiark / gitweb /
50e023391600f2a92e8099cf21a90debc76b0840
[cura.git] / Cura / fabmetheus_utilities / fabmetheus_tools / wikifier.py
1 """
2 Wikifier is a script to add spaces to the pydoc files and move them to the documentation folder.
3
4 """
5
6 from __future__ import absolute_import
7 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
8 import __init__
9
10 from fabmetheus_utilities import archive
11 from fabmetheus_utilities import gcodec
12 from fabmetheus_utilities import settings
13 import cStringIO
14 import os
15
16
17 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
18 __date__ = '$Date: 2008/21/04 $'
19 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
20
21
22 globalWikiLinkStart = '[<a href='
23
24
25 def addToHeadings(headingLineTable, headings, line):
26         'Add the line to the headings.'
27         for depth in xrange(4, -1, -1):
28                 equalSymbolLength = depth + 2
29                 if line[: equalSymbolLength] == '=' * equalSymbolLength:
30                         headings.append(Heading(depth).getFromLine(headingLineTable, line))
31                         return
32
33 def getLinkLine(line):
34         'Get the link line with the wiki style link converted into a hypertext link.'
35         linkStartIndex = line.find(globalWikiLinkStart)
36         squareEndBracketIndex = line.find(']', linkStartIndex)
37         greaterThanIndex = line.find('>', linkStartIndex, squareEndBracketIndex)
38         greaterThanIndexPlusOne = greaterThanIndex + 1
39         closeATagIndex = line.find('</a>', greaterThanIndexPlusOne, squareEndBracketIndex)
40         linkText = line[closeATagIndex + len('</a>') + 1: squareEndBracketIndex]
41         linkLine = line[: linkStartIndex] + line[linkStartIndex + 1: greaterThanIndexPlusOne] + linkText + '</a>' + line[squareEndBracketIndex + 1 :]
42         return linkLine
43
44 def getNavigationHypertext(fileText, transferredFileNameIndex, transferredFileNames):
45         'Get the hypertext help with navigation lines.'
46         helpTextEnd = fileText.find('</p>')
47         helpTextStart = fileText.find('<p>')
48         helpText = fileText[helpTextStart : helpTextEnd]
49         lines = archive.getTextLines(helpText)
50         headings = []
51         headingLineTable = {}
52         for line in lines:
53                 addToHeadings(headingLineTable, headings, line)
54         headingsToBeenAdded = True
55         output = cStringIO.StringIO()
56         for line in lines:
57                 if line[: 2] == '==':
58                         if headingsToBeenAdded:
59                                 output.write('<br />\n')
60                                 for heading in headings:
61                                         heading.addToOutput(output)
62                                 output.write('<br />\n')
63                                 headingsToBeenAdded = False
64                         if line in headingLineTable:
65                                 line = headingLineTable[line]
66                 if '&lt;a href=' in line:
67                         line = line.replace('&lt;', '<').replace('&gt;', '>')
68                 while globalWikiLinkStart in line and ']' in line:
69                         line = getLinkLine(line)
70                 output.write(line + '\n')
71         helpText = output.getvalue()
72         previousFileName = 'contents.html'
73         previousIndex = transferredFileNameIndex - 1
74         if previousIndex >= 0:
75                 previousFileName = transferredFileNames[previousIndex]
76         previousLinkText = '<a href="%s">Previous</a>' % previousFileName
77         nextLinkText = getNextLinkText(transferredFileNames, transferredFileNameIndex + 1)
78         navigationLine = getNavigationLine('<a href="contents.html">Contents</a>', previousLinkText, nextLinkText)
79         helpText = navigationLine + helpText + '<br />\n<br />\n' + navigationLine + '<hr>\n'
80         return fileText[: helpTextStart] + helpText + fileText[helpTextEnd :]
81
82 def getNavigationLine(contentsLinkText, previousLinkText, nextLinkText):
83         'Get the wrapped pydoc hypertext help.'
84         return '<p>\n%s / %s / %s\n</p>\n' % (previousLinkText, nextLinkText, contentsLinkText)
85
86 def getNextLinkText(hypertextFiles, nextIndex):
87         'Get the next link text.'
88         nextFileName = 'contents.html'
89         if nextIndex < len(hypertextFiles):
90                 nextFileName = hypertextFiles[nextIndex]
91         return '<a href="%s">Next</a>' % nextFileName
92
93 def getWrappedHypertext(fileText, hypertextFileIndex, hypertextFiles):
94         'Get the wrapped pydoc hypertext help.'
95         helpTextEnd = fileText.find('</p>')
96         if helpTextEnd < 0:
97                 print('Failed to find the helpTextEnd in getWrappedHypertext in docwrap.')
98         helpTextStart = fileText.find('<p>')
99         if helpTextStart < 0:
100                 print('Failed to find the helpTextStart in getWrappedHypertext in docwrap.')
101         helpText = fileText[helpTextStart : helpTextEnd]
102         helpText = helpText.replace('&nbsp;', ' ')
103         return fileText[: helpTextStart] + helpText + fileText[helpTextEnd :]
104
105 def readWriteDeleteHypertextHelp(documentDirectoryPath, hypertextFileIndex, hypertextFiles, transferredFileNames):
106         'Read the pydoc hypertext help documents, write them in the documentation folder then delete the originals.'
107         fileName = os.path.basename(hypertextFiles[hypertextFileIndex])
108         print('readWriteDeleteHypertextHelp ' + fileName)
109         filePath = os.path.join(documentDirectoryPath, fileName)
110         fileText = archive.getFileText(fileName)
111         fileText = getWrappedHypertext(fileText, hypertextFileIndex, hypertextFiles)
112         if fileText.find('This page is in the table of contents.') > - 1:
113                 fileText = fileText.replace('This page is in the table of contents.', '')
114                 transferredFileNames.append(fileName)
115         archive.writeFileText(filePath, fileText)
116         os.remove(fileName)
117
118 def readWriteNavigationHelp(documentDirectoryPath, transferredFileNameIndex, transferredFileNames):
119         'Read the hypertext help documents, and add the navigation lines to them.'
120         fileName = os.path.basename(transferredFileNames[transferredFileNameIndex])
121         print('readWriteNavigationHelp ' + fileName)
122         filePath = os.path.join(documentDirectoryPath, fileName)
123         fileText = archive.getFileText(filePath)
124         fileText = getNavigationHypertext(fileText, transferredFileNameIndex, transferredFileNames)
125         archive.writeFileText(filePath, fileText)
126
127 def removeFilesInDirectory(directoryPath):
128         'Remove all the files in a directory.'
129         fileNames = os.listdir(directoryPath)
130         for fileName in fileNames:
131                 filePath = os.path.join(directoryPath, fileName)
132                 os.remove(filePath)
133
134 def writeContentsFile(documentDirectoryPath, hypertextFiles):
135         'Write the contents file.'
136         output = cStringIO.StringIO()
137         output.write('<html>\n  <head>\n    <title>Contents</title>\n  </head>\n  <body>\n')
138         navigationLine = getNavigationLine('Contents', 'Previous', getNextLinkText(hypertextFiles, 0))
139         output.write(navigationLine)
140         for hypertextFile in hypertextFiles:
141                 writeContentsLine(hypertextFile, output)
142         output.write(navigationLine)
143         output.write('  </body>\n</html>\n')
144         filePath = os.path.join( documentDirectoryPath, 'contents.html')
145         archive.writeFileText(filePath, output.getvalue())
146
147 def writeContentsLine(hypertextFile, output):
148         'Write a line of the contents file.'
149         summarizedFileName = hypertextFile[: hypertextFile.rfind('.')]
150         numberOfDots = summarizedFileName.count('.')
151         prefixSpaces = '&nbsp;&nbsp;' * numberOfDots
152         if numberOfDots > 0:
153                 summarizedFileName = summarizedFileName[summarizedFileName.rfind('.') + 1 :]
154         capitalizedSummarizedFileName = settings.getEachWordCapitalized(summarizedFileName)
155         output.write('%s<a href="%s">%s</a><br>\n' % (prefixSpaces, hypertextFile, capitalizedSummarizedFileName))
156
157 def writeHypertext():
158         'Run pydoc, then read, write and delete each of the files.'
159         shellCommand = 'pydoc -w ./'
160         commandResult = os.system(shellCommand)
161         if commandResult != 0:
162                 print('Failed to execute the following command in writeHypertext in docwrap.')
163                 print(shellCommand)
164         hypertextFiles = archive.getFilesWithFileTypeWithoutWords('html')
165         if len( hypertextFiles ) <= 0:
166                 print('Failed to find any help files in writeHypertext in docwrap.')
167                 return
168         documentDirectoryPath = archive.getAbsoluteFolderPath( hypertextFiles[0], 'documentation')
169         removeFilesInDirectory(documentDirectoryPath)
170         sortedReplaceFiles = []
171         for hypertextFile in hypertextFiles:
172                 sortedReplaceFiles.append(hypertextFile.replace('.html', '. html'))
173         sortedReplaceFiles.sort()
174         hypertextFiles = []
175         for sortedReplaceFile in sortedReplaceFiles:
176                 hypertextFiles.append(sortedReplaceFile.replace('. html', '.html'))
177         transferredFileNames = []
178         for hypertextFileIndex in xrange(len(hypertextFiles)):
179                 readWriteDeleteHypertextHelp(documentDirectoryPath, hypertextFileIndex, hypertextFiles, transferredFileNames)
180         for transferredFileNameIndex in xrange(len(transferredFileNames)):
181                 readWriteNavigationHelp(documentDirectoryPath, transferredFileNameIndex, transferredFileNames)
182         writeContentsFile(documentDirectoryPath, transferredFileNames)
183         print('%s files were wrapped.' % len(transferredFileNames))
184
185
186 class Heading:
187         'A class to hold the heading and subheadings.'
188         def __init__(self, depth=0):
189                 'Initialize.'
190                 self.depth = depth
191
192         def addToOutput(self, output):
193                 'Add to the output.'
194                 line = '&nbsp;&nbsp;' * self.depth + '<a href="#%s">%s</a><br />\n' % (self.name, self.name)
195                 output.write(line)
196
197         def getFromLine(self, headingLineTable, line):
198                 'Get the heading from a line.'
199                 heading = 'h%s' % (self.depth + 2)
200                 nextLine = '\n<hr>\n'
201                 if self.depth > 0:
202                         nextLine = '\n'
203                 self.name = line.replace('=', '').replace('<br>', '')
204                 name = self.name
205                 headingLine = '<a name="%s" id="%s"></a><%s>%s</%s>%s' % (name, name, heading, name, heading, nextLine)
206                 headingLineTable[line] = headingLine
207                 return self
208
209
210 def main():
211         'Display the craft dialog.'
212         writeHypertext()
213
214 if __name__ == '__main__':
215         main()