2 # -*- coding: utf-8 -*-
3 # This file is part of the LyX Documentation
4 # Copyright (C) 2004 José Matos <jamatos@lyx.org>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # This script creates a "master table of contents" for a set of LyX docs.
21 # It does so by going through the files and printing out all of the
22 # chapter, section, and sub(sub)section headings out. (It numbers the
23 # sections sequentially; hopefully noone's using Section* in the docs.)
28 if __name__ == "__main__":
29 srcdir = os.path.dirname(sys.argv[0])
32 sys.path.insert(0, srcdir + "/../lyx2lyx")
34 # when doc_toc is imported by scons, sys.path is set over there
39 # Specific language information
40 # info["isoname"] = (language, language_quotes, enconding, TOC_translated)
41 info = { 'cs' : ('czech', 'german', 'latin2', "Obsah dokumentace LyXu"),
42 'da' : ('danish', 'german', 'latin1', "Indholdsfortegnelse over LyX's dokumentation"),
43 'de' : ('german', 'german', 'latin1', "Inhaltsverzeichnis LyX Dokumentation"),
44 'fr' : ('french', 'french', 'latin1', "Plan de la documentation"),
45 'ru' : ('russian', 'english', 'koi8-r', "LyX Documentation Table of Contents"),
46 'sl' : ('slovene', 'german', 'latin2', "Kazalo dokumentacije LyXa"),
47 'en' : ('english', 'english', 'latin1', "LyX Documentation Table of Contents")}
50 print """Usage: %s lang output
52 lang is the language to build the TOC file,
56 transform_table = {'Title' : 'Section*', 'Chapter': 'Enumerate',
57 'Section':'Enumerate', 'Subsection': 'Enumerate',
58 'Subsubsection' : 'Enumerate'}
60 def build_from_toc(par_list):
64 if par_list[0].name == 'Title':
66 par.name = transform_table[par.name]
68 if len(par_list) == 1:
71 for i in range(1, len(par_list)):
72 if par_list[i].name == 'Title':
74 build_from_toc(par_list[1:i]) + \
75 build_from_toc(par_list[i:])
77 return [par] + build_from_toc(par_list[1:])
79 if par_list[0].name in ('Chapter', 'Section', 'Subsection', 'Subsubsection'):
80 return nest_struct(par_list[0].name, par_list)
83 def nest_struct(name, par_list):
84 if par_list[0].name == name:
86 par.name = transform_table[par.name]
88 if len(par_list) == 1:
91 for i in range(1, len(par_list)):
92 if par_list[i].name == name:
93 par.child = build_from_toc(par_list[1:i])
94 return [par] + build_from_toc(par_list[i:])
95 par.child = build_from_toc(par_list[1:])
99 def build_toc(output, documents, lang=None):
100 # Determine existing translated documents for that language.
102 for file in documents:
103 file = LyX.File(input= file)
105 toc_general.extend(file.get_toc())
107 # if lang is not given, guess from document names. (Used in scons build)
110 for file in documents:
111 dir = file.split(os.sep)[-2]
112 if dir in info.keys():
114 file = LyX.NewFile(output = output)
116 file.set_header(language = data[0], language_quotes = data[1], inputencoding = "auto")
117 file.language = data[0]
118 file.encoding = "utf-8"
119 body = [ LyX.Paragraph('Title', [data[3]])]
120 body.extend(build_from_toc(toc_general))
131 if not os.path.isdir(os.path.join(argv[2], lang)):
132 # need to create lang dir if build dir != src dir
133 os.mkdir(os.path.join(argv[2], lang))
135 # choose language files
137 output = os.path.join(argv[2], 'TOC.lyx')
139 output = os.path.join(argv[2], lang, 'TOC.lyx')
144 build_toc(output, depend.documents(srcdir, lang), lang)
147 if __name__ == "__main__":