]> git.lyx.org Git - lyx.git/blob - lib/doc/doc_toc.py
Update TOC building for all the translated languages. (Documentation)
[lyx.git] / lib / doc / doc_toc.py
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3 # This file is part of the LyX Documentation
4 # Copyright (C) 2004 José Matos <jamatos@lyx.org>
5 #
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.
10 #
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.
15 #
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.
19
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.)
24
25 import sys
26 import os
27
28 sys.path.insert(0,"../lyx2lyx")
29 import parser_tools
30 import LyX
31 import depend
32
33 # Specific language information
34 # info["isoname"] = (language, language_quotes, enconding, TOC_translated)
35 info = { 'da' : ('danish', 'german', 'latin1', "Indholdsfortegnelse over LyX's dokumentation"),
36          'de' : ('german', 'german', 'latin1', "Inhaltsverzeichnis LyX Dokumentation"),
37          'fr' : ('french', 'french', 'latin1', "Plan de la documentation"),
38          'ru' : ('russian', 'english', 'koi8-r', "LyX Documentation Table of Contents"),
39          'sl' : ('slovene', 'german', 'latin2', "Kazalo dokumentacije LyXa"),
40          'en' : ('english', 'english', 'latin1', "LyX Documentation Table of Contents")}
41
42 def usage(pname):
43     print """Usage: %s [lang]
44
45     lang is the language to build the TOC file, if not present use english.
46 """ % pname
47
48
49 transform_table = {'Title' : 'Section*', 'Chapter': 'Enumerate',
50                    'Section':'Enumerate', 'Subsection': 'Enumerate',
51                    'Subsubsection' : 'Enumerate'}
52
53 def build_from_toc(par_list):
54     if not par_list:
55         return []
56
57     if par_list[0].name == 'Title':
58         par = par_list[0]
59         par.name = transform_table[par.name]
60
61         if len(par_list) == 1:
62             return par_list
63         
64         for i in range(1, len(par_list)):
65             if par_list[i].name == 'Title':
66                 return [par] + \
67                         build_from_toc(par_list[1:i]) + \
68                         build_from_toc(par_list[i:])
69
70         return [par] + build_from_toc(par_list[1:])
71
72     if par_list[0].name in ('Chapter', 'Section', 'Subsection', 'Subsubsection'):
73         return nest_struct(par_list[0].name, par_list)
74
75
76 def nest_struct(name, par_list):
77     if par_list[0].name == name:
78         par = par_list[0]
79         par.name = transform_table[par.name]
80
81         if len(par_list) == 1:
82             return par_list
83         
84         for i in range(1, len(par_list)):
85             if par_list[i].name == name:
86                 par.child = build_from_toc(par_list[1:i])
87                 return [par] + build_from_toc(par_list[i:])
88         par.child = build_from_toc(par_list[1:])
89         return [ par ]
90
91
92 def main(argv):
93     if len(argv) > 2:
94         usage()
95         sys.exit(1)
96
97     # choose language and prefix for files
98     if len(argv) == 1:
99         lang = "en"
100         pref = ""
101     else:
102         lang = argv[1]
103         pref = lang + '_'
104         # fallback
105         if lang not in info:
106             lang = 'en'
107
108     # Determine existing translated documents for that language.
109     toc_general = []
110     for file in depend.documents(pref):
111         file = LyX.File(input= file)
112         file.convert()
113         toc_general.extend(file.get_toc())
114         
115     file = LyX.NewFile(output= pref + 'TOC.lyx')
116     data = info[lang]
117     file.set_header(language = data[0], language_quotes = data[1], inputencoding = data[2])
118     body = [ LyX.Paragraph('Title', [data[3]])]
119     body.extend(build_from_toc(toc_general))
120     file.set_body(body)
121     file.write()
122
123
124 if __name__ == "__main__":
125     main(sys.argv)