front-matter 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -10
- data/bin/front_matter +6 -22
- data/lib/front_matter.rb +34 -82
- data/spec/front_matter_spec.rb +27 -23
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 337d37bb9c3faadf64d8c03c86286de9097e4b4e
|
4
|
+
data.tar.gz: 0473934f4b36d31f878889861cbe1ec63a51a7b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b92c9fe14b26de9492a37e5999510b6d205efea8b26f1350e9ae429ac45ae991b26cc3cdc59491526ff46dadccfbc0d772247e62232098a808050ccae5e2b5b
|
7
|
+
data.tar.gz: d8ab7084aa147a7ed9db710a64f7c92db7f60fc1af4b7641ec38573cfad009921a0c6d8b0a118f4558fd0cbbdd61ab26ef06a2b46bc62cd877500fa01c845bff
|
data/README.md
CHANGED
@@ -11,7 +11,7 @@ This gem extracts embeded info (yaml front matters, for example ) in source code
|
|
11
11
|
comments with predefined format:
|
12
12
|
|
13
13
|
1. the leading comment string can be different based on the language.
|
14
|
-
2. the leading column of yaml docs
|
14
|
+
2. the leading column of yaml docs should be aligned to be converted to yaml.
|
15
15
|
|
16
16
|
### Format 1
|
17
17
|
|
@@ -22,26 +22,25 @@ comments with predefined format:
|
|
22
22
|
# Tag : [ ruby, yaml, comment ]
|
23
23
|
# ---
|
24
24
|
|
25
|
-
|
25
|
+
OR
|
26
26
|
|
27
|
-
#
|
27
|
+
# ------------- - ------------------------------------------------------------
|
28
28
|
# Gem : front_matter
|
29
29
|
# Author : Zhao Cai
|
30
30
|
# Email : caizhaoff@gmail.com
|
31
31
|
# Homepage : https://github.com/zhaocai/
|
32
32
|
# Tag : [ ruby, yaml, comment ]
|
33
|
-
# ============= = ============================================================
|
34
|
-
|
35
|
-
OR
|
36
|
-
|
37
33
|
# ------------- - ------------------------------------------------------------
|
34
|
+
|
35
|
+
### Format 2
|
36
|
+
# ---
|
38
37
|
# Gem : front_matter
|
39
38
|
# Author : Zhao Cai
|
40
39
|
# Email : caizhaoff@gmail.com
|
41
|
-
# Homepage : https://github.com/zhaocai/
|
42
40
|
# Tag : [ ruby, yaml, comment ]
|
43
|
-
#
|
41
|
+
#
|
44
42
|
|
43
|
+
... source code
|
45
44
|
|
46
45
|
|
47
46
|
## SYNOPSIS:
|
@@ -55,7 +54,7 @@ fm = FrontMatter.new(:unindent => true, :as_yaml => true)
|
|
55
54
|
file = "README.md"
|
56
55
|
|
57
56
|
|
58
|
-
ap YAML.load(fm.extract_file(file)[
|
57
|
+
ap YAML.load(fm.extract_file(file)[0])
|
59
58
|
|
60
59
|
# {
|
61
60
|
# "Gem" => "front_matter",
|
data/bin/front_matter
CHANGED
@@ -9,16 +9,12 @@ def parse_opt()
|
|
9
9
|
|
10
10
|
# optparse definition
|
11
11
|
optparse = OptionParser.new do |opts|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
puts opts
|
18
|
-
exit
|
19
|
-
end
|
12
|
+
opts.on("-u", "--[no-]unindent", "unindent white space") do |v|
|
13
|
+
options[:unindent] = v
|
14
|
+
end
|
15
|
+
opts.on("-y", "--[no-]yaml", "formet output string as yaml") do |v|
|
16
|
+
options[:as_yaml] = v
|
20
17
|
end
|
21
|
-
|
22
18
|
opts.on('-h', '--help', 'Help Message') do
|
23
19
|
puts opts
|
24
20
|
exit
|
@@ -49,7 +45,7 @@ end
|
|
49
45
|
|
50
46
|
|
51
47
|
options = parse_opt()
|
52
|
-
fm = FrontMatter.new()
|
48
|
+
fm = FrontMatter.new(options)
|
53
49
|
|
54
50
|
ARGV.each do |f|
|
55
51
|
if File.readable?(f)
|
@@ -59,15 +55,3 @@ ARGV.each do |f|
|
|
59
55
|
puts "File: #{f} is not readable!"
|
60
56
|
end
|
61
57
|
end
|
62
|
-
|
63
|
-
# begin
|
64
|
-
# comments = fm.extract(f)
|
65
|
-
# rescue IOError => e
|
66
|
-
# puts "Could not write to file"
|
67
|
-
# end
|
68
|
-
# comments.each { |k, v|
|
69
|
-
# File.open(options[:to] + '/' + k, "w") do |file|
|
70
|
-
# file.puts v
|
71
|
-
# end
|
72
|
-
# }
|
73
|
-
|
data/lib/front_matter.rb
CHANGED
@@ -11,119 +11,71 @@
|
|
11
11
|
# =============== ============================================================
|
12
12
|
|
13
13
|
require 'facets/hash'
|
14
|
+
require 'facets/string'
|
14
15
|
require 'front_matter/core/array'
|
15
16
|
|
16
17
|
class FrontMatter
|
17
|
-
VERSION = '1.
|
18
|
-
attr_accessor :
|
18
|
+
VERSION = '1.2.0'
|
19
|
+
attr_accessor :setting
|
19
20
|
def initialize( opts={} )
|
20
21
|
comment_marker = %r{(?<comment> ^\s* \W{1,2} )}x
|
21
|
-
@
|
22
|
+
@setting = {
|
22
23
|
:patterns => {
|
23
24
|
:header => {
|
24
|
-
:comment_marker => comment_marker,
|
25
25
|
:filetype => %r{.*},
|
26
|
-
:
|
27
|
-
|
28
|
-
|
26
|
+
:pattern => %r{
|
27
|
+
(?<start> #{comment_marker} \s* [-=#*]{3,} .* )[\r\n]
|
28
|
+
(?<content> (?: \k<comment> .* [\r\n])+)
|
29
|
+
\k<start>
|
30
|
+
}x
|
29
31
|
},
|
30
32
|
:yaml => {
|
31
|
-
:comment_marker => comment_marker,
|
32
33
|
:filetype => %r{.*},
|
33
|
-
:
|
34
|
-
|
35
|
-
|
34
|
+
:pattern => %r{
|
35
|
+
(?<start> #{comment_marker} \s* -{3} )[\r\n]
|
36
|
+
(?<content> (?: \k<comment> .* [\r\n])+)
|
37
|
+
(?<empty>^ \s* $)
|
38
|
+
}x
|
36
39
|
},
|
37
40
|
},
|
38
41
|
:unindent => false ,
|
39
42
|
:as_yaml => false ,
|
40
43
|
}
|
41
|
-
@
|
44
|
+
@setting.merge!(opts)
|
42
45
|
end
|
43
46
|
|
44
|
-
def
|
45
|
-
content={}
|
47
|
+
def extract(contents, filetype=[])
|
46
48
|
unless filetype.empty?
|
47
|
-
patterns = @
|
49
|
+
patterns = @setting[:patterns].select { |kind, pattern|
|
48
50
|
filetype.any { |ft| pattern[:filetype].match(ft)}
|
49
51
|
}
|
50
52
|
else
|
51
|
-
patterns = @
|
53
|
+
patterns = @setting[:patterns]
|
52
54
|
end
|
53
55
|
|
54
|
-
patterns.
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
:valid => [],
|
59
|
-
:invalid => [],
|
60
|
-
:unbound => [],
|
61
|
-
}
|
62
|
-
|
63
|
-
lines.each { |line|
|
64
|
-
if ! in_comment
|
65
|
-
case line
|
66
|
-
when pattern[:start]
|
67
|
-
in_comment = true
|
68
|
-
next
|
69
|
-
end
|
70
|
-
else
|
71
|
-
if pattern[:end] =~ line
|
72
|
-
in_comment = false
|
73
|
-
content[kind].push(in_content)
|
74
|
-
next
|
75
|
-
end
|
76
|
-
|
77
|
-
m = pattern[:content].match(line)
|
78
|
-
begin
|
79
|
-
in_content[:valid].push(m[:content])
|
80
|
-
rescue IndexError,NoMethodError
|
81
|
-
in_content[:invalid].push(line)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
}
|
85
|
-
|
86
|
-
# fail to match ending
|
87
|
-
if in_comment
|
88
|
-
in_content[:unbound] = in_content[:valid]
|
89
|
-
in_content[:valid] = []
|
90
|
-
content[kind].push(in_content)
|
91
|
-
end
|
92
|
-
}
|
93
|
-
|
94
|
-
results = {
|
95
|
-
:valid => [],
|
96
|
-
:invalid => [],
|
97
|
-
:unbound => [],
|
98
|
-
}
|
99
|
-
|
100
|
-
content.each_pair { |kind, v|
|
101
|
-
v.each { |c| c.each_pair { |status, content|
|
102
|
-
results[status].push(content) unless content.empty? }
|
103
|
-
}
|
56
|
+
union_patterns = Regexp.union(patterns.collect{ |k, p| p[:pattern] })
|
57
|
+
results = []
|
58
|
+
contents.mscan(union_patterns).each { |m|
|
59
|
+
results << m[:content].gsub(/^#{Regexp.escape(m[:comment])}/, "")
|
104
60
|
}
|
105
|
-
|
61
|
+
|
62
|
+
results.map! { |r| r.unindent } if @setting[:unindent]
|
63
|
+
results.map! { |r| "---\n#{r}" } if @setting[:as_yaml]
|
106
64
|
|
107
|
-
|
108
|
-
results.traverse! { |k,v|
|
109
|
-
[k, v.map {|i| i.unindent }]
|
110
|
-
}
|
111
|
-
end
|
112
|
-
|
113
|
-
if @options[:as_yaml]
|
114
|
-
results.traverse! { |k,v|
|
115
|
-
[k, v.map {|i| "---\n#{i.join("\n")}"}]
|
116
|
-
}
|
117
|
-
end
|
118
|
-
results
|
65
|
+
return results
|
119
66
|
end
|
120
67
|
|
121
68
|
def extract_file(path, opts={})
|
69
|
+
|
122
70
|
filetype = opts[:filetype] ? opts[:filetype] : []
|
123
|
-
|
124
|
-
|
71
|
+
if opts[:firstline] || opts[:lastline]
|
72
|
+
firstline = opts[:firstline] ? opts[:firstline] - 1 : 0
|
73
|
+
lastline = opts[:lastline] ? opts[:lastline] -1 : -1
|
74
|
+
return extract(File.readlines(path)[firstline..lastline].join("\n"), filetype)
|
75
|
+
else
|
76
|
+
return extract(File.read(path), filetype)
|
77
|
+
end
|
125
78
|
|
126
|
-
return extract_lines(File.readlines(path)[firstline..lastline].map(&:chomp), filetype)
|
127
79
|
end
|
128
80
|
|
129
81
|
end
|
data/spec/front_matter_spec.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require "yaml"
|
3
3
|
describe "Front_Matter" do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
before :all do
|
5
|
+
Dir.chdir("test/")
|
6
|
+
end
|
8
7
|
|
9
8
|
context "Extraction" do
|
10
9
|
before :all do
|
@@ -24,12 +23,12 @@ describe "Front_Matter" do
|
|
24
23
|
valid_content = [
|
25
24
|
" FileName : extract_sh" ,
|
26
25
|
" Desc : extract shell functions from shell script" ,
|
27
|
-
" Author : Zhao Cai <caizhaoff@gmail.com>"
|
26
|
+
" Author : Zhao Cai <caizhaoff@gmail.com>",
|
27
|
+
""
|
28
28
|
]
|
29
|
-
|
30
|
-
|
31
|
-
content[:valid][0].should == valid_content
|
29
|
+
front_matters = @fm.extract(code)
|
32
30
|
|
31
|
+
front_matters[0].should == valid_content.join("\n")
|
33
32
|
end
|
34
33
|
|
35
34
|
it "should extract header front matter" do
|
@@ -44,11 +43,12 @@ describe "Front_Matter" do
|
|
44
43
|
valid_content = [
|
45
44
|
" FileName : extract_sh" ,
|
46
45
|
" Desc : extract shell functions from shell script" ,
|
47
|
-
" Author : Zhao Cai <caizhaoff@gmail.com>"
|
46
|
+
" Author : Zhao Cai <caizhaoff@gmail.com>",
|
47
|
+
""
|
48
48
|
]
|
49
|
-
|
49
|
+
front_matters = @fm.extract(code)
|
50
50
|
|
51
|
-
|
51
|
+
front_matters[0].should == valid_content.join("\n")
|
52
52
|
|
53
53
|
end
|
54
54
|
|
@@ -66,10 +66,11 @@ describe "Front_Matter" do
|
|
66
66
|
" FileName : extract_sh" ,
|
67
67
|
" Desc : extract shell functions from shell script" ,
|
68
68
|
" Author : Zhao Cai <caizhaoff@gmail.com>",
|
69
|
+
""
|
69
70
|
]
|
70
|
-
|
71
|
+
front_matters = @fm.extract(code)
|
71
72
|
|
72
|
-
|
73
|
+
front_matters[0].should == valid_content.join("\n")
|
73
74
|
end
|
74
75
|
end
|
75
76
|
|
@@ -93,11 +94,12 @@ describe "Front_Matter" do
|
|
93
94
|
valid_content = [
|
94
95
|
"FileName : extract_sh" ,
|
95
96
|
"Desc : extract shell functions from shell script" ,
|
96
|
-
"Author : Zhao Cai <caizhaoff@gmail.com>"
|
97
|
+
"Author : Zhao Cai <caizhaoff@gmail.com>",
|
98
|
+
""
|
97
99
|
]
|
98
|
-
|
100
|
+
front_matters = @fm.extract(code)
|
99
101
|
|
100
|
-
|
102
|
+
front_matters[0].should == valid_content.join("\n")
|
101
103
|
|
102
104
|
end
|
103
105
|
|
@@ -113,11 +115,12 @@ describe "Front_Matter" do
|
|
113
115
|
valid_content = [
|
114
116
|
"FileName : extract_sh" ,
|
115
117
|
"Desc : extract shell functions from shell script" ,
|
116
|
-
"Author : Zhao Cai <caizhaoff@gmail.com>"
|
118
|
+
"Author : Zhao Cai <caizhaoff@gmail.com>",
|
119
|
+
""
|
117
120
|
]
|
118
|
-
|
121
|
+
front_matters = @fm.extract(code)
|
119
122
|
|
120
|
-
|
123
|
+
front_matters[0].should == valid_content.join("\n")
|
121
124
|
|
122
125
|
end
|
123
126
|
|
@@ -135,10 +138,11 @@ describe "Front_Matter" do
|
|
135
138
|
"FileName : extract_sh" ,
|
136
139
|
"Desc : extract shell functions from shell script" ,
|
137
140
|
"Author : Zhao Cai <caizhaoff@gmail.com>",
|
141
|
+
""
|
138
142
|
]
|
139
|
-
|
143
|
+
front_matters = @fm.extract(code)
|
140
144
|
|
141
|
-
|
145
|
+
front_matters[0].should == valid_content.join("\n")
|
142
146
|
end
|
143
147
|
end
|
144
148
|
|
@@ -163,10 +167,10 @@ describe "Front_Matter" do
|
|
163
167
|
"Desc : extract shell functions from shell script" ,
|
164
168
|
"Author : Zhao Cai <caizhaoff@gmail.com>"
|
165
169
|
].join("\n"))
|
166
|
-
content = @fm.extract_lines(code.split("\n"))
|
167
170
|
|
168
|
-
|
171
|
+
front_matters = @fm.extract(code)
|
169
172
|
|
173
|
+
YAML.load(front_matters[0]).should == valid_yaml
|
170
174
|
end
|
171
175
|
|
172
176
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: front-matter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zhao Cai
|
@@ -225,7 +225,7 @@ description: |-
|
|
225
225
|
comments with predefined format:
|
226
226
|
|
227
227
|
1. the leading comment string can be different based on the language.
|
228
|
-
2. the leading column of yaml docs
|
228
|
+
2. the leading column of yaml docs should be aligned to be converted to yaml.
|
229
229
|
email:
|
230
230
|
- caizhaoff@gmail.com
|
231
231
|
executables:
|