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 # the first line might not have a field in it in which
230 # case the variable field won't have a value, so check
233 lfun[field] = lfun[field] + pre_space + line
235 # TODO: sort out chopping lines of more that 80 chars in length
239 def write_fields(file, lfun):
240 """Writes the LFUN contained in the dict lfun to the file. Does not write a the file header or footer"""
241 # add lfun to LFUNs.lyx
242 file.write("\\begin_layout Subsection*\n")
243 file.write(lfun["name"] + "\n")
244 file.write("\\end_layout\n")
246 if lfun["action"] != "":
247 file.write("\\begin_layout Description\n")
248 file.write("Action " + lfun["action"] + "\n")
250 file.write("\\end_layout\n")
252 if lfun["notion"] != "":
253 file.write("\\begin_layout Description\n")
254 file.write("Notion " + lfun["notion"] + "\n")
255 file.write("\\end_layout\n")
257 if lfun["syntax"] != "":
258 file.write("\\begin_layout Description\n")
259 file.write("Syntax " + lfun["syntax"] + "\n")
260 file.write("\\end_layout\n")
262 if lfun["params"] != "":
263 file.write("\\begin_layout Description\n")
264 file.write("Params " + lfun["params"] + "\n")
265 file.write("\\end_layout\n")
267 if lfun["sample"] != "":
268 file.write("\\begin_layout Description\n")
269 file.write("Sample " + lfun["sample"] + "\n")
270 file.write("\\end_layout\n")
272 if lfun["origin"] != "":
273 file.write("\\begin_layout Description\n")
274 file.write("Origin " + lfun["origin"] + "\n")
275 file.write("\\end_layout\n")
280 # parse command line arguments
281 script_path, script_name = os.path.split(argv[0])
283 error(usage(script_name))
285 lyxaction_path = argv[1]
286 if not os.path.exists(lyxaction_path):
287 error(script_name + ": %s is not a valid path" % lyxaction_path, usage(argv[0]))
291 if os.path.isdir(lfuns_path):
292 lfuns_path = lfuns_path + "LFUNs.lyx"
293 elif os.path.exists(lfuns_path):
294 error(script_name + ": %s already exists, delete it and rerun the script" % lfuns_path)
296 print(script_name + ": Start processing " + argv[1])
297 # Read the input file and write the output file
298 lyxaction_file = open(lyxaction_path, 'rb')
299 lfuns_file = open(lfuns_path, 'wb')
301 lyxaction_text = lyxaction_file.read()
303 lfuns_file.write(LFUNS_HEADER)
305 # seek to the important bit of LyXAction.cpp
307 start = lyxaction_text.index("ev_item const items[] = {")
309 lyxaction_file.close()
311 error(script_name + ": LFUNs not found in " + lyxaction_file)
316 # look for a doxygen comment
317 start = lyxaction_text.find(DOXYGEN_START, start)
318 end = lyxaction_text.find(DOXYGEN_END, start) + len(DOXYGEN_END)
319 # parse the lfun if it is found
322 lfun = parse_lfun(lyxaction_text[start:end])
323 # write the lfun to the file
324 write_fields(lfuns_file, lfun)
325 # done adding current lfun to LFUNs.lyx so get the next one
328 # if no more lfuns are found, EOF reached
331 print(script_name + ": Created documentation for " + str(count) + " LFUNs")
333 # write the last part of LFUNs.lyx
334 lfuns_file.write(LFUNS_FOOTER)
336 lyxaction_file.close()
339 print(script_name + ": Finished")
341 if __name__ == "__main__":