# -*- coding: utf-8 -*- # book_docbook.py # # This file is part of LilyPond, the GNU music typesetter. # # Copyright (C) 2010 Reinhold Kainhofer , # 2020--2020 Han-Wen Nienhuys # # LilyPond is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # LilyPond is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with LilyPond. If not, see . import os import book_base import book_snippets # Recognize special sequences in the input. # # (?Pregex) -- 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. # See book_base.BookOutputFormat for possible keys Docbook_snippet_res = { 'lilypond': r'''(?smx) (?P <(?P(inline)?)mediaobject>\s* \s* .*?)")?> (?P.*?) \s* \s* )''', 'lilypond_block': r'''(?smx) (?P <(?P(inline)?)mediaobject>\s* \s* .*?)")?> (?P.*?) \s* \s* )''', 'lilypond_file': r'''(?smx) (?P <(?P(inline)?)mediaobject>\s* \s* |>\s*)\s* \s* )''', 'multiline_comment': r'''(?smx) (?P \s*(?!@c\s+) (?P) \s)''', } Docbook_output = { book_snippets.FILTER: r''' %(code)s ''', book_snippets.OUTPUT: r''' ''', book_snippets.PRINTFILENAME: r''' %(filename)s ''', book_snippets.VERBATIM: r''' %(verb)s''', book_snippets.VERSION: r'''%(program_version)s''', } class BookDocbookOutputFormat (book_base.BookOutputFormat): def __init__(self): book_base.BookOutputFormat.__init__(self) self.format = "docbook" self.default_extension = ".xml" self.snippet_res = Docbook_snippet_res self.output = Docbook_output self.handled_extensions = ['.lyxml'] self.snippet_option_separator = r'\s+' def adjust_snippet_command(self, cmd): if '--formats' not in cmd: return cmd + ' --formats=png,pdf ' else: return cmd def snippet_output(self, basename, snippet): s = '' rep = snippet.get_replacements() for image in snippet.get_images(): rep['image'] = image (rep['base'], rep['ext']) = os.path.splitext(image) s += self.output[book_snippets.OUTPUT] % rep s += self.output_print_filename(basename, snippet) if snippet.substring('inline') == 'inline': s = '' + s + '' else: s = '' + s + '' if book_snippets.VERBATIM in snippet.option_dict: rep['verb'] = book_base.verbatim_html(snippet.verb_ly()) s = self.output[book_snippets.VERBATIM] % rep + s return s book_base.register_format(BookDocbookOutputFormat())