Esempio n. 1
0
    def test_basic_texi(self):
        formatter = book_texinfo.BookTexinfoOutputFormat()
        global_options = Dummy()
        global_options.information = {"program_version": "1.2.3"}
        chunks = book_base.find_toplevel_snippets(
            r"""\input texinfo @c -*- coding: utf-8; mode: texinfo; -*- 1
@setfilename texinfo-include-file.info 2
@settitle Include lilypond files in texinfo 3
4
@node Top 5
@top Include lilypond files in texinfo 6

Lilypond files included in texinfo without any options: 8

@lilypondfile{input/regression/les-nereides.ly}

From a subdirectory: 12

@lilypondfile{input/regression/morgenlied.ly}

Within a lilypond block: 16

@lilypond
 % \include "include/myvar.ily"
  \relative c'' { c e g }
@end lilypond

Include a file that includes a file:
""", formatter, global_options)

        # comment
        types = [
            book_snippets.Substring,
            book_snippets.Snippet,  # @c
            book_snippets.Substring,
            book_snippets.LilypondFileSnippet,
            book_snippets.Substring,
            book_snippets.LilypondFileSnippet,
            book_snippets.Substring,
            book_snippets.LilypondSnippet,
            book_snippets.Substring
        ]
        for i in range(0, len(types)):
            self.assertIsInstance(chunks[i], types[i])
        self.assertEqual(chunks[7].line_number, 18)
Esempio n. 2
0
def do_file(input_filename, included=False):
    # Ugh.
    input_absname = input_filename
    if not input_filename or input_filename == '-':
        in_handle = sys.stdin
        input_fullname = '<stdin>'
    else:
        if os.path.exists(input_filename):
            input_fullname = input_filename
        else:
            input_fullname = global_options.formatter.input_fullname(
                input_filename)
        # Normalize path to absolute path, since we will change cwd to the output dir!
        # Otherwise, "lilypond-book -o out test.tex" will complain that it is
        # overwriting the input file (which it is actually not), since the
        # input filename is relative to the CWD...
        input_absname = os.path.abspath(input_fullname)

        note_input_file(input_fullname)
        in_handle = codecs.open(input_fullname, 'r', 'utf-8')

    if input_filename == '-':
        global_options.input_dir = os.getcwd()
        input_base = 'stdin'
    elif included:
        input_base = os.path.splitext(input_filename)[0]
    else:
        global_options.input_dir = os.path.split(input_absname)[0]
        input_base = os.path.basename(os.path.splitext(input_filename)[0])

    # don't complain when global_options.output_dir is existing
    if not global_options.output_dir:
        global_options.output_dir = os.getcwd()
    else:
        global_options.output_dir = os.path.abspath(global_options.output_dir)
        os.makedirs(global_options.output_dir, 0o777, exist_ok=True)
        os.chdir(global_options.output_dir)

    output_filename = os.path.join(
        global_options.output_dir,
        input_base + global_options.formatter.default_extension)
    if (os.path.exists(input_filename) and os.path.exists(output_filename)
            and samefile(output_filename, input_absname)):
        error(_("Output would overwrite input file; use --output."))
        exit(2)

    try:
        progress(_("Reading `%s'") % input_absname)
        source = in_handle.read()

        if not included:
            global_options.formatter.init_default_snippet_options(source)

        progress(_("Dissecting..."))
        chunks = book_base.find_toplevel_snippets(source,
                                                  global_options.formatter,
                                                  global_options)

        # Let the formatter modify the chunks before further processing
        chunks = global_options.formatter.process_chunks(chunks)

        if global_options.filter_cmd:
            write_if_updated(output_filename,
                             [c.filter_text() for c in chunks])
        elif global_options.process_cmd:
            do_process_cmd(chunks, input_fullname, global_options)
            progress(_("Compiling `%s'...") % output_filename)
            write_if_updated(output_filename,
                             [s.replacement_text() for s in chunks])

        def process_include(snippet):
            os.chdir(original_dir)
            name = snippet.substring('filename')
            progress(_("Processing include `%s'") % name)
            return do_file(name, included=True)

        include_chunks = list(
            map(process_include, [
                x
                for x in chunks if isinstance(x, book_snippets.IncludeSnippet)
            ]))

        return chunks + reduce(lambda x, y: x + y, include_chunks, [])

    except book_snippets.CompileError:
        os.chdir(original_dir)
        progress(_("Removing `%s'") % output_filename)
        raise book_snippets.CompileError