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