md_metadata_extractor 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 50071436090d012df8b8310ee9c0063c25486c8792856d9921f20870b7416cc1
4
- data.tar.gz: 6a1c992cc84bd4a96d6a6adf77ea17f268eef3c2d8a7df88bb0c687a7246810b
3
+ metadata.gz: a9f0b91416a5b9ae2f53c23a870285a0a2a9866558cbf84b4203c413084b32b4
4
+ data.tar.gz: dde346391a2ad7a160a8c710b16aa68b625f55e76e084e90e426623389dfa9d7
5
5
  SHA512:
6
- metadata.gz: ee81b949e9e842fc947290ac662059424c52f95c3ec53d81f0e35fc4cb7a84baacda45dae4384768a18df1c3a8bb285e8b3b3eea425fee99c2ef7aa73fa2711d
7
- data.tar.gz: 1b504b93576c7bed3f3787b722b33b304a13f54aebbf0a93dc631837dc4a0588e72f62b39f8e9021938a92c1dd0248c95eb361b3f7e195245e167c84689df10b
6
+ metadata.gz: 14c93f64ddbd586b8517910a6b8dbf0b34a6e6a9033c37425cb36066d9e7be5fd75627a5e8f4d31cef87adbfec07cb97c73013aeef67dbcd67e1c8c68adc84f8
7
+ data.tar.gz: 69be39484b1196899e19bdfa805cdaa5c7de644ecd90acad95b1baeedb0c660867acd0f680b54cc3ee53e73782e1de33e4c23be381a9439dd29cd9ed6eb35957
@@ -2,40 +2,52 @@ require "pp"
2
2
  module MdMetadata
3
3
  class Extractor
4
4
  attr_accessor :mode
5
- def initialize
5
+ def initialize(mode = :hugo)
6
6
  # Define input data
7
7
  @input = ""
8
- @result = {}
9
- @mode = :hugo
8
+ @result = { :metadata => {} , :content => ""}
9
+ @mode = mode
10
+ end
11
+ def metadata_regex
12
+ /#{Regexp.quote(delimiter)}(.*)#{Regexp.quote(delimiter)}/m
13
+ end
14
+ def content_regex
15
+ /#{Regexp.quote(delimiter)}.*#{Regexp.quote(delimiter)}(.*)\z/m
16
+ end
17
+ def delimiter
18
+ case @mode
19
+ when :hugo
20
+ "+++"
21
+ when :jekyll
22
+ "---"
23
+ end
10
24
  end
11
25
 
12
- ##
13
- # Loads an input string for processing, returns a hash of values
14
- # based on said metadata
15
- def load_md(input)
16
- @input = input
17
- @metadata = ""
26
+ def separator
18
27
  case @mode
19
28
  when :hugo
20
- @metadata = /\+\+\+(.*)\+\+\+/m.match(@input).to_s.delete("+++")
29
+ "="
21
30
  when :jekyll
22
- @metadata = /\-\-\-(.*)\-\-\-/m.match(@input).to_s.delete("---")
31
+ ":"
23
32
  end
33
+ end
34
+
35
+ ##
36
+ # Loads an input string for processing, returns a hash of values
37
+ # based on said metadata
38
+ def extract(input)
39
+ raise "Malformed input file" unless metadata_regex.match?(input)
40
+ @metadata = metadata_regex.match(input).to_s.delete(delimiter)
24
41
  @metadata.each_line do |line|
25
- case @mode
26
- when :hugo
27
- meta = line.split("=")
28
- when :jekyll
29
- meta = line.split(":")
30
- end
42
+ meta = line.split(separator)
31
43
  unless meta[1].nil?
32
44
  meta[0].strip!
33
- meta[1].strip!.gsub! '"', ''
34
- @result[meta[0]] = meta[1]
45
+ meta[1].gsub!( '"', ' ').strip!
46
+ @result[:metadata][meta[0]] = meta[1]
35
47
  end
36
48
  end
49
+ @result[:content] = content_regex.match(input)[-1].to_s.strip!
37
50
  @result
38
51
  end
39
-
40
52
  end
41
53
  end
@@ -8,8 +8,10 @@ class TestExtractor < Minitest::Test
8
8
  ---
9
9
  title : "My Title"
10
10
  date : "2014-11-17 19:38:31 +0000 UTC"
11
+
11
12
  description : "here goes the post description"
12
13
  keywords : "here goes the metadata"
14
+
13
15
  ---
14
16
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
15
17
  MD
@@ -22,18 +24,38 @@ class TestExtractor < Minitest::Test
22
24
  +++
23
25
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
24
26
  MD
27
+ @testCaseBroken = <<-MD
28
+ +-+
29
+ title = "My Title"
30
+ date "2014-11-17 19:38:31 +0000 UTC" =
31
+ description = "here goes the post description"
32
+ keywords = "here goes the metadata"
33
+ +++
34
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit.
35
+ MD
25
36
  end
26
- def test_load_md
27
- output = @importer.load_md @testCaseHugo
37
+ def test_extract
38
+ output = @importer.extract @testCaseHugo
28
39
  assert_instance_of Hash, output
29
40
  end
30
41
  def test_extracts_title_from_hugo_metadata
31
- output = @importer.load_md @testCaseHugo
32
- assert_equal "My Title", output["title"]
42
+ output = @importer.extract @testCaseHugo
43
+ assert_equal "My Title", output[:metadata]["title"]
33
44
  end
34
45
  def test_extracts_title_from_jekyll_metadata
35
46
  @importer.mode = :jekyll
36
- output = @importer.load_md @testCaseJekyll
37
- assert_equal "My Title", output["title"]
38
- end
47
+ output = @importer.extract @testCaseJekyll
48
+ assert_equal "My Title", output[:metadata]["title"]
49
+ end
50
+ def test_extracts_content_from_hugo_md
51
+ output = @importer.extract @testCaseHugo
52
+ assert_equal "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", output[:content]
53
+ end
54
+
55
+
56
+ def test_raises_exception_on_malformed_file
57
+ assert_raises RuntimeError do
58
+ output = @importer.extract @testCaseBroken
59
+ end
60
+ end
39
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: md_metadata_extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Francisco Giménez Silva