Class: Amber::StaticPage

Inherits:
Object
  • Object
show all
Defined in:
lib/amber/static_page.rb,
lib/amber/static_page/render.rb,
lib/amber/static_page/filesystem.rb,
lib/amber/static_page/property_set.rb,
lib/amber/static_page/page_properties.rb

Defined Under Namespace

Classes: PageProperties, PropertySet, ThisPropertySet

Constant Summary collapse

FORBIDDEN_PAGE_CHARS_RE =

INSTANCE METHODS

/[A-Z\.\?\|\[\]\{\}\$\^\*~!@#%&='"<>]/
LOCALES_RE =

e.g. en, de, pt

/(?<locale>#{Amber::POSSIBLE_LANGUAGE_CODES.join('|')})/
LOCALES_GLOB =
"{#{Amber::POSSIBLE_LANGUAGE_CODES.join(',')}}"
PAGE_SUFFIXES_RE =

e.g. haml, md, text

/(?<suffix>#{Amber::PAGE_SUFFIXES.join('|')})/
PAGE_SUFFIXES_GLOB =
"{#{Amber::PAGE_SUFFIXES.join(',')}}"
LOCALE_FILE_MATCH_RE =

e.g. en.haml or es.md or index.pt.text

/^(index\.)?#{LOCALES_RE}\.#{PAGE_SUFFIXES_RE}$/
LOCALE_FILE_MATCH_GLOB =
"{index.,}#{LOCALES_GLOB}.#{PAGE_SUFFIXES_GLOB}"
SIMPLE_FILE_MATCH_RE =
lambda {|name| /^(#{Regexp.escape(name)})(\.#{LOCALES_RE})?\.#{PAGE_SUFFIXES_RE}$/ }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, file_path = nil, path_prefix = "/") ⇒ StaticPage

Returns a new instance of StaticPage.



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
# File 'lib/amber/static_page.rb', line 30

def initialize(parent, name, file_path=nil, path_prefix="/")
  @valid     = true
  @children  = PageArray.new  # array of StaticPages
  @nav_title = {} # key is locale
  @title     = {} # key is locale

  @name, @suffix = parse_source_file_name(name)

  # set @parent & @path
  if parent
    @parent = parent
    @config = @parent.config
    @parent.add_child(self)
    @path = [@parent.path, @name].flatten.compact
  else
    @path = (path_prefix||"").split('/')
  end

  if @name =~ FORBIDDEN_PAGE_CHARS_RE
    Amber.logger.error "Illegal page name #{@name} at path /#{self.path.join('/')} -- must not have symbols, uppercase, or periods."
    @valid = false
  end

  # set the @file_path
  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)

  # eval the property headers, if any
  @props = load_properties()
end

Instance Attribute Details

#propsObject (readonly)

set of page properties (PropertySet)



20
21
22
# File 'lib/amber/static_page.rb', line 20

def props
  @props
end

Class Method Details

.scan_directory_tree(parent_page, absolute_dir_path, relative_dir_path, &block) ⇒ Object

Recursively decends the directory tree, yielding pages and directories it encounters.

yield has two arguments:

(1) StaticPage instance, or nil. (2) Directory path string if #1 is nil. Path is relative.

Directory paths are dirs in the tree that don’t contain any pages.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/amber/static_page/filesystem.rb', line 24

def self.scan_directory_tree(parent_page, absolute_dir_path, relative_dir_path, &block)
  Dir.foreach(absolute_dir_path).each do |child_path|
    next if child_path =~ /^\./
    abs_path = File.join(absolute_dir_path, child_path)
    rel_path = File.join(relative_dir_path, child_path)
    if parent_page && is_directory_page?(abs_path)
      child_page = StaticPage.new(parent_page, child_path)
      if child_page.valid?
        yield child_page, nil
        scan_directory_tree(child_page, abs_path, rel_path, &block)
      end
    elsif parent_page && is_simple_page?(abs_path)
      child_page = StaticPage.new(parent_page, child_path)
      if child_page.valid?
        yield child_page, nil
      end
    elsif File.directory?(abs_path)
      yield nil, rel_path
      scan_directory_tree(nil, abs_path, rel_path, &block)
    end
  end
end

Instance Method Details

#add_child(page) ⇒ Object



68
69
70
# File 'lib/amber/static_page.rb', line 68

def add_child(page)
  @children << page
end

#aliases(locale = I18n.default_locale) ⇒ Object

returns an array of normalized aliases based on the :alias property defined for a page.

aliases are defined with a leading slash for absolute paths, or without a slash for relative paths. this method converts this to a format that amber uses (all absolute, with no leading slash, as an array instead of a string).



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
# File 'lib/amber/static_page.rb', line 132

def aliases(locale=I18n.default_locale)
  @aliases ||= begin
    aliases_hash = Hash.new([])
    @props.locales.each do |locale|
      aliases = @props.prop_without_inheritance(locale, :alias)
      aliases_hash[locale] = begin
        if aliases.nil?
          []
        else
          [aliases].flatten.collect {|alias_path|
            if alias_path =~ /^\//
              alias_path.sub(/^\//, '').split('/')
            elsif @parent
              @parent.path + [alias_path]
            else
              alias_path.split('/')
            end
          }
        end
      end
    end
    aliases_hash
  end
  @aliases[locale]
end

#all_childrenObject



72
73
74
# File 'lib/amber/static_page.rb', line 72

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

#child(name) ⇒ Object

returns a child matching name, if any.



107
108
109
# File 'lib/amber/static_page.rb', line 107

def child(name)
  children.detect {|child| child.name == name}
end

#content_file(locale) ⇒ Object

full filesystem path name of the source content file e.g. /home/user/mysite/pages/about-us/contact/en.md



85
86
87
# File 'lib/amber/static_page/filesystem.rb', line 85

def content_file(locale)
  content_files[locale] || content_files[I18n.default_locale] || content_files.values.first
end

#content_file_exists?(locale) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/amber/static_page/filesystem.rb', line 89

def content_file_exists?(locale)
  !!content_files[locale]
end

#destination_file(dest_dir, locale) ⇒ Object

full filesystem path name of the destination rendered file e.g. /home/user/mysite/public/about-us/contact/index.en.html



97
98
99
# File 'lib/amber/static_page/filesystem.rb', line 97

def destination_file(dest_dir, locale)
  File.join(dest_dir, *@path, "index.#{locale}.html")
end

#explicit_title(locale) ⇒ Object

returns title iff explicitly set.



95
96
97
98
# File 'lib/amber/static_page.rb', line 95

def explicit_title(locale)
  @props.prop_without_inheritance(locale, :title) ||
  @props.prop_without_inheritance(I18n.default_locale, :title)
end

#idObject



100
101
102
# File 'lib/amber/static_page.rb', line 100

def id
  self.name
end

#inspectObject



76
77
78
# File 'lib/amber/static_page.rb', line 76

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

creates symlinks for aliases to this page. called by Page#render_to_file and Site#render_short_path_aliases



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/amber/static_page/render.rb', line 48

def link_page_aliases(dest_dir, alias_paths, locale=I18n.default_locale)
  alias_paths.each do |alias_path|
    alias_file_path = File.join(dest_dir, alias_path)
    #if locale != I18n.default_locale
    #  alias_file_path += ".#{locale}"
    #end
    alias_file_path = Pathname.new(alias_file_path)
    page_file_path  = Pathname.new(File.join(dest_dir, *@path))
    symlink(page_file_path, alias_file_path)
  end
end

#localesObject

Returns array of locale symbols for all locales with properties set Note: there might be a content for a locale that does not show up in this array, if the content file does not set any properties.



120
121
122
# File 'lib/amber/static_page.rb', line 120

def locales
  @props.locales
end


86
87
88
89
90
# File 'lib/amber/static_page.rb', line 86

def nav_title(locale=I18n.locale)
  @nav_title[locale] ||= begin
    @props.prop_with_fallback(locale, [:nav_title, :title]) || @name
  end
end

#prop(*args) ⇒ Object



111
112
113
# File 'lib/amber/static_page.rb', line 111

def prop(*args)
  @props.prop(*args)
end

#render_to_file(dest_dir, options = {}) ⇒ Object

render a static copy

dest_dir - e.g. amber_root/public/



34
35
36
37
38
39
40
41
42
# File 'lib/amber/static_page/render.rb', line 34

def render_to_file(dest_dir, options={})
  render_content_files(dest_dir, options)
  render_assets(dest_dir)
  @props.locales.each do |locale|
    if aliases(locale).any?
      link_page_aliases(dest_dir, aliases(locale), locale)
    end
  end
end

#scan_directory_tree(&block) ⇒ Object



47
48
49
# File 'lib/amber/static_page/filesystem.rb', line 47

def scan_directory_tree(&block)
  StaticPage.scan_directory_tree(self, self.file_path, File.join(self.path), &block)
end

#title(locale = I18n.locale) ⇒ Object



80
81
82
83
84
# File 'lib/amber/static_page.rb', line 80

def title(locale=I18n.locale)
  @title[locale] ||= begin
    @props.prop_with_fallback(locale, [:title, :nav_title]) || @name
  end
end