Class: DocTemplate::Template
- Inherits:
-
Object
- Object
- DocTemplate::Template
- Defined in:
- lib/doc_template/template.rb
Defined Under Namespace
Classes: TagRegistry
Instance Attribute Summary collapse
-
#css_styles ⇒ Object
readonly
Returns the value of attribute css_styles.
-
#metadata_service ⇒ Object
readonly
Returns the value of attribute metadata_service.
-
#toc ⇒ Object
readonly
Returns the value of attribute toc.
Class Method Summary collapse
- .parse(source, type: :document) ⇒ Object
- .register_tag(name, klass) ⇒ Object
- .tags ⇒ Object
- .unregister_tag(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(type = :document) ⇒ Template
constructor
A new instance of Template.
- #material? ⇒ Boolean
- #metadata ⇒ Object
- #parse(source) ⇒ Object
- #parts ⇒ Object
- #prereq? ⇒ Boolean
- #remove_part(type, context_type) ⇒ Object
- #render(options = {}) ⇒ Object
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_styles ⇒ Object (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_service ⇒ Object (readonly)
Returns the value of attribute metadata_service.
34 35 36 |
# File 'lib/doc_template/template.rb', line 34 def @metadata_service end |
#toc ⇒ Object (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) [key_for(name)] = klass end |
.tags ⇒ Object
45 46 47 |
# File 'lib/doc_template/template.rb', line 45 def @tags ||= TagRegistry.new end |
.unregister_tag(name) ⇒ Object
49 50 51 |
# File 'lib/doc_template/template.rb', line 49 def unregister_tag(name) .delete(key_for(name)) end |
Instance Method Details
#material? ⇒ Boolean
67 68 69 |
# File 'lib/doc_template/template.rb', line 67 def material? @type.to_sym == :material end |
#metadata ⇒ Object
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| = @metadata_service. context_type @documents[context_type] = ::DocTemplate::Document.parse(@content.dup, ) @documents[context_type].parts << { content: render(), context_type: context_type, data: {}, materials: [], optional: false, part_type: :layout, placeholder: nil } @toc ||= ::DocTemplate::DocumentToc.parse() unless material? end self end |
#parts ⇒ Object
110 111 112 |
# File 'lib/doc_template/template.rb', line 110 def parts @documents.values.flat_map(&:parts) end |
#prereq? ⇒ 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( = {}) type = .fetch(:context_type, ::DocTemplate.context_types.first) DocTemplate.sanitizer.post_processing(@documents[type]&.render.presence || '', ) end |