summaryrefslogtreecommitdiff
path: root/lib/static_page.rb
blob: eb32319172a8c4bf46023b3fcd58fb54d024976f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#
# class StaticPage
#
# represents a static website page.
#
#

require 'property_set'
require 'i18n'
require 'pathname'

class StaticPage
  attr_accessor :path, :children, :name, :file_path, :props, :parent, :mount_point

  ##
  ## CLASS METHODS
  ##

  def self.find(site, filter)
    if filter =~ /\//
      path = filter.split('/').map{|segment| segment.gsub(/[^0-9a-z_-]/, '')}
      page = site.pages[path.join('/')]
      if page
        return page
      else
        return site.pages[path.last]
      end
    else
      site.pages[filter]
    end
  end

  #
  # loads a directory, creating StaticPages from the directory structure
  #
  def scan(&block)
    Dir.chdir(file_path) do
      Dir.glob("*").each do |child_name|
        if File.directory?(child_name)
          child = StaticPage.new(self, child_name)
          yield child
          child.scan(&block)
        elsif is_simple_page?(child_name)
          child = StaticPage.new(self, file_without_suffix(child_name))
          yield child
        end
      end
    end
  end

  ##
  ## INSTANCE METHODS
  ##

  def initialize(parent, name, file_path=nil)
    @children = []
    @name = name
    if parent
      @parent = parent
      @mount_point = @parent.mount_point
      @parent.add_child(self)
      @path = [@parent.path, @name].flatten.compact
    else
      @path = []
    end
    if file_path
      @file_path = file_path
    elsif @parent && @parent.file_path
      @file_path = File.join(@parent.file_path, @name)
    else
      raise 'file path must be specified or in parent'
    end
    @simple_page = !File.directory?(@file_path)
    @props = load_properties
  end

  def add_child(page)
    @children << page
  end

  def all_children
    StaticPageArray.new(child_tree.flatten.compact)
  end

  #
  # e.g. /home/user/dev/leap-public-site/app/views/pages/about-us/contact
  #
  #def file_path
  #  "#{@mount_point.directory}/#{@path.join('/')}"
  #end

  #
  # e.g. pages/about-us/contact/en
  #
  def template_path(locale=I18n.locale)
    if @simple_page
      "#{@mount_point.relative_directory}/#{@path.join('/')}"
    else
      "#{@mount_point.relative_directory}/#{@path.join('/')}/#{locale}"
    end
  end

  def inspect
    "<'#{@path.join('/')}' #{children.inspect}>"
  end

  def title
    begin
      I18n.t!('pages.' + @name, :raise => true)
    rescue I18n::MissingTranslationData
      props.title
    end
  end

  def id
    self.name
  end

  protected

  def child_tree
    [self, children.collect{|child| child.child_tree}]
  end

  private

  #def self.relative_to_rails_view_root(absolute_path)
  #  if Rails.root
  #    absolute = Pathname.new(absolute_path)
  #    rails_view_root = Pathname.new(Rails.root + 'app/views')
  #    absolute.relative_path_from(rails_view_root).to_s
  #  end
  #end

  def load_properties
    props = PropertySet.new(self)
    Dir.glob(file_path + '/*.haml') do |content_file_path|
      locale = File.basename(content_file_path).sub(File.extname(content_file_path),'')
      #variable_header = ""
      #File.open(content_file_path) do |f|
      #  while (line = f.gets) =~ /^- @/
      #    variable_header << line
      #  end
      #end
      props.eval(locale, File.read(content_file_path))
    end
    return props
  end

  SUFFIXES = '(haml|md)'

  #
  # returns true if the name of a file could be a 'simple' static page
  # with only one translation.
  #
  # rules:
  # * we include files that end in appriopriate suffixes
  # * we exclude file names that are locales.
  #
  def is_simple_page?(name)
    name =~ /\.#{SUFFIXES}$/ && name !~ /^(#{AVAILABLE_LANGUAGES.join('|')})\.#{SUFFIXES}$/
  end

  def file_without_suffix(name)
    name.sub(/^(.*?)\.#{SUFFIXES}$/, "\\1")
  end
end