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