Skip to content

Commit

Permalink
support for gregoriotex version 5
Browse files Browse the repository at this point in the history
ref #2
  • Loading branch information
igneus committed May 13, 2020
1 parent a6a4a80 commit ee19f8c
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/gly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Gly; end
preview_generator
preview_builder
lister
tags
gregorio_version_detector
string_helpers).each do |mod|
require "gly/#{mod}"
end
1 change: 1 addition & 0 deletions lib/gly/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def gabc(*files)
option :no_document, type: :boolean, aliases: :D, banner: 'produce main LaTeX file without document definition; in this case --no-build is applied automatically'
option :full_headers, type: :boolean, aliases: :H, banner: 'include full document and score headers'
option :template, aliases: :t, banner: 'use custom document template'
option :gregoriotex_version, aliases: :g, banner: 'for which gregoriotex version [4 and 5 supported] to generate gregoriotex commands'
def preview(*files)
tpl = nil
if options[:template]
Expand Down
20 changes: 20 additions & 0 deletions lib/gly/gregorio_version_detector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Gly
class GregorioVersionDetector
# Tries to determine version of available Gregorio.
def self.version
begin
version_output = `gregorio --version`
rescue Errno::ENOENT
# 'gregorio' command not available
return nil
end

version_output.match(/^Gregorio (\d+\.\d+\.\d+)/) do |m|
return Gem::Version.new(m[1])
end

# failed to parse gregorio version from the output
nil
end
end
end
19 changes: 11 additions & 8 deletions lib/gly/preview_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ module Gly
# Takes Gly::Document, builds a pdf preview
# (or at least generates all necessary assets)
class PreviewGenerator
# For which gregoriotex version to generate commands by default
DEFAULT_GREGORIOTEX_VERSION = 5

def initialize(**options)
@preview_dest = nil

@template = options.delete(:template) || default_template
@builder = options.delete(:builder) || PreviewBuilder.new
@options = options.delete(:options) || {}
@tags = Gly::Tags[
options[:gregoriotex_version] ||
GregorioVersionDetector.version ||
DEFAULT_GREGORIOTEX_VERSION
]
end

# IO to which the main LaTeX document should be written.
Expand Down Expand Up @@ -82,17 +90,12 @@ def render_score(score, gabc_fname, gtex_fname)
val = "\\texttt{\\##{val}}" if val && m == 'id'
val
end.delete_if(&:nil?).join ', '
r.puts "\\commentary{\\footnotesize{#{piece_title}}}\n" unless piece_title.empty?
r.puts @tags.commentary(piece_title) unless piece_title.empty?

annotations = score.headers.each_value('annotation')
begin
r.puts "\\setfirstannotation{#{annotations.next}}"
r.puts "\\setsecondannotation{#{annotations.next}}"
rescue StopIteration
# ok, no more annotations
end
r.puts @tags.annotations(annotations.next, annotations.next)

r.puts "\\includescore{#{gtex_fname}}\n\\vspace{1cm}"
r.puts @tags.score(gtex_fname)

r.string
end
Expand Down
54 changes: 54 additions & 0 deletions lib/gly/tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Gly
# Contains "strategies" for generation of GregorioTeX
# commands depending on Gregorio version.
module Tags
# Receives Gregorio version (as `Gem::Version` instance or number),
# returns strategy instance.
def self.[](version)
is4 =
if version.is_a?(Gem::Version)
Gem::Version.new('5') > version
else
5 > version
end

if is4
return Gregorio4.new
end

return Gregorio5.new
end

class Gregorio4
def commentary(str)
"\\commentary{\\footnotesize{#{str}}}\n"
end

def annotations(first, second)
"\\setfirstannotation{#{first}}" +
"\\setsecondannotation{#{second}}"
end

def score(filename)
# TODO: space between scores should not be hardcoded this way
"\\includescore{#{filename}}\n\\vspace{1cm}"
end
end

class Gregorio5
def commentary(str)
"\\grecommentary{\\footnotesize{#{str}}}\n"
end

def annotations(first, second)
"\\greannotation{#{first}}" +
"\\greannotation{#{second}}"
end

def score(filename)
# TODO: space between scores should not be hardcoded this way
"\\gregorioscore{#{filename}}\n\\vspace{1cm}"
end
end
end
end
1 change: 1 addition & 0 deletions tests/run.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative 'examples'
require_relative 'no_crash'
require_relative 'string_helpers_test'
require_relative 'tags_test'
require_relative 'parser'
require_relative 'music_with_lyrics'
44 changes: 44 additions & 0 deletions tests/tags_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative 'test_helper'

class TagsTest < MiniTest::Test
# save some typing
V = Gem::Version
Gregorio4 = Gly::Tags::Gregorio4
Gregorio5 = Gly::Tags::Gregorio5

examples = [
['gregorio4', V.new('4'), Gregorio4],
['gregorio4_semantic', V.new('4.1.2'), Gregorio4],
['gregorio5', V.new('5'), Gregorio5],
# integer works, too
['integer_4', 4, Gregorio4],
['integer_5', 5, Gregorio5],
# for older versions Gregorio 4 strategy is used
['gregorio1', V.new('1'), Gregorio4],
['gregorio2', V.new('2'), Gregorio4],
['gregorio3', V.new('3'), Gregorio4],
# for versions newer than 5 use Gregorio 5 strategy
['gregorio6', V.new('6'), Gregorio5],
]

examples.each do |e|
name, given, expected = e
define_method "test_#{name}" do
assert_kind_of expected, Gly::Tags[given]
end
end

invalid_examples = [
['nil', nil],
['string', '1.1.0'],
]

invalid_examples.each do |e|
name, given = e
define_method "test_invalid_#{name}" do
assert_raises ArgumentError do
Gly::Tags[given]
end
end
end
end

0 comments on commit ee19f8c

Please sign in to comment.