cmless 0.0.5 → 0.0.8

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 +32 -13
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eb8a359f53083d2d2da6220c06da3f6701281639
4
- data.tar.gz: b80f25065a23c1c85f55c0998ee7c18bb9d0b30f
3
+ metadata.gz: 4500759e27ced354cca8b411ffda462bf234558d
4
+ data.tar.gz: e939ee54802ca5362b5f1273265fc07d50ff71e1
5
5
  SHA512:
6
- metadata.gz: d03df5485f2b8120c4af361e13d5253131a06cb36f71fe5c5d5d5bcacb970e105856d917ac9c1421fc62f4e2ed86468bdb79ea86fb7e74ae06a92de6fb37f553
7
- data.tar.gz: 569223ae59ed582ab39e30a7d7200110a3026dcddc6f4fc44b2d2946e064183c838c2e29ea9ad03b39f3d093d03a7e6fe706d16c4c58aaa38ab291b22df0549a
6
+ metadata.gz: 393902304e99166f5ae52693a8feeee270947527874b4e58e3926456ab6a10220171d72ca84f15c025b344d4e34d8bc92bc0e479ec91121d0ae992ad443bae46
7
+ data.tar.gz: 0e5f31d1d646220b824083f7626833416ef08819372d99f0165773b12b6b63ceaf402a211cba49c7840be959f9944794f6b0883529bbc9bc16592b9fe0578f56
data/lib/cmless.rb CHANGED
@@ -7,7 +7,7 @@ require 'singleton'
7
7
  require 'nokogiri'
8
8
 
9
9
  # CMS alternative: Content in markdown / Extract HTML and data for display
10
- class Cmless # rubocop:disable Metrics/ClassLength
10
+ class Cmless
11
11
  attr_reader :path
12
12
  attr_reader :title
13
13
  attr_reader :title_html
@@ -15,7 +15,7 @@ class Cmless # rubocop:disable Metrics/ClassLength
15
15
  private
16
16
 
17
17
  # You should use find_by_path rather than creating your own instances.
18
- def initialize(file_path) # rubocop:disable Metrics/MethodLength
18
+ def initialize(file_path)
19
19
  @path = self.class.path_from_file_path(file_path)
20
20
  Nokogiri::HTML(Markdowner.instance.render(File.read(file_path))).tap do |doc|
21
21
  html_methods = self.class.instance_methods
@@ -40,7 +40,13 @@ class Cmless # rubocop:disable Metrics/ClassLength
40
40
 
41
41
  html_methods.each do |method|
42
42
  h2_name = method.to_s.gsub(/\_html$/, '').gsub('_', ' ').capitalize
43
- instance_variable_set("@#{method}", Cmless.extract_html(doc, h2_name))
43
+ value = Cmless.extract_html(doc, h2_name)
44
+ value ||= if parent # Look at parent if missing on self.
45
+ parent.send(method)
46
+ else
47
+ fail(IndexError.new("Can't find '#{method}'"))
48
+ end
49
+ instance_variable_set("@#{method}", value)
44
50
  end
45
51
 
46
52
  doc.text.strip.tap do |extra|
@@ -48,17 +54,24 @@ class Cmless # rubocop:disable Metrics/ClassLength
48
54
  fail("#{file_path} has extra unused text: '#{escaped}'") unless extra == ''
49
55
  end
50
56
  end
57
+ rescue => e
58
+ raise(e.message + ' in ' + file_path)
51
59
  end
52
60
 
53
61
  public
54
62
 
55
63
  # Instance methods:
56
64
 
65
+ def parent
66
+ ancestors.last
67
+ end
68
+
57
69
  def ancestors
58
70
  @ancestors ||= begin
59
71
  split = path.split('/')
60
72
  (1..split.size - 1).to_a.map do |i|
61
- self.class.objects_by_path[split[0, i].join('/')]
73
+ # to avoid infinite recursion, only look at the ones already loaded.
74
+ self.class.objects_by_path_in_progress[split[0, i].join('/')]
62
75
  end
63
76
  end
64
77
  end
@@ -83,7 +96,9 @@ class Cmless # rubocop:disable Metrics/ClassLength
83
96
  end
84
97
 
85
98
  def all
86
- objects_by_path.values
99
+ @all ||= objects_by_path.values.sort_by do |object|
100
+ object.head_html.gsub('<p>', '').to_f rescue object.path
101
+ end
87
102
  end
88
103
 
89
104
  def find_by_path(path)
@@ -93,18 +108,22 @@ class Cmless # rubocop:disable Metrics/ClassLength
93
108
  "Expected one of #{objects_by_path.keys}"))
94
109
  end
95
110
 
96
- def objects_by_path # rubocop:disable Metrics/MethodLength
111
+ def objects_by_path_in_progress
112
+ @object_by_path_in_progress
113
+ end
114
+
115
+ def objects_by_path
97
116
  @objects_by_path ||=
98
117
  begin
99
118
  unless File.directory?(self::ROOT)
100
119
  fail StandardError.new("#{self::ROOT} is not a directory")
101
120
  end
102
- Hash[
103
- Dir[Pathname(self::ROOT) + '**/*.md'].sort.map do |path|
104
- object = new(path)
105
- [object.path, object]
106
- end
107
- ]
121
+ @object_by_path_in_progress = {}
122
+ Dir[Pathname(self::ROOT) + '**/*.md'].sort.each do |full_path|
123
+ object = new(full_path)
124
+ @object_by_path_in_progress[object.path] = object
125
+ end
126
+ @object_by_path_in_progress
108
127
  end
109
128
  end
110
129
 
@@ -117,7 +136,7 @@ class Cmless # rubocop:disable Metrics/ClassLength
117
136
  def extract_html(doc, title)
118
137
  following_siblings = []
119
138
  doc.xpath("//h2[text()='#{title}']").first.tap do |header|
120
- fail IndexError.new("Can't find header '#{title}'") unless header
139
+ return nil unless header
121
140
  while header.next_element && !header.next_element.name.match(/h2/)
122
141
  following_siblings.push(header.next_element.remove)
123
142
  end
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.5
4
+ version: 0.0.8
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-27 00:00:00.000000000 Z
11
+ date: 2015-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redcarpet