]> git.donarmstrong.com Git - lilypond.git/blob - python/book_html.py
Web-ja: update introduction
[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     'musicxml_file':
44          r'''(?mx)
45           (?P<match>
46           <musicxmlfile\s*(?P<options>.*?)\s*>
47           \s*(?P<filename>.*?)\s*
48           </musicxmlfile\s*>)''',
49
50     'verb':
51          r'''(?x)(?P<match>(?P<code><pre>.*?</pre>))''',
52
53     'verbatim':
54          r'''(?xs)(?P<match>(?P<code><pre>\s.*?</pre>\s))''',
55
56     'lilypondversion':
57          r'''(?mx)(?P<match><lilypondversion\s*/>)''',
58 }
59
60
61 HTML_output = {
62     FILTER: r'''<lilypond %(options)s>
63 %(code)s
64 </lilypond>
65 ''',
66
67     AFTER: r'''
68  </a>
69 </p>''',
70
71     BEFORE: r'''<p>
72  <a href="%(base)s%(ext)s">''',
73
74     OUTPUT: r'''
75   <img align="middle"
76        border="0"
77        src="%(image)s"
78        alt="%(alt)s">''',
79
80     PRINTFILENAME: '<p><tt><a href="%(base)s%(ext)s">%(filename)s</a></tt></p>',
81
82     QUOTE: r'''<blockquote>
83 %(str)s
84 </blockquote>
85 ''',
86
87     VERBATIM: r'''<pre>
88 %(verb)s</pre>''',
89
90     VERSION: r'''%(program_version)s''',
91 }
92
93
94
95
96
97
98
99 class BookHTMLOutputFormat (BookBase.BookOutputFormat):
100     def __init__ (self):
101         BookBase.BookOutputFormat.__init__ (self)
102         self.format = "html"
103         self.default_extension = ".html"
104         self.snippet_res = HTML_snippet_res
105         self.output = HTML_output
106         self.handled_extensions = ['.html', '.xml','.htmly']
107         self.snippet_option_separator = '\s*'
108
109     def split_snippet_options (self, option_string):
110         if option_string:
111             options = re.findall('[-\w\.-:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|\S+))?',
112                                  option_string)
113             options = [re.sub('^([^=]+=\s*)(?P<q>["\'])(.*)(?P=q)', '\g<1>\g<3>', opt)
114                        for opt in options]
115             return options
116         return []
117
118     def adjust_snippet_command (self, cmd):
119         if '--formats' not in cmd:
120             return cmd + ' --formats=png '
121         else:
122             return cmd
123
124     def snippet_output (self, basename, snippet):
125         str = ''
126         rep = snippet.get_replacements ();
127         rep['base'] = basename
128         rep['filename'] = os.path.basename (snippet.filename)
129         rep['ext'] = snippet.ext
130         str += self.output_print_filename (basename, snippet)
131         if VERBATIM in snippet.option_dict:
132             rep['verb'] = BookBase.verbatim_html (snippet.verb_ly ())
133             str += self.output[VERBATIM] % rep
134         if QUOTE in snippet.option_dict:
135             str = self.output[QUOTE] % {'str': str}
136
137         str += self.output[BEFORE] % rep
138         for image in snippet.get_images ():
139             rep1 = copy.copy (rep)
140             rep1['image'] = image
141             (rep1['base'], rep1['ext']) = os.path.splitext (image)
142             rep1['alt'] = snippet.option_dict[ALT]
143             str += self.output[OUTPUT] % rep1
144
145         str += self.output[AFTER] % rep
146         return str
147
148     def required_files (self, snippet, base, full, required_files):
149         return self.required_files_png (snippet, base, full, required_files)
150
151
152 BookBase.register_format (BookHTMLOutputFormat ());