Class: Wikimovia::TemplateExtractor

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/wikimovia/template_extractor.rb

Instance Method Summary collapse

Methods included from Utilities

#extract_pattern, template_regex

Instance Method Details

#extract_first(string, options = {}) ⇒ Object

Given some wikitext syntax, and a :template_name, extracts the first template with that name it can find and converts it to a Hash.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wikimovia/template_extractor.rb', line 7

def extract_first(string, options = {})
  pattern = Wikimovia::Utilities.template_regex options[:template_name],
    use_vertical_bar: false

  wikitext = WikiCloth::Parser.new(data: string).to_wikitext
  template = extract_pattern(pattern, wikitext)

  if template
    info_lines     = extract_info_lines(template)
    split_lines    = split_infolines(info_lines)
    cleaned_lines  = clean_infolines(split_lines)
    filtered_lines = filter_infolines(cleaned_lines)
    hash = Hash[*filtered_lines.flatten]
    hash.keys.each do |key|
      hash[(key.to_sym rescue key) || key] = hash.delete(key)
    end
    hash
  end
end