Module: LilyPond

Defined in:
lib/lilypond/builder.rb,
lib/lilypond-ruby.rb,
lib/lilypond/config.rb

Overview

The LilyPond module provides an interface for generating LilyPond notation.

Defined Under Namespace

Classes: Builder, Configuration

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configuration {|@configuration| ... } ⇒ Object

Returns the value of attribute configuration.

Yields:



5
6
7
# File 'lib/lilypond/config.rb', line 5

def configuration
  @configuration
end

Class Method Details

.generate_with_lilypond(file_name, lilypond_code, output_type = nil) ⇒ Object

Approved output types: ‘[:pdf, :svg, :png]` If none is provided, it defaults to LilyPond.configuration.default_output, which initializes as `:pdf`



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lilypond-ruby.rb', line 28

def generate_with_lilypond(file_name, lilypond_code, output_type = nil)
  tempfile = Tempfile.new(file_name)
  tempfile.write(lilypond_code)
  tempfile.close
  output_type ||= default_output
  Dir.chdir(destination) do
    Open3.popen3(path, "--#{output_type}", tempfile.path) do |stdin, stdout, stderr, wait_thr|
      # Write the Lilypond code to stdin
      stdin.write(lilypond_code)
      stdin.close

      # Wait for the process to complete
      Process.detach(wait_thr.pid)

      # Read and process the output and error streams
      loop do
        # Wait for output to become available for reading
        ready = IO.select([stdout, stderr])
        next unless ready

        # Read available data from the streams
        ready[0].each do |stream|
          data = stream.read_nonblock(1024)
          puts data # or process the data as necessary
        end
      rescue IO::WaitReadable, IO::WaitWritable
        # Continue waiting if the streams are not yet ready
        IO.select([stdout, stderr])
        retry
      rescue EOFError
        # Stop waiting if the streams have been closed
        break
      end
    end
  end
  File.delete(tempfile.path)
end

.versionObject



8
9
10
11
12
13
14
15
# File 'lib/lilypond-ruby.rb', line 8

def version
  output, error, status = Open3.capture3(path, "--version")
  if status.success?
    puts output
  else
    puts error
  end
end

.version_numberObject



17
18
19
20
21
22
23
24
# File 'lib/lilypond-ruby.rb', line 17

def version_number
  output, error, status = Open3.capture3(path, "--version")
  if status.success?
    return output.match(/GNU LilyPond (\d+\.\d+\.\d+)/)[1]
  else
    return "#{error}"
  end
end