Class: Giblish::ExpandAdoc
- Inherits:
-
Object
- Object
- Giblish::ExpandAdoc
- Defined in:
- lib/giblish/search/expand_adoc.rb
Overview
Expands any ‘include’ preprocessor directives found in the given document and merges the the lines in the included document with the ones from the including document. Nesting of includes is supported to the given level.
NOTE: Only includes of asciidoc files are supported, other includes (eg url) are silently dropped.
Constant Summary collapse
- INCLUDE_DIRECTIVE_REGEX =
/^(\\)?include::([^\[][^\[]*)\[(.+)?\]$/
Instance Method Summary collapse
-
#initialize(document, target_lines, max_depth = 3) ⇒ ExpandAdoc
constructor
A new instance of ExpandAdoc.
-
#replace_attrs(attrs, line) ⇒ Object
replace a_doc_attr with the value of the attribute.
- #resolve_include_path(document, target, attrlist) ⇒ Object
- #wash_line(document, line) ⇒ Object
Constructor Details
#initialize(document, target_lines, max_depth = 3) ⇒ ExpandAdoc
Returns a new instance of ExpandAdoc.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/giblish/search/expand_adoc.rb', line 13 def initialize(document, target_lines, max_depth = 3) source_lines = document.reader.source_lines source_lines.each do |line| if INCLUDE_DIRECTIVE_REGEX =~ line next unless max_depth > 0 p = resolve_include_path(document, $2, $3) next if p.nil? sub_doc = Asciidoctor.load_file(p, {parse: false, safe: :unsafe}) ExpandAdoc.new(sub_doc, target_lines, max_depth - 1) else target_lines << wash_line(document, line) end end end |
Instance Method Details
#replace_attrs(attrs, line) ⇒ Object
replace a_doc_attr with the value of the attribute
46 47 48 49 50 51 52 53 |
# File 'lib/giblish/search/expand_adoc.rb', line 46 def replace_attrs(attrs, line) # find all '{...}' occurrences m_arr = line.scan(/\{\w+\}/) # replace each found occurence with its doc attr if exists m_arr.inject(line) do |memo, match| attrs.key?(match[1..-2]) ? memo.gsub(match.to_s, attrs[match[1..-2]]) : memo end end |
#resolve_include_path(document, target, attrlist) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/giblish/search/expand_adoc.rb', line 30 def resolve_include_path(document, target, attrlist) target = replace_attrs(document.attributes, target) parsed_attrs = document.parse_attributes attrlist, [], sub_input: true # use an asciidoctor-internal method to resolve the path in an attempt to keep compatibility inc_path, target_type, _relpath = document.reader.send(:resolve_include_path, target, attrlist, parsed_attrs) return nil unless target_type == :file inc_path end |
#wash_line(document, line) ⇒ Object
41 42 43 |
# File 'lib/giblish/search/expand_adoc.rb', line 41 def wash_line(document, line) replace_attrs(document.attributes, line) end |