-def determine_format (str):
- global format
- if format == '':
- html = re.search ('(?i)<[dh]tml', str[:200])
- latex = re.search (r'''\\document''', str[:200])
- texi = re.search ('@node|@setfilename', str[:200])
-
- f = ''
- g = None
-
- if html and not latex and not texi:
- f = 'html'
- elif latex and not html and not texi:
- f = 'latex'
- elif texi and not html and not latex:
- f = 'texi'
- else:
- error ("can't determine format, please specify")
- format = f
-
- global paperguru
- if paperguru == None:
- if format == 'html':
- g = HtmlPaper ()
- elif format == 'latex':
- g = LatexPaper ()
- elif format == 'texi':
- g = TexiPaper ()
-
- paperguru = g
-
-
-def read_doc_file (filename):
- '''Read the input file, find verbatim chunks and do \input and \include
- '''
- (str, path) = find_file (filename)
- determine_format (str)
-
- chunks = [('input', str)]
-
- # we have to check for verbatim before doing include,
- # because we don't want to include files that are mentioned
- # inside a verbatim environment
- chunks = chop_chunks (chunks, 'verbatim', make_verbatim)
-
- chunks = chop_chunks (chunks, 'verb', make_verb)
- chunks = chop_chunks (chunks, 'multiline-comment', do_ignore)
- #ugh fix input
- chunks = chop_chunks (chunks, 'include', do_include_file, 1)
- chunks = chop_chunks (chunks, 'input', do_input_file, 1)
- return chunks
-
-
-taken_file_names = {}
-
-def unique_file_name (body):
- return 'lily-' + `abs (hash (body))`
-
-def schedule_lilypond_block (chunk):
- '''Take the body and options from CHUNK, figure out how the
- real .ly should look, and what should be left MAIN_STR (meant
- for the main file). The .ly is written, and scheduled in
- TODO.
-
- Return: multiple chunks.
-
- The chunk pertaining to the lilypond output
- has the format (TYPE_STR, MAIN_STR, OPTIONS, TODO, BASE),
- where TODO has format [basename, extension, extension, ... ]
- '''
-
- return_chunks = []
-
- (type, body, opts) = chunk
- assert type == 'lilypond'
- file_body = compose_full_body (body, opts)
- ## Hmm, we should hash only lilypond source, and skip the
- ## %options are ...
- ## comment line
- basename = unique_file_name (file_body)
- for o in opts:
- m = re.search ('filename="(.*?)"', o)
- if m:
- basename = m.group (1)
- if not taken_file_names.has_key (basename):
- taken_file_names[basename] = 0
- else:
- taken_file_names[basename] = taken_file_names[basename] + 1
- basename = basename + "-%i" % taken_file_names[basename]
- update_file (file_body, os.path.join (g_outdir, basename) + '.ly')
- needed_filetypes = ['tex']
-
- if format == 'html' or g_make_html:
- needed_filetypes.append ('eps')
- needed_filetypes.append ('png')
- if 'eps' in opts and not ('eps' in needed_filetypes):
- needed_filetypes.append ('eps')
-
- pathbase = os.path.join (g_outdir, basename)
- def f (base, ext1, ext2):
- a = os.path.isfile (base + ext2)
- if (os.path.isfile (base + ext1) and
- os.path.isfile (base + ext2) and
- os.stat (base+ext1)[stat.ST_MTIME] >
- os.stat (base+ext2)[stat.ST_MTIME]) or \
- not os.path.isfile (base + ext2):
- return 1
- todo = []
- if 'tex' in needed_filetypes and f (pathbase, '.ly', '.tex'):
- todo.append ('tex')
- if 'eps' in needed_filetypes and f (pathbase, '.tex', '.eps'):
- todo.append ('eps')
- if 'png' in needed_filetypes and f (pathbase, '.eps', '.png'):
- todo.append ('png')
- newbody = ''
-
- filename_chunk = None
- if 'printfilename' in opts:
- for o in opts:
- m= re.match ("filename=(.*)", o)
- if m:
- template = get_output ("output-filename")
- b = basename + '.ly'
- human_base = os.path.basename (m.group (1))
-
- ## todo: include path, but strip
- ## first part of the path.
- filename_chunk = ('input', template % (human_base, b,human_base))
- break
-
-
- if 'smallverbatim' in opts:
- newbody += output_verbatim (body, 1)
- elif 'verbatim' in opts:
- newbody += output_verbatim (body, 0)
-
- for o in opts:
- m = re.search ('intertext="(.*?)"', o)
- if m:
- newbody = newbody + "\n"
- if format == 'texi':
- newbody = newbody + "@noindent\n"
- elif format == 'latex':
- newbody = newbody + "\\noindent\n"
- newbody = newbody + m.group (1) + "\n"
-
- if 'noinline' in opts:
- s = 'output-noinline'
- elif format == 'latex':
- if 'eps' in opts:
- s = 'output-eps'
- else:
- if 'quote' in opts:
- s = 'output-latex-quoted'
- else:
- s = 'output-latex-noquote'
- elif format == 'texi':
- if 'quote' in opts:
- s = 'output-texi-quoted'
- else:
- s = 'output-texi-noquote'
- else: # format == 'html'
- s = 'output-html'
- newbody = newbody + get_output (s) % {'fn': basename }