Class: DocTemplate::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/doc_template/template.rb

Defined Under Namespace

Classes: TagRegistry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = :document) ⇒ Template

Returns a new instance of Template.



62
63
64
65
# File 'lib/doc_template/template.rb', line 62

def initialize(type = :document)
  @type = type
  @documents = {}
end

Instance Attribute Details

#css_stylesObject (readonly)

Returns the value of attribute css_styles.



34
35
36
# File 'lib/doc_template/template.rb', line 34

def css_styles
  @css_styles
end

#metadata_serviceObject (readonly)

Returns the value of attribute metadata_service.



34
35
36
# File 'lib/doc_template/template.rb', line 34

def 
  @metadata_service
end

#tocObject (readonly)

Returns the value of attribute toc.



34
35
36
# File 'lib/doc_template/template.rb', line 34

def toc
  @toc
end

Class Method Details

.parse(source, type: :document) ⇒ Object



37
38
39
# File 'lib/doc_template/template.rb', line 37

def parse(source, type: :document)
  new(type).parse(source)
end

.register_tag(name, klass) ⇒ Object



41
42
43
# File 'lib/doc_template/template.rb', line 41

def register_tag(name, klass)
  tags[key_for(name)] = klass
end

.tagsObject



45
46
47
# File 'lib/doc_template/template.rb', line 45

def tags
  @tags ||= TagRegistry.new
end

.unregister_tag(name) ⇒ Object



49
50
51
# File 'lib/doc_template/template.rb', line 49

def unregister_tag(name)
  tags.delete(key_for(name))
end

Instance Method Details

#material?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/doc_template/template.rb', line 67

def material?
  @type.to_sym == :material
end

#metadataObject



71
72
73
# File 'lib/doc_template/template.rb', line 71

def 
  ..data.presence || {}
end

#parse(source) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/doc_template/template.rb', line 75

def parse(source)
  doc = Nokogiri::HTML(source)
  # get css styles from head to keep classes for lists (preserve list-style-type)
  doc = DocTemplate.sanitizer.process_list_styles doc
  @css_styles = DocTemplate.sanitizer.sanitize_css(doc.xpath('//html/head/style/text()').to_s)

  # initial content sanitization
  body_node = ::DocTemplate
                .config['sanitizer'].constantize
                .sanitize(doc.xpath('//html/body/*').to_s)
  @content = Nokogiri::HTML.fragment(body_node)

  @metadata_service = ::DocTemplate
                        .config.dig('metadata', 'service').constantize
                        .parse(@content, material: material?)

  ::DocTemplate.context_types.each do |context_type|
    options = @metadata_service.options_for context_type
    @documents[context_type] = ::DocTemplate::Document.parse(@content.dup, options)
    @documents[context_type].parts << {
      content: render(options),
      context_type: context_type,
      data: {},
      materials: [],
      optional: false,
      part_type: :layout,
      placeholder: nil
    }

    @toc ||= ::DocTemplate::DocumentToc.parse(options) unless material?
  end

  self
end

#partsObject



110
111
112
# File 'lib/doc_template/template.rb', line 110

def parts
  @documents.values.flat_map(&:parts)
end

#prereq?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/doc_template/template.rb', line 114

def prereq?
  ['type'].to_s.casecmp('prereq').zero?
end

#remove_part(type, context_type) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/doc_template/template.rb', line 118

def remove_part(type, context_type)
  result = nil
  @documents.each_key do |k|
    result = @documents[k].parts.detect { |p| p[:part_type] == type && p[:context_type] == context_type }
    @documents[k].parts.delete(result) && break if result.present?
  end
  result
end

#render(options = {}) ⇒ Object



127
128
129
130
# File 'lib/doc_template/template.rb', line 127

def render(options = {})
  type = options.fetch(:context_type, ::DocTemplate.context_types.first)
  DocTemplate.sanitizer.post_processing(@documents[type]&.render.presence || '', options)
end