Class: Broadway::File
- Inherits:
-
Object
- Object
- Broadway::File
- Defined in:
- lib/broadway/resources/file.rb
Constant Summary collapse
- SRC_MATCHER =
/^(.+\/)*(?:(\d+-\d+-\d+)-)?(.*)\.([^.]+)$/
- URL_MATCHER =
/^(.+\/)*(.*)$/
Instance Attribute Summary collapse
-
#categories ⇒ Object
Returns the value of attribute categories.
-
#date ⇒ Object
Returns the value of attribute date.
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#path ⇒ Object
Returns the value of attribute path.
-
#site ⇒ Object
Returns the value of attribute site.
-
#slug ⇒ Object
Returns the value of attribute slug.
Class Method Summary collapse
Instance Method Summary collapse
- #full_path ⇒ Object
- #header ⇒ Object
-
#initialize(site, options = {}) ⇒ File
constructor
A new instance of File.
- #inspect ⇒ Object
- #read(attribute = nil) ⇒ Object
- #render(attribute = nil, extension = self.extension) ⇒ Object
- #write(dest) ⇒ Object
Constructor Details
#initialize(site, options = {}) ⇒ File
Returns a new instance of File.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/broadway/resources/file.rb', line 11 def initialize(site, = {}) self.site = site self.path = [:file] || [:path] site_source = self.site.source self.name = ::File.basename(self.path) self.directory = ::File.dirname(self.path) n, cats, date, slug, extension = *self.path.match(SRC_MATCHER) self.parent = self.directory.gsub(/#{site_source}/, "").squeeze("/") self.date = Time.parse(date) if date base = self.parent.blank? ? "" : self.parent self.categories = base.split('/').reject { |x| x.empty? } self.slug = slug self.extension = extension end |
Instance Attribute Details
#categories ⇒ Object
Returns the value of attribute categories.
6 7 8 |
# File 'lib/broadway/resources/file.rb', line 6 def categories @categories end |
#date ⇒ Object
Returns the value of attribute date.
6 7 8 |
# File 'lib/broadway/resources/file.rb', line 6 def date @date end |
#directory ⇒ Object
Returns the value of attribute directory.
6 7 8 |
# File 'lib/broadway/resources/file.rb', line 6 def directory @directory end |
#extension ⇒ Object
Returns the value of attribute extension.
6 7 8 |
# File 'lib/broadway/resources/file.rb', line 6 def extension @extension end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/broadway/resources/file.rb', line 6 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
6 7 8 |
# File 'lib/broadway/resources/file.rb', line 6 def parent @parent end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/broadway/resources/file.rb', line 6 def path @path end |
#site ⇒ Object
Returns the value of attribute site.
6 7 8 |
# File 'lib/broadway/resources/file.rb', line 6 def site @site end |
#slug ⇒ Object
Returns the value of attribute slug.
6 7 8 |
# File 'lib/broadway/resources/file.rb', line 6 def slug @slug end |
Class Method Details
.file_ext(full_path) ⇒ Object
82 83 84 |
# File 'lib/broadway/resources/file.rb', line 82 def file_ext(full_path) ::File.extname(full_path).gsub(".", "") end |
.file_name(full_path) ⇒ Object
78 79 80 |
# File 'lib/broadway/resources/file.rb', line 78 def file_name(full_path) ::File.basename(full_path).split(".").first end |
Instance Method Details
#full_path ⇒ Object
64 65 66 |
# File 'lib/broadway/resources/file.rb', line 64 def full_path ::File.(self.path) end |
#header ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/broadway/resources/file.rb', line 31 def header content = IO.read(self.path) if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m YAML.load($1) else {} end end |
#inspect ⇒ Object
73 74 75 |
# File 'lib/broadway/resources/file.rb', line 73 def inspect "#<Broadway:File @path=#{self.path.inspect}>" end |
#read(attribute = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/broadway/resources/file.rb', line 41 def read(attribute = nil) content = IO.read(self.path) if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m if attribute output = YAML.load($1)[attribute] || "" return output end content = content[($1.size + $2.size)..-1] end content end |
#render(attribute = nil, extension = self.extension) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/broadway/resources/file.rb', line 53 def render(attribute = nil, extension = self.extension) content = read(attribute) case extension.to_s when "markdown", "md" content = RDiscount.new(content, :filter_html, :smart).to_html when "textile" content = RedCloth.new(content).to_html end content end |