cmless 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cmless.rb +66 -52
- 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: 2a16d15914097df733758df778e02046ac51b97c
|
4
|
+
data.tar.gz: 5cddab116a3ab382c0b368c689a3746aa844eac7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea5295bd65a53012ae6619eec0a2f2e4188da31d1f868bc6e8fd21db293a86c6eaf52c57fbbc8c33a86c561d17ec6671ae28b67c20c319c268e77704aa8a7c8a
|
7
|
+
data.tar.gz: 8b92dc8fc197d66d4acd478950b3f8b979c1fada342a8a621a85ed58d4af5029a7106f7009e141c184a30c17bf460bd535ee760e864b53a9269a229761e01b21
|
data/lib/cmless.rb
CHANGED
@@ -7,9 +7,9 @@ require 'singleton'
|
|
7
7
|
require 'nokogiri'
|
8
8
|
|
9
9
|
# CMS alternative: Content in markdown / Extract HTML and data for display
|
10
|
-
class Cmless
|
10
|
+
class Cmless # rubocop:disable Metrics/ClassLength
|
11
11
|
attr_reader :path
|
12
|
-
attr_reader :
|
12
|
+
attr_reader :title_html
|
13
13
|
|
14
14
|
private
|
15
15
|
|
@@ -17,18 +17,22 @@ class Cmless
|
|
17
17
|
def initialize(file_path) # rubocop:disable Metrics/MethodLength
|
18
18
|
@path = self.class.path_from_file_path(file_path)
|
19
19
|
Nokogiri::HTML(Markdowner.instance.render(File.read(file_path))).tap do |doc|
|
20
|
-
@title = doc.xpath('//h1').first.remove.text
|
21
|
-
|
22
20
|
html_methods = self.class.instance_methods
|
23
21
|
.select { |method| method.to_s.match(/\_html$/) }
|
24
22
|
|
23
|
+
doc.xpath('//h1').first.tap do |h1|
|
24
|
+
@title_html = h1.inner_html
|
25
|
+
h1.remove
|
26
|
+
html_methods.delete(:title_html)
|
27
|
+
end
|
28
|
+
|
25
29
|
if html_methods.include?(:head_html)
|
26
|
-
|
30
|
+
@head_html = Cmless.extract_head_html(doc)
|
27
31
|
html_methods.delete(:head_html)
|
28
32
|
end
|
29
33
|
|
30
34
|
if html_methods.include?(:body_html)
|
31
|
-
|
35
|
+
@body_html = Cmless.extract_body_html(doc)
|
32
36
|
html_methods.delete(:body_html)
|
33
37
|
end
|
34
38
|
|
@@ -67,67 +71,77 @@ class Cmless
|
|
67
71
|
end
|
68
72
|
end
|
69
73
|
|
70
|
-
|
74
|
+
class << self
|
75
|
+
include Enumerable
|
71
76
|
|
72
|
-
|
73
|
-
|
74
|
-
|
77
|
+
def each(&block)
|
78
|
+
all.each do |cmless|
|
79
|
+
block.call(cmless)
|
80
|
+
end
|
81
|
+
end
|
75
82
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
83
|
+
def all
|
84
|
+
objects_by_path.values
|
85
|
+
end
|
86
|
+
|
87
|
+
def find_by_path(path)
|
88
|
+
objects_by_path[path] ||
|
89
|
+
fail(IndexError.new(
|
90
|
+
"'#{path}' is not a valid path under '#{self::ROOT}'; " \
|
91
|
+
"Expected one of #{objects_by_path.keys}"))
|
92
|
+
end
|
93
|
+
|
94
|
+
def objects_by_path # rubocop:disable Metrics/MethodLength
|
95
|
+
@objects_by_path ||=
|
96
|
+
begin
|
97
|
+
unless File.directory?(self::ROOT)
|
98
|
+
fail StandardError.new("#{self::ROOT} is not a directory")
|
86
99
|
end
|
87
|
-
|
88
|
-
|
89
|
-
|
100
|
+
Hash[
|
101
|
+
Dir[Pathname(self::ROOT) + '**/*.md'].sort.map do |path|
|
102
|
+
object = new(path)
|
103
|
+
[object.path, object]
|
104
|
+
end
|
105
|
+
]
|
106
|
+
end
|
107
|
+
end
|
90
108
|
|
91
|
-
|
92
|
-
objects_by_path[path] || fail(IndexError.new("'#{path}' is not a valid path under '#{self::ROOT}'; Expected one of #{objects_by_path.keys}"))
|
93
|
-
end
|
109
|
+
# These are just used by the initialize. Perhaps there is a better place.
|
94
110
|
|
95
|
-
|
96
|
-
|
97
|
-
|
111
|
+
def path_from_file_path(file_path)
|
112
|
+
file_path.to_s.gsub(self::ROOT + '/', '').gsub(/\.md$/, '')
|
113
|
+
end
|
98
114
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
115
|
+
def extract_html(doc, title)
|
116
|
+
following_siblings = []
|
117
|
+
doc.xpath("//h2[text()='#{title}']").first.tap do |header|
|
118
|
+
fail IndexError.new("Can't find header '#{title}'") unless header
|
119
|
+
while header.next_element && !header.next_element.name.match(/h2/)
|
120
|
+
following_siblings.push(header.next_element.remove)
|
121
|
+
end
|
122
|
+
header.remove
|
105
123
|
end
|
106
|
-
|
124
|
+
following_siblings.map(&:to_s).join
|
107
125
|
end
|
108
|
-
following_siblings.map(&:to_s).join
|
109
|
-
end
|
110
126
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
127
|
+
def extract_head_html(doc)
|
128
|
+
siblings = []
|
129
|
+
body = doc.xpath('//body').first
|
130
|
+
while body.children.first && !body.children.first.name.match(/h2/)
|
131
|
+
siblings.push(body.children.first.remove)
|
132
|
+
end
|
133
|
+
siblings.map(&:to_s).join.strip
|
116
134
|
end
|
117
|
-
siblings.map(&:to_s).join.strip
|
118
|
-
end
|
119
135
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
siblings.
|
136
|
+
def extract_body_html(doc)
|
137
|
+
siblings = []
|
138
|
+
body = doc.xpath('//body').first
|
139
|
+
siblings.push(body.children.first.remove) while body.children.first
|
140
|
+
siblings.map(&:to_s).join.strip
|
125
141
|
end
|
126
|
-
siblings.map(&:to_s).join.strip
|
127
142
|
end
|
128
143
|
|
129
144
|
# Utility class: (This could move.)
|
130
|
-
|
131
145
|
# Just a wrapper for Redcarpet
|
132
146
|
class Markdowner
|
133
147
|
include Singleton
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cmless
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chuck McCallum
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redcarpet
|