Class: Metanorma::Plugin::Lutaml::SourceExtractor
- Inherits:
-
Object
- Object
- Metanorma::Plugin::Lutaml::SourceExtractor
- Includes:
- Utils
- Defined in:
- lib/metanorma/plugin/lutaml/source_extractor.rb
Constant Summary collapse
- ANCHOR_REGEX_1 =
example:
- [[abc]] /^\[\[(?<id>[^\]]*)\]\]\s*$/- ANCHOR_REGEX_2 =
examples:
- [#abc] - [source#abc,ruby] /^\[[^#,]*#(?<id>[^,\]]*)[,\]]/- ANCHOR_REGEX_3 =
examples:
- [id=abc] - [source,id="abc"] /^\[(?:.+,)?id=['"]?(?<id>[^,\]'"]*)['"]?[,\]]/
Constants included from Utils
Class Method Summary collapse
Instance Method Summary collapse
-
#extract ⇒ Object
rubocop:disable Metrics/AbcSize.
- #filename(document, line) ⇒ Object
-
#initialize(document, input_lines) ⇒ SourceExtractor
constructor
A new instance of SourceExtractor.
- #match_anchor(line) ⇒ Object
- #read(inc_path) ⇒ Object
- #read_section(lines) ⇒ Object
- #readlines_safe(file) ⇒ Object
Methods included from Utils
create_liquid_environment, error_signature, load_express_from_folder, load_express_from_index, load_express_repo_from_cache, load_express_repo_from_path, load_express_repositories, notify_render_errors, parse_document_express_indexes, processed_lines, relative_file_path, render_liquid_string, save_express_repo_to_cache
Constructor Details
#initialize(document, input_lines) ⇒ SourceExtractor
Returns a new instance of SourceExtractor.
23 24 25 26 27 28 |
# File 'lib/metanorma/plugin/lutaml/source_extractor.rb', line 23 def initialize(document, input_lines) @document = document @input_lines = input_lines @document.attributes["source_blocks"] ||= {} end |
Class Method Details
.extract(document, input_lines) ⇒ Object
30 31 32 |
# File 'lib/metanorma/plugin/lutaml/source_extractor.rb', line 30 def self.extract(document, input_lines) new(document, input_lines).extract end |
Instance Method Details
#extract ⇒ Object
rubocop:disable Metrics/AbcSize
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/metanorma/plugin/lutaml/source_extractor.rb', line 34 def extract # rubocop:disable Metrics/AbcSize lines = @input_lines.to_enum loop do line = lines.next if /^embed::|^include::/.match?(line.strip) file_lines = read(filename(@document, line)) or next SourceExtractor.extract(@document, file_lines) elsif m = match_anchor(line) @document.attributes["source_blocks"][m[:id]] = read_section lines end end end |
#filename(document, line) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/metanorma/plugin/lutaml/source_extractor.rb', line 68 def filename(document, line) m = /(^include::|^embed::)([^\[]+)\[/.match(line) return nil unless m file_path = relative_file_path(document, m[2]) File.exist?(file_path) ? file_path : nil end |
#match_anchor(line) ⇒ Object
49 50 51 52 53 |
# File 'lib/metanorma/plugin/lutaml/source_extractor.rb', line 49 def match_anchor(line) line.match(ANCHOR_REGEX_1) || line.match(ANCHOR_REGEX_2) || line.match(ANCHOR_REGEX_3) end |
#read(inc_path) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/metanorma/plugin/lutaml/source_extractor.rb', line 61 def read(inc_path) inc_path or return nil ::File.open inc_path, "r" do |fd| readlines_safe(fd).map(&:chomp) end end |
#read_section(lines) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/metanorma/plugin/lutaml/source_extractor.rb', line 77 def read_section(lines) m = lines.next.match(/^--+/) return "" unless m end_mark = m[0] current_section = [] while (line = lines.next) != end_mark current_section << line end current_section.join("\n") end |
#readlines_safe(file) ⇒ Object
55 56 57 58 59 |
# File 'lib/metanorma/plugin/lutaml/source_extractor.rb', line 55 def readlines_safe(file) return [] if file.eof? file.readlines end |