X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=scripts%2Flilypond-book.py;h=e851a40662c134302f7e5bb9508e1a103fd6e83f;hb=38d7d319eabc906e82fb42002678c6d42a23b6f7;hp=be9fef694a5d81049d92799b4ecb39d18498fbc9;hpb=1f419a2af14d4a5daf9bc9eb5f7368eedb5c6021;p=lilypond.git diff --git a/scripts/lilypond-book.py b/scripts/lilypond-book.py index be9fef694a..e851a40662 100644 --- a/scripts/lilypond-book.py +++ b/scripts/lilypond-book.py @@ -15,7 +15,7 @@ classic lilypond-book: TODO: * this script is too complex. Modularize. - + * ly-options: intertext? * --line-width? * eps in latex / eps by lilypond -b ps? @@ -51,6 +51,17 @@ ly.require_python_version () program_version = '@TOPLEVEL_VERSION@' program_name = os.path.basename (sys.argv[0]) +# Check if program_version contains @ characters. This will be the case if +# the .py file is called directly while building the lilypond documentation. +# If so, try to check for the env var LILYPOND_VERSION, which is set by our +# makefiles and use its value. +at_re = re.compile (r'@') +if at_re.match (program_version): + if os.environ.has_key('LILYPOND_VERSION'): + program_version = os.environ['LILYPOND_VERSION'] + else: + program_version = "unknown" + original_dir = os.getcwd () backend = 'ps' @@ -97,12 +108,12 @@ def warranty (): ly.encoded_write (sys.stdout, ''' %s -%s + %s %s %s -''' % ( _ ('Copyright (c) %s by') % '2001--2007', - ' '.join (authors), +''' % ( _ ('Copyright (c) %s by') % '2001--2009', + '\n '.join (authors), _ ("Distributed under terms of the GNU General Public License."), _ ("It comes with NO WARRANTY."))) @@ -144,18 +155,18 @@ def get_option_parser (): action='store', dest='latex_program', default='latex') - p.add_option ('--left-padding', + p.add_option ('--left-padding', metavar=_ ("PAD"), dest="padding_mm", help=_ ("pad left side of music to align music inspite of uneven bar numbers (in mm)"), type="float", default=3.0) - + p.add_option ("-o", '--output', help=_ ("write output to DIR"), metavar=_ ("DIR"), action='store', dest='output_dir', default='') - + p.add_option ('--skip-lily-check', help=_ ("do not fail if no lilypond output is found"), metavar=_ ("DIR"), @@ -167,16 +178,16 @@ def get_option_parser (): metavar=_ ("DIR"), action='store_true', dest='skip_png_check', default=False) - + p.add_option ('--lily-output-dir', help=_ ("write lily-XXX files to DIR, link into --output dir"), metavar=_ ("DIR"), action='store', dest='lily_output_dir', default=None) - + p.add_option ('-P', '--process', metavar=_ ("COMMAND"), help = _ ("process ly_files using COMMAND FILE..."), - action='store', + action='store', dest='process_cmd', default='') p.add_option ('--pdf', @@ -200,8 +211,9 @@ def get_option_parser (): action='store_true') p.add_option_group ('', description=( - _ ("Report bugs via") - + ' http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs\n')) + _ ("Report bugs via %s") + % ' http://post.gmane.org/post.php' + '?group=gmane.comp.gnu.lilypond.bugs') + '\n') return p lilypond_binary = os.path.join ('@bindir@', 'lilypond') @@ -241,6 +253,7 @@ LAYOUT = 'layout' LINE_WIDTH = 'line-width' LILYQUOTE = 'lilyquote' NOFRAGMENT = 'nofragment' +NOGETTEXT = 'nogettext' NOINDENT = 'noindent' NOQUOTE = 'noquote' NORAGGED_RIGHT = 'noragged-right' @@ -248,7 +261,6 @@ NOTES = 'body' NOTIME = 'notime' OUTPUT = 'output' OUTPUTIMAGE = 'outputimage' -PACKED = 'packed' PAPER = 'paper' PREAMBLE = 'preamble' PRINTFILENAME = 'printfilename' @@ -260,6 +272,7 @@ DOCTITLE = 'doctitle' TEXIDOC = 'texidoc' TEXINFO = 'texinfo' VERBATIM = 'verbatim' +VERSION = 'lilypondversion' FONTLOAD = 'fontload' FILENAME = 'filename' ALT = 'alt' @@ -277,6 +290,9 @@ no_options = { # # (?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. @@ -290,17 +306,36 @@ snippet_res = { 'lilypond': r'''(?smx) (?P - <(?P(inline)?)mediaobject>\s*\s*.*?)")?>(?P.*?)\s*\s*)''', + <(?P(inline)?)mediaobject>\s* + \s* + .*?)")?> + (?P.*?) + \s* + \s* + )''', 'lilypond_block': r'''(?smx) (?P - <(?P(inline)?)mediaobject>\s*\s*.*?)")?>(?P.*?)\s*\s*)''', + <(?P(inline)?)mediaobject>\s* + \s* + .*?)")?> + (?P.*?) + \s* + \s* + )''', 'lilypond_file': r'''(?smx) (?P - <(?P(inline)?)mediaobject>\s*\s*.*?\.ly)"\s*(role="(?P.*?)")?\s*(/>|>\s*)\s*\s*)''', + <(?P(inline)?)mediaobject>\s* + \s* + |>\s*)\s* + \s* + )''', 'multiline_comment': r'''(?smx) @@ -316,9 +351,11 @@ snippet_res = { no_match, 'verbatim': - no_match, - - }, + no_match, + + 'lilypondversion': + no_match, + }, ## HTML: { 'include': @@ -370,6 +407,11 @@ snippet_res = { (?s) (?P (?P
\s.*?
\s))''', + + 'lilypondversion': + r'''(?mx) + (?P + )''', }, ## @@ -443,6 +485,12 @@ snippet_res = { \\begin\s*{verbatim} .*? \\end\s*{verbatim}))''', + + 'lilypondversion': + r'''(?smx) + (?P + \\lilypondversion)[^a-zA-Z]''', + }, ## @@ -509,17 +557,21 @@ snippet_res = { @example \s.*? @end\s+example\s))''', - }, -} + 'lilypondversion': + r'''(?mx) + [^@](?P + @lilypondversion)[^a-zA-Z]''', + }, +} format_res = { - DOCBOOK: { - 'intertext': r',?\s*intertext=\".*?\"', + DOCBOOK: { + 'intertext': r',?\s*intertext=\".*?\"', 'option_sep': '\s*', - }, + }, HTML: { 'intertext': r',?\s*intertext=\".*?\"', 'option_sep': '\s*', @@ -536,11 +588,13 @@ format_res = { }, } + # Options without a pattern in ly_options. simple_options = [ EXAMPLEINDENT, FRAGMENT, NOFRAGMENT, + NOGETTEXT, NOINDENT, PRINTFILENAME, DOCTITLE, @@ -572,8 +626,6 @@ ly_options = { RAGGED_RIGHT: r'''ragged-right = ##t''', NORAGGED_RIGHT: r'''ragged-right = ##f''', - - PACKED: r'''packed = ##t''', }, ## @@ -597,19 +649,37 @@ ly_options = { output = { ## - DOCBOOK: { - FILTER: r'''%(code)s''', - - OUTPUT: r''' - - - - - ''', - - VERBATIM: r'''%(verb)s''', - - PRINTFILENAME: '%(filename)s' + DOCBOOK: { + FILTER: r''' + + +%(code)s + + +''', + + OUTPUT: r''' + + + + +''', + + VERBATIM: r''' +%(verb)s''', + + VERSION: program_version, + + PRINTFILENAME: r''' + + + + %(filename)s + + + +''' }, ## HTML: { @@ -626,8 +696,10 @@ output = { ''', OUTPUT: r''' - %(alt)s''', + %(alt)s''', PRINTFILENAME: '

%(filename)s

', @@ -638,34 +710,38 @@ output = { VERBATIM: r'''
 %(verb)s
''', + + VERSION: program_version, }, ## LATEX: { OUTPUT: r'''{%% -\parindent 0pt%% -\ifx\preLilyPondExample \undefined%% - \relax%% -\else%% - \preLilyPondExample%% -\fi%% +\parindent 0pt +\ifx\preLilyPondExample \undefined +\else + \expandafter\preLilyPondExample +\fi \def\lilypondbook{}%% -\input %(base)s-systems.tex%% -\ifx\postLilyPondExample \undefined%% - \relax%% -\else%% - \postLilyPondExample%% -\fi%% +\input %(base)s-systems.tex +\ifx\postLilyPondExample \undefined +\else + \expandafter\postLilyPondExample +\fi }''', PRINTFILENAME: '''\\texttt{%(filename)s} - ''', +''', - QUOTE: r'''\begin{quotation}%(str)s + QUOTE: r'''\begin{quotation} +%(str)s \end{quotation}''', VERBATIM: r'''\noindent -\begin{verbatim}%(verb)s\end{verbatim}''', +\begin{verbatim}%(verb)s\end{verbatim} +''', + + VERSION: program_version, FILTER: r'''\begin{lilypond}[%(options)s] %(code)s @@ -692,7 +768,9 @@ output = {

%(alt)s + border="0" + src="%(image)s" + alt="%(alt)s">

@end html @@ -721,6 +799,8 @@ output = { %(verb)s@end verbatim ''', + VERSION: program_version, + ADDVERSION: r'''@example \version @w{"@version{}"} @end example @@ -745,7 +825,7 @@ PREAMBLE_LY = '''%%%% Generated by %(program_name)s %% **************************************************************** -%% Start cut-&-pastable-section +%% Start cut-&-pastable-section %% **************************************************************** %(preamble_string)s @@ -827,7 +907,7 @@ def find_file (name, raise_error=True): full = os.path.join (i, name) if os.path.exists (full): return full - + if raise_error: error (_ ("file not found: %s") % name + '\n') exit (1) @@ -840,6 +920,8 @@ def verbatim_html (s): ly_var_def_re = re.compile (r'^([a-zA-Z]+)[\t ]*=', re.M) ly_comment_re = re.compile (r'(%+[\t ]*)(.*)$', re.M) +ly_context_id_re = re.compile ('\\\\(?:new|context)\\s+(?:[a-zA-Z]*?(?:Staff\ +(?:Group)?|Voice|FiguredBass|FretBoards|Names|Devnull))\\s+=\\s+"?([a-zA-Z]+)"?\\s+') def ly_comment_gettext (t, m): return m.group (1) + t (m.group (2)) @@ -853,11 +935,16 @@ def verb_ly_gettext (s): return s s = ly_comment_re.sub (lambda m: ly_comment_gettext (t, m), s) - - for v in ly_var_def_re.findall (s): - s = re.sub (r"(?m)(^|[' \\#])%s([^a-zA-Z])" % v, - "\\1" + t (v) + "\\2", - s) + + if langdefs.LANGDICT[document_language].enable_ly_identifier_l10n: + for v in ly_var_def_re.findall (s): + s = re.sub (r"(?m)(^|[' \\#])%s([^a-zA-Z])" % v, + "\\1" + t (v) + "\\2", + s) + for id in ly_context_id_re.findall (s): + s = re.sub (r'(\s+|")%s(\s+|")' % id, + "\\1" + t (id) + "\\2", + s) return s texinfo_lang_re = re.compile ('(?m)^@documentlanguage (.*?)( |$)') @@ -895,7 +982,7 @@ class Chunk: def is_plain (self): return False - + class Substring (Chunk): """A string that does not require extra memory.""" def __init__ (self, source, start, end, line_number): @@ -904,7 +991,7 @@ class Substring (Chunk): self.end = end self.line_number = line_number self.override_text = None - + def is_plain (self): return True @@ -950,7 +1037,10 @@ class LilypondSnippet (Snippet): self.do_options (os, self.type) def verb_ly (self): - return verb_ly_gettext (self.substring ('code')) + if NOGETTEXT in self.option_dict: + return self.substring ('code') + else: + return verb_ly_gettext (self.substring ('code')) def ly (self): contents = self.substring ('code') @@ -1149,7 +1239,7 @@ class LilypondSnippet (Snippet): ## let's not create too long names. self.checksum = hash.hexdigest ()[:10] - + return self.checksum def basename (self): @@ -1168,7 +1258,8 @@ class LilypondSnippet (Snippet): file (path + '.txt', 'w').write ('image of music') def relevant_contents (self, ly): - return re.sub (r'\\(version|sourcefileline|sourcefilename)[^\n]*\n', '', ly) + return re.sub (r'\\(version|sourcefileline|sourcefilename)[^\n]*\n|' + + NOGETTEXT + '[,\]]', '', ly) def link_all_output_files (self, output_dir, output_dir_files, destination): existing, missing = self.all_output_files (output_dir, output_dir_files) @@ -1188,7 +1279,7 @@ class LilypondSnippet (Snippet): os.makedirs (dst_path) os.link (src, dst) - + def all_output_files (self, output_dir, output_dir_files): """Return all files generated in lily_output_dir, a set. @@ -1201,7 +1292,7 @@ class LilypondSnippet (Snippet): def consider_file (name): if name in output_dir_files: result.add (name) - + def require_file (name): if name in output_dir_files: result.add (name) @@ -1256,14 +1347,14 @@ class LilypondSnippet (Snippet): # markups do not output a signature. if 'ddump-signature' in global_options.process_cmd: consider_file (systemfile + '.signature') - - + + return (result, missing) - + def is_outdated (self, output_dir, current_files): found, missing = self.all_output_files (output_dir, current_files) return missing - + def filter_text (self): """Run snippet bodies through a command (say: convert-ly). @@ -1288,14 +1379,14 @@ class LilypondSnippet (Snippet): single = '%(base)s.png' % vars () multiple = '%(base)s-page1.png' % vars () images = (single,) - if (os.path.exists (multiple) + if (os.path.exists (multiple) and (not os.path.exists (single) or (os.stat (multiple)[stat.ST_MTIME] > os.stat (single)[stat.ST_MTIME]))): count = ps_page_count ('%(base)s.eps' % vars ()) images = ['%s-page%d.png' % (base, page) for page in range (1, count+1)] images = tuple (images) - + return images def output_docbook (self): @@ -1304,8 +1395,8 @@ class LilypondSnippet (Snippet): for image in self.get_images (): (base, ext) = os.path.splitext (image) str += output[DOCBOOK][OUTPUT] % vars () - str += self.output_print_filename (DOCBOOK) - if (self.substring('inline') == 'inline'): + str += self.output_print_filename (DOCBOOK) + if (self.substring('inline') == 'inline'): str = '' + str + '' else: str = '' + str + '' @@ -1313,7 +1404,7 @@ class LilypondSnippet (Snippet): verb = verbatim_html (self.verb_ly ()) str = output[DOCBOOK][VERBATIM] % vars () + str return str - + def output_html (self): str = '' base = self.basename () @@ -1364,7 +1455,7 @@ class LilypondSnippet (Snippet): if 0: breaks = self.ly ().count ("\n") str += "".ljust (breaks, "\n").replace ("\n","%\n") - + if QUOTE in self.option_dict: str = output[LATEX][QUOTE] % vars () return str @@ -1385,9 +1476,9 @@ class LilypondSnippet (Snippet): doctitle = base + '.doctitle' translated_doctitle = doctitle + document_language if os.path.exists (translated_doctitle): - str += '@lydoctitle %s\n' % open (translated_doctitle).read () + str += '@lydoctitle %s\n\n' % open (translated_doctitle).read () elif os.path.exists (doctitle): - str += '@lydoctitle %s\n' % open (doctitle).read () + str += '@lydoctitle %s\n\n' % open (doctitle).read () if TEXIDOC in self.option_dict: texidoc = base + '.texidoc' translated_texidoc = texidoc + document_language @@ -1440,11 +1531,21 @@ class LilypondFileSnippet (LilypondSnippet): % (name, self.contents)) +class LilyPondVersionString (Snippet): + """A string that does not require extra memory.""" + def __init__ (self, type, match, format, line_number): + Snippet.__init__ (self, type, match, format, line_number) + + def replacement_text (self): + return output[self.format][self.type] + + snippet_type_to_class = { 'lilypond_file': LilypondFileSnippet, 'lilypond_block': LilypondSnippet, 'lilypond': LilypondSnippet, 'include': IncludeSnippet, + 'lilypondversion': LilyPondVersionString, } def find_linestarts (s): @@ -1487,7 +1588,7 @@ def find_toplevel_snippets (input_string, format, types): for type in types: if not found[type] or found[type][0] < index: found[type] = None - + m = res[type].search (input_string[index:endex]) if not m: continue @@ -1506,8 +1607,8 @@ def find_toplevel_snippets (input_string, format, types): found[type] = (start, snip) - if (found[type] - and (not first + if (found[type] + and (not first or found[type][0] < found[first][0])): first = type @@ -1540,7 +1641,7 @@ def find_toplevel_snippets (input_string, format, types): def filter_pipe (input, cmd): """Pass input through cmd, and return the result.""" - + if global_options.verbose: progress (_ ("Opening filter `%s'") % cmd) @@ -1575,13 +1676,13 @@ def system_in_directory (cmd, directory): Because of win32 compatibility, we can't simply use subprocess. """ - + current = os.getcwd() os.chdir (directory) - ly.system(cmd, be_verbose=global_options.verbose, + ly.system(cmd, be_verbose=global_options.verbose, progress_p=1) os.chdir (current) - + def process_snippets (cmd, snippets, format, lily_output_dir): @@ -1589,14 +1690,14 @@ def process_snippets (cmd, snippets, if not snippets: return - + if format in (HTML, TEXINFO) and '--formats' not in cmd: cmd += ' --formats=png ' elif format in (DOCBOOK) and '--formats' not in cmd: cmd += ' --formats=png,pdf ' checksum = snippet_list_checksum (snippets) - contents = '\n'.join (['snippet-map-%d.ly' % checksum] + contents = '\n'.join (['snippet-map-%d.ly' % checksum] + [snip.basename() + '.ly' for snip in snippets]) name = os.path.join (lily_output_dir, 'snippet-names-%d.ly' % checksum) @@ -1604,8 +1705,8 @@ def process_snippets (cmd, snippets, system_in_directory (' '.join ([cmd, ly.mkarg (name)]), lily_output_dir) - - + + ### # Retrieve dimensions from LaTeX LATEX_INSPECTION_DOCUMENT = r''' @@ -1623,13 +1724,13 @@ def get_latex_textwidth (source): m = re.search (r'''(?P\\begin\s*{document})''', source) if m == None: warning (_ ("cannot find \\begin{document} in LaTeX document")) - + ## what's a sensible default? return 550.0 - + preamble = source[:m.start (0)] latex_document = LATEX_INSPECTION_DOCUMENT % vars () - + (handle, tmpfile) = tempfile.mkstemp('.tex') logfile = os.path.splitext (tmpfile)[0] + '.log' logfile = os.path.split (logfile)[1] @@ -1637,11 +1738,11 @@ def get_latex_textwidth (source): tmp_handle = os.fdopen (handle,'w') tmp_handle.write (latex_document) tmp_handle.close () - + ly.system ('%s %s' % (global_options.latex_program, tmpfile), be_verbose=global_options.verbose) parameter_string = file (logfile).read() - + os.unlink (tmpfile) os.unlink (logfile) @@ -1672,7 +1773,7 @@ def modify_preamble (chunk): r"\\usepackage{graphics}" + '\n' + r"\\begin{document}", str) - chunk.override_text = str + chunk.override_text = str format2ext = { @@ -1720,8 +1821,8 @@ def do_process_cmd (chunks, input_name, options): output_files = split_output_files (options.lily_output_dir) outdated = [c for c in snippets if c.is_outdated (options.lily_output_dir, output_files)] - - write_file_map (outdated, input_name) + + write_file_map (outdated, input_name) progress (_ ("Writing snippets...")) for snippet in outdated: snippet.write_ly() @@ -1840,15 +1941,15 @@ def do_file (input_filename, included=False): global_options.output_dir = os.getcwd() else: global_options.output_dir = os.path.abspath(global_options.output_dir) - + if not os.path.isdir (global_options.output_dir): os.mkdir (global_options.output_dir, 0777) os.chdir (global_options.output_dir) output_filename = os.path.join(global_options.output_dir, input_base + format2ext[global_options.format]) - if (os.path.exists (input_filename) - and os.path.exists (output_filename) + if (os.path.exists (input_filename) + and os.path.exists (output_filename) and samefile (output_filename, input_fullname)): error ( _ ("Output would overwrite input file; use --output.")) @@ -1873,6 +1974,7 @@ def do_file (input_filename, included=False): 'lilypond_file', 'include', 'lilypond', + 'lilypondversion', ) progress (_ ("Dissecting...")) chunks = find_toplevel_snippets (source, global_options.format, snippet_types) @@ -1895,7 +1997,7 @@ def do_file (input_filename, included=False): write_if_updated (output_filename, [s.replacement_text () for s in chunks]) - + def process_include (snippet): os.chdir (original_dir) name = snippet.substring ('filename') @@ -1908,7 +2010,7 @@ def do_file (input_filename, included=False): chunks)) return chunks + reduce (lambda x, y: x + y, include_chunks, []) - + except CompileError: os.chdir (original_dir) progress (_ ("Removing `%s'") % output_filename) @@ -1924,14 +2026,14 @@ def do_options (): global_options.format = TEXINFO global_options.include_path = map (os.path.abspath, global_options.include_path) - + if global_options.warranty: warranty () exit (0) if not args or len (args) > 1: opt_parser.print_help () exit (2) - + return args def main (): @@ -1940,7 +2042,7 @@ def main (): basename = os.path.splitext (files[0])[0] basename = os.path.split (basename)[1] - + if not global_options.format: global_options.format = guess_format (files[0]) @@ -1949,7 +2051,7 @@ def main (): formats += ',png' if global_options.process_cmd == '': - global_options.process_cmd = (lilypond_binary + global_options.process_cmd = (lilypond_binary + ' --formats=%s -dbackend=eps ' % formats) if global_options.process_cmd: @@ -1966,13 +2068,13 @@ def main (): global_options.process_cmd += ' --formats=eps ' if global_options.create_pdf: global_options.process_cmd += "--pdf -dinclude-eps-fonts -dgs-load-fonts " - + if global_options.verbose: global_options.process_cmd += " --verbose " if global_options.padding_mm: global_options.process_cmd += " -deps-box-padding=%f " % global_options.padding_mm - + global_options.process_cmd += " -dread-file-list -dno-strip-output-dir" if global_options.lily_output_dir: @@ -1981,7 +2083,7 @@ def main (): os.makedirs (global_options.lily_output_dir) else: global_options.lily_output_dir = os.path.abspath(global_options.output_dir) - + identify () try: @@ -1997,7 +2099,7 @@ def main (): final_output_file = os.path.join (global_options.output_dir, base_file_name + '.%s' % global_options.format) - + os.chdir (original_dir) file (dep_file, 'w').write ('%s: %s' % (final_output_file, ' '.join (inputs)))