Module: Lite::Utils::MdCode::Extraction

Defined in:
lib/lite/utils/md_code/extraction.rb

Class Method Summary collapse

Class Method Details

.extract(path, dir, tag) ⇒ Object



11
12
13
# File 'lib/lite/utils/md_code/extraction.rb', line 11

def self.extract(path, dir, tag)
  extract_each(Markly.parse(read(path, dir)), tag)
end

.extract_each(doc, tag) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lite/utils/md_code/extraction.rb', line 15

def self.extract_each(doc, tag)
  specs = {}
  regex = Regexp.new("^\\S+\\s+#{tag}\\s+(\\S+)$")

  doc.walk do |node|
    next unless node.type == :code_block
    next unless (match = regex.match(node.fence_info))

    key = match[1].to_sym
    raise Error, "Duplicate #{tag} key: #{key}" if specs.key?(key)

    specs.store(key, node.string_content.freeze)
  end

  specs.freeze
end

.full_path(path, dir) ⇒ Object



36
37
38
# File 'lib/lite/utils/md_code/extraction.rb', line 36

def self.full_path(path, dir)
  dir ? Pathname.new(dir).join(path) : Pathname.new(path)
end

.read(path, dir) ⇒ Object



32
33
34
# File 'lib/lite/utils/md_code/extraction.rb', line 32

def self.read(path, dir)
  full_path(path, dir).read
end