]> git.lyx.org Git - lyx.git/blob - lib/doc/doc_toc.py
UserGuide.lyx updates: - revise descriptions of Branches and Nomenclature
[lyx.git] / lib / doc / doc_toc.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
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 is called by the script depend.py to create a "master table of contents"
21 # for a set of LyX docs.
22 # It does so by going through the files and printing out all of the
23 # chapter, section, and sub(sub)section headings out. (It numbers the
24 # sections sequentially; hopefully noone's using Section* in the docs.)
25 # It is called using this syntax:
26 # depend.py doc_toc.py SetOfDocuments
27 # where SetOfDocuments is a set of documents
28
29 import sys
30 import os
31
32 if __name__ == "__main__":
33     srcdir = os.path.dirname(sys.argv[0])
34     if srcdir == '':
35         srcdir = '.'
36     sys.path.insert(0, srcdir + "/../lyx2lyx")
37
38 # when doc_toc is imported by scons, sys.path is set over there
39 import parser_tools
40 import LyX
41 import depend
42
43 # Specific language information
44 # info["isoname"] = (language, language_quotes, enconding, TOC_translated)
45 info = { 'cs' : ('czech', 'german', 'latin2', "Obsah dokumentace LyXu"),
46          'da' : ('danish', 'german', 'latin1', "Indholdsfortegnelse over LyX's dokumentation"),
47          'de' : ('german', 'german', 'latin1', "Inhaltsverzeichnis LyX Dokumentation"),
48          'fr' : ('french', 'french', 'latin1', "Plan de la documentation"),
49          'ru' : ('russian', 'english', 'koi8-r', "LyX Documentation Table of Contents"),
50          'sl' : ('slovene', 'german', 'latin2', "Kazalo dokumentacije LyXa"),
51          'en' : ('english', 'english', 'latin1', "LyX Documentation Table of Contents")}
52
53 def usage(pname):
54     print """Usage: %s lang output
55
56     lang is the language to build the TOC file,
57 """ % pname
58
59
60 transform_table = {'Title' : 'Section*', 'Chapter': 'Enumerate',
61                    'Section':'Enumerate', 'Subsection': 'Enumerate',
62                    'Subsubsection' : 'Enumerate'}
63
64 def build_from_toc(par_list):
65     if not par_list:
66         return []
67
68     if par_list[0].name == 'Title':
69         par = par_list[0]
70         par.name = transform_table[par.name]
71
72         if len(par_list) == 1:
73             return par_list
74
75         for i in range(1, len(par_list)):
76             if par_list[i].name == 'Title':
77                 return [par] + \
78                         build_from_toc(par_list[1:i]) + \
79                         build_from_toc(par_list[i:])
80
81         return [par] + build_from_toc(par_list[1:])
82
83     if par_list[0].name in ('Chapter', 'Section', 'Subsection', 'Subsubsection'):
84         return nest_struct(par_list[0].name, par_list)
85
86
87 def nest_struct(name, par_list):
88     if par_list[0].name == name:
89         par = par_list[0]
90         par.name = transform_table[par.name]
91
92         if len(par_list) == 1:
93             return par_list
94
95         for i in range(1, len(par_list)):
96             if par_list[i].name == name:
97                 par.child = build_from_toc(par_list[1:i])
98                 return [par] + build_from_toc(par_list[i:])
99         par.child = build_from_toc(par_list[1:])
100         return [ par ]
101
102
103 def build_toc(output, documents, lang=None):
104     # Determine existing translated documents for that language.
105     toc_general = []
106     for file in documents:
107         file = LyX.File(input= file)
108         file.convert()
109         toc_general.extend(file.get_toc())
110
111     # if lang is not given, guess from document names. (Used in scons build)
112     if lang is None:
113         lang = 'en'
114         for file in documents:
115             dir = file.split(os.sep)[-2]
116             if dir in info.keys():
117                 lang = dir
118     file = LyX.NewFile(output = output)
119     data = info[lang]
120     file.set_header(language = data[0], language_quotes = data[1], inputencoding = "auto")
121     file.language = data[0]
122     file.encoding = "utf-8"
123     body = [ LyX.Paragraph('Title', [data[3]])]
124     body.extend(build_from_toc(toc_general))
125     file.set_body(body)
126     file.write()
127
128     
129 def main(argv):
130     if len(argv) != 3:
131         usage()
132         sys.exit(1)
133
134     lang = argv[1]
135     if not os.path.isdir(os.path.join(argv[2], lang)):
136         # need to create lang dir if build dir != src dir
137         os.mkdir(os.path.join(argv[2], lang))
138
139     # choose language files
140     if lang == 'en':
141         output = os.path.join(argv[2], 'TOC.lyx')
142     else:
143         output = os.path.join(argv[2], lang, 'TOC.lyx')
144         # fallback
145         if lang not in info:
146             lang = 'en'
147
148     build_toc(output, depend.documents(srcdir, lang), lang)
149
150
151 if __name__ == "__main__":
152     main(sys.argv)