]> git.lyx.org Git - lyx.git/blob - lib/doc/doc_toc.py
EmbeddedObjects.lyx: tiny fix
[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 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 if __name__ == "__main__":
29     srcdir = os.path.dirname(sys.argv[0])
30     if srcdir == '':
31         srcdir = '.'
32     sys.path.insert(0, srcdir + "/../lyx2lyx")
33
34 # when doc_toc is imported by scons, sys.path is set over there
35 import parser_tools
36 import LyX
37 import depend
38
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")}
48
49 def usage(pname):
50     print """Usage: %s lang output
51
52     lang is the language to build the TOC file,
53 """ % pname
54
55
56 transform_table = {'Title' : 'Section*', 'Chapter': 'Enumerate',
57                    'Section':'Enumerate', 'Subsection': 'Enumerate',
58                    'Subsubsection' : 'Enumerate'}
59
60 def build_from_toc(par_list):
61     if not par_list:
62         return []
63
64     if par_list[0].name == 'Title':
65         par = par_list[0]
66         par.name = transform_table[par.name]
67
68         if len(par_list) == 1:
69             return par_list
70
71         for i in range(1, len(par_list)):
72             if par_list[i].name == 'Title':
73                 return [par] + \
74                         build_from_toc(par_list[1:i]) + \
75                         build_from_toc(par_list[i:])
76
77         return [par] + build_from_toc(par_list[1:])
78
79     if par_list[0].name in ('Chapter', 'Section', 'Subsection', 'Subsubsection'):
80         return nest_struct(par_list[0].name, par_list)
81
82
83 def nest_struct(name, par_list):
84     if par_list[0].name == name:
85         par = par_list[0]
86         par.name = transform_table[par.name]
87
88         if len(par_list) == 1:
89             return par_list
90
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:])
96         return [ par ]
97
98
99 def build_toc(output, documents, lang=None):
100     # Determine existing translated documents for that language.
101     toc_general = []
102     for file in documents:
103         file = LyX.File(input= file)
104         file.convert()
105         toc_general.extend(file.get_toc())
106
107     # if lang is not given, guess from document names. (Used in scons build)
108     if lang is None:
109         lang = 'en'
110         for file in documents:
111             dir = file.split(os.sep)[-2]
112             if dir in info.keys():
113                 lang = dir
114     file = LyX.NewFile(output = output)
115     data = info[lang]
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))
121     file.set_body(body)
122     file.write()
123
124     
125 def main(argv):
126     if len(argv) != 3:
127         usage()
128         sys.exit(1)
129
130     lang = argv[1]
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))
134
135     # choose language files
136     if lang == 'en':
137         output = os.path.join(argv[2], 'TOC.lyx')
138     else:
139         output = os.path.join(argv[2], lang, 'TOC.lyx')
140         # fallback
141         if lang not in info:
142             lang = 'en'
143
144     build_toc(output, depend.documents(srcdir, lang), lang)
145
146
147 if __name__ == "__main__":
148     main(sys.argv)