]> git.donarmstrong.com Git - lilypond.git/blob - python/book_docbook.py
Local update of LSR
[lilypond.git] / python / book_docbook.py
1 # -*- coding: utf-8 -*-
2
3 import book_base as BookBase
4 from book_snippets import *
5
6 # Recognize special sequences in the input.
7 #
8 #   (?P<name>regex) -- Assign result of REGEX to NAME.
9 #   *? -- Match non-greedily.
10 #   (?!...) -- Match if `...' doesn't match next (without consuming
11 #              the string).
12 #
13 #   (?m) -- Multiline regex: Make ^ and $ match at each line.
14 #   (?s) -- Make the dot match all characters including newline.
15 #   (?x) -- Ignore whitespace in patterns.
16 # Possible keys are:
17 #     'multiline_comment', 'verbatim', 'lilypond_block', 'singleline_comment',
18 #     'lilypond_file', 'include', 'lilypond', 'lilypondversion'
19 Docbook_snippet_res = {
20     'lilypond':
21          r'''(?smx)
22           (?P<match>
23           <(?P<inline>(inline)?)mediaobject>\s*
24           <textobject.*?>\s*
25           <programlisting\s+language="lilypond".*?(role="(?P<options>.*?)")?>
26           (?P<code>.*?)
27           </programlisting\s*>\s*
28           </textobject\s*>\s*
29           </(inline)?mediaobject>)''',
30
31     'lilypond_block':
32          r'''(?smx)
33           (?P<match>
34           <(?P<inline>(inline)?)mediaobject>\s*
35           <textobject.*?>\s*
36           <programlisting\s+language="lilypond".*?(role="(?P<options>.*?)")?>
37           (?P<code>.*?)
38           </programlisting\s*>\s*
39           </textobject\s*>\s*
40           </(inline)?mediaobject>)''',
41
42     'lilypond_file':
43          r'''(?smx)
44           (?P<match>
45           <(?P<inline>(inline)?)mediaobject>\s*
46           <imageobject.*?>\s*
47           <imagedata\s+
48            fileref="(?P<filename>.*?\.ly)"\s*
49            (role="(?P<options>.*?)")?\s*
50            (/>|>\s*</imagedata>)\s*
51           </imageobject>\s*
52           </(inline)?mediaobject>)''',
53
54     'multiline_comment':
55          r'''(?smx)
56           (?P<match>
57           \s*(?!@c\s+)
58           (?P<code><!--\s.*?!-->)
59           \s)''',
60 }
61
62
63 Docbook_output = {
64     FILTER: r'''<mediaobject>
65   <textobject>
66     <programlisting language="lilypond"
67                     role="%(options)s">
68 %(code)s
69     </programlisting>
70   </textobject>
71 </mediaobject>''',
72
73     OUTPUT: r'''<imageobject role="latex">
74   <imagedata fileref="%(base)s.pdf" format="PDF"/>
75 </imageobject>
76 <imageobject role="html">
77   <imagedata fileref="%(base)s.png" format="PNG"/>
78 </imageobject>''',
79
80     PRINTFILENAME: r'''<textobject>
81   <simpara>
82     <ulink url="%(base)s%(ext)s">
83       <filename>
84         %(filename)s
85       </filename>
86     </ulink>
87   </simpara>
88 </textobject>''',
89
90     VERBATIM: r'''<programlisting>
91 %(verb)s</programlisting>''',
92
93     VERSION: r'''%(program_version)s''',
94 }
95
96
97
98
99 class BookDocbookOutputFormat (BookBase.BookOutputFormat):
100     def __init__ (self):
101         BookBase.BookOutputFormat.__init__ (self)
102         self.format = "docbook"
103         self.default_extension = ".xml"
104         self.snippet_res = Docbook_snippet_res
105         self.output = Docbook_output
106         self.handled_extensions = ['.lyxml']
107         self.snippet_option_separator = '\s*'
108
109     def adjust_snippet_command (self, cmd):
110         if '--formats' not in cmd:
111             return cmd + ' --formats=png,pdf '
112         else:
113             return cmd
114
115     def snippet_output (self, basename, snippet):
116         str = ''
117         rep = snippet.get_replacements ();
118         for image in snippet.get_images ():
119             rep['image'] = image
120             (rep['base'], rep['ext']) = os.path.splitext (image)
121             str += self.output[OUTPUT] % rep
122             str += self.output_print_filename (basename, snippet)
123             if (snippet.substring('inline') == 'inline'):
124                 str = '<inlinemediaobject>' + str + '</inlinemediaobject>'
125             else:
126                 str = '<mediaobject>' + str + '</mediaobject>'
127         if VERBATIM in snippet.option_dict:
128                 rep['verb'] = BookBase.verbatim_html (snippet.verb_ly ())
129                 str = self.output[VERBATIM]  % rep + str
130         return str
131
132
133 BookBase.register_format (BookDocbookOutputFormat ());