Class: Cmless
Overview
CMS alternative: Content in markdown / Extract HTML and data for display
Defined Under Namespace
Classes: Markdowner
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#title_html ⇒ Object
readonly
Returns the value of attribute title_html.
Class Method Summary collapse
- .all ⇒ Object
- .each(&block) ⇒ Object
- .extract_body_html(doc) ⇒ Object
- .extract_head_html(doc) ⇒ Object
- .extract_html(doc, title) ⇒ Object
- .find_by_path(path) ⇒ Object
- .objects_by_path ⇒ Object
- .objects_by_path_in_progress ⇒ Object
-
.path_from_file_path(file_path) ⇒ Object
These are just used by the initialize.
Instance Method Summary collapse
- #ancestors ⇒ Object
- #children ⇒ Object
-
#parent ⇒ Object
Instance methods:.
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/cmless.rb', line 11 def path @path end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
12 13 14 |
# File 'lib/cmless.rb', line 12 def title @title end |
#title_html ⇒ Object (readonly)
Returns the value of attribute title_html.
13 14 15 |
# File 'lib/cmless.rb', line 13 def title_html @title_html end |
Class Method Details
.all ⇒ Object
98 99 100 101 102 |
# File 'lib/cmless.rb', line 98 def all @all ||= objects_by_path.values.sort_by do |object| object.head_html.gsub('<p>', '').to_f rescue object.path end end |
.each(&block) ⇒ Object
92 93 94 95 96 |
# File 'lib/cmless.rb', line 92 def each(&block) all.each do |cmless| block.call(cmless) end end |
.extract_body_html(doc) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/cmless.rb', line 157 def extract_body_html(doc) siblings = [] body = doc.xpath('//body').first siblings.push(body.children.first.remove) while body.children.first siblings.map(&:to_s).join.strip end |
.extract_head_html(doc) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/cmless.rb', line 148 def extract_head_html(doc) siblings = [] body = doc.xpath('//body').first while body.children.first && !body.children.first.name.match(/h2/) siblings.push(body.children.first.remove) end siblings.map(&:to_s).join.strip end |
.extract_html(doc, title) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/cmless.rb', line 136 def extract_html(doc, title) following_siblings = [] doc.xpath("//h2[text()='#{title}']").first.tap do |header| return nil unless header while header.next_element && !header.next_element.name.match(/h2/) following_siblings.push(header.next_element.remove) end header.remove end following_siblings.map(&:to_s).join end |
.find_by_path(path) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/cmless.rb', line 104 def find_by_path(path) objects_by_path[path] || fail(IndexError.new( "'#{path}' is not a valid path under '#{self::ROOT}'; " \ "Expected one of #{objects_by_path.keys}")) end |
.objects_by_path ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/cmless.rb', line 115 def objects_by_path @objects_by_path ||= begin unless File.directory?(self::ROOT) fail StandardError.new("#{self::ROOT} is not a directory") end @object_by_path_in_progress = {} Dir[Pathname(self::ROOT) + '**/*.md'].sort.each do |full_path| object = new(full_path) @object_by_path_in_progress[object.path] = object end @object_by_path_in_progress end end |
.objects_by_path_in_progress ⇒ Object
111 112 113 |
# File 'lib/cmless.rb', line 111 def objects_by_path_in_progress @object_by_path_in_progress end |
.path_from_file_path(file_path) ⇒ Object
These are just used by the initialize. Perhaps there is a better place.
132 133 134 |
# File 'lib/cmless.rb', line 132 def path_from_file_path(file_path) file_path.to_s.gsub(self::ROOT + '/', '').gsub(/\.md$/, '') end |
Instance Method Details
#ancestors ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/cmless.rb', line 69 def ancestors @ancestors ||= begin split = path.split('/') (1..split.size - 1).to_a.map do |i| # to avoid infinite recursion, only look at the ones already loaded. self.class.objects_by_path_in_progress[split[0, i].join('/')] end end end |
#children ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/cmless.rb', line 79 def children @children ||= begin self.class.objects_by_path.select do |other_path, _other_object| other_path.match(/^#{path}\/[^\/]+$/) # TODO: escape end.map do |_other_path, other_object| other_object end end end |
#parent ⇒ Object
Instance methods:
65 66 67 |
# File 'lib/cmless.rb', line 65 def parent ancestors.last end |