-\documentclass[a4paper, 12pt, oneside]{article}
+\documentclass[a4paper, 12pt]{article}
\usepackage{t1enc,isolatin1}
\usepackage{graphics}
+%\usepackage[dvips]{graphicx}
+\usepackage[rflt]{floatflt}
%\usepackage{showframe}
-\sloppy
-\def\postMudelaExample{}
+%\sloppy
+\def\postMudelaExample{\setlength{\parindent}{1em}}
+\title{Mudela-book tutorial}
\begin{document}
+\maketitle
%\printparam
-Mudela-book is a script that process your \LaTeX ~file and
-with great help from GNU LilyPond
-translates blocks of mudela code it finds inside \verb|mudela|
-environment to tex or eps graphics. It then creates a new
-file that can be sent through \LaTeX~ to create a document with
-lines of music integrated with text.
+Mudela-book is a script that process your \LaTeX~file and with great
+help from GNU LilyPond it translates blocks of mudela code it finds
+inside \verb|mudela| environments to tex or eps graphics. It then
+creates a new file that can be sendt through \LaTeX~to create a .dvi
+file with lines of music integrated with text.
-So what does this look like? Well, here is a really
-simple example:
+Mudela-book will do its best to try to align the music to the left and
+right margins. Currently the most used papersizes and one- and
+twocolumn mode is supported, but if you use the geometry-package from
+\LaTeX~or change the margins things will break.
-\begin{mudela}[fragment, verbatim]
- \relative c'{c4 d e f | g2 g}
-\end{mudela}
-
-As you can see the mudela code is put inside a environment
-called \verb|mudela|, shouldn't be too difficult to remember.
-If you have never heard about \LaTeX, Mudela or that stuff
-about environment, you should probably read some other manuals
-before this.
-
-The code above produces this music:
-
-\begin{mudela}[fragment]
- \relative c' {c4 d e f | g2 g }
-\end{mudela}
+This document assumes you have basic knowledge of GNU LilyPond and
+\LaTeX. If the authors not-so-good english irritates you, please
+send a patch.
-The parameter \verb|fragment| means that mudela-book will insert
-some red tape for you. There are one thing to remember. Currently
-\verb|fragment|s don't know about margins and line width, so if
-you type more than a few bars and want a line break, you can not
-use \verb|fragment|. The \verb|verbatim| parameter is used to
-make mudela-book print the mudela source instead of the
-music. The parameter will probably not be visible in later versions
-of mudela-book.
+So what does this look like? Well, here is an example:
-There are some conventions to follow to make the music respect
-margins. You have to use \verb|\default_paper| as your paper,
-to make mudela-book adjust the music to reach from the
-left to the right margin.
-
-The following code
\begin{mudela}[verbatim]
\score{
- \notes\relative c'{c d e f | g2 g | a4 a a a | g1 |
- f4 f f f | e2 e | d4 d d d | c1}
- \paper{}
+ \notes\relative c'{
+ \time 5/8;
+ [e16( g b c a g][e a b d] | )e2 d,8 |
+ [e16( g b c a g][e a b d] | )b2 [a16( f] |
+ [e a b d] )e4 c8 | [es16( bes a as g es][d c b! )g] |
+ [f( a b d b a][f a b d] | )e2
+ }
}
\end{mudela}
-gives this music:
+
+\noindent produces this music:
+
\begin{mudela}
\score{
- \notes\relative c'{c d e f | g2 g | a4 a a a | g1 |
- f4 f f f | e2 e | d4 d d d | c1}
- \paper{}
+ \notes\relative c'{
+ \time 5/8;
+ [e16( g b c a g][e a b d] | )e2 d,8 |
+ [e16( g b c a g][e a b d] | )b2 [a16( f] |
+ [e a b d] )e4 c8 | [es16( bes a as g es][d c b! )g] |
+ [f( a b d b a][f a b d] | )e2
+ }
}
\end{mudela}
-On my Debian 2.0 with LilyPond 1.0.17, teTex 0.9,
-a4 paper and default line widht, the music is printet with
-7 bars on the first line and 1 bars on the second.
+If you are lucky, the above example show a nice feature of LilyPond
+and \LaTeX. Since LilyPond can output the music as \TeX~graphics,
+\LaTeX~can insert pagebreaks between the lines of music.
-This does not look to well, but you can fix it by setting
-the \verb|linewidth| variable to, let's say 8 cm. The paper
-definition
+Notice that there is no \verb|\paper| statement in the example
+above. Mudela-book will insert some code for you that defines the
+linewidth and the font to use. If you don't want to change the default,
+there is no need to put an empty \verb|\paper{}| inside the \verb|\score|.
+In the example above, something like
+this might be inserted before your code:
\begin{verbatim}
-\paper{
-linewidth=8.\cm;}
+\include "paper16.ly"
+\paper{ \paper_sixteen
+ linewidth = 390.\pt;
+ castingalgorithm = \Gourlay;
+}
\end{verbatim}
-makes this score:
+The actual values for linewidth will differ depending on papersize and
+number of columns. Also, if you use a different fontsize for the
+music, another file than \verb|paper16.ly| will be included.
+
+If you want to make the music not so wide, you can insert a
+\verb|\paper| statement that set the linewidth:
+
+\begin{mudela}[verbatim]
+\score{
+ \notes\relative c'{
+ \time 5/8;
+ [e16( g b c a g][e a b d] | )e2 d,8 |
+ [e16( g b c a g][e a b d] | )b2 [a16( f] |
+ [e a b d] )e4 c8 | [es16( bes a as g es][d c b! )g] |
+ [f( a b d b a][f a b d] | )e2
+ }
+ \paper{linewidth = 10.\cm;}
+}
+\end{mudela}
+
+\noindent produces this music:
\begin{mudela}
+% dette er en kommentar
\score{
- \notes\relative c'{c d e f | g2 g | a4 a a a | g1 |
- f4 f f f | e2 e | d4 d d d | c1}
- \paper{
- linewidth=8.\cm;}
+ \notes\relative c'{
+ \time 5/8;
+ [e16( g b c a g][e a b d] | )e2 d,8 |
+ [e16( g b c a g][e a b d] | )b2 [a16( f] |
+ [e a b d] )e4 c8 | [es16( bes a as g es][d c b! )g] |
+ [f( a b d b a][f a b d] | )e2
+ }
+ \paper{linewidth = 10.\cm;}
}
\end{mudela}
-The \verb|linewidth| variable is a LilyPond paper variable.
-It is not spesific to mudela-book. See LilyPond tutorial and
-reference manual for other variables.
+Very often, if you mix music and text, the music is often only a
+few notes or at most a few bars. This music should be as short as
+possible and not stretched to be aligned to the right margin.
-You can also,
-\begin{mudela}[fragment, floating]
- \relative c'{c c g' g | a a g2}
-\end{mudela}
-, include music in the middle of your text. Just give the option
-\verb|floating| to \verb|\begin{mudela}|
+If you only write voice-contents in the mudela block, mudela-book
+will set the \verb|linewidth| variable to -1, so Lilypond
+will make the music as short as possible but without breaking the
+line. Here is a well know harmonic progression:
+\begin{mudela}
+% dette er en kommentar
+ <c' e' g'> <b d' g'> <c'2 e' g'>
+\end{mudela}
+\noindent that is made by this code:
+\begin{mudela}[verbatim]
+ <c' e' g'> <b d' g'> <c'2 e' g'>
+\end{mudela}
+
+If you want to place music examples in the text,
+\begin{mudela}[eps]
+ <c' e' g'> <b d' g'> <c'2 e' g'>
+\end{mudela}
+, you can use the \verb|eps| option. This will create the music as
+eps graphics and include it into the document with the
+\verb|\includegraphics| command.
+
+The code used look like this:
+\begin{mudela}[eps, verbatim]
+ <c' e' g'> <b d' g'> <c'2 e' g'>
+\end{mudela}
-Oh, you think the notes are to big to be in the middle of all those
-small characters, that something like this,
-\begin{mudela}[fragment, floating, 11pt]
- \relative c'{c c g' g | a a g2}
+You can also use the \verb|eps| option if the block is a complete
+mudela source. This 5 cm long empty line,
+\begin{mudela}[eps]
+\score{
+ \notes{s}
+ \paper{ linewidth = 5.\cm;}
+}
\end{mudela}
-, is better? If you have a lot of small music examples like this
-in the middle of your text, you might get a nicer look by using
-``double'' line spacing. Put the \verb|\linespread{1.6}| command
-into the preamble of your document. Then the line spacing will not
-be increased between the lines where you have music printed with the
-smalles font size.
+was created with this code:
+\begin{mudela}[eps, verbatim]
+\score{
+ \notes{s}
+ \paper{ linewidth = 5.\cm;}
+}
+\end{mudela}
+To avoid that \LaTeX~places the music on a line of its one, there should
+be no empty lines between the normal text and the mudela
+environment.
+
+There is an even shorter way to write small musical fragments. This
+triad, \mudela{<c' e' g'>}, was created with this code:
+\verb|\mudela{<c' e' g'>}|
+Mudela-book let you define your own commands, see the file
+\verb|mudela-book-defs.py|. In a later version, it will be possible to
+have your own file that defines new commands. To print small rhythms
+like this, \mudelaRhythm{c4 r8 [c16 c] c4 c}, the default
+\verb|mudela-book-defs.py| also defines a command \verb|mudelaRhythm|.
+The last music was created like this:
+\verb|\mudelaRhytm{c4 r8 [c16 c] c4 c}|. This feature is higly
+experimental, so Things Will Change.
-To change font size is easy, just give the fontsize as a parameter to
-\verb|\begin{mudela}|. The last piece of music was created with this
-code:
-\begin{mudela}[fragment, floating, 11pt, verbatim]
- \relative c'{c c g' g | a a g2}
+The last example show that we need options to change the
+% LONG line just to test multiple mudela on one line
+fontsize of the music. 11pt, \mudela[11pt]{<c' e' g'>} or 13pt, \mudela[13pt]{<c' e' g'>} is probably a better fontsize
+if you include music in the middle of the text. The code looks like this:
+\verb|\mudela[11pt]{<c' e' g'>}|
+
+or
+\begin{mudela}[13pt, verbatim]
+<c' e' g'>
\end{mudela}
-You can use the following font sizes:
+The following options set the fontsize:
\begin{itemize}
-\item 11 pt
-\begin{mudela}[11pt, fragment, floating]
+\item \verb|11pt|
+\begin{mudela}[11pt, eps]
\relative c'{
r16 [c d e][f d e c] [g'8 c][b-\prall c] |
[d16 g, a b][c a b g][d'8 g f-\prall g]
}
\end{mudela}
-\item 13 pt
-\begin{mudela}[13pt, fragment, floating]
+\item \verb|13pt|
+\begin{mudela}[13pt, eps]
\relative c'{
r16 [c d e][f d e c] [g'8 c][b-\prall c] |
[d16 g, a b][c a b g][d'8 g f-\prall g]
}
\end{mudela}
-\item 16 pt
-\begin{mudela}[16pt, fragment, floating]
+\item \verb|16pt|
+\begin{mudela}[16pt, eps]
\relative c'{
r16 [c d e][f d e c] [g'8 c][b-\prall c] |
[d16 g, a b][c a b g][d'8 g f-\prall g]
}
\end{mudela}
-\item 20 pt
-\begin{mudela}[20pt, fragment, floating]
+\item \verb|20pt|
+\begin{mudela}[20pt, eps]
\relative c'{
r16 [c d e][f d e c] [g'8 c][b-\prall c] |
[d16 g, a b][c a b g][d'8 g f-\prall g]
}
\end{mudela}
-\item 26 pt
-\begin{mudela}[26pt, fragment, floating]
+\item \verb|26pt|
+\begin{mudela}[26pt, eps]
\relative c'{
r16 [c d e][f d e c] [g'8 c][b-\prall c] |
[d16 g, a b][c a b g][d'8 g f-\prall g]
\end{mudela}
\end{itemize}
-As\marginpar{
+This was all options to \verb|\begin{mudela}|. The rest of the
+document will show some ways you can use mudela in
+\LaTeX~documents. It will also act as a simple test-suite for
+mudela-book. You \marginpar{
marginpar!
-\begin{mudela}[11pt, fragment, floating]
- \relative c''{\key es; r8 [g g g] es2}
+\begin{mudela}[eps, 11pt]
+ \relative c'' {\key c \minor; r4 [g8 g g] es2}
\end{mudela}
-}
-you can see, \verb|floating| mudela don't know about margins.
-Remember that to place the music anywhere\footnote{
+}can place \verb|eps| mudela in footnotes\footnote{
footnote!
-\begin{mudela}[11pt, fragment, floating]
- \relative c''{\key es; r8 [g g g] es2}
+\begin{mudela}[eps, 11pt]
+ \relative c'' {\key c \minor;r4 [g8 g g] es2}
\end{mudela}
}
- else than at the left margin you must use the option \verb|floating|.
+and marginspars just as any other included eps graphics.
+
+\begin{floatingfigure}[r]{7cm}
+\begin{mudela}
+\score{
+ \notes\relative c'{
+ \time 12/8;
+ r4-\fermata [b16-.( )b-.] [f'8-- dis16-.( )dis-. gis8--]
+ [f16-.( )f-. dis8-- gis16-.( )gis-.] cis4.-\fermata |
+
+ r4.-\fermata [cis,16 cis g'8 f16 f b][g16 g f8 b16 b] dis4.-\fermata
+ }
+ \paper{linewidth = 7.\cm;}
+}
+\end{mudela}
+\end{floatingfigure}
+
+To the right you can see some bars from the trumpet fanfara from the
+beginning of the fantastic street opera ``Houdini the Great'', by the
+danish composer Andy Pape. The music is put inside a
+\verb|floatingfigure| environment, and the music will be aligned by
+the right marging if you set floatingfigure width and mudela linewidth
+to the same value. The code looks like this:
+
+\verb|\begin{floatingfigure}{7cm}|
+\begin{mudela}[verbatim]
+\score{
+ \notes\relative c'{
+ \time 12/8;
+ r4.-\fermata [b16-.( )b-.] [f'8-- dis16-.( )dis-. gis8--]
+ [f16-.( )f-. dis8-- gis16-.( )gis-.] cis8.-\fermata |
+
+ r4.-\fermata [cis,16 cis g'8 f16 f b]
+ [g16 g f8 b16 b] dis4.-\fermata
+ }
+ \paper{linewidth = 7.\cm;}
+}
+\end{mudela}
+\verb|\end{floatingfigure}|
-\twocolumn[\Large The following is written in two columns \normalsize]
-Sinse the version number is quite low, 0.4.0 last I checked, you
-should be careful not to mix braces that belongs to mudela-book and
-\LaTeX ~on the same line. The following code will probably break:
+\twocolumn[\large The following is written in two columns just to show
+what mudela-book can do..\normalsize]
-\verb|\footnote{\be|\verb|gin{mudela}|
+Tabulars is also nice. Here you have a chance to learn the norwegian
+names of some triads:
+\vspace{1em}
+
+\begin{tabular}{|l|l|}
+\hline
+
+dur &
+\begin{mudela}[eps]
+<c' e' g'>
+\end{mudela}
+\\
+\hline moll &
+\begin{mudela}[eps]
+<c' ees' g'>
+\end{mudela}
+\\
+\hline forminsket &
+\begin{mudela}[eps]
+<c' es' ges'>
+\end{mudela}
+\\
+\hline forstørret &
+\begin{mudela}[eps]
+<c' e' gis'>
+\end{mudela}
+\\
+\hline
+\end{tabular}
+
+\vspace{1em}
+If you have a lot of small music examples
+like this in the middle of your text, you might get a nicer look by
+using ``double'' line spacing. Put the \verb|\linespread{1.6}| command
+into the preamble of your document. Then the line spacing will not be
+increased between the lines where you have music printed with the
+smallest font size.
+
+Sinse the version number is quite low, you should be careful not to
+mix braces that belongs to mudela-book and \LaTeX~on the same
+line. The following code will probably break:
+
+\verb|\footnote{\begin{mudela}|
\verb|\relative c''{ \key es; r8 [g g g] es2} |
-\verb|\e|\verb|nd{mudela}}|
+\verb|\end{mudela}}|
Mudela-book does know about \verb|\onecolumn| and \verb|\twocolumn|.
So the music will be adjusted to the new linewith:
\score{
\notes\relative c'{c d e f | g2 g | a4 a a a | g1 |
f4 f f f | e2 e | d4 d d d | c1}
- \paper{}
}
\end{mudela}
produces
\score{
\notes\relative c'{c d e f | g2 g | a4 a a a | g1 |
f4 f f f | e2 e | d4 d d d | c1}
- \paper{}
}
\end{mudela}
-As in one column mode, \verb|fragment|s don't know about
-the line width:
-\begin{mudela}[fragment]
- \relative c'{[c8 e][d f][e g][f a] | [g b][a c][b d] c4 |
- [e8 c][d b][c a][b g] | [a f][g e][f d] c4 }
-\end{mudela}
Verbatim environments will also ignore the page margins. That is
-a feature of \LaTeX.
+a feature of \LaTeX. (But you usually put things inside a verbatim
+environment when you don't want \LaTeX~to do any linebreaking)
+
\end{document}
+
+pl 9
+ - fixed \relative
+
+pl8.tca1
+ - changes to mudela-book. Junked fragment and floating options,
+ added eps option. Options should not stay as long that you get
+ comfortable with them. ;)
+ - Also possible to define your own commands with
+ a syntax like \mudela{c d e}
+ - updated mudela-book-doc.doc
+
+pl 8.jcn3
+ - website: gnuisation of footers
+
+pl 8.jcn2
+ - input/test/vertical-text.ly: urg ydim of text chords
+ - bf: one set of |: :| volta per staff
+ - bf: init/table* braces + more braces
+ - try at score-volta-bracket
+
+pl 8.jcn1
+ - bf: lyric-engraver: handle multiple requests (don't overwrite)
+ - bf: guile-config
+ - music-list-iterator
+
+pl 8.hwn3
+ - coriolan corno 1
+
+pl 8.hwn2
+ - more output revamping.
+
+pl 8.hwn1
+ - Scheme output stuff
+ - tutorial fixes.
+
+*****************
pl 8
pl7.jcn3
PACKAGE_NAME=LilyPond
MAJOR_VERSION=1
MINOR_VERSION=1
-PATCH_LEVEL=8
+PATCH_LEVEL=9
MY_PATCH_LEVEL=
# use the above to send patches: MY_PATCH_LEVEL is always empty for a
#!@PYTHON@
# All non-english comments are NOT in swedish, they are norwegian!
-
-# TODO: center option (??)
-# * the verbatim option should not be visible in the created latex file
-# * what the h.. does castingalgorithm do/mean???
+# TODO:
+# * center option (??)
+# * mudela-book should treat \begin{verbatim} that contains inline mudela
+# correctly.
+# * make mudela-book understand usepackage{geometry}
+# * check that linewidth set in \paper is not wider than actual linewidth?
# * the following fails because mudelabook doesn't care that the
# last } after \end{mudela} finishes the marginpar:
# \marginpar{
-# \begin{mudela}[fragment]
+# \begin{mudela}
# c d e f g
# \end{mudela}}
-# * fragments should know about margins
-
+# * force-verbatim is maybe not that useful since latex fails with footnotes,
+# marginpars and others
# log:
# 0.3:
# rewrite in Python.
# - cleanup
# - speedup, do all mudela parsing at the same time to avoid multiple
# guile startup that takes some seconds on my computer
+# 0.4.2:
+# - fixed default latex fontsize, it should be 10pt not 11pt
+# - verbatim option no longer visible
+# - mudela-book produces .dvi output
+# - change to use castingalgorithm = \Gourlay instead of \Wordwrap. It gives
+# better result on small linewidths.
+# - mudela-book-doc.doc rewritten
+# 0.5:
+# - junked floating and fragment options, added eps option
+# - mudela-book will scan the mudela code to find out if it has to add
+# paper-definition and \score{\notes{...}}
+# - possible to define commands that look like this: \mudela{ c d e }
import os
import string
import sys
outdir = 'out'
-program_version = '0.4.1'
+initfile = ''
+program_version = '0.5'
out_files = []
-fontsize_i2a = {11:'eleven', 13:'thirteen', 16:'sixteen', 20:'twenty', 26:'twentysix'}
+fontsize_i2a = {11:'eleven', 13:'thirteen', 16:'sixteen',
+ 20:'twenty', 26:'twentysix'}
fontsize_pt2i = {'11pt':11, '13pt':13, '16pt':16, '20pt':20, '26pt':26}
begin_mudela_re = re.compile ('^ *\\\\begin{mudela}')
extract_papersize_re = re.compile('\\\\documentclass[\[, ]*(\w*)paper[\w ,]*\]\{\w*\}')
-extract_fontsize1_re = re.compile('\\\\documentclass\[[^\]]*\]\{[^\}]*\}')
-extract_fontsize2_re = re.compile('[ ,\[]*([0-9]*)pt')
+extract_fontsize_re = re.compile('[ ,\[]*([0-9]*)pt')
begin_mudela_opts_re = re.compile('\[[^\]]*\]')
end_mudela_re = re.compile ('^ *\\\\end{mudela}')
section_re = re.compile ('\\\\section')
'mudela-fontsize' : {'init': 16},
'papersize' : {'init': 'a4'},
'num-column' : {'init': 1},
- 'tex-fontsize' : {'init': 11}
+ 'tex-fontsize' : {'init': 10}
}
def clear_for_new_file(self):
for var in self.__data.keys():
self.basename = basename
self.temp_filename = "%s/%s" %(outdir, 'mudela-temp.ly')
self.file = open (self.temp_filename, 'w')
+ self.__lines = []
# 'tex' or 'eps'
self.graphic_type = 'tex'
- self.fragment = 0
+ self.code_type = 'unknown'
def write (self, line):
# match only if there is nothing but whitespace before \begin HACK
if re.search('^\s*\\\\begin{mudela}', line):
self.scan_begin_statement(line)
- self.write_red_tape()
else:
- self.file.write (line)
+ if self.code_type == 'unknown':
+ if re.search('^\s*\\\\score', line) or \
+ re.search('^\s*\\\\paper', line) or \
+ re.search('^\s*\\\\header', line) or \
+ re.search('^\s*[A-Za-z]*\s*=', line):
+ self.code_type = 'ly'
+ self.__lines.append(line)
def scan_begin_statement(self, line):
r = begin_mudela_opts_re.search(line)
if r:
- o = r.group()[1:][:-1]
- optlist = re.compile('[ ,]*').split(o)
+ o = r.group()[1:-1]
+ optlist = re.compile('[\s,]*').split(o)
else:
optlist = []
+ if 'fragment' in optlist:
+ print "warning: obsolete option: fragment"
if 'floating' in optlist:
+ print "warning: obsolete option: floating, change to eps"
+ if 'eps' in optlist:
self.graphic_type = 'eps'
- else:
- self.graphic_type = 'tex'
- if 'fragment' in optlist:
- self.fragment = 1
- else:
- self.fragment = 0
for pt in fontsize_pt2i.keys():
if pt in optlist:
Props.setMudelaFontsize(fontsize_pt2i[pt], 'block')
def write_red_tape(self):
- self.file.write ('\\include \"paper%d.ly\"\n' % Props.getMudelaFontsize())
+ self.file.write ('\\include \"paper%d.ly\"\n' \
+ % Props.getMudelaFontsize())
+
s = fontsize_i2a[Props.getMudelaFontsize()]
- if self.fragment:
- self.file.write("\\paper {" # mudela 1.0.4
- + "\\paper_%s\n linewidth = -1.\\pt;" % s
- + "castingalgorithm = \Wordwrap; indent = 2.\cm; \n}")
- self.file.write("\\score{\n\\notes{")
+ if self.code_type == 'fly':
+ linewidth_str = 'linewidth = -1.\cm;'
else:
- self.file.write ("\paper {"
- + "\\paper_%s\n linewidth = %i.\\pt;" % \
- (s, Props.getLineWidth()) \
- + "castingalgorithm = \Wordwrap; indent = 2.\cm;\n}")
+ linewidth_str = 'linewidth = %i.\\pt;' % Props.getLineWidth()
+ self.file.write("\\paper {"
+ + "\\paper_%s " % s
+ + linewidth_str
+ + "castingalgorithm = \Gourlay; \n}")
+ #+ "castingalgorithm = \Wordwrap; indent = 2.\cm; \n}")
+ if self.code_type == 'fly':
+ self.file.write('\\score{\n\\notes{')
def close (self):
- if self.fragment:
- self.file.write ('}\\paper { } }\n')
- self.file.close ()
+ if self.code_type == 'unknown':
+ self.code_type = 'fly'
+ if self.code_type == 'ly':
+ self.write_red_tape()
+ for l in self.__lines:
+ self.file.write(l)
+ else:
+ self.write_red_tape()
+ for l in self.__lines:
+ self.file.write(l)
+ self.file.write('}}')
+
+ self.file.close()
inf = outdir + self.basename + '.ly'
outf = outdir + self.basename + '.tex'
def __init__ (self, name):
self.output_fn = '%s/%s' % (outdir, name)
self.__lines = []
- def open_verbatim (self):
+ def open_verbatim (self, line):
self.__lines.append('\\begin{verbatim}\n')
+ s = re.sub('[\s,]*verbatim[\s]*', '', line)
+ s = re.sub('\[\]', '', s)
+ self.__lines.append(s);
def close_verbatim (self):
self.__lines.append('\\end{verbatim}\n')
def write (self, s):
if need_recompile_b(outdir+g[1]+'.ly', outdir+g[1]+'.tex'):
s = s + ' ' + g[1]+'.ly'
if s != '':
- os.system('cd %s; lilypond %s' %(outdir, s))
+ e = os.system('cd %s; lilypond %s' %(outdir, s))
+ if e:
+ print "error: lilypond exited with value", e
+ sys.exit(e)
for g in g_vec:
if g[0] == 'eps':
compile('tex %s' % g[1]+'.tex', outdir, g[1]+'.tex', g[1]+'.dvi')
compile('dvips -E -o %s %s' %(g[1]+'.eps', g[1]+'.dvi'), outdir,
g[1]+'.dvi', g[1]+'.eps')
def write_outfile(self):
- outfn = self.output_fn+'.latex'
- sys.stderr.write ('Writing output to `%s\'\n'% outfn)
- file = open(outfn, 'w')
+ file = open(self.output_fn+'.latex', 'w')
file.write('% Created by mudela-book\n')
for line in self.__lines:
if type(line)==type([]):
if line[0] == 'tex':
file.write('\\preMudelaExample\\input %s\n\postMudelaExample '\
-# % (outdir+line[1]+'.tex'))
-
# TeX applies the prefix of the main source automatically.
- % (line[1]+'.tex'))
+ % (line[1]+'.tex'))
+# % (outdir+line[1]+'.tex'))
if line[0] == 'eps':
ps_dim = ps_dimention(outdir+line[1]+'.eps')
file.write('\\parbox{%ipt}{\includegraphics{%s}}\n' \
-# % (ps_dim[0], outdir+line[1]+'.eps'))
% (ps_dim[0], line[1]+'.eps'))
-
+# % (ps_dim[0], outdir+line[1]+'.eps'))
else:
file.write(line)
file.close()
for fn in [filename, filename+'.tex', filename+'.doc']:
try:
self.infile = open (fn)
+ self.filename = fn
+ return
except:
continue
- self.filename = fn
- break
+ raise IOError
def get_lines (self):
lines = self.infile.readlines ()
(retlines, retdeps) = ([],[self.filename])
for line in lines:
r_inp = input_re.search (line)
r_inc = include_re.search (line)
+
# Filename rules for \input :
# input: no .tex ext
# 1. will search for file with exact that name (tex-input.my will be found)
# Filename rules for \include :
# 1. will only find files with name given to \include + .tex ext
if r_inp:
- t = Tex_input (r_inp.groups()[0])
- ls = t.get_lines ()
- retlines = retlines + ls[0]
- retdeps = retdeps + ls[1]
+ try:
+ t = Tex_input (r_inp.groups()[0])
+ ls = t.get_lines ()
+ retlines = retlines + ls[0]
+ retdeps = retdeps + ls[1]
+ except:
+ print "warning: can't find %s, let's hope latex will" \
+ % r_inp.groups()[0]
+ retlines.append (line)
elif r_inc:
- t = Tex_input (r_inc.groups()[0]+'.tex')
- ls =t.get_lines ()
- ls[0].insert(0, '\\newpage\n')
- ls[0].append('\\newpage\n')
- retlines = retlines + ls[0]
- retdeps = retdeps + ls[1]
+ try:
+ t = Tex_input (r_inc.groups()[0]+'.tex')
+ ls =t.get_lines ()
+ ls[0].insert(0, '\\newpage\n')
+ ls[0].append('\\newpage\n')
+ retlines = retlines + ls[0]
+ retdeps = retdeps + ls[1]
+ except:
+ print "warning: can't find %s, let's hope latex will" \
+ % r_inc.groups()[0]
+ retlines.append (line)
else:
- retlines.append (line)
- return (retlines, retdeps)
+ r_mud = defined_mudela_cmd_re.search(line)
+ if r_mud:
+ ss = "\\\\verb(?P<xx>[^a-zA-Z])\s*\\\\%s\s*(?P=xx)" \
+ % re.escape(r_mud.group()[1:])
+ if re.search(ss, line):
+ retlines.append(line)
+ continue
+ while 1:
+ opts = r_mud.groups()[2]
+ if opts == None:
+ opts = ''
+ else:
+ opts = ', '+opts
+ (start, rest) = string.split(line, r_mud.group(), 1)
+ retlines.append(start+'\n')
+ v = string.split(defined_mudela_cmd[r_mud.groups()[0]], '\n')
+ for l in v[1:-1]:
+ l = re.sub(r'\\fontoptions', opts, l)
+ l = re.sub(r'\\maininput', r_mud.groups()[3], l)
+ retlines.append(l)
+ r_mud = defined_mudela_cmd_re.search(rest)
+ if not r_mud:
+ retlines.append(rest)
+ break;
+ line = rest
+ else:
+ retlines.append (line)
+ else:
+ return (retlines, retdeps)
class Main_tex_input(Tex_input):
self.chapter = self.chapter + 1
def gen_basename (self):
- return '%s-%d.%d.%d' % (self.outname,self.chapter,self.section,self.fine_count)
- #return '%s/%s-%d.%d.%d' % (outdir, self.outname,self.chapter,self.section,self.fine_count)
-
+ return '%s-%d.%d.%d' % (self.outname, self.chapter,
+ self.section, self.fine_count)
def extract_papersize_from_documentclass(self, line):
pre = extract_papersize_re.search(line)
if not pre:
return None
return pre.groups()[0]
def extract_fontsize_from_documentclass(self, line):
- if extract_fontsize1_re.search(line):
- r = extract_fontsize2_re.search(line)
- if r:
- return int(r.groups()[0])
+ r = extract_fontsize_re.search(line)
+ if r:
+ return int(r.groups()[0])
def do_it(self):
preMudelaDef = postMudelaDef = 0
(lines, self.deps) = self.get_lines ()
optlist = []
if ('verbatim' in optlist) or (Props.force_verbatim_b):
self.verbatim = 1
- self.mudtex.open_verbatim ()
+ self.mudtex.open_verbatim (line)
+ continue
else:
self.verbatim = 0
self.mudela = Mudela_output (self.gen_basename ())
-
elif end_mudela_re.search (line):
if __debug__:
if self.mode != 'mudela':
-o, --outname=FILE prefix for filenames
--default-mudela-fontsize=??pt default fontsize for music
--force-mudela-fontsize=??pt force fontsize for all inline mudela
- --force-verbatim make all mudela verbatim\n"""
+ --force-verbatim make all mudela verbatim\n
+ --dependencies write dependencies
+ --init mudela-book initfile
+ """
)
sys.exit (0)
def write_deps (fn, out, deps):
out_fn = outdir + '/' + fn
- out_fn = re.sub ('//', '/', out_fn)
- print 'writing `%s\'\n' % out_fn
+ print '`writing `%s\'\n\'' % out_fn
f = open (out_fn, 'w')
f.write ('%s: %s\n'% (outdir + '/' + out + '.dvi',
sys.stderr.write ('This is %s version %s\n' % ('mudela-book', program_version))
def main():
- global outdir
+ global outdir, initfile, defined_mudela_cmd, defined_mudela_cmd_re
outname = ''
try:
(options, files) = getopt.getopt(
'default-mudela-fontsize=',
'force-mudela-fontsize=',
'help', 'dependencies',
- 'force-verbatim'])
+ 'force-verbatim', 'init='])
except getopt.error, msg:
print "error:", msg
sys.exit(1)
do_deps = 0
- for opt in options:
+ for opt in options:
o = opt[0]
a = opt[1]
if o == '--outname' or o == '-o':
Props.force_mudela_fontsize = fontsize_pt2i[a]
if o == '--force-verbatim':
Props.force_verbatim_b = 1
-
+ if o == '--init':
+ initfile = a
if outdir[-1:] != '/':
outdir = outdir + '/'
+ std_init_filename = ''
+ for p in string.split(os.environ['LILYINCLUDE'], ':'):
+ try:
+ std_init_filename = p+os.sep+'mudela-book-defs.py'
+ break
+ except:
+ continue
+ if std_init_filename == '':
+ print "error: Can't find mudela-book-defs.py"
+ os.exit(-1)
+ f = open(std_init_filename)
+ s = f.read()
+ f.close()
+ defined_mudela_cmd = eval(s)
+
+ if initfile != '':
+ f = open(initfile)
+ s = f.read()
+ f.close()
+ d = eval(s)
+ for i in d.keys():
+ defined_mudela_cmd[i] = d[i]
+ del d
+
+ c = defined_mudela_cmd.keys()[0]
+ for x in defined_mudela_cmd.keys()[1:]:
+ c = c + '|'+x
+ defined_mudela_cmd_re = re.compile("\\\\(%s)(\[(\d*pt)\])*{([^}]*)}" %c)
+
if not os.path.isdir(outdir):
os.system('mkdir %s' % outdir)
my_depname = my_outname + '.dep'
inp = Main_tex_input (input_filename, my_outname)
inp.do_it ()
-
-
+# os.system('latex %s/%s.latex' % (outdir, my_outname))
if do_deps:
write_deps (my_depname, my_outname, inp.deps)
--- /dev/null
+#!@PYTHON@
+# All non-english comments are NOT in swedish, they are norwegian!
+
+# TODO: center option (??)
+# * the verbatim option should not be visible in the created latex file
+# * what the h.. does castingalgorithm do/mean???
+# * the following fails because mudelabook doesn't care that the
+# last } after \end{mudela} finishes the marginpar:
+# \marginpar{
+# \begin{mudela}[fragment]
+# c d e f g
+# \end{mudela}}
+# * fragments should know about margins
+
+# log:
+# 0.3:
+# rewrite in Python.
+# 0.4:
+# much rewritten by new author. I think the work has been split more
+# logical between different classes.
+# 0.4.1:
+# - cleanup
+# - speedup, do all mudela parsing at the same time to avoid multiple
+# guile startup that takes some seconds on my computer
+
+import os
+import string
+import re
+import getopt
+import sys
+
+outdir = 'out'
+program_version = '0.4.1'
+
+out_files = []
+
+fontsize_i2a = {11:'eleven', 13:'thirteen', 16:'sixteen', 20:'twenty', 26:'twentysix'}
+fontsize_pt2i = {'11pt':11, '13pt':13, '16pt':16, '20pt':20, '26pt':26}
+
+begin_mudela_re = re.compile ('^ *\\\\begin{mudela}')
+extract_papersize_re = re.compile('\\\\documentclass[\[, ]*(\w*)paper[\w ,]*\]\{\w*\}')
+extract_fontsize1_re = re.compile('\\\\documentclass\[[^\]]*\]\{[^\}]*\}')
+extract_fontsize2_re = re.compile('[ ,\[]*([0-9]*)pt')
+begin_mudela_opts_re = re.compile('\[[^\]]*\]')
+end_mudela_re = re.compile ('^ *\\\\end{mudela}')
+section_re = re.compile ('\\\\section')
+chapter_re = re.compile ('\\\\chapter')
+input_re = re.compile ('^\\\\input{([^}]*)')
+include_re = re.compile ('^\\\\include{([^}]*)')
+begin_document_re = re.compile ('^ *\\\\begin{document}')
+documentclass_re = re.compile('\\\\documentclass')
+twocolumn_re = re.compile('\\\\twocolumn')
+onecolumn_re = re.compile('\\\\onecolumn')
+preMudelaExample_re = re.compile('\\\\def\\\\preMudelaExample')
+postMudelaExample_re = re.compile('\\\\def\\\\postMudelaExample')
+boundingBox_re = re.compile('%%BoundingBox: ([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)')
+
+def file_exist_b(name):
+ try:
+ f = open(name)
+ except IOError:
+ return 0
+ f.close ()
+ return 1
+
+def ps_dimention(fname):
+ fd = open(fname)
+ lines = fd.readlines()
+ for line in lines:
+ s = boundingBox_re.search(line)
+ if s:
+ break
+ return (int(s.groups()[2])-int(s.groups()[0]),
+ int(s.groups()[3])-int(s.groups()[1]))
+
+
+class CompileStatus:
+ pass
+class SomethingIsSeriouslyBroken:
+ pass
+
+def file_mtime (name):
+ return os.stat (name)[8] #mod time
+
+def need_recompile_b(infile, outfile):
+ indate = file_mtime (infile)
+ try:
+ outdate = file_mtime (outfile)
+ return indate > outdate
+ except os.error:
+ return 1
+
+#
+# executes os.system(command) if infile is newer than
+# outfile or outfile don't exist
+#
+def compile (command, workingdir, infile, outfile):
+ "Test if infile is newer than outfile. If so, cd to workingdir"
+ "and execute command"
+ indate = file_mtime (workingdir+infile)
+ try:
+ outdate = file_mtime (workingdir+outfile)
+ recompile = indate > outdate
+
+ except os.error:
+ recompile = 1
+
+ if recompile:
+ sys.stderr.write ('invoking `%s\'\n' % command)
+ if workingdir == '':
+ status = os.system (command)
+ else:
+ status = os.system ('cd %s; %s' %(workingdir, command))
+ if status:
+ raise CompileStatus
+
+class Properties:
+ #
+ # init
+ #
+ def __init__(self):
+ self.__linewidth = {
+ 1: {'a4':{10: 345, 11: 360, 12: 390},
+ 'a5':{10: 276, 11: 276, 12: 276},
+ 'b5':{10: 345, 11: 356, 12: 356},
+ 'letter':{10: 345, 11: 360, 12: 390},
+ 'legal': {10: 345, 11: 360, 12: 390},
+ 'executive':{10: 345, 11: 360, 12: 379}},
+ 2: {'a4':{10: 167, 11: 175, 12: 190},
+ 'a5':{10: 133, 11: 133, 12: 133},
+ 'b5':{10: 167, 11: 173, 12: 173},
+ 'letter':{10: 167, 11: 175, 12: 190},
+ 'legal':{10: 167, 11: 175, 12: 190},
+ 'executive':{10: 167, 11: 175, 12: 184}}}
+ # >0 --> force all mudela to this pt size
+ self.force_mudela_fontsize = 0
+ self.force_verbatim_b = 0
+ self.__data = {
+ 'mudela-fontsize' : {'init': 16},
+ 'papersize' : {'init': 'a4'},
+ 'num-column' : {'init': 1},
+ 'tex-fontsize' : {'init': 11}
+ }
+ def clear_for_new_file(self):
+ for var in self.__data.keys():
+ self.__data[var] = {'init': self.__data[var]['init']}
+ def clear_for_new_block(self):
+ for var in self.__data.keys():
+ if self.__data[var].has_key('block'):
+ del self.__data[var]['block']
+ def __get_variable(self, var):
+ if self.__data[var].has_key('block'):
+ return self.__data[var]['block']
+ elif self.__data[var].has_key('file'):
+ return self.__data[var]['file']
+ else:
+ return self.__data[var]['init']
+ def setPapersize(self, size, requester):
+ self.__data['papersize'][requester] = size
+ def getPapersize(self):
+ return self.__get_variable('papersize')
+ def setMudelaFontsize(self, size, requester):
+ self.__data['mudela-fontsize'][requester] = size
+ def getMudelaFontsize(self):
+ if self.force_mudela_fontsize:
+ return self.force_mudela_fontsize
+ return self.__get_variable('mudela-fontsize')
+ def setTexFontsize(self, size, requester):
+ self.__data['tex-fontsize'][requester] = size
+ def getTexFontsize(self):
+ return self.__get_variable('tex-fontsize')
+ def setNumColumn(self, num, requester):
+ self.__data['num-column'][requester] = num
+ def getNumColumn(self):
+ return self.__get_variable('num-column')
+ def getLineWidth(self):
+ return self.__linewidth[self.getNumColumn()][self.getPapersize()][self.getTexFontsize()]
+
+
+class Mudela_output:
+ def __init__ (self, basename):
+ self.basename = basename
+ self.temp_filename = "%s/%s" %(outdir, 'mudela-temp.ly')
+ self.file = open (self.temp_filename, 'w')
+ # 'tex' or 'eps'
+ self.graphic_type = 'tex'
+ self.fragment = 0
+ def write (self, line):
+ # match only if there is nothing but whitespace before \begin HACK
+ if re.search('^\s*\\\\begin{mudela}', line):
+ self.scan_begin_statement(line)
+ self.write_red_tape()
+ else:
+ self.file.write (line)
+ def scan_begin_statement(self, line):
+ r = begin_mudela_opts_re.search(line)
+ if r:
+ o = r.group()[1:][:-1]
+ optlist = re.compile('[ ,]*').split(o)
+ else:
+ optlist = []
+ if 'floating' in optlist:
+ self.graphic_type = 'eps'
+ else:
+ self.graphic_type = 'tex'
+ if 'fragment' in optlist:
+ self.fragment = 1
+ else:
+ self.fragment = 0
+ for pt in fontsize_pt2i.keys():
+ if pt in optlist:
+ Props.setMudelaFontsize(fontsize_pt2i[pt], 'block')
+ def write_red_tape(self):
+ self.file.write ('\\include \"paper%d.ly\"\n' % Props.getMudelaFontsize())
+ s = fontsize_i2a[Props.getMudelaFontsize()]
+ if self.fragment:
+ self.file.write("\\paper {" # mudela 1.0.4
+ + "\\paper_%s\n linewidth = -1.\\pt;" % s
+ + "castingalgorithm = \Wordwrap; indent = 2.\cm; \n}")
+ self.file.write("\\score{\n\\notes{")
+ else:
+ self.file.write ("\paper {"
+ + "\\paper_%s\n linewidth = %i.\\pt;" % \
+ (s, Props.getLineWidth()) \
+ + "castingalgorithm = \Wordwrap; indent = 2.\cm;\n}")
+ def close (self):
+ if self.fragment:
+ self.file.write ('}\\paper { } }\n')
+ self.file.close ()
+
+ inf = outdir + self.basename + '.ly'
+ outf = outdir + self.basename + '.tex'
+ if not os.path.isfile(inf):
+ status = 1
+ else:
+ status = os.system ('diff -q %s %s' % (self.temp_filename, inf))
+ if status:
+ os.rename (self.temp_filename, inf)
+ if need_recompile_b(inf, outf):
+ out_files.append((self.graphic_type, inf))
+ def insert_me_string(self):
+ "Returns a string that can be used directly in latex."
+ if self.graphic_type == 'tex':
+ return ['tex', self.basename]
+ elif self.graphic_type == 'eps':
+ return ['eps', self.basename]
+ else:
+ raise SomethingIsSeriouslyBroken
+
+class Tex_output:
+ def __init__ (self, name):
+ self.output_fn = '%s/%s' % (outdir, name)
+ self.__lines = []
+ def open_verbatim (self):
+ self.__lines.append('\\begin{verbatim}\n')
+ def close_verbatim (self):
+ self.__lines.append('\\end{verbatim}\n')
+ def write (self, s):
+ self.__lines.append(s)
+ def create_graphics(self):
+ s = ''
+ g_vec = []
+ for line in self.__lines:
+ if type(line)==type([]):
+ g_vec.append(line)
+ for g in g_vec:
+ if need_recompile_b(outdir+g[1]+'.ly', outdir+g[1]+'.tex'):
+ s = s + ' ' + g[1]+'.ly'
+ if s != '':
+ os.system('cd %s; lilypond %s' %(outdir, s))
+ for g in g_vec:
+ if g[0] == 'eps':
+ compile('tex %s' % g[1]+'.tex', outdir, g[1]+'.tex', g[1]+'.dvi')
+ compile('dvips -E -o %s %s' %(g[1]+'.eps', g[1]+'.dvi'), outdir,
+ g[1]+'.dvi', g[1]+'.eps')
+ def write_outfile(self):
+ outfn = self.output_fn+'.latex'
+ sys.stderr.write ('Writing output to `%s\'\n'% outfn)
+ file = open(outfn, 'w')
+ file.write('% Created by mudela-book\n')
+ for line in self.__lines:
+ if type(line)==type([]):
+ if line[0] == 'tex':
+ file.write('\\preMudelaExample\\input %s\n\postMudelaExample '\
+# % (outdir+line[1]+'.tex'))
+
+ # TeX applies the prefix of the main source automatically.
+ % (line[1]+'.tex'))
+ if line[0] == 'eps':
+ ps_dim = ps_dimention(outdir+line[1]+'.eps')
+ file.write('\\parbox{%ipt}{\includegraphics{%s}}\n' \
+# % (ps_dim[0], outdir+line[1]+'.eps'))
+ % (ps_dim[0], line[1]+'.eps'))
+
+ else:
+ file.write(line)
+ file.close()
+class Tex_input:
+ def __init__ (self, filename):
+ for fn in [filename, filename+'.tex', filename+'.doc']:
+ try:
+ self.infile = open (fn)
+ except:
+ continue
+ self.filename = fn
+ break
+ def get_lines (self):
+ lines = self.infile.readlines ()
+ (retlines, retdeps) = ([],[self.filename])
+ for line in lines:
+ r_inp = input_re.search (line)
+ r_inc = include_re.search (line)
+ # Filename rules for \input :
+ # input: no .tex ext
+ # 1. will search for file with exact that name (tex-input.my will be found)
+ # 2. will search for file with .tex ext, (tex-input.my
+ # will find tex-input.my.tex)
+ # input: with .tex ext
+ # 1. will find exact match
+
+ # Filename rules for \include :
+ # 1. will only find files with name given to \include + .tex ext
+ if r_inp:
+ t = Tex_input (r_inp.groups()[0])
+ ls = t.get_lines ()
+ retlines = retlines + ls[0]
+ retdeps = retdeps + ls[1]
+ elif r_inc:
+ t = Tex_input (r_inc.groups()[0]+'.tex')
+ ls =t.get_lines ()
+ ls[0].insert(0, '\\newpage\n')
+ ls[0].append('\\newpage\n')
+ retlines = retlines + ls[0]
+ retdeps = retdeps + ls[1]
+ else:
+ retlines.append (line)
+ return (retlines, retdeps)
+
+
+class Main_tex_input(Tex_input):
+ def __init__ (self, name, outname):
+
+ Tex_input.__init__ (self, name) # ugh
+ self.outname = outname
+ self.chapter = 0
+ self.section = 0
+ self.fine_count =0
+ self.mudtex = Tex_output (self.outname)
+ self.mudela = None
+ self.deps = []
+ self.verbatim = 0
+ # set to 'mudela' when we are processing mudela code,
+ # both verbatim and graphic-to-be
+ self.mode = 'latex'
+ def set_sections (self, l):
+ if section_re.search (l):
+ self.section = self.section + 1
+ if chapter_re.search (l):
+ self.section = 0
+ self.chapter = self.chapter + 1
+
+ def gen_basename (self):
+ return '%s-%d.%d.%d' % (self.outname,self.chapter,self.section,self.fine_count)
+ #return '%s/%s-%d.%d.%d' % (outdir, self.outname,self.chapter,self.section,self.fine_count)
+
+ def extract_papersize_from_documentclass(self, line):
+ pre = extract_papersize_re.search(line)
+ if not pre:
+ return None
+ return pre.groups()[0]
+ def extract_fontsize_from_documentclass(self, line):
+ if extract_fontsize1_re.search(line):
+ r = extract_fontsize2_re.search(line)
+ if r:
+ return int(r.groups()[0])
+ def do_it(self):
+ preMudelaDef = postMudelaDef = 0
+ (lines, self.deps) = self.get_lines ()
+ for line in lines:
+ if documentclass_re.search (line):
+ p = self.extract_papersize_from_documentclass (line)
+ if p:
+ Props.setPapersize(p, 'file')
+ f = self.extract_fontsize_from_documentclass (line)
+ if f:
+ Props.setTexFontsize (f, 'file')
+ elif twocolumn_re.search (line):
+ Props.setNumColumn (2, 'file')
+ elif onecolumn_re.search (line):
+ Props.setNumColumn (1, 'file')
+ elif preMudelaExample_re.search (line):
+ preMudelaDef = 1
+ elif postMudelaExample_re.search (line):
+ postMudelaDef = 1
+ elif begin_document_re.search (line):
+ if not preMudelaDef:
+ self.mudtex.write ('\\def\\preMudelaExample{}\n')
+ if not postMudelaDef:
+ self.mudtex.write ('\\def\\postMudelaExample{}\n')
+ elif begin_mudela_re.search (line):
+ Props.clear_for_new_block()
+ if __debug__:
+ if self.mode == 'mudela':
+ raise AssertionError
+ self.mode = 'mudela'
+ r = begin_mudela_opts_re.search (line)
+ if r:
+ o = r.group()[1:][:-1]
+ optlist = re.compile('[ ,]*').split(o)
+ else:
+ optlist = []
+ if ('verbatim' in optlist) or (Props.force_verbatim_b):
+ self.verbatim = 1
+ self.mudtex.open_verbatim ()
+ else:
+ self.verbatim = 0
+ self.mudela = Mudela_output (self.gen_basename ())
+
+ elif end_mudela_re.search (line):
+ if __debug__:
+ if self.mode != 'mudela':
+ raise AssertionError
+ if self.mudela:
+ self.mudela.close ()
+ self.mudtex.write (self.mudela.insert_me_string())
+ del self.mudela
+ self.mudela = None
+ self.fine_count = self.fine_count + 1
+ else:
+ self.mudtex.write (line)
+ self.mudtex.close_verbatim ()
+ self.mode = 'latex'
+ continue
+
+ if self.mode == 'mudela' and not self.verbatim:
+ self.mudela.write (line)
+ else:
+ self.mudtex.write (line)
+ self.set_sections(line)
+ self.mudtex.create_graphics()
+ self.mudtex.write_outfile()
+ del self.mudtex
+
+
+def help():
+ sys.stdout.write("""Usage: mudela-book [options] FILE\n
+Generate hybrid LaTeX input from Latex + mudela
+Options:\n
+ -h, --help print this help
+ -d, --outdir=DIR directory to put generated files
+ -o, --outname=FILE prefix for filenames
+ --default-mudela-fontsize=??pt default fontsize for music
+ --force-mudela-fontsize=??pt force fontsize for all inline mudela
+ --force-verbatim make all mudela verbatim\n"""
+ )
+ sys.exit (0)
+
+
+def write_deps (fn, out, deps):
+ out_fn = outdir + '/' + fn
+ out_fn = re.sub ('//', '/', out_fn)
+ print 'writing `%s\'\n' % out_fn
+
+ f = open (out_fn, 'w')
+ f.write ('%s: %s\n'% (outdir + '/' + out + '.dvi',
+ reduce (lambda x,y: x + ' '+ y, deps)))
+ f.close ()
+
+def identify():
+ sys.stderr.write ('This is %s version %s\n' % ('mudela-book', program_version))
+
+def main():
+ global outdir
+ outname = ''
+ try:
+ (options, files) = getopt.getopt(
+ sys.argv[1:], 'hd:o:', ['outdir=', 'outname=',
+ 'default-mudela-fontsize=',
+ 'force-mudela-fontsize=',
+ 'help', 'dependencies',
+ 'force-verbatim'])
+ except getopt.error, msg:
+ print "error:", msg
+ sys.exit(1)
+
+ do_deps = 0
+ for opt in options:
+ o = opt[0]
+ a = opt[1]
+ if o == '--outname' or o == '-o':
+ if len(files) > 1:
+ #HACK
+ print "Mudela-book is confused by --outname on multiple files"
+ sys.exit(1)
+ outname = a
+ if o == '--outdir' or o == '-d':
+ outdir = a
+ if o == '--help' or o == '-h':
+ help ()
+ if o == '--dependencies':
+ do_deps = 1
+ if o == '--default-mudela-fontsize':
+ if not fontsize_pt2i.has_key(a):
+ print "Error: illegal fontsize:", a
+ print " accepted fontsizes are: 11pt, 13pt, 16pt, 20pt, 26pt"
+ sys.exit()
+ Props.setMudelaFontsize(fontsize_pt2i[a], 'init')
+ if o == '--force-mudela-fontsize':
+ if not fontsize_pt2i.has_key(a):
+ print "Error: illegal fontsize:", a
+ print " accepted fontsizes are: 11pt, 13pt, 16pt, 20pt, 26pt"
+ sys.exit()
+ Props.force_mudela_fontsize = fontsize_pt2i[a]
+ if o == '--force-verbatim':
+ Props.force_verbatim_b = 1
+
+ if outdir[-1:] != '/':
+ outdir = outdir + '/'
+
+ if not os.path.isdir(outdir):
+ os.system('mkdir %s' % outdir)
+
+ for input_filename in files:
+ Props.clear_for_new_file()
+ if outname:
+ my_outname = outname
+ else:
+ my_outname = os.path.basename(os.path.splitext(input_filename)[0])
+ my_depname = my_outname + '.dep'
+ inp = Main_tex_input (input_filename, my_outname)
+ inp.do_it ()
+
+
+ if do_deps:
+ write_deps (my_depname, my_outname, inp.deps)
+
+identify()
+Props = Properties()
+main()