Class: Maneki
- Inherits:
-
Object
- Object
- Maneki
- Defined in:
- lib/maneki.rb
Overview
Maneki
Load documents from a directory and parse them. The idea is that any of the given documents (Markdown only for now) should be readable in their own format without parsing but be formatted in such way as to present meta information (title, headers, etc) in an easily parsable way.
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#category ⇒ Object
Returns the value of attribute category.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#slug ⇒ Object
Returns the value of attribute slug.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
-
.all ⇒ Object
Grab all documents.
-
.categories ⇒ Object
Get a list of all categories.
-
.category(value = nil) ⇒ Object
Set the general category for these documents.
-
.find(args = {}) ⇒ Object
Search for any documents.
-
.method_missing(method, *args) ⇒ Object
Look up a document category.
-
.next_after(document, args = {}) ⇒ Object
The next document after a given document.
-
.path(value = nil) ⇒ Object
Set or return the search path.
-
.path=(value) ⇒ Object
Set the search path.
-
.previous_before(document, args = {}) ⇒ Object
The previous document before a given document.
Instance Method Summary collapse
-
#<=>(rhs) ⇒ Object
Sort by filename.
-
#==(rhs) ⇒ Object
Documents are the same if their IDs are the same.
-
#initialize(args = {}) ⇒ Maneki
constructor
Create a new document.
-
#to_s ⇒ Object
Stringify this document.
-
#valid? ⇒ Boolean
A callback to see if this document should be added to the list.
Constructor Details
#initialize(args = {}) ⇒ Maneki
Create a new document
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/maneki.rb', line 123 def initialize (args = {}) @filename = File.(args[:filename]) @body = '' if File.exists? @filename @category = File.dirname(@filename).split('/').last unless @category @slug = @filename.gsub(File.(self.class.path) + '/', '').gsub(/\..+$/, '') body = File.open(@filename).readlines body.each do |line| line.chomp!("\r") # fix up any DOS EOLs end title = body.find { |item| /^\#.+/.match item } body = body[2..body.length-1] if title # consume title # check for headers @headers = Hash.new if /^\s?\-.+/.match(body[0]) headers_end = body.index("\n") || 1 headers = body[0..headers_end-1] if headers_end # next blank line is end of headers body_start = 1 body_start = body.index(headers.last) + 1 if headers body = body[body_start..body.length - 1] if title headers.each do |header| unless header.strip == '' or /^\#.+/.match header values = header.gsub(/^\s?\-/, '').strip.split(/:\s/) key = values.shift.downcase.gsub(/[^\w\_]+/, '_').to_sym value = values.join(': ').strip # check for special header values (true, false, lists, etc) if ['true', 't', 'yes', 'y'].include? value value = true elsif ['false', 'f', 'no', 'n'].include? value value = false elsif value.include? ',' value = value.split(/\,\s?/) end @headers[key] = value end end if headers end # title @title = title.gsub(/^\#\s/, '').strip if title # content @body = body.join("").strip end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
120 121 122 |
# File 'lib/maneki.rb', line 120 def body @body end |
#category ⇒ Object
Returns the value of attribute category.
120 121 122 |
# File 'lib/maneki.rb', line 120 def category @category end |
#filename ⇒ Object
Returns the value of attribute filename.
120 121 122 |
# File 'lib/maneki.rb', line 120 def filename @filename end |
#headers ⇒ Object
Returns the value of attribute headers.
120 121 122 |
# File 'lib/maneki.rb', line 120 def headers @headers end |
#slug ⇒ Object
Returns the value of attribute slug.
120 121 122 |
# File 'lib/maneki.rb', line 120 def slug @slug end |
#title ⇒ Object
Returns the value of attribute title.
120 121 122 |
# File 'lib/maneki.rb', line 120 def title @title end |
Class Method Details
.all ⇒ Object
Grab all documents
37 38 39 |
# File 'lib/maneki.rb', line 37 def self.all prepare end |
.categories ⇒ Object
Get a list of all categories
30 31 32 33 |
# File 'lib/maneki.rb', line 30 def self.categories prepare @categorised.keys end |
.category(value = nil) ⇒ Object
Set the general category for these documents
23 24 25 26 |
# File 'lib/maneki.rb', line 23 def self.category (value = nil) @category = value @category end |
.find(args = {}) ⇒ Object
Search for any documents
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/maneki.rb', line 43 def self.find (args = {}) if args.is_a? String return find_by_slug(args) elsif args[:slug] return find_by_slug(args[:slug]) end match = args[:match] || :all args.delete(:match) all.select do |item| matches = 0 if args[:title] if item.title == args[:title] matched ||= true if match == :any matches += 1 if match == :all else matched ||= false if match == :all end end if args[:body] if item.body.downcase.include? args[:body].downcase matched ||= true if match == :any matches += 1 if match == :all else matched ||= false if match == :all end end if args[:headers] args[:headers].each_pair do |header, value| if item.headers[header] and (item.headers[header] == value or item.headers[header].include? value) matched ||= true if match == :any matches += 1 if match == :all else matched ||= false if match == :all end end end matched || (matches >= args.size) end end |
.method_missing(method, *args) ⇒ Object
Look up a document category
114 115 116 117 |
# File 'lib/maneki.rb', line 114 def self.method_missing (method, *args) prepare @categorised[method.to_s] unless @categorised.nil? end |
.next_after(document, args = {}) ⇒ Object
The next document after a given document
102 103 104 105 106 107 108 109 110 |
# File 'lib/maneki.rb', line 102 def self.next_after (document, args = {}) prepare if args[:category] index = @categorised[args[:category]].index(document) else index = @documents.index(document) end @documents[index + 1] unless index == @documents.length end |
.path(value = nil) ⇒ Object
Set or return the search path
15 16 17 18 19 |
# File 'lib/maneki.rb', line 15 def self.path (value = nil) @path = value if value @category = File.basename(value) unless @category @path end |
.path=(value) ⇒ Object
Set the search path
10 11 12 |
# File 'lib/maneki.rb', line 10 def self.path= (value) path value end |
.previous_before(document, args = {}) ⇒ Object
The previous document before a given document
91 92 93 94 95 96 97 98 99 |
# File 'lib/maneki.rb', line 91 def self.previous_before (document, args = {}) prepare if args[:category] index = @categorised[args[:category]].index(document) else index = @documents.index(document) end @documents[index - 1] unless index == 0 end |
Instance Method Details
#<=>(rhs) ⇒ Object
Sort by filename
189 190 191 |
# File 'lib/maneki.rb', line 189 def <=> (rhs) @filename <=> rhs.filename end |
#==(rhs) ⇒ Object
Documents are the same if their IDs are the same
183 184 185 |
# File 'lib/maneki.rb', line 183 def == (rhs) @slug == rhs.slug end |
#to_s ⇒ Object
Stringify this document
195 196 197 |
# File 'lib/maneki.rb', line 195 def to_s title end |
#valid? ⇒ Boolean
A callback to see if this document should be added to the list
177 178 179 |
# File 'lib/maneki.rb', line 177 def valid? true end |