]> git.donarmstrong.com Git - lilypond.git/blob - python/book_base.py
Web-ja: update introduction
[lilypond.git] / python / book_base.py
1 # -*- coding: utf-8 -*-
2
3 import lilylib as ly
4 import book_snippets as BookSnippet
5 from book_snippets import *
6 import re
7 global _;_=ly._
8
9 progress = ly.progress
10 warning = ly.warning
11 error = ly.error
12
13 ########################################################################
14 # Helper functions
15 ########################################################################
16
17 def find_file (name, include_path, working_dir=None, raise_error=True):
18     current_path = working_dir or os.getcwd();
19     for i in [current_path] + include_path:
20         full = os.path.join (i, name)
21         full = os.path.normpath (os.path.join (current_path, full))
22         if os.path.exists (full):
23             return full
24
25     if raise_error:
26         error (_ ("file not found: %s") % name + '\n')
27         exit (1)
28     return ''
29
30 def verbatim_html (s):
31     return re.sub ('>', '>',
32            re.sub ('<', '&lt;',
33                re.sub ('&', '&amp;', s)))
34
35
36 ########################################################################
37 # Option handling
38 ########################################################################
39
40 #TODO: Definitions just once in all files!!!
41 LINE_WIDTH = 'line-width'
42
43 # TODO: Implement the intertext snippet option:
44 #         'intertext': r',?\s*intertext=\".*?\"',
45
46 default_snippet_opts = { 'alt': "[image of music]" }
47
48
49 ########################################################################
50 # format handling
51 ########################################################################
52
53 all_formats = []
54 def register_format (fmt):
55   all_formats.append (fmt)
56
57
58
59 ########################################################################
60 # Snippet handling
61 ########################################################################
62
63 # Use this for sorting the keys of the defined snippet types (the dict
64 # is unsorted, so we need to return sorted keys to ensure processing
65 # in a pre-defined order)
66 # Containing blocks must be first, see find_toplevel_snippets.
67
68 snippet_type_order = [
69     'multiline_comment',
70     'verbatim',
71     'verb',
72     'lilypond_block',
73     'singleline_comment',
74     'lilypond_file',
75     'include',
76     'lilypond',
77     'lilypondversion',
78 ]
79
80
81
82 ########################################################################
83 # Base class for all output formats
84 ########################################################################
85
86 class BookOutputFormat:
87     def __init__ (self):
88         self.format = None
89         self.default_extension = None
90         self.snippet_res = {}
91         self.output = {}
92         self.handled_extensions = []
93         self.image_formats = "ps,png"
94         self.global_options = {}
95         self.document_language = ''
96         self.default_snippet_options = default_snippet_opts
97         self.snippet_option_separator = "\s*,\s*"
98
99     def supported_snippet_types (self):
100         # Sort according to snippet_type_order, unknown keys come last
101         keys = self.snippet_res.keys ()
102         # First the entries in snippet_type_order in that order (if present)
103         # then all entries not in snippet_type_order in given order
104         res = filter (lambda x:x in keys, snippet_type_order) + filter (lambda x:x not in snippet_type_order, keys)
105         return res
106
107     def snippet_regexp (self, snippettype):
108         return self.snippet_res.get (snippettype, None)
109
110     def can_handle_format (self, format):
111         return format == self.format
112     def can_handle_extension (self, extension):
113         return extension in self.handled_extensions
114
115     def add_options (self, option_parser):
116         pass
117
118     def process_options (self, global_options):
119         pass
120
121
122     def process_options_pdfnotdefault (self, global_options):
123         ## prevent PDF from being switched on by default.
124         global_options.process_cmd += ' --formats=eps '
125         if global_options.create_pdf:
126             global_options.process_cmd += "--pdf -dinclude-eps-fonts -dgs-load-fonts "
127             if global_options.latex_program == 'latex':
128                     global_options.latex_program = 'pdflatex'
129
130
131     def snippet_class (self, type):
132       return BookSnippet.snippet_type_to_class.get (type, BookSnippet.Snippet)
133
134     def get_document_language (self, source):
135         return ''
136
137
138     def init_default_snippet_options (self, source):
139         self.document_language = self.get_document_language (source)
140         if LINE_WIDTH not in self.default_snippet_options:
141             line_width = self.get_line_width (source)
142             if line_width:
143                 self.default_snippet_options[LINE_WIDTH] = line_width
144
145     def get_line_width (self, source):
146         return None;
147
148     def split_snippet_options (self, option_string):
149         if option_string:
150             return re.split (self.snippet_option_separator, option_string)
151         return []
152
153     def input_fullname (self, input_filename):
154         return find_file (input_filename, self.global_options.include_path,
155             self.global_options.original_dir)
156
157     def adjust_snippet_command (self, cmd):
158         return cmd
159
160     def process_chunks (self, chunks):
161         return chunks
162
163     def snippet_output (self, basename, snippet):
164         warning (_("Output function not implemented"))
165         return ''
166
167     def output_simple (self, type, snippet):
168         return self.output.get (type, '') % snippet.get_replacements ()
169
170     def output_simple_replacements (self, type, variables):
171         return self.output.get (type, '') % variables
172
173     def output_print_filename (self, basename, snippet):
174         str = ''
175         rep = snippet.get_replacements ()
176         if PRINTFILENAME in snippet.option_dict:
177             rep['base'] = basename
178             rep['filename'] = os.path.basename (snippet.filename)
179             rep['ext'] = snippet.ext
180             str = self.output[PRINTFILENAME] % rep
181
182         return str
183
184     def required_files (self, snippet, base, full, required_files):
185         return []
186
187     def required_files_png (self, snippet, base, full, required_files):
188         # UGH - junk global_options
189         res = []
190         if (base + '.eps' in required_files and not snippet.global_options.skip_png_check):
191             page_count = BookSnippet.ps_page_count (full + '.eps')
192             if page_count <= 1:
193                 res.append (base + '.png')
194             else:
195                 for page in range (1, page_count + 1):
196                     res.append (base + '-page%d.png' % page)
197         return res