Module: Amber::Render::HtmlHelper

Included in:
View
Defined in:
lib/amber/render/helpers/date_helper.rb,
lib/amber/render/helpers/html_helper.rb

Instance Method Summary collapse

Instance Method Details

#amber_path(page_or_array, locale = I18n.locale) ⇒ Object

returns the ideal full url path for a page or path (expressed as an array).



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/amber/render/helpers/html_helper.rb', line 74

def amber_path(page_or_array, locale=I18n.locale)
  page = nil
  if page_or_array.is_a? Array
    page = @site.find_page_by_path(page_or_array)
  elsif page_or_array.is_a? StaticPage
    page = page_or_array
  end
  if page.nil?
    return ''
  end
  full_path = []
  full_path << @site.path_prefix if @site.path_prefix
  full_path << locale # always do this?
  if page.aliases(locale).any?
    full_path += page.aliases(locale).first
  else
    full_path += page.path
  end
  "/" + full_path.join('/')
end

#html_head_baseObject

return html markup suitable for setting the href base of the document. If this is added to the document’s HEAD, then relative urls will work as expected even though the url path does not end with a ‘/’



11
12
13
14
15
16
17
# File 'lib/amber/render/helpers/html_helper.rb', line 11

def html_head_base
  if @page
    "<base href=\"#{amber_path(@page)}/\" />"
  else
    ""
  end
end

three forms:

(1) link('page-name')
(2) link('label' => 'page-name')
(3) link('label' => 'https://url')

both accept optional options hash:

(1) link('page-name', :class => 'x')
(2) link('label' => 'page-name', :class => 'x')


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
# File 'lib/amber/render/helpers/html_helper.rb', line 31

def link(name, options=nil)
  options = nil if options && !options.is_a?(Hash)
  if name.is_a? Hash
    klass = name.delete(:class)
    label, name = name.to_a.first
    if label.is_a? Symbol
      label = I18n.t label
    end
  else
    klass = options[:class] if options
  end
  if name =~ /^#/ || name =~ /^http/ || name =~ /\./
    path = name
    label ||= name
  else
    if index = name.index('#')
      anchor = name[index..-1]
      name_without_anchor = name[0..(index-1)]
    else
      anchor = ''
      name_without_anchor = name
    end
    page = @site.find_page(name_without_anchor)
    if page
      label ||= page.nav_title
      path = amber_path(page) + anchor
    else
      puts "warning: dead link to `#{name_without_anchor}` from page `/#{I18n.locale}/#{@page.path.join('/')}`"
      label ||= name_without_anchor
      label += ' [dead link]'
      path = name_without_anchor
    end
  end
  if klass
    %(<a href="#{path}" class="#{klass}">#{label}</a>)
  else
    %(<a href="#{path}">#{label}</a>)
  end
end

#time_tag(date_or_time, *args, &block) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/amber/render/helpers/date_helper.rb', line 4

def time_tag(date_or_time, *args, &block)
  options  = args.last.is_a?(Hash) ? args.pop : {}
  format   = options.delete(:format) || :long
  content  = args.first || I18n.l(date_or_time, :format => format)
  #datetime = date_or_time.acts_like?(:time) ? date_or_time.xmlschema : date_or_time.iso8601
  datetime = date_or_time.iso8601
  (:time, content, {:datetime => datetime}.merge(options), &block)
end