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
56 \\font_typewriter default
57 \\font_default_family default
64 \\paperfontsize default
73 \\paperorientation portrait
80 \\paragraph_separation indent
82 \\quotes_language english
85 \\paperpagestyle default
86 \\tracking_changes false
87 \\output_changes false
94 \\begin_layout Section*""" + "\nLFUNs documentation automatically generated " + str(date.today()) + """
97 \\begin_layout Standard
101 \\begin_layout Plain Layout
111 \\begin_inset VSpace 1cm
117 LFUNS_FOOTER = """\\end_body
122 """Takes a comment block (str) and parses it for fields describing the LFUN. Returns a dict containing the fields."""
124 lfun = dict(name="", action="", notion="", syntax="", params="", sample="", origin="")
126 lines = str.splitlines()
127 # strip leading whitespace and * from the lines of the comment to get
128 # rid of unimportant characters
129 for i in range(0, len(lines)):
130 lines[i] = lines[i].strip(" *")
132 for i in range(0, len(lines) - 1):
133 # work out what field is being read if none of these is found, the line will be added
134 # to the last field edited
135 # since the field identifier is not included skip it out if it's found, otherwise skip
136 # nothing as an existing field is being added to
137 # if a field id is found, then its the first line of the field so set the pre_space to ""
138 # so that the first line isn't prespaced
139 if lines[i].startswith(LFUN_NAME_ID):
142 skip = len(ID_DICT[field])
143 elif lines[i].startswith(LFUN_ACTION_ID):
146 skip = len(ID_DICT[field])
147 elif lines[i].startswith(LFUN_NOTION_ID):
150 skip = len(ID_DICT[field])
151 elif lines[i].startswith(LFUN_SYNTAX_ID):
154 skip = len(ID_DICT[field])
155 elif lines[i].startswith(LFUN_PARAMS_ID):
158 skip = len(ID_DICT[field])
159 elif lines[i].startswith(LFUN_SAMPLE_ID):
162 skip = len(ID_DICT[field])
163 elif lines[i].startswith(LFUN_ORIGIN_ID):
166 skip = len(ID_DICT[field])
167 elif lines[i].startswith(LFUN_ENDVAR):
171 # if a manual line break was found last line, don't prespace this line
172 if i > 1 and lines[i-1].endswith("\\n"):
177 # add the line to the field, processing it for \ characters and \n
178 # which, if occurring at the end of a line, must become a LYX_NEWLINE
179 line = lines[i][skip:]
181 # deal with \htmlonly
182 # TODO: convert chars found in htmlonly to unicode
183 start = line.find(HTMLONLY_START)
185 # if removing the htmlonly element leaves a double space, go back one to remove it
186 if line[start-1] == " ":
188 end = line.find(HTMLONLY_END)
190 end = line.find(HTMLONLY_END) + len(HTMLONLY_END)
191 line = line[:start] + line[end:]
193 # TODO: if HTMLONLY_END is not found, look on the next line
194 # TODO: in the current LyXAction.cpp there are no htmlonly fields which go over a line break
196 # deal with \ but leave \n if at the end of the line
197 slash_idx = line.find("\\")
198 while slash_idx >= 0:
199 if slash_idx < len(line)-2 \
200 or slash_idx == len(line)-1:
201 # true when the \ is not the last or second last char
202 # or when the slash is the last char of the line
204 # slash must be interpreted literaly so swap it for a LYX_BACKSLASH
205 line = line[:slash_idx] + LYX_BACKSLASH + line[slash_idx+1:]
206 # skip the index ahead beyond the added text
207 slash_idx = slash_idx + len(LYX_BACKSLASH)
208 elif line[slash_idx+1] != "n": # only evaluated if the line ends "\x" where 'x' != 'n'
209 line = line[:slash_idx] + LYX_BACKSLASH + line[slash_idx+1:]
210 # skip the index ahead beyond the added text
211 slash_idx = slash_idx + len(LYX_BACKSLASH)
212 # look for the next \
213 slash_idx = line.find("\\", slash_idx+1)
215 # \n at the end of lines will not be processed by the above while loop
216 # so sort those out now
217 # sometime lines end " \n" so chop the space if its there
218 if line.endswith(" \\n"):
219 line = line[:len(line)-3] + LYX_NEWLINE
220 elif line.endswith("\\n"):
221 line = line[:len(line)-2] + LYX_NEWLINE
223 # any references to other LFUNs need the # removing
224 # TODO: actually insert a cross-reference here
225 line = line.replace("#LFUN", "LFUN")
227 # handle the few #lyx:: cases
228 line = line.replace("#lyx::", "lyx::")
230 # the first line might not have a field in it in which
231 # case the variable field won't have a value, so check
234 lfun[field] = lfun[field] + pre_space + line
236 # TODO: sort out chopping lines of more that 80 chars in length
240 def write_fields(file, lfun):
241 """Writes the LFUN contained in the dict lfun to the file. Does not write a the file header or footer"""
242 # add lfun to LFUNs.lyx
243 file.write("\\begin_layout Subsection*\n")
244 file.write(lfun["name"] + "\n")
245 file.write("\\end_layout\n")
247 if lfun["action"] != "":
248 file.write("\\begin_layout Description\n")
249 file.write("Action " + lfun["action"] + "\n")
251 file.write("\\end_layout\n")
253 if lfun["notion"] != "":
254 file.write("\\begin_layout Description\n")
255 file.write("Notion " + lfun["notion"] + "\n")
256 file.write("\\end_layout\n")
258 if lfun["syntax"] != "":
259 file.write("\\begin_layout Description\n")
260 file.write("Syntax " + lfun["syntax"] + "\n")
261 file.write("\\end_layout\n")
263 if lfun["params"] != "":
264 file.write("\\begin_layout Description\n")
265 file.write("Params " + lfun["params"] + "\n")
266 file.write("\\end_layout\n")
268 if lfun["sample"] != "":
269 file.write("\\begin_layout Description\n")
270 file.write("Sample " + lfun["sample"] + "\n")
271 file.write("\\end_layout\n")
273 if lfun["origin"] != "":
274 file.write("\\begin_layout Description\n")
275 file.write("Origin " + lfun["origin"] + "\n")
276 file.write("\\end_layout\n")
281 # parse command line arguments
282 script_path, script_name = os.path.split(argv[0])
284 error(usage(script_name))
286 lyxaction_path = argv[1]
287 if not os.path.exists(lyxaction_path):
288 error(script_name + ": %s is not a valid path" % lyxaction_path, usage(argv[0]))
293 if os.path.isdir(lfuns_path):
294 lfuns_path = lfuns_path + "LFUNs.lyx"
295 elif os.path.exists(lfuns_path):
296 error(script_name + ": %s already exists, delete it and rerun the script" % lfuns_path)
297 lfuns_file = open(lfuns_path, 'wb')
299 lfuns_file = sys.stdout
301 sys.stderr.write(script_name + ": Start processing " + argv[1] + '\n')
302 # Read the input file and write the output file
303 lyxaction_file = open(lyxaction_path, 'rb')
305 lyxaction_text = lyxaction_file.read()
307 lfuns_file.write(LFUNS_HEADER)
309 # seek to the important bit of LyXAction.cpp
311 start = lyxaction_text.index("ev_item const items[] = {")
313 lyxaction_file.close()
315 error(script_name + ": LFUNs not found in " + lyxaction_file)
320 # look for a doxygen comment
321 start = lyxaction_text.find(DOXYGEN_START, start)
322 end = lyxaction_text.find(DOXYGEN_END, start) + len(DOXYGEN_END)
323 # parse the lfun if it is found
326 lfun = parse_lfun(lyxaction_text[start:end])
327 # write the lfun to the file
328 write_fields(lfuns_file, lfun)
329 # done adding current lfun to LFUNs.lyx so get the next one
332 # if no more lfuns are found, EOF reached
335 sys.stderr.write(script_name + ": Created documentation for " + str(count) + " LFUNs\n")
337 # write the last part of LFUNs.lyx
338 lfuns_file.write(LFUNS_FOOTER)
340 lyxaction_file.close()
343 sys.stderr.write(script_name + ": Finished\n")
345 if __name__ == "__main__":