Sha256: c47df82932cff5a33672d555fcb39fc415a550ec26700a98e9b1ab762047df24

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

require "open3"
require "lilypond/builder"
require "lilypond/config"
require "guile"

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

    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

    # Approved output types: `[:pdf, :svg, :png]` If none is provided, it defaults to
    # LilyPond.configuration.default_output, which initializes as `:pdf`
    def generate_pdf_with_lilypond(file_name, lilypond_code, output_type = nil)
      tempfile = Tempfile.new(file_name)
      tempfile.write(lilypond_code)
      tempfile.close
      output_type ||= LilyPond.configuration.default_output
      Open3.popen3(path, "--#{output}", 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
      File.delete(tempfile.path)
    end

    private
      def path
        LilyPond.configuration.lilypond_path
      end

  end # end self
end # end LilyPond

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
LilyPond-Ruby-0.1.5.2 lib/lilypond-ruby.rb