1 # -*- coding: utf-8 -*-
3 import book_base as BookBase
5 from book_snippets import *
7 # Recognize special sequences in the input.
9 # (?P<name>regex) -- Assign result of REGEX to NAME.
10 # *? -- Match non-greedily.
11 # (?!...) -- Match if `...' doesn't match next (without consuming
14 # (?m) -- Multiline regex: Make ^ and $ match at each line.
15 # (?s) -- Make the dot match all characters including newline.
16 # (?x) -- Ignore whitespace in patterns.
18 # 'multiline_comment', 'verbatim', 'lilypond_block', 'singleline_comment',
19 # 'lilypond_file', 'include', 'lilypond', 'lilypondversion'
24 <lilypond(\s+(?P<options>.*?))?\s*:\s*(?P<code>.*?)\s*/>)''',
29 <lilypond\s*(?P<options>.*?)\s*>
36 <lilypondfile\s*(?P<options>.*?)\s*>
37 \s*(?P<filename>.*?)\s*
38 </lilypondfile\s*>)''',
41 r'''(?smx)(?P<match>\s*(?!@c\s+)(?P<code><!--\s.*?!-->)\s)''',
44 r'''(?x)(?P<match>(?P<code><pre>.*?</pre>))''',
47 r'''(?xs)(?P<match>(?P<code><pre>\s.*?</pre>\s))''',
50 r'''(?mx)(?P<match><lilypondversion\s*/>)''',
55 FILTER: r'''<lilypond %(options)s>
65 <a href="%(base)s.ly">''',
73 PRINTFILENAME: '<p><tt><a href="%(base)s.ly">%(filename)s</a></tt></p>',
75 QUOTE: r'''<blockquote>
83 VERSION: r'''%(program_version)s''',
92 class BookHTMLOutputFormat (BookBase.BookOutputFormat):
94 BookBase.BookOutputFormat.__init__ (self)
96 self.default_extension = ".html"
97 self.snippet_res = HTML_snippet_res
98 self.output = HTML_output
99 self.handled_extensions = ['.html', '.xml','.htmly']
100 self.snippet_option_separator = '\s*'
102 def split_snippet_options (self, option_string):
104 options = re.findall('[-\w\.-:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|\S+))?',
106 options = [re.sub('^([^=]+=\s*)(?P<q>["\'])(.*)(?P=q)', '\g<1>\g<3>', opt)
111 def adjust_snippet_command (self, cmd):
112 if '--formats' not in cmd:
113 return cmd + ' --formats=png '
117 def snippet_output (self, basename, snippet):
119 rep = snippet.get_replacements ();
120 rep['base'] = basename
121 str += self.output_print_filename (basename, snippet)
122 if VERBATIM in snippet.option_dict:
123 rep['verb'] = BookBase.verbatim_html (snippet.verb_ly ())
124 str += self.output[VERBATIM] % rep
125 if QUOTE in snippet.option_dict:
126 str = self.output[QUOTE] % {'str': str}
128 str += self.output[BEFORE] % rep
129 for image in snippet.get_images ():
130 rep1 = copy.copy (rep)
131 rep1['image'] = image
132 (rep1['base'], rep1['ext']) = os.path.splitext (image)
133 rep1['alt'] = snippet.option_dict[ALT]
134 str += self.output[OUTPUT] % rep1
136 str += self.output[AFTER] % rep
139 def required_files (self, snippet, base, full, required_files):
140 return self.required_files_png (snippet, base, full, required_files)
143 BookBase.register_format (BookHTMLOutputFormat ());