Class: Metanorma::Plugin::Glossarist::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/plugin/glossarist/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.



12
13
14
15
# File 'lib/metanorma/plugin/glossarist/document.rb', line 12

def initialize
  @content = []
  @bibliographies = []
end

Instance Attribute Details

#bibliographiesObject

Returns the value of attribute bibliographies.



10
11
12
# File 'lib/metanorma/plugin/glossarist/document.rb', line 10

def bibliographies
  @bibliographies
end

#contentObject

Returns the value of attribute content.



10
11
12
# File 'lib/metanorma/plugin/glossarist/document.rb', line 10

def content
  @content
end

#file_systemObject

Returns the value of attribute file_system.



10
11
12
# File 'lib/metanorma/plugin/glossarist/document.rb', line 10

def file_system
  @file_system
end

Instance Method Details

#add_content(content, options = {}) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/metanorma/plugin/glossarist/document.rb', line 17

def add_content(content, options = {})
  @content << if options[:render]
                render_liquid(content)
              else
                content
              end
end

#create_liquid_environmentObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/metanorma/plugin/glossarist/document.rb', line 42

def create_liquid_environment
  ::Liquid::Environment.new.tap do |liquid_env|
    liquid_env.register_tag(
      "with_glossarist_context",
      ::Metanorma::Plugin::Glossarist::Liquid::CustomBlocks::
      WithGlossaristContext,
    )
    liquid_env.register_filter(
      ::Metanorma::Plugin::Glossarist::Liquid::CustomFilters::Filters,
    )
  end
end

#render_liquid(file_content) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/metanorma/plugin/glossarist/document.rb', line 29

def render_liquid(file_content)
  template = ::Liquid::Template
    .parse(file_content, environment: create_liquid_environment)

  template.registers[:file_system] = file_system
  rendered_template = template.render(strict_variables: false,
                                      error_mode: :warn)

  return rendered_template unless template.errors.any?

  raise template.errors.first.cause
end

#to_sObject



25
26
27
# File 'lib/metanorma/plugin/glossarist/document.rb', line 25

def to_s
  @content.compact.join("\n")
end