+
+
+ def get_toc(self, depth = 4):
+ " Returns the TOC of this LyX document."
+ paragraphs_filter = {'Title' : 0,'Chapter' : 1, 'Section' : 2, 'Subsection' : 3, 'Subsubsection': 4}
+ allowed_insets = ['Quotes']
+ allowed_parameters = '\\paragraph_spacing', '\\noindent', '\\align', '\\labelwidthstring', "\\start_of_appendix", "\\leftindent"
+
+ sections = []
+ for section in paragraphs_filter.keys():
+ sections.append('\\begin_layout %s' % section)
+
+ toc_par = []
+ i = 0
+ while 1:
+ i = find_tokens(self.body, sections, i)
+ if i == -1:
+ break
+
+ j = find_end_of(self.body, i + 1, '\\begin_layout', '\\end_layout')
+ if j == -1:
+ self.warning('Incomplete file.', 0)
+ break
+
+ section = string.split(self.body[i])[1]
+ if section[-1] == '*':
+ section = section[:-1]
+
+ par = []
+
+ k = i + 1
+ # skip paragraph parameters
+ while not string.strip(self.body[k]) or string.split(self.body[k])[0] in allowed_parameters:
+ k = k +1
+
+ while k < j:
+ if check_token(self.body[k], '\\begin_inset'):
+ inset = string.split(self.body[k])[1]
+ end = find_end_of_inset(self.body, k)
+ if end == -1 or end > j:
+ self.warning('Malformed file.', 0)
+
+ if inset in allowed_insets:
+ par.extend(self.body[k: end+1])
+ k = end + 1
+ else:
+ par.append(self.body[k])
+ k = k + 1
+
+ # trim empty lines in the end.
+ while string.strip(par[-1]) == '' and par:
+ par.pop()
+
+ toc_par.append(Paragraph(section, par))
+
+ i = j + 1
+
+ return toc_par
+
+
+class File(LyX_Base):
+ " This class reads existing LyX files."
+ def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level, try_hard = 0):
+ LyX_Base.__init__(self, end_format, input, output, error, debug, try_hard)
+ self.read()
+
+
+class NewFile(LyX_Base):
+ " This class is to create new LyX files."
+ def set_header(self, **params):
+ # set default values
+ self.header.extend([
+ "#LyX xxxx created this file. For more info see http://www.lyx.org/",
+ "\\lyxformat xxx",
+ "\\begin_document",
+ "\\begin_header",
+ "\\textclass article",
+ "\\language english",
+ "\\inputencoding auto",
+ "\\fontscheme default",
+ "\\graphics default",
+ "\\paperfontsize default",
+ "\\papersize default",
+ "\\use_geometry false",
+ "\\use_amsmath 1",
+ "\\cite_engine basic",
+ "\\use_bibtopic false",
+ "\\paperorientation portrait",
+ "\\secnumdepth 3",
+ "\\tocdepth 3",
+ "\\paragraph_separation indent",
+ "\\defskip medskip",
+ "\\quotes_language english",
+ "\\papercolumns 1",
+ "\\papersides 1",
+ "\\paperpagestyle default",
+ "\\tracking_changes false",
+ "\\end_header"])
+
+ self.format = get_end_format()
+ for param in params:
+ self.set_parameter(param, params[param])
+
+
+ def set_body(self, paragraphs):
+ self.body.extend(['\\begin_body',''])
+
+ for par in paragraphs:
+ self.body.extend(par.asLines())
+
+ self.body.extend(['','\\end_body', '\\end_document'])
+
+
+class Paragraph:
+ # unfinished implementation, it is missing the Text and Insets representation.
+ " This class represents the LyX paragraphs."
+ def __init__(self, name, body=[], settings = [], child = []):
+ """ Parameters:
+ name: paragraph name.
+ body: list of lines of body text.
+ child: list of paragraphs that descend from this paragraph.
+ """
+ self.name = name
+ self.body = body
+ self.settings = settings
+ self.child = child
+
+ def asLines(self):
+ " Converts the paragraph to a list of strings, representing it in the LyX file."
+ result = ['','\\begin_layout %s' % self.name]
+ result.extend(self.settings)
+ result.append('')
+ result.extend(self.body)
+ result.append('\\end_layout')
+
+ if not self.child:
+ return result
+
+ result.append('\\begin_deeper')
+ for node in self.child:
+ result.extend(node.asLines())
+ result.append('\\end_deeper')
+
+ return result
+
+
+class Inset:
+ " This class represents the LyX insets."
+ pass
+
+
+class Text:
+ " This class represents simple chuncks of text."
+ pass