#!@PYTHON@
'''
-TODO: latex/paper parameters
- [verbatim]
- \relative ly:export?
+TODO: @\ lilypondfile
+ \input, @include
+ check latex parameters
+ papersizes?
+
Example usage:
(_ ("FILTER"), 'F', 'filter', _ ("pipe snippets through FILTER [convert-ly -n -]")),
('', 'h', 'help', _ ("print this help")),
(_ ("COMMAND"), 'P', 'process', _ ("process ly_files using COMMAND FILE...")),
+ (_ ("FILE"), 'o', 'output', _ ("write output to FILE")),
('', 'V', 'verbose', _ ("be verbose")),
('', 'v', 'version', _ ("print version information")),
('', 'w', 'warranty', _ ("show warranty and copyright")),
use_hash_p = 1
format = 0
+output_name = 0
+latex_filter_cmd = 'latex "\\nonstopmode \input /dev/stdin"'
filter_cmd = 'convert-ly --no-version --from=2.0.0 -'
#filter_cmd = 0
#process_cmd = 'convert-ly --no-version --from=2.0.0'
process_cmd = 0
+default_ly_options = { }
LATEX = 'latex'
HTML = 'html'
TEXINFO = 'texinfo'
BEFORE = 'before'
+OUTPUT = 'output'
AFTER = 'after'
+VERBATIM = 'verbatim'
+LINEWIDTH = 'linewidth'
## lilypond-book heritage. to be cleaned
'include': no_match,
'input': no_match,
'header': no_match,
- 'preamble-end': no_match,
'landscape': no_match,
'verbatim': r'''(?s)(?P<code><pre>\s.*?</pre>\s)''',
'verb': r'''(?P<code><pre>.*?</pre>)''',
},
LATEX: {
- 'input': r'(?m)^[^%\n]*?(?P<match>\\mbinput{?([^}\t \n}]*))',
- 'include': r'(?m)^[^%\n]*?(?P<match>\\mbinclude{(?P<filename>[^}]+)})',
+ 'junkmeinput': r'(?m)^[^%\n]*?(?P<match>\\mbinput{?([^}\t \n}]*))',
+ 'junkmeinclude': r'(?m)^[^%\n]*?(?P<match>\\mbinclude{(?P<filename>[^}]+)})',
+ 'include': r'(?m)^[^%\n]*?(?P<match>\\input{(?P<filename>[^}]+)})',
'option-sep' : ',\s*',
'header': r"\n*\\documentclass\s*(\[.*?\])?",
- 'preamble-end': r'(?P<code>\\begin\s*{document})',
'verbatim': r"(?s)(?P<code>\\begin\s*{verbatim}.*?\\end{verbatim})",
'verb': r"(?P<code>\\verb(?P<del>.).*?(?P=del))",
'lilypond-file': r'(?m)^[^%\n]*?(?P<match>\\lilypondfile\s*(\[(?P<options>.*?)\])?\s*\{(?P<filename>.+)})',
# why do we have distinction between @mbinclude and @include?
TEXINFO: {
- 'include': '(?m)^[^%\n]*?(?P<match>@mbinclude\s+(?P<filename>\S*))',
+ 'junkmeinclude': '(?m)^[^%\n]*?(?P<match>@mbinclude\s+(?P<filename>\S*))',
+ 'include': '(?m)^[^%\n]*?(?P<match>@include\s+(?P<filename>\S*))',
'input': no_match,
'header': no_match,
- 'preamble-end': no_match,
'landscape': no_match,
'verbatim': r'''(?s)(?P<code>@example\s.*?@end example\s)''',
'verb': r'''(?P<code>@code{.*?})''',
ly_options = {
NOTES: {
- 'relative': r'''\relative #(ly:make-pitch %(relative)s 0 0)'''
+ #'xrelative': r'''\relative #(ly:make-pitch %(relative)s 0 0)''',
+ 'relative': r'''\relative c%(relative_quotes)s''',
},
PAPER: {
'indent' : r'''
\remove Time_signature_engraver
}''',
'raggedright' : r'''
+ indent = 0.0\mm
raggedright = ##t''',
},
PREAMBLE: {
},
}
+# WIP
+output = {
+ HTML : {
+ AFTER: '',
+ BEFORE: '',
+ OUTPUT: r'''<src image="%(base)s.png">''',
+ VERBATIM: r'''<pre>
+%(code)s</pre>''',
+ },
+
+ LATEX : {
+ AFTER: '',
+ BEFORE: '',
+ OUTPUT: r'''{\parindent 0pt
+\ifx\preLilyPondExample\preLilyPondExample\fi
+\def\lilypondbook{}
+\input %(base)s.tex
+\ifx\preLilyPondExample\postLilyPondExample\fi}
+''',
+ VERBATIM: r'''\begin{verbatim}
+%(code)s\end{verbatim}
+''',
+ },
+
+ TEXINFO : {
+ BEFORE: '',
+ AFTER: '',
+ },
+
+ }
PREAMBLE_LY = r'''%% Generated by %(program_name)s
%% Options: [%(option_string)s]
def compose_ly (code, option_string):
+ options = []
+ # urg
+ for i in default_ly_options.keys ():
+ options.append (i)
+ vars ()[i] = default_ly_options[i]
+
+ options = options + string.split (option_string, ',')
+
m = re.search (r'''\\score''', code)
- options = string.split (option_string, ',')
if not m and (not options \
or not 'nofragment' in options \
or 'fragment' in options):
body = FULL_LY
# defaults
- relative = "0"
- staffsize = "16"
+ relative = 0
+ staffsize = '16'
notes_options = []
paper_options = []
key = i
if key in ly_options[NOTES].keys ():
- notes_options.append (ly_options[NOTES][key] % vars ())
+ notes_options.append (ly_options[NOTES][key])
elif key in ly_options[PREAMBLE].keys ():
- preamble_options.append (ly_options[PREAMBLE][key] \
- % vars ())
+ preamble_options.append (ly_options[PREAMBLE][key])
elif key in ly_options[PAPER].keys ():
- paper_options.append (ly_options[PAPER][key] % vars ())
+ paper_options.append (ly_options[PAPER][key])
+ relative_quotes = (",,,", ",,", ",", "", "'", "''", "'''")[relative-3]
program_name = __main__.program_name
- notes_string = string.join (notes_options, '\n ')
- paper_string = string.join (paper_options, '\n ')
- preamble_string = string.join (preamble_options, '\n ')
+ notes_string = string.join (notes_options, '\n ') % vars ()
+ paper_string = string.join (paper_options, '\n ') % vars ()
+ preamble_string = string.join (preamble_options, '\n ') % vars ()
return (PREAMBLE_LY + body) % vars ()
-output = {
- HTML : {
- BEFORE: '',
- AFTER: '',
- },
-
- LATEX : {
- BEFORE: '',
- AFTER: '',
- },
-
- TEXINFO : {
- BEFORE: '',
- AFTER: '',
- },
-
- }
-
# BARF
# use lilypond-bin for latex (.lytex) books,
def output_html (self, source):
base = self.basename (source)
+ if VERBATIM in self.match.group ('options'):
+ code = self.substring (source, 'code')
+ h.write (output[HTML][VERBATIM] % vars ())
h.write (output[HTML][BEFORE])
- h.write ('<src image="%(base)s.png">' % vars ())
+ h.write (output[HTML][OUTPUT] % vars ())
h.write (output[HTML][AFTER])
def output_latex (self, source):
- h.write (output[HTML][BEFORE])
- name = self.basename (source) + '.tex'
- h.write (open (name).read ())
- h.write (output[HTML][AFTER])
+ if VERBATIM in self.match.group ('options'):
+ code = self.substring (source, 'code')
+ h.write (output[LATEX][VERBATIM] % vars ())
+ h.write (output[LATEX][BEFORE])
+ base = self.basename (source)
+ # h.write (open (base + '.tex').read ())
+ # output = open (base + '.tex').read ()
+ h.write (output[LATEX][OUTPUT] % vars ())
+ h.write (output[LATEX][AFTER])
def output_texinfo (self, source):
h.write ('\n@tex\n')
LATEX_DOCUMENT = r'''
%(preamble)s
\begin{document}
-\typeout{columnsep=\the\columnsep}
\typeout{textwidth=\the\textwidth}
+\typeout{columnsep=\the\columnsep}
+\makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
\end{document}
'''
#need anything else besides textwidth?
-def get_latex_parameters (source):
- snippet, = find_snippets (source, 'preamble-end')
- latex_cmd = 'latex "\\nonstopmode \input /dev/stdin"'
- preamble = source[:snippet.start (0)]
+def get_latex_textwidth (source):
+ m = re.search (r'''(?P<preabmle>\\begin\s*{document})''', source)
+ preamble = source[:m.start (0)]
latex_document = LATEX_DOCUMENT % vars ()
- parameter_string = filter_pipe (latex_document, latex_cmd)
+ parameter_string = filter_pipe (latex_document, latex_filter_cmd)
+
+ columns = 0
+ m = re.search ('columns=([0-9.]*)', parameter_string)
+ if m:
+ columns = string.atoi (m.group (1))
columnsep = 0
m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
m = re.search('textwidth=([0-9.]*)pt', parameter_string)
if m:
textwidth = string.atof (m.group (1))
- if columnsep:
- textwidth -= columnsep
+ if columns:
+ textwidth = (textwidth - columnsep) / columns
return textwidth
h = open (input_filename)
source = h.read ()
- #snippet_types = ('lilypond', 'lilypond-block')
snippet_types = ('verbatim', 'verb', 'multiline-comment',
'lilypond', 'lilypond-block')
snippets = []
snippets.sort (compare_index)
h = sys.stdout
+ if output_name and output_name != '-':
+ if not os.path.isdir (output_name):
+ os.mkdir (output_name, 0777)
+ if input_filename != '-':
+ h = open (output_name + '/' + input_filename + '.out',
+ 'w')
+ os.chdir (output_name)
def filter_source (snippet, source):
global index
snippet_output (snippet, source)
index = snippet.end (0)
- if format == LATEX:
- textwdith = get_latex_parameters (source)
- #TODO: set global option
+ global default_ly_options
+ if format == LATEX and LINEWIDTH not in default_ly_options.keys ():
+ textwidth = get_latex_textwidth (source)
+ default_ly_options[LINEWIDTH] = '''%.0f\pt''' % textwidth
global index
if filter_cmd:
do_snippets (source, snippets, compile_output)
h.write (source[index:])
+ # misusing snippet bit silly?
+ includes = find_snippets (source, 'include')
+ for i in includes:
+ os.chdir (original_dir)
+ sys.stderr.write ('DO: ' + sys.argv[0] + ' OPTIONS ' + i.substring (source, 'filename') + '\n')
+
def do_options ():
- global format
+ global format, output_name
global filter_cmd, process_cmd, verbose_p
(sh, long) = ly.getopt_args (option_definitions)
elif o == '--help' or o == '-h':
ly.help ()
sys.exit (0)
+ elif o == '--output' or o == '-o':
+ output_name = a
elif o == '--process' or o == '-P':
process_cmd = a
filter_cmd = 0