Class: Weblog::Snippet
- Inherits:
-
Object
- Object
- Weblog::Snippet
- Defined in:
- lib/weblog/snippet.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #candidates ⇒ Object
- #content ⇒ Object
- #find_snippet_file ⇒ Object
-
#initialize(weblog, object, path) ⇒ Snippet
constructor
A new instance of Snippet.
- #read ⇒ Object
- #source_type ⇒ Object
- #title ⇒ Object
Constructor Details
#initialize(weblog, object, path) ⇒ Snippet
Returns a new instance of Snippet.
5 6 7 8 9 |
# File 'lib/weblog/snippet.rb', line 5 def initialize(weblog, object, path) @weblog = weblog @path = path find_snippet_file end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
3 4 5 |
# File 'lib/weblog/snippet.rb', line 3 def filename @filename end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/weblog/snippet.rb', line 3 def path @path end |
Instance Method Details
#candidates ⇒ Object
11 12 13 14 15 |
# File 'lib/weblog/snippet.rb', line 11 def candidates %w(post.md post.html).map do |name| File.join(@weblog.path, @path, name) end end |
#content ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/weblog/snippet.rb', line 41 def content return '' if read.empty? case source_type when :markdown RDiscount.new(read[1..-1].join("\n")).to_html when :html read[1..-1].join("\n") end end |
#find_snippet_file ⇒ Object
17 18 19 20 21 |
# File 'lib/weblog/snippet.rb', line 17 def find_snippet_file @filename = candidates.detect do |filename| File.exist?(filename) end end |
#read ⇒ Object
27 28 29 |
# File 'lib/weblog/snippet.rb', line 27 def read @read ||= File.read(filename).split("\n") end |
#source_type ⇒ Object
23 24 25 |
# File 'lib/weblog/snippet.rb', line 23 def source_type @filename.end_with?('.md') ? :markdown : :html end |
#title ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/weblog/snippet.rb', line 31 def title return '' if read.empty? case source_type when :markdown /^#(.*)$/.match(read[0])[1] when :html /<h1>(.*)<\/h1>/.match(read[0])[1] end end |