Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / python / book_base.py
blobb73f37336e4f3975dce9ffd7c7a47de6bcd33b8f
1 # -*- coding: utf-8 -*-
3 import lilylib as ly
4 import book_snippets as BookSnippet
5 from book_snippets import *
6 import re
7 global _;_=ly._
9 progress = ly.progress
10 warning = ly.warning
11 error = ly.error
13 ########################################################################
14 # Helper functions
15 ########################################################################
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
23 if raise_error:
24 error (_ ("file not found: %s") % name + '\n')
25 exit (1)
26 return ''
28 def verbatim_html (s):
29 return re.sub ('>', '>',
30 re.sub ('<', '&lt;',
31 re.sub ('&', '&amp;', s)))
34 ########################################################################
35 # Option handling
36 ########################################################################
38 #TODO: Definitions just once in all files!!!
39 LINE_WIDTH = 'line-width'
41 # TODO: Implement the intertext snippet option:
42 # 'intertext': r',?\s*intertext=\".*?\"',
44 default_snippet_opts = { 'alt': "[image of music]" }
47 ########################################################################
48 # format handling
49 ########################################################################
51 all_formats = []
52 def register_format (fmt):
53 all_formats.append (fmt)
57 ########################################################################
58 # Snippet handling
59 ########################################################################
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.
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',
80 ########################################################################
81 # Base class for all output formats
82 ########################################################################
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*"
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
105 def snippet_regexp (self, snippettype):
106 return self.snippet_res.get (snippettype, None)
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
113 def add_options (self, option_parser):
114 pass
116 def process_options (self, global_options):
117 pass
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'
129 def snippet_class (self, type):
130 return BookSnippet.snippet_type_to_class.get (type, BookSnippet.Snippet)
132 def get_document_language (self, source):
133 return ''
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
143 def get_line_width (self, source):
144 return None;
146 def split_snippet_options (self, option_string):
147 if option_string:
148 return re.split (self.snippet_option_separator, option_string)
149 return []
151 def input_fullname (self, input_filename):
152 return find_file (input_filename, self.global_options.include_path)
154 def adjust_snippet_command (self, cmd):
155 return cmd
157 def process_chunks (self, chunks):
158 return chunks
160 def snippet_output (self, basename, snippet):
161 warning (_("Output function not implemented"))
162 return ''
164 def output_simple (self, type, snippet):
165 return self.output.get (type, '') % snippet.get_replacements ()
167 def output_simple_replacements (self, type, variables):
168 return self.output.get (type, '') % variables
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.substring ('filename'))
176 str = self.output[PRINTFILENAME] % rep
178 return str
180 def required_files (self, snippet, base, full, required_files):
181 return []
183 def required_files_png (self, snippet, base, full, required_files):
184 # UGH - junk global_options
185 res = []
186 if (base + '.eps' in required_files and not snippet.global_options.skip_png_check):
187 page_count = BookSnippet.ps_page_count (full + '.eps')
188 if page_count <= 1:
189 res.append (base + '.png')
190 else:
191 for page in range (1, page_count + 1):
192 res.append (base + '-page%d.png' % page)
193 return res