2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
10 # Full author contact details are available in file CREDITS
13 # gen_lfuns.py <path/to/LyXAction.cpp> <where/to/save/LFUNs.lyx>
17 from datetime import date
20 sys.stderr.write(message + '\n')
24 return "Usage: %s <path/to/LyXAction.cpp> [<where/to/save/LFUNs.lyx>]" % prog_name
29 LYX_NEWLINE = "\n\\begin_inset Newline newline\n\\end_inset\n\n"
30 LYX_BACKSLASH = "\n\\backslash\n"
32 HTMLONLY_START = "\\htmlonly"
33 HTMLONLY_END = "\\endhtmlonly"
34 LFUN_NAME_ID = "\\var lyx::FuncCode lyx::"
35 LFUN_ACTION_ID = "\\li Action: "
36 LFUN_NOTION_ID = "\\li Notion: "
37 LFUN_SYNTAX_ID = "\\li Syntax: "
38 LFUN_PARAMS_ID = "\\li Params: "
39 LFUN_SAMPLE_ID = "\\li Sample: "
40 LFUN_ORIGIN_ID = "\\li Origin: "
41 LFUN_ENDVAR = "\\endvar"
43 ID_DICT = dict(name=LFUN_NAME_ID, action=LFUN_ACTION_ID, notion=LFUN_NOTION_ID,
44 syntax=LFUN_SYNTAX_ID, params=LFUN_PARAMS_ID, sample=LFUN_SAMPLE_ID, origin=LFUN_ORIGIN_ID)
46 LFUNS_HEADER = """# gen_lfuns.py generated this file. For more info see http://www.lyx.org/
51 \\use_default_options false
59 \\font_typewriter default
60 \\font_default_family default
67 \\paperfontsize default
75 \\paperorientation portrait
82 \\paragraph_separation indent
84 \\quotes_language english
87 \\paperpagestyle default
88 \\tracking_changes false
89 \\output_changes false
96 \\begin_layout Section*""" + "\nLFUNs documentation automatically generated " + str(date.today()) + """
99 \\begin_layout Standard
103 \\begin_layout Plain Layout
113 \\begin_inset VSpace 1cm
119 LFUNS_FOOTER = """\\end_body
124 """Takes a comment block (str) and parses it for fields describing the LFUN. Returns a dict containing the fields."""
126 lfun = dict(name="", action="", notion="", syntax="", params="", sample="", origin="")
128 lines = str.splitlines()
129 # strip leading whitespace and * from the lines of the comment to get
130 # rid of unimportant characters
131 for i in range(0, len(lines)):
132 lines[i] = lines[i].strip(" *")
134 for i in range(0, len(lines) - 1):
135 # work out what field is being read if none of these is found, the line will be added
136 # to the last field edited
137 # since the field identifier is not included skip it out if it's found, otherwise skip
138 # nothing as an existing field is being added to
139 # if a field id is found, then its the first line of the field so set the pre_space to ""
140 # so that the first line isn't prespaced
141 if lines[i].startswith(LFUN_NAME_ID):
144 skip = len(ID_DICT[field])
145 elif lines[i].startswith(LFUN_ACTION_ID):
148 skip = len(ID_DICT[field])
149 elif lines[i].startswith(LFUN_NOTION_ID):
152 skip = len(ID_DICT[field])
153 elif lines[i].startswith(LFUN_SYNTAX_ID):
156 skip = len(ID_DICT[field])
157 elif lines[i].startswith(LFUN_PARAMS_ID):
160 skip = len(ID_DICT[field])
161 elif lines[i].startswith(LFUN_SAMPLE_ID):
164 skip = len(ID_DICT[field])
165 elif lines[i].startswith(LFUN_ORIGIN_ID):
168 skip = len(ID_DICT[field])
169 elif lines[i].startswith(LFUN_ENDVAR):
173 # if a manual line break was found last line, don't prespace this line
174 if i > 1 and lines[i-1].endswith("\\n"):
179 # add the line to the field, processing it for \ characters and \n
180 # which, if occurring at the end of a line, must become a LYX_NEWLINE
181 line = lines[i][skip:]
183 # deal with \htmlonly
184 # TODO: convert chars found in htmlonly to unicode
185 start = line.find(HTMLONLY_START)
187 # if removing the htmlonly element leaves a double space, go back one to remove it
188 if line[start-1] == " ":
190 end = line.find(HTMLONLY_END)
192 end = line.find(HTMLONLY_END) + len(HTMLONLY_END)
193 line = line[:start] + line[end:]
195 # TODO: if HTMLONLY_END is not found, look on the next line
196 # TODO: in the current LyXAction.cpp there are no htmlonly fields which go over a line break
198 # deal with \ but leave \n if at the end of the line
199 slash_idx = line.find("\\")
200 while slash_idx >= 0:
201 if slash_idx < len(line)-2 \
202 or slash_idx == len(line)-1:
203 # true when the \ is not the last or second last char
204 # or when the slash is the last char of the line
206 # slash must be interpreted literaly so swap it for a LYX_BACKSLASH
207 line = line[:slash_idx] + LYX_BACKSLASH + line[slash_idx+1:]
208 # skip the index ahead beyond the added text
209 slash_idx = slash_idx + len(LYX_BACKSLASH)
210 elif line[slash_idx+1] != "n": # only evaluated if the line ends "\x" where 'x' != 'n'
211 line = line[:slash_idx] + LYX_BACKSLASH + line[slash_idx+1:]
212 # skip the index ahead beyond the added text
213 slash_idx = slash_idx + len(LYX_BACKSLASH)
214 # look for the next \
215 slash_idx = line.find("\\", slash_idx+1)
217 # \n at the end of lines will not be processed by the above while loop
218 # so sort those out now
219 # sometime lines end " \n" so chop the space if its there
220 if line.endswith(" \\n"):
221 line = line[:len(line)-3] + LYX_NEWLINE
222 elif line.endswith("\\n"):
223 line = line[:len(line)-2] + LYX_NEWLINE
225 # any references to other LFUNs need the # removing
226 # TODO: actually insert a cross-reference here
227 line = line.replace("#LFUN", "LFUN")
229 # handle the few #lyx:: cases
230 line = line.replace("#lyx::", "lyx::")
232 # the first line might not have a field in it in which
233 # case the variable field won't have a value, so check
236 lfun[field] = lfun[field] + pre_space + line
238 # TODO: sort out chopping lines of more that 80 chars in length
242 def write_fields(file, lfun):
243 """Writes the LFUN contained in the dict lfun to the file. Does not write a the file header or footer"""
244 # add lfun to LFUNs.lyx
245 file.write("\\begin_layout Subsection*\n")
246 file.write(lfun["name"] + "\n")
247 file.write("\\end_layout\n")
249 if lfun["action"] != "":
250 file.write("\\begin_layout Description\n")
251 file.write("Action " + lfun["action"] + "\n")
253 file.write("\\end_layout\n")
255 if lfun["notion"] != "":
256 file.write("\\begin_layout Description\n")
257 file.write("Notion " + lfun["notion"] + "\n")
258 file.write("\\end_layout\n")
260 if lfun["syntax"] != "":
261 file.write("\\begin_layout Description\n")
262 file.write("Syntax " + lfun["syntax"] + "\n")
263 file.write("\\end_layout\n")
265 if lfun["params"] != "":
266 file.write("\\begin_layout Description\n")
267 file.write("Params " + lfun["params"] + "\n")
268 file.write("\\end_layout\n")
270 if lfun["sample"] != "":
271 file.write("\\begin_layout Description\n")
272 file.write("Sample " + lfun["sample"] + "\n")
273 file.write("\\end_layout\n")
275 if lfun["origin"] != "":
276 file.write("\\begin_layout Description\n")
277 file.write("Origin " + lfun["origin"] + "\n")
278 file.write("\\end_layout\n")
283 # parse command line arguments
284 script_path, script_name = os.path.split(argv[0])
286 error(usage(script_name))
288 lyxaction_path = argv[1]
289 if not os.path.exists(lyxaction_path):
290 error(script_name + ": %s is not a valid path" % lyxaction_path, usage(argv[0]))
295 if os.path.isdir(lfuns_path):
296 lfuns_path = lfuns_path + "LFUNs.lyx"
297 elif os.path.exists(lfuns_path):
298 error(script_name + ": %s already exists, delete it and rerun the script" % lfuns_path)
299 lfuns_file = open(lfuns_path, 'wb')
301 lfuns_file = sys.stdout
303 sys.stderr.write(script_name + ": Start processing " + argv[1] + '\n')
304 # Read the input file and write the output file
305 lyxaction_file = open(lyxaction_path, 'rb')
307 lyxaction_text = lyxaction_file.read()
309 lfuns_file.write(LFUNS_HEADER)
311 # seek to the important bit of LyXAction.cpp
313 start = lyxaction_text.index("ev_item const items[] = {")
315 lyxaction_file.close()
317 error(script_name + ": LFUNs not found in " + lyxaction_file)
322 # look for a doxygen comment
323 start = lyxaction_text.find(DOXYGEN_START, start)
324 end = lyxaction_text.find(DOXYGEN_END, start) + len(DOXYGEN_END)
325 # parse the lfun if it is found
328 lfun = parse_lfun(lyxaction_text[start:end])
329 # write the lfun to the file
330 write_fields(lfuns_file, lfun)
331 # done adding current lfun to LFUNs.lyx so get the next one
334 # if no more lfuns are found, EOF reached
337 sys.stderr.write(script_name + ": Created documentation for " + str(count) + " LFUNs\n")
339 # write the last part of LFUNs.lyx
340 lfuns_file.write(LFUNS_FOOTER)
342 lyxaction_file.close()
345 sys.stderr.write(script_name + ": Finished\n")
347 if __name__ == "__main__":