Class: MdMetadata::Extractor
- Inherits:
-
Object
- Object
- MdMetadata::Extractor
- Defined in:
- lib/md_metadata_extractor.rb
Instance Attribute Summary collapse
-
#mode ⇒ Object
Returns the value of attribute mode.
Instance Method Summary collapse
- #content_regex ⇒ Object
- #delimiter ⇒ Object
-
#extract(input) ⇒ Object
Loads an input string for processing, returns a hash of values based on said metadata.
-
#initialize(mode = :hugo) ⇒ Extractor
constructor
A new instance of Extractor.
- #metadata_regex ⇒ Object
- #separator ⇒ Object
Constructor Details
#initialize(mode = :hugo) ⇒ Extractor
Returns a new instance of Extractor.
5 6 7 8 9 10 |
# File 'lib/md_metadata_extractor.rb', line 5 def initialize(mode = :hugo) # Define input data @input = "" @result = { :metadata => {} , :content => ""} @mode = mode end |
Instance Attribute Details
#mode ⇒ Object
Returns the value of attribute mode.
4 5 6 |
# File 'lib/md_metadata_extractor.rb', line 4 def mode @mode end |
Instance Method Details
#content_regex ⇒ Object
14 15 16 |
# File 'lib/md_metadata_extractor.rb', line 14 def content_regex /#{Regexp.quote(delimiter)}.*#{Regexp.quote(delimiter)}(.*)\z/m end |
#delimiter ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/md_metadata_extractor.rb', line 17 def delimiter case @mode when :hugo "+++" when :jekyll "---" end end |
#extract(input) ⇒ Object
Loads an input string for processing, returns a hash of values based on said metadata
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/md_metadata_extractor.rb', line 38 def extract(input) raise "Malformed input file" unless .match?(input) @metadata = .match(input).to_s.delete(delimiter) @metadata.each_line do |line| = line.split(separator) unless [1].nil? [0].strip! [1].gsub!( '"', ' ').strip! @result[:metadata][[0]] = [1] end end @result[:content] = content_regex.match(input)[-1].to_s.strip! @result end |
#metadata_regex ⇒ Object
11 12 13 |
# File 'lib/md_metadata_extractor.rb', line 11 def /#{Regexp.quote(delimiter)}(.*)#{Regexp.quote(delimiter)}/m end |
#separator ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/md_metadata_extractor.rb', line 26 def separator case @mode when :hugo "=" when :jekyll ":" end end |