cmless 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cmless.rb +66 -52
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80a73ce680c00058814943a07f603a248c0a4538
4
- data.tar.gz: e2d8fa87688b99521a5b7df724e0d710ae82f6b9
3
+ metadata.gz: 2a16d15914097df733758df778e02046ac51b97c
4
+ data.tar.gz: 5cddab116a3ab382c0b368c689a3746aa844eac7
5
5
  SHA512:
6
- metadata.gz: 0dc0a639d2d7f1bcfa579ddf5333275c72bb05c05dfb92b0760af45fdf5aee4efb7b444d72062aad3a6799c9547acdfad9c13b55e2a823b4e6e4c8c2b2bb5f12
7
- data.tar.gz: 44fd575eca0b00851b25a74581f5a76974cf059e5c5311dd925964548fe922891718a4ba201104d582d8843ae21b3f3cbda5470a547dca44b66f31927bb5a779
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 :title
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
- instance_variable_set('@head_html', Cmless.extract_head_html(doc))
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
- instance_variable_set('@body_html', Cmless.extract_body_html(doc))
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
- # Class methods:
74
+ class << self
75
+ include Enumerable
71
76
 
72
- def self.all
73
- objects_by_path.values
74
- end
77
+ def each(&block)
78
+ all.each do |cmless|
79
+ block.call(cmless)
80
+ end
81
+ end
75
82
 
76
- def self.objects_by_path
77
- @objects_by_path ||=
78
- begin
79
- unless File.directory?(self::ROOT)
80
- fail StandardError.new("#{self::ROOT} is not a directory")
81
- end
82
- Hash[
83
- Dir[Pathname(self::ROOT) + '**/*.md'].sort.map do |path|
84
- object = new(path)
85
- [object.path, object]
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
- end
89
- end
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
- def self.find_by_path(path)
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
- def self.path_from_file_path(file_path)
96
- file_path.to_s.gsub(self::ROOT + '/', '').gsub(/\.md$/, '')
97
- end
111
+ def path_from_file_path(file_path)
112
+ file_path.to_s.gsub(self::ROOT + '/', '').gsub(/\.md$/, '')
113
+ end
98
114
 
99
- def self.extract_html(doc, title)
100
- following_siblings = []
101
- doc.xpath("//h2[text()='#{title}']").first.tap do |header|
102
- fail IndexError.new("Can't find header '#{title}'") unless header
103
- while header.next_element && !header.next_element.name.match(/h2/)
104
- following_siblings.push(header.next_element.remove)
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
- header.remove
124
+ following_siblings.map(&:to_s).join
107
125
  end
108
- following_siblings.map(&:to_s).join
109
- end
110
126
 
111
- def self.extract_head_html(doc)
112
- siblings = []
113
- body = doc.xpath('//body').first
114
- while body.children.first && !body.children.first.name.match(/h2/)
115
- siblings.push(body.children.first.remove)
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
- def self.extract_body_html(doc)
121
- siblings = []
122
- body = doc.xpath('//body').first
123
- while body.children.first
124
- siblings.push(body.children.first.remove)
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.3
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-24 00:00:00.000000000 Z
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redcarpet