Class: Weblog::Snippet

Inherits:
Object
  • Object
show all
Defined in:
lib/weblog/snippet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#filenameObject

Returns the value of attribute filename.



3
4
5
# File 'lib/weblog/snippet.rb', line 3

def filename
  @filename
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/weblog/snippet.rb', line 3

def path
  @path
end

Instance Method Details

#candidatesObject



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

#contentObject



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_fileObject



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

#readObject



27
28
29
# File 'lib/weblog/snippet.rb', line 27

def read
  @read ||= File.read(filename).split("\n")
end

#source_typeObject



23
24
25
# File 'lib/weblog/snippet.rb', line 23

def source_type
  @filename.end_with?('.md') ? :markdown : :html
end

#titleObject



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