Mercurial > hg > ltpda
comparison m-toolbox/html_help/help/mkhelpfiles.py @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 #!/usr/bin/python | |
2 # Filename: mkhelpfiles.py | |
3 | |
4 | |
5 """ | |
6 mkhelpfiles.py | |
7 | |
8 Build MATLAB help files based on XML TOC. | |
9 | |
10 Usage: mkhelpfiles.py -i helptoc.xml | |
11 | |
12 M Hewitson 01-03-07 | |
13 | |
14 $Id: mkhelpfiles.py,v 1.3 2007/11/14 11:20:11 hewitson Exp $ | |
15 | |
16 """ | |
17 | |
18 | |
19 import glob | |
20 import os | |
21 import sys | |
22 from string import * | |
23 import getopt | |
24 import platform | |
25 import time | |
26 import datetime | |
27 | |
28 | |
29 def extract(text, sub1, sub2): | |
30 """extract a substring between two substrings sub1 and sub2 in text""" | |
31 print(text.split(sub1)) | |
32 return text.split(sub1)[-1].split(sub2)[0] | |
33 | |
34 ####################################################################### | |
35 # | |
36 # mkdir | |
37 # | |
38 def _mkdir(newdir): | |
39 """works the way a good mkdir should :) | |
40 - already exists, silently complete | |
41 - regular file in the way, raise an exception | |
42 - parent directory(ies) does not exist, make them as well | |
43 """ | |
44 if os.path.isdir(newdir): | |
45 pass | |
46 elif os.path.isfile(newdir): | |
47 raise OSError("a file with the same name as the desired " \ | |
48 "dir, '%s', already exists." % newdir) | |
49 else: | |
50 head, tail = os.path.split(newdir) | |
51 if head and not os.path.isdir(head): | |
52 _mkdir(head) | |
53 #print "_mkdir %s" % repr(newdir) | |
54 if tail: | |
55 os.mkdir(newdir) | |
56 return None; | |
57 | |
58 | |
59 | |
60 ###################################################################### | |
61 # Main | |
62 # | |
63 def main(): | |
64 | |
65 try: | |
66 opts, args = getopt.getopt(sys.argv[1:], "i:", ["input="]) | |
67 except getopt.GetoptError: | |
68 print(__doc__) | |
69 sys.exit(2) | |
70 | |
71 # Default settings for database | |
72 tocfile = '' | |
73 | |
74 # process args | |
75 for o, a in opts: | |
76 if o in ("-i", "--input"): | |
77 tocfile = a | |
78 | |
79 # check for required fields | |
80 if tocfile=="": | |
81 print("\n### Incorrect inputs.") | |
82 print(__doc__) | |
83 sys.exit(2) | |
84 | |
85 # Load XML file | |
86 try: | |
87 fin = open(tocfile); | |
88 allLines = fin.readlines(); | |
89 fin.close() | |
90 except IOError: | |
91 print("# error opening file.") | |
92 sys.exit(-1) | |
93 | |
94 # Load Template file | |
95 try: | |
96 fin = open('template.html'); | |
97 templateFile = fin.read(); | |
98 fin.close() | |
99 except IOError: | |
100 print("# error opening file.") | |
101 sys.exit(-1) | |
102 | |
103 # Go through each line and look for 'target=' | |
104 entries = []; | |
105 | |
106 for l in allLines: | |
107 line = strip(l) | |
108 if line.find("target=") > 0: | |
109 tmp = split(line, "<")[1].split(">"); | |
110 title = strip(tmp[1]) | |
111 htmlfile = strip(tmp[0].split('"')[1]) | |
112 | |
113 # If this is an html file we can continue | |
114 if htmlfile.endswith('.html'): | |
115 print "%s - %s" % (htmlfile, title) | |
116 | |
117 entry = {} | |
118 entry["html"] = htmlfile | |
119 entry["title"] = title | |
120 entries.append(entry) | |
121 | |
122 | |
123 for j in range(0,len(entries)): | |
124 entry = entries[j] | |
125 print("--- Processing %s ------------------------" % entry) | |
126 htmlfile = entry["html"] | |
127 title = entry["title"] | |
128 | |
129 if len(split(htmlfile, '/')) == 2: | |
130 outdir = split(htmlfile, '/')[0] | |
131 outfile = split(htmlfile, '/')[1] | |
132 _mkdir(outdir) | |
133 else: | |
134 outdir = '' | |
135 outfile = split(htmlfile, '/')[0] | |
136 | |
137 # insert title | |
138 html = templateFile; | |
139 html = html.replace(">TITLE", ">%s"%title) | |
140 | |
141 # insert pre_title | |
142 if j>=1: | |
143 lastentry = entries[j-1] | |
144 else: | |
145 lastentry = entries[-1] | |
146 | |
147 last = split(lastentry["html"], '/')[-1] | |
148 print("PRE: inserting %s" % last) | |
149 html = html.replace("PRE_TITLE", lastentry["title"]) | |
150 | |
151 # insert prehtml | |
152 html = html.replace("prehtml.html", last) | |
153 | |
154 # insert post_title | |
155 if j<len(entries)-1: | |
156 nextentry = entries[j+1] | |
157 else: | |
158 nextentry = entries[0] | |
159 | |
160 # print "POST: inserting %s" % split(nextentry["html"], '/')[-1] | |
161 next = split(nextentry["html"], '/')[-1] | |
162 print("POST: inserting %s" % next) | |
163 html = html.replace("POST_TITLE", nextentry["title"]) | |
164 | |
165 # insert posthtml | |
166 html = html.replace("posthtml.html", next) | |
167 | |
168 # insert content link | |
169 contentFilePath = split(htmlfile, '.html')[0]+'_content.html' | |
170 if len(split(contentFilePath, '/'))>1: | |
171 conoutdir = split(contentFilePath, '/')[0] | |
172 conout = split(contentFilePath, '/')[1] | |
173 else: | |
174 conoutdir = '' | |
175 conout = split(contentFilePath, '/')[0] | |
176 | |
177 | |
178 | |
179 # make contents version of htmlfile | |
180 # - only if file doesn't exist | |
181 if os.path.exists(contentFilePath): | |
182 try: | |
183 fin = open(contentFilePath); | |
184 line1 = fin.readline(); | |
185 if line1.startswith("<!-- $Id:"): | |
186 line1 = "" | |
187 content = line1+fin.read(); | |
188 fin.close() | |
189 except IOError: | |
190 print("# error opening file.") | |
191 sys.exit(-1) | |
192 else: | |
193 content = "<p>Content needs written...</p>" | |
194 | |
195 html = html.replace("CONTENT", content) | |
196 # write out | |
197 fout = open(htmlfile, "w+"); | |
198 fout.write(html) | |
199 fout.close(); | |
200 | |
201 # call main | |
202 if __name__ == "__main__": | |
203 main() |