File: book_html.py

package info (click to toggle)
lilypond 2.19.81+really-2.18.2-13
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 68,528 kB
  • sloc: cpp: 76,619; ansic: 75,407; lisp: 56,140; xml: 31,046; python: 20,799; sh: 7,946; yacc: 3,266; perl: 3,045; asm: 1,587; lex: 1,405; makefile: 1,048; tcl: 457; awk: 199; php: 53; csh: 50
file content (152 lines) | stat: -rw-r--r-- 4,224 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -*- coding: utf-8 -*-

import book_base as BookBase
import copy
from book_snippets import *

# Recognize special sequences in the input.
#
#   (?P<name>regex) -- Assign result of REGEX to NAME.
#   *? -- Match non-greedily.
#   (?!...) -- Match if `...' doesn't match next (without consuming
#              the string).
#
#   (?m) -- Multiline regex: Make ^ and $ match at each line.
#   (?s) -- Make the dot match all characters including newline.
#   (?x) -- Ignore whitespace in patterns.
# Possible keys are:
#     'multiline_comment', 'verbatim', 'lilypond_block', 'singleline_comment',
#     'lilypond_file', 'include', 'lilypond', 'lilypondversion'
HTML_snippet_res = {
    'lilypond':
         r'''(?mx)
          (?P<match>
          <lilypond(\s+(?P<options>.*?))?\s*:\s*(?P<code>.*?)\s*/>)''',

    'lilypond_block':
         r'''(?msx)
          (?P<match>
          <lilypond\s*(?P<options>.*?)\s*>
          (?P<code>.*?)
          </lilypond\s*>)''',

    'lilypond_file':
         r'''(?mx)
          (?P<match>
          <lilypondfile\s*(?P<options>.*?)\s*>
          \s*(?P<filename>.*?)\s*
          </lilypondfile\s*>)''',

    'multiline_comment':
         r'''(?smx)(?P<match>\s*(?!@c\s+)(?P<code><!--\s.*?!-->)\s)''',

    'musicxml_file':
         r'''(?mx)
          (?P<match>
          <musicxmlfile\s*(?P<options>.*?)\s*>
          \s*(?P<filename>.*?)\s*
          </musicxmlfile\s*>)''',

    'verb':
         r'''(?x)(?P<match>(?P<code><pre>.*?</pre>))''',

    'verbatim':
         r'''(?xs)(?P<match>(?P<code><pre>\s.*?</pre>\s))''',

    'lilypondversion':
         r'''(?mx)(?P<match><lilypondversion\s*/>)''',
}


HTML_output = {
    FILTER: r'''<lilypond %(options)s>
%(code)s
</lilypond>
''',

    AFTER: r'''
 </a>
</p>''',

    BEFORE: r'''<p>
 <a href="%(base)s%(ext)s">''',

    OUTPUT: r'''
  <img align="middle"
       border="0"
       src="%(image)s"
       alt="%(alt)s">''',

    PRINTFILENAME: '<p><tt><a href="%(base)s%(ext)s">%(filename)s</a></tt></p>',

    QUOTE: r'''<blockquote>
%(str)s
</blockquote>
''',

    VERBATIM: r'''<pre>
%(verb)s</pre>''',

    VERSION: r'''%(program_version)s''',
}







class BookHTMLOutputFormat (BookBase.BookOutputFormat):
    def __init__ (self):
        BookBase.BookOutputFormat.__init__ (self)
        self.format = "html"
        self.default_extension = ".html"
        self.snippet_res = HTML_snippet_res
        self.output = HTML_output
        self.handled_extensions = ['.html', '.xml','.htmly']
        self.snippet_option_separator = '\s*'

    def split_snippet_options (self, option_string):
        if option_string:
            options = re.findall('[-\w\.-:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|\S+))?',
                                 option_string)
            options = [re.sub('^([^=]+=\s*)(?P<q>["\'])(.*)(?P=q)', '\g<1>\g<3>', opt)
                       for opt in options]
            return options
        return []

    def adjust_snippet_command (self, cmd):
        if '--formats' not in cmd:
            return cmd + ' --formats=png '
        else:
            return cmd

    def snippet_output (self, basename, snippet):
        str = ''
        rep = snippet.get_replacements ();
        rep['base'] = basename
        rep['filename'] = os.path.basename (snippet.filename)
        rep['ext'] = snippet.ext
        str += self.output_print_filename (basename, snippet)
        if VERBATIM in snippet.option_dict:
            rep['verb'] = BookBase.verbatim_html (snippet.verb_ly ())
            str += self.output[VERBATIM] % rep
        if QUOTE in snippet.option_dict:
            str = self.output[QUOTE] % {'str': str}

        str += self.output[BEFORE] % rep
        for image in snippet.get_images ():
            rep1 = copy.copy (rep)
            rep1['image'] = image
            (rep1['base'], rep1['ext']) = os.path.splitext (image)
            rep1['alt'] = snippet.option_dict[ALT]
            str += self.output[OUTPUT] % rep1

        str += self.output[AFTER] % rep
        return str

    def required_files (self, snippet, base, full, required_files):
        return self.required_files_png (snippet, base, full, required_files)


BookBase.register_format (BookHTMLOutputFormat ());