1 # -*- coding: utf-8 -*-
4 import book_snippets as BookSnippet
5 from book_snippets import *
13 ########################################################################
15 ########################################################################
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):
26 error (_ ("file not found: %s") % name + '\n')
30 def verbatim_html (s):
31 return re.sub ('>', '>',
33 re.sub ('&', '&', s)))
36 ########################################################################
38 ########################################################################
40 #TODO: Definitions just once in all files!!!
41 LINE_WIDTH = 'line-width'
43 # TODO: Implement the intertext snippet option:
44 # 'intertext': r',?\s*intertext=\".*?\"',
46 default_snippet_opts = { 'alt': "[image of music]" }
49 ########################################################################
51 ########################################################################
54 def register_format (fmt):
55 all_formats.append (fmt)
59 ########################################################################
61 ########################################################################
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.
68 snippet_type_order = [
82 ########################################################################
83 # Base class for all output formats
84 ########################################################################
86 class BookOutputFormat:
89 self.default_extension = None
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*"
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)
107 def snippet_regexp (self, snippettype):
108 return self.snippet_res.get (snippettype, None)
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
115 def add_options (self, option_parser):
118 def process_options (self, global_options):
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'
131 def snippet_class (self, type):
132 return BookSnippet.snippet_type_to_class.get (type, BookSnippet.Snippet)
134 def get_document_language (self, source):
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)
143 self.default_snippet_options[LINE_WIDTH] = line_width
145 def get_line_width (self, source):
148 def split_snippet_options (self, option_string):
150 return re.split (self.snippet_option_separator, option_string)
153 def input_fullname (self, input_filename):
154 return find_file (input_filename, self.global_options.include_path,
155 self.global_options.original_dir)
157 def adjust_snippet_command (self, cmd):
160 def process_chunks (self, chunks):
163 def snippet_output (self, basename, snippet):
164 warning (_("Output function not implemented"))
167 def output_simple (self, type, snippet):
168 return self.output.get (type, '') % snippet.get_replacements ()
170 def output_simple_replacements (self, type, variables):
171 return self.output.get (type, '') % variables
173 def output_print_filename (self, basename, snippet):
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
184 def required_files (self, snippet, base, full, required_files):
187 def required_files_png (self, snippet, base, full, required_files):
188 # UGH - junk global_options
190 if (base + '.eps' in required_files and not snippet.global_options.skip_png_check):
191 page_count = BookSnippet.ps_page_count (full + '.eps')
193 res.append (base + '.png')
195 for page in range (1, page_count + 1):
196 res.append (base + '-page%d.png' % page)