Module: StaticMatic::Helpers
Instance Method Summary collapse
- #current_page ⇒ Object
-
#img(name, options = {}) ⇒ Object
Generates an image tag always relative to the current page unless absolute path or http url specified.
-
#javascripts(*files) ⇒ Object
Generate javascript source tags for the specified files.
-
#link(title, href = "", options = {}) ⇒ Object
(also: #link_to)
Generate an HTML link.
-
#partial(name, options = {}) ⇒ Object
Include a partial template.
-
#stylesheets(*params) ⇒ Object
Generates links to all stylesheets in the source directory = stylesheets or specific stylesheets in a specific order = stylesheets :reset, :application Can also pass options hash in at the end so you can specify :media => :print.
-
#tag(name, options = {}, &block) ⇒ Object
Generates HTML tags:.
-
#text_area(name, value, options = {}) ⇒ Object
Generate a form textarea.
-
#text_field(name, value, options = {}) ⇒ Object
Generates a form text field.
-
#urlify(string) ⇒ Object
Generates a URL friendly string from the value passed:.
Instance Method Details
#current_page ⇒ Object
181 182 183 |
# File 'lib/staticmatic/helpers.rb', line 181 def current_page @staticmatic.current_page end |
#img(name, options = {}) ⇒ Object
Generates an image tag always relative to the current page unless absolute path or http url specified.
img(‘test_image.gif’) -> <img src=“/images/test_image.gif” alt=“Test image”/> img(‘contact/test_image.gif’) -> <img src=“/images/contact/test_image.gif” alt=“Test image”/> img(‘’) -> <img src=“” alt=“Test image”/>
133 134 135 136 137 |
# File 'lib/staticmatic/helpers.rb', line 133 def img(name, = {}) [:src] = name.match(%r{^((\.\.?)?/|https?://)}) ? name : "#{current_page_relative_path}images/#{name}" [:alt] ||= name.split('/').last.split('.').first.capitalize.gsub(/_|-/, ' ') tag :img, end |
#javascripts(*files) ⇒ Object
Generate javascript source tags for the specified files
javascripts(‘test’) -> <script language=“javascript” src=“javascripts/test.js”></script>
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/staticmatic/helpers.rb', line 66 def javascripts(*files) relative_path = current_page_relative_path output = "" files.each do |file| file_str = file.to_s src = file_str.match(%r{^((\.\.?)?/|https?://)}) ? file_str : "#{relative_path}javascripts/#{file_str}.js" output << tag(:script, :language => 'javascript', :src => src, :type => "text/javascript") { "" } end output end |
#link(title, href = "", options = {}) ⇒ Object Also known as: link_to
Generate an HTML link
If only the title is passed, it will automatically create a link from this value:
link(‘Test’) -> <a href=“test.html”>Test</a>
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 |
# File 'lib/staticmatic/helpers.rb', line 99 def link(title, href = "", = {}) if href.is_a?(Hash) = href href = "" end if href.nil? || href.strip.length < 1 path_prefix = '' if title.match(/^(\.\.?)?\//) # starts with relative path so strip it off and prepend it to the urlified title path_prefix_match = title.match(/^[^\s]*\//) path_prefix = path_prefix_match[0] if path_prefix_match title = title[path_prefix.length, title.length] end href = path_prefix + urlify(title) + ".html" end [:href] = "#{current_page_relative_path(href)}#{href}" local_page = ([:href].match(/^(\#|.+?\:)/) == nil) unless @staticmatic.configuration.use_extensions_for_page_links || !local_page [:href].chomp!(".html") [:href].chomp!("index") if [:href][-5, 5] == 'index' end tag(:a, ) { title } end |
#partial(name, options = {}) ⇒ Object
Include a partial template
177 178 179 |
# File 'lib/staticmatic/helpers.rb', line 177 def partial(name, = {}) @staticmatic.generate_partial(name, ) end |
#stylesheets(*params) ⇒ Object
Generates links to all stylesheets in the source directory
stylesheets
or specific stylesheets in a specific order
stylesheets :reset, :application
Can also pass options hash in at the end so you can specify :media => :print
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 |
# File 'lib/staticmatic/helpers.rb', line 10 def stylesheets(*params) = {} if params.last.is_a?(Hash) = params.last params.slice!(-1, 1) end [:media] = 'all' unless .has_key?(:media) [:rel] = 'stylesheet'; [:type] = 'text/css' relative_path = current_page_relative_path output = "" if params.length == 0 # no specific files requested so include all in no particular order stylesheet_dir = File.join(@staticmatic.src_dir, 'stylesheets') stylesheet_directories = Dir[File.join(stylesheet_dir, '**','*.sass')] # Bit of a hack here - adds any stylesheets that exist in the site/ dir that haven't been generated from source sass Dir[File.join(@staticmatic.site_dir, 'stylesheets', '*.css')].each do |filename| search_filename = File.basename(filename).chomp(File.extname(filename)) already_included = false stylesheet_directories.each do |path| if File.basename(path).include?(search_filename) already_included = true break end end stylesheet_directories << filename unless already_included end stylesheet_directories.each do |path| filename_without_extension = File.basename(path).chomp(File.extname(path)) [:href] = "#{relative_path}stylesheets/#{filename_without_extension}.css" output << tag(:link, ) end else #specific files requested and in a specific order params.each do |file| if File.exist?(File.join(@staticmatic.src_dir, 'stylesheets', "#{file}.sass")) || File.exist?(File.join(@staticmatic.site_dir, 'stylesheets', "#{file}.css")) [:href] = "#{relative_path}stylesheets/#{file}.css" output << tag(:link, ) end end end output end |
#tag(name, options = {}, &block) ⇒ Object
Generates HTML tags:
tag(:br) -> <br/> tag(:a, :href => ‘test.html’) { “Test” } -> <a href=“test.html”>Test</a>
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/staticmatic/helpers.rb', line 144 def tag(name, = {}, &block) [:id] ||= [:name] if [:name] output = "<#{name}" .keys.sort { |a, b| a.to_s <=> b.to_s }.each do |key| output << " #{key}=\"#{[key]}\"" if [key] end if block_given? output << ">" output << yield output << "</#{name}>" else output << "/>" end output end |
#text_area(name, value, options = {}) ⇒ Object
Generate a form textarea
87 88 89 90 |
# File 'lib/staticmatic/helpers.rb', line 87 def text_area(name, value, = {}) .merge!(:name => name) tag(:textarea, ) { value } end |
#text_field(name, value, options = {}) ⇒ Object
Generates a form text field
80 81 82 83 |
# File 'lib/staticmatic/helpers.rb', line 80 def text_field(name, value, = {}) .merge!(:type => "text", :name => name, :value => value) tag(:input, ) end |
#urlify(string) ⇒ Object
Generates a URL friendly string from the value passed:
“We love Haml” -> “we_love_haml” “Elf & Ham” -> “elf_and_ham” “Stephen’s gem” -> “stephens_gem”
167 168 169 170 171 172 173 174 |
# File 'lib/staticmatic/helpers.rb', line 167 def urlify(string) string.tr(" ", "_"). sub("&", "and"). sub("@", "at"). tr("^A-Za-z0-9_", ""). sub(/_{2,}/, "_"). downcase end |