Class: Gly::PreviewGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/gly/preview_generator.rb

Overview

Takes Gly::Document, builds a pdf preview (or at least generates all necessary assets)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ PreviewGenerator

Returns a new instance of PreviewGenerator.



7
8
9
10
11
12
13
# File 'lib/gly/preview_generator.rb', line 7

def initialize(**options)
  @preview_dest = nil

  @template = options.delete(:template) || default_template
  @builder = options.delete(:builder) || PreviewBuilder.new
  @options = options.delete(:options) || {}
end

Instance Attribute Details

#preview_destObject

IO to which the main LaTeX document should be written. If not set, a file will be created with name based on the source file name.



18
19
20
# File 'lib/gly/preview_generator.rb', line 18

def preview_dest
  @preview_dest
end

Instance Method Details

#process(document) ⇒ Object



20
21
22
23
24
25
26
27
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
# File 'lib/gly/preview_generator.rb', line 20

def process(document)
  convertor = DocumentGabcConvertor.new(document, **@options)
  convertor.convert

  doc_body = fw = StringIO.new

  if @options[:full_headers]
    fw.puts header_table document.header
  end

  scores_with_names = convertor.each_score_with_gabcname

  document.content.each do |c|
    if c.is_a? Markup
      fw.puts render_markup(c)
    else
      score, gabc_fname, gtex_fname = scores_with_names.next
      fw.puts render_score(score, gabc_fname, gtex_fname)
    end
  end

  if @options['no_document']
    tex = doc_body.string
  else
    replacements = {
      'glyvars' => header_variables(document.header),
      'body' => doc_body.string
    }
    tex = @template.gsub(/\{\{(\w+)\}\}/) {|m| replacements[m[2..-3]] }
  end

  with_preview_io(document.path) do |fw|
    @builder.main_tex = fw.path if fw.respond_to? :path

    fw.puts tex
  end

  build_disabled = @options['no_build'] || @options['no_document']
  if @builder.main_tex && !build_disabled
    @builder.build
  end
end