Class: Writecast::Document
- Inherits:
-
Object
- Object
- Writecast::Document
- Defined in:
- lib/writecast/models.rb
Overview
represents a document; either a post or a page.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#link_path ⇒ Object
readonly
Returns the value of attribute link_path.
-
#raw_file_path ⇒ Object
readonly
Returns the value of attribute raw_file_path.
-
#summary ⇒ Object
readonly
Returns the value of attribute summary.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#initialize(file_path) ⇒ Document
constructor
A new instance of Document.
Constructor Details
#initialize(file_path) ⇒ Document
Returns a new instance of Document.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/writecast/models.rb', line 13 def initialize(file_path) doc_string = IO.read(file_path) @raw_file_path = file_path if doc_string =~ /(\A---\n(.*)---\n)?(.*)/m doc_header = ($2 ? YAML.load($2) : {}) || {} summary,body = $3.split("\n\n",2) else raise ArgumentError, "Parse error in #{file_path}." end file_basename = File.basename(file_path) @title = doc_header['title'] || file_basename.chomp(File.extname(file_path)) @time = doc_header['time'] || File.stat(file_path).mtime @tags = doc_header['tags'] if summary # the first regex matches kramdown image syntax, and stores two backreferences # the second just matches the syntax image_regex_select = /(!\[.*\]\()(.*)\)/ image_regex = /!\[.*\]\(.*\)/ # swap all relative image paths with absolute paths image_swaps = {} summary.scan(image_regex_select).zip(summary.scan(image_regex)) do |ab,image_match| a,b = ab image_swaps[image_match] = "#{a}/#{"%04d" % @time.year}/" + "#{"%02d" % @time.month}/#{file_basename}_static/" + "#{File.basename(b)})" end @summary = summary.gsub(image_regex, image_swaps).chomp else @summary = '' end @body = body ? body.chomp : '' end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
11 12 13 |
# File 'lib/writecast/models.rb', line 11 def body @body end |
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
11 12 13 |
# File 'lib/writecast/models.rb', line 11 def file_path @file_path end |
#link_path ⇒ Object (readonly)
Returns the value of attribute link_path.
11 12 13 |
# File 'lib/writecast/models.rb', line 11 def link_path @link_path end |
#raw_file_path ⇒ Object (readonly)
Returns the value of attribute raw_file_path.
11 12 13 |
# File 'lib/writecast/models.rb', line 11 def raw_file_path @raw_file_path end |
#summary ⇒ Object (readonly)
Returns the value of attribute summary.
11 12 13 |
# File 'lib/writecast/models.rb', line 11 def summary @summary end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
11 12 13 |
# File 'lib/writecast/models.rb', line 11 def @tags end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
11 12 13 |
# File 'lib/writecast/models.rb', line 11 def time @time end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
11 12 13 |
# File 'lib/writecast/models.rb', line 11 def title @title end |