Class: Broadway::File

Inherits:
Object
  • Object
show all
Defined in:
lib/broadway/resources/file.rb

Constant Summary collapse

SRC_MATCHER =
/^(.+\/)*(?:(\d+-\d+-\d+)-)?(.*)\.([^.]+)$/
URL_MATCHER =
/^(.+\/)*(.*)$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  self.site = site
  self.path = options[:file] || options[: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

#categoriesObject

Returns the value of attribute categories.



6
7
8
# File 'lib/broadway/resources/file.rb', line 6

def categories
  @categories
end

#dateObject

Returns the value of attribute date.



6
7
8
# File 'lib/broadway/resources/file.rb', line 6

def date
  @date
end

#directoryObject

Returns the value of attribute directory.



6
7
8
# File 'lib/broadway/resources/file.rb', line 6

def directory
  @directory
end

#extensionObject

Returns the value of attribute extension.



6
7
8
# File 'lib/broadway/resources/file.rb', line 6

def extension
  @extension
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/broadway/resources/file.rb', line 6

def name
  @name
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/broadway/resources/file.rb', line 6

def parent
  @parent
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/broadway/resources/file.rb', line 6

def path
  @path
end

#siteObject

Returns the value of attribute site.



6
7
8
# File 'lib/broadway/resources/file.rb', line 6

def site
  @site
end

#slugObject

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_pathObject



64
65
66
# File 'lib/broadway/resources/file.rb', line 64

def full_path
  ::File.expand_path(self.path)
end

#headerObject



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

#inspectObject



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

#write(dest) ⇒ Object



68
69
70
71
# File 'lib/broadway/resources/file.rb', line 68

def write(dest)
  FileUtils.mkdir_p(File.join(dest, File.dirname(directory)))
  FileUtils.cp(@path, File.join(dest, directory))
end