Module: Moka::Helpers

Included in:
PageScope
Defined in:
lib/commands/lib/helpers.rb

Instance Method Summary collapse

Instance Method Details

#current_page?(page) ⇒ Boolean

Returns true if the page passed as an argument is the page currently being compiled (a.k.a. the @current_page), else returns false. The argument can be a PageNode object, or a string in the form ‘groupname:pagename’ (if group isn’t specified, ‘root’ is assumed).

Examples:

<ul>

<% @current_group.pages.each do |page| %>
  <li>
    <% if current_page? page %>
      <%= page.name.titleize %>
    <% else %>
      <%= link_to page.name.titleize, page %>
    <% end %>
  </li>
<% end %>

</ul>

Returns:

  • (Boolean)

22
23
24
25
26
27
28
29
# File 'lib/commands/lib/helpers.rb', line 22

def current_page?(page)
  unless page.respond_to? :path
    page_name, group_name = Moka::Utilities.parse_grouppage(page)
    return @current_page.path == @site.find_group(group_name).find_page(page_name).path
  else
    return @current_page.path == page.path
  end
end

#image_tag(*args) ⇒ Object

Returns an <img> tag for the file path passed as a parameter. If the file path is relative and does not start with “/”, then the path is assumed to be relative to the compiled/images directory, else it is assumed to be relative to the root (compiled) directory or absolute.


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/commands/lib/helpers.rb', line 32

def image_tag(*args)
  html_options = args.extract_options!
  tag_options = ""
  html_options.each do |opt, val|
    tag_options << " #{opt.to_s}=\"#{val}\""
  end
  tag_string = ""
  img = args.first
  if img.index(/\A(http:\/\/|https:\/\/)/i).nil?
    img = adapt_path((img[0] == ?/) ? img : "images/" + img)
  end
  tag_string << "<img src=\"#{img}\"#{tag_options} />\n"
  return tag_string
end

#javascript_include_tag(*sources) ⇒ Object

Returns one or more <script> tags to include each Javascript file path (absolute or relative) passed as a parameter. If a file has no extension, “.js” is assumed. If a relative file path does not start with “/”, then the path is assumed to be relative to the compiled/javascripts directory, else it is assumed to be relative to the root (compiled) directory. If the first parameter is :all, then a script tag for every file in the compiled/javascripts directory is returned.


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/commands/lib/helpers.rb', line 48

def javascript_include_tag(*sources)
  html_options = sources.extract_options!
  tag_options = ""
  html_options.each do |opt, val|
    tag_options << " #{opt.to_s}=\"#{val}\""
  end
  tag_string = ""
  if sources.first.to_s == "all"
    sources = Dir.glob(File.expand_path("compiled/javascripts/*.js", MOKA_ROOT)).collect{|f| File.basename(f)}
  end
  sources.each do |s|
    if s.index(/\A(http:\/\/|https:\/\/)/i).nil?
      s = (File.extname(s) == "") ? s + ".js" : s
      s = adapt_path((s[0] == ?/) ? s : "javascripts/" + s)
    end
    tag_string << "<script type=\"text/javascript\" src=\"#{s}\"#{tag_options}></script>\n"
  end
  return tag_string
end

Returns an html link tag (<a …>…</a>) for the PageNode or path passed as the second argument. Syntax is link_to(anchor_text, [page or path], [html_options]). If a relative path is passed, it must be relative to the root directory (the function takes care of making it relative to the @current_page).


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/commands/lib/helpers.rb', line 69

def link_to(*args)
  name         = args[0]
  page         = args[1] || name
  html_options = args[2]

  url = path_to(page)

  if html_options
    tag_options = ""
    html_options.each do |opt, val|
      tag_options << " #{opt.to_s}=\"#{val}\""
    end
    href = html_options['href']
  else
    tag_options = nil
  end

  href_attr = "href=\"#{url}\"" unless href
  "<a #{href_attr}#{tag_options}>#{name.respond_to?(:name) ? name.name : name}</a>"
end

#path_to(page) ⇒ Object

If the argument is a PageNode object, returns the path to that page relative to the @current_page. If the argument is a relative path, it is assumed to be relative to the root, changed and made relative to the @current_page and returned. If the argument is an absolute path, it is returned unchanged.


91
92
93
94
95
96
97
98
99
100
# File 'lib/commands/lib/helpers.rb', line 91

def path_to(page)
  pre = back_to_root(@current_page.path)
  if page.is_a? Moka::SiteTree::PageNode
    # page is a PageNode object
    return adapt_path(page.path)
  else
    # page is a path
    return adapt_path(page.to_s)
  end
end

Returns one or more <link> tags to include each stylesheet file path passed as a parameter. If a file path has no extension, “.css” is assumed. The path can be absolute or relative. If a relative file path does not start with “/”, then the path is assumed to be relative to the compiled/stylesheets directory, else it is assumed to be relative to the root (compiled) directory. If the first parameter is :all, then a link tag for every file in the compiled/stylesheets directory (or that will be compiled in compiled/stylesheets) is returned.


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/commands/lib/helpers.rb', line 103

def stylesheet_link_tag(*sources)
  html_options = sources.extract_options!
  tag_options = ""
  html_options.each do |opt, val|
    tag_options << " #{opt.to_s}=\"#{val}\""
  end
  tag_string = ""
  if sources.first.to_s == "all"
    sources = Dir.glob(File.expand_path("compiled/stylesheets/*.css", MOKA_ROOT)).collect{|f| File.basename(f)}
    sources += Dir.glob(File.expand_path("project/styles/*.sass", MOKA_ROOT)).collect{|f| File.basename(f, ".sass") + ".css"}
    sources.uniq!
  end
  sources.each do |s|
    if s.index(/\A(http:\/\/|https:\/\/)/i).nil?
      s = (File.extname(s) == "") ? s + ".css" : s
      s = adapt_path((s[0] == ?/) ? s : "stylesheets/" + s)
    end
    tag_string << "<link rel=\"stylesheet\" href=\"#{s}\" type=\"text/css\"#{tag_options} />\n"
  end
  return tag_string
end