]> git.donarmstrong.com Git - lilypond.git/blob - python/book_html.py
Imported Upstream version 2.14.2
[lilypond.git] / python / book_html.py
1 # -*- coding: utf-8 -*-
2
3 import book_base as BookBase
4 import copy
5 from book_snippets import *
6
7 # Recognize special sequences in the input.
8 #
9 #   (?P<name>regex) -- Assign result of REGEX to NAME.
10 #   *? -- Match non-greedily.
11 #   (?!...) -- Match if `...' doesn't match next (without consuming
12 #              the string).
13 #
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.
17 # Possible keys are:
18 #     'multiline_comment', 'verbatim', 'lilypond_block', 'singleline_comment',
19 #     'lilypond_file', 'include', 'lilypond', 'lilypondversion'
20 HTML_snippet_res = {
21     'lilypond':
22          r'''(?mx)
23           (?P<match>
24           <lilypond(\s+(?P<options>.*?))?\s*:\s*(?P<code>.*?)\s*/>)''',
25
26     'lilypond_block':
27          r'''(?msx)
28           (?P<match>
29           <lilypond\s*(?P<options>.*?)\s*>
30           (?P<code>.*?)
31           </lilypond\s*>)''',
32
33     'lilypond_file':
34          r'''(?mx)
35           (?P<match>
36           <lilypondfile\s*(?P<options>.*?)\s*>
37           \s*(?P<filename>.*?)\s*
38           </lilypondfile\s*>)''',
39
40     'multiline_comment':
41          r'''(?smx)(?P<match>\s*(?!@c\s+)(?P<code><!--\s.*?!-->)\s)''',
42
43     'verb':
44          r'''(?x)(?P<match>(?P<code><pre>.*?</pre>))''',
45
46     'verbatim':
47          r'''(?xs)(?P<match>(?P<code><pre>\s.*?</pre>\s))''',
48
49     'lilypondversion':
50          r'''(?mx)(?P<match><lilypondversion\s*/>)''',
51 }
52
53
54 HTML_output = {
55     FILTER: r'''<lilypond %(options)s>
56 %(code)s
57 </lilypond>
58 ''',
59
60     AFTER: r'''
61  </a>
62 </p>''',
63
64     BEFORE: r'''<p>
65  <a href="%(base)s.ly">''',
66
67     OUTPUT: r'''
68   <img align="middle"
69        border="0"
70        src="%(image)s"
71        alt="%(alt)s">''',
72
73     PRINTFILENAME: '<p><tt><a href="%(base)s.ly">%(filename)s</a></tt></p>',
74
75     QUOTE: r'''<blockquote>
76 %(str)s
77 </blockquote>
78 ''',
79
80     VERBATIM: r'''<pre>
81 %(verb)s</pre>''',
82
83     VERSION: r'''%(program_version)s''',
84 }
85
86
87
88
89
90
91
92 class BookHTMLOutputFormat (BookBase.BookOutputFormat):
93     def __init__ (self):
94         BookBase.BookOutputFormat.__init__ (self)
95         self.format = "html"
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*'
101
102     def split_snippet_options (self, option_string):
103         if option_string:
104             options = re.findall('[-\w\.-:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|\S+))?',
105                                  option_string)
106             options = [re.sub('^([^=]+=\s*)(?P<q>["\'])(.*)(?P=q)', '\g<1>\g<3>', opt)
107                        for opt in options]
108             return options
109         return []
110
111     def adjust_snippet_command (self, cmd):
112         if '--formats' not in cmd:
113             return cmd + ' --formats=png '
114         else:
115             return cmd
116
117     def snippet_output (self, basename, snippet):
118         str = ''
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}
127
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
135
136         str += self.output[AFTER] % rep
137         return str
138
139     def required_files (self, snippet, base, full, required_files):
140         return self.required_files_png (snippet, base, full, required_files)
141
142
143 BookBase.register_format (BookHTMLOutputFormat ());