1 # -*- coding: utf-8 -*-
4 import book_snippets
as BookSnippet
5 from book_snippets
import *
13 ########################################################################
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
):
24 error (_ ("file not found: %s") % name
+ '\n')
28 def verbatim_html (s
):
29 return re
.sub ('>', '>',
31 re
.sub ('&', '&', s
)))
34 ########################################################################
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 ########################################################################
49 ########################################################################
52 def register_format (fmt
):
53 all_formats
.append (fmt
)
57 ########################################################################
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
= [
80 ########################################################################
81 # Base class for all output formats
82 ########################################################################
84 class BookOutputFormat
:
87 self
.default_extension
= None
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
)
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
):
116 def process_options (self
, global_options
):
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
):
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
)
141 self
.default_snippet_options
[LINE_WIDTH
] = line_width
143 def get_line_width (self
, source
):
146 def split_snippet_options (self
, option_string
):
148 return re
.split (self
.snippet_option_separator
, option_string
)
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
):
157 def process_chunks (self
, chunks
):
160 def snippet_output (self
, basename
, snippet
):
161 warning (_("Output function not implemented"))
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
):
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
180 def required_files (self
, snippet
, base
, full
, required_files
):
183 def required_files_png (self
, snippet
, base
, full
, required_files
):
184 # UGH - junk global_options
186 if (base
+ '.eps' in required_files
and not snippet
.global_options
.skip_png_check
):
187 page_count
= BookSnippet
.ps_page_count (full
+ '.eps')
189 res
.append (base
+ '.png')
191 for page
in range (1, page_count
+ 1):
192 res
.append (base
+ '-page%d.png' % page
)