]> git.lyx.org Git - lyx.git/blob - lib/doc/doc_toc.py
UserGuide.lyx: clarify the description of the character encodings
[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, TOC_translated)
45 info = { 'cs' : ('czech', 'german', u"Obsah dokumentace LyXu"),
46          'da' : ('danish', 'german', u"Indholdsfortegnelse over LyX's dokumentation"),
47          'de' : ('german', 'german', u"Inhaltsverzeichnis LyX Dokumentation"),
48          'es' : ('spanish', 'spanish', u"Índice general LyX documentation"),
49          'fr' : ('french', 'french', u"Plan de la documentation LyX"),
50          'ja' : ('japanese', 'japanese', u"LyX取扱説明書目次"),
51          'ru' : ('russian', 'english', u"LyX Documentation Table of Contents"),
52          'sl' : ('slovene', 'german', u"Kazalo dokumentacije LyXa"),
53          'en' : ('english', 'english', u"LyX Documentation Table of Contents")}
54
55 def usage(pname):
56     print """Usage: %s lang output
57
58     lang is the language to build the TOC file,
59 """ % pname
60
61
62 transform_table = {'Title' : 'Section*', 'Chapter': 'Enumerate',
63                    'Section':'Enumerate', 'Subsection': 'Enumerate',
64                    'Subsubsection' : 'Enumerate'}
65
66 def build_from_toc(par_list):
67     if not par_list:
68         return []
69
70     if par_list[0].name == 'Title':
71         par = par_list[0]
72         par.name = transform_table[par.name]
73
74         if len(par_list) == 1:
75             return par_list
76
77         for i in range(1, len(par_list)):
78             if par_list[i].name == 'Title':
79                 return [par] + \
80                         build_from_toc(par_list[1:i]) + \
81                         build_from_toc(par_list[i:])
82
83         return [par] + build_from_toc(par_list[1:])
84
85     if par_list[0].name in ('Chapter', 'Section', 'Subsection', 'Subsubsection'):
86         return nest_struct(par_list[0].name, par_list)
87
88
89 def nest_struct(name, par_list):
90     if par_list[0].name == name:
91         par = par_list[0]
92         par.name = transform_table[par.name]
93
94         if len(par_list) == 1:
95             return par_list
96
97         for i in range(1, len(par_list)):
98             if par_list[i].name == name:
99                 par.child = build_from_toc(par_list[1:i])
100                 return [par] + build_from_toc(par_list[i:])
101         par.child = build_from_toc(par_list[1:])
102         return [ par ]
103
104
105 def build_toc(output, documents, lang=None):
106     # Determine existing translated documents for that language.
107     toc_general = []
108     for file in documents:
109         file = LyX.File(input= file)
110         file.convert()
111         toc_general.extend(file.get_toc())
112
113     # if lang is not given, guess from document names. (Used in scons build)
114     if lang is None:
115         lang = 'en'
116         for file in documents:
117             dir = file.split(os.sep)[-2]
118             if dir in info.keys():
119                 lang = dir
120     file = LyX.NewFile(output = output)
121     data = info[lang]
122     file.set_header(language = data[0], language_quotes = data[1], inputencoding = "auto")
123     file.language = data[0]
124     file.encoding = "utf-8"
125     body = [ LyX.Paragraph('Title', [data[2]])]
126     body.extend(build_from_toc(toc_general))
127     file.set_body(body)
128     file.write()
129
130     
131 def main(argv):
132     if len(argv) != 3:
133         usage(argv[0])
134         sys.exit(1)
135
136     lang = argv[1]
137     if not os.path.isdir(os.path.join(argv[2], lang)):
138         # need to create lang dir if build dir != src dir
139         os.mkdir(os.path.join(argv[2], lang))
140
141     # choose language files
142     if lang == 'en':
143         output = os.path.join(argv[2], 'TOC.lyx')
144     else:
145         output = os.path.join(argv[2], lang, 'TOC.lyx')
146         # fallback
147         if lang not in info:
148             lang = 'en'
149
150     build_toc(output, depend.documents(srcdir, lang), lang)
151
152
153 if __name__ == "__main__":
154     main(sys.argv)