Module: Lydown::CLI::Translation

Defined in:
lib/lydown/cli/translation.rb

Constant Summary collapse

WORK_FILENAME =
'_work.yml'
MOVEMENT_FILENAME =
'_movement.yml'
PATH_MACROS =
{}

Class Method Summary collapse

Class Method Details

.load_macros(path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lydown/cli/translation.rb', line 57

def load_macros(path)
  base_dir = File.directory?(path) ? path : File.dirname(path)
  parent_dir = File.expand_path(File.join(base_dir, '..'))
  
  yml = 
    (YAML.load_file(File.join(parent_dir, WORK_FILENAME)) rescue nil) || 
    (YAML.load_file(File.join(base_dir, WORK_FILENAME)) rescue nil) ||
    {}
  macros = yml['macros'] || {}
  
  yml = 
    (YAML.load_file(File.join(base_dir, MOVEMENT_FILENAME)) rescue nil) ||
    {}
  macros.merge(yml['macros'] || {})
end

.macros_for_path(path) ⇒ Object



53
54
55
# File 'lib/lydown/cli/translation.rb', line 53

def macros_for_path(path)
  PATH_MACROS[path] ||= load_macros(path)
end

.process(opts) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/lydown/cli/translation.rb', line 5

def process(opts)
  if File.directory?(opts[:path])
    process_directory(opts[:path])
  elsif File.file?(opts[:path])
    process_file(opts[:path])
  elsif File.file?(opts[:path] + '.rpl')
    process_file(opts[:path] + '.rpl')
  end
end

.process_directory(path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/lydown/cli/translation.rb', line 15

def process_directory(path)
  macros = load_macros(path)
  Dir["#{path}/*"].entries.each do |entry|
    if File.file?(entry) && (entry =~ /\.rpl$/)
      process_file(entry)
    elsif File.directory?(entry)
      process_directory(entry)
    end
  end
end

.process_file(path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lydown/cli/translation.rb', line 26

def process_file(path)
  ext = File.extname(path)
  base_path = ext == '.rpl' ? path.gsub(/#{ext}$/, '') : path
  output_path = base_path + '.ld'
  lyrics_path = base_path + '.lyr'
  figures_path = base_path + '.fig'
  
  $stderr.puts "Translating #{path}..."
  
  code = {
    path: path,
    ripple: IO.read(path),
    lyrics: File.file?(lyrics_path) && IO.read(lyrics_path),
    figures: File.file?(figures_path) && IO.read(figures_path),
    macros: macros_for_path(path)
  }
  
  ld_code = Lydown::Translation.process(code)

  File.open(output_path, 'w+') {|f| f.write(ld_code)}
end