Module: StaticMatic::Helpers::UrlHelper
- Extended by:
- UrlHelper
- Included in:
- StaticMatic::Helpers, UrlHelper
- Defined in:
- lib/staticmatic/helpers/url_helper.rb
Instance Method Summary collapse
-
#link(title, href = "", options = {}) ⇒ Object
(also: #link_to)
Generate an HTML link.
-
#urlify(string) ⇒ Object
Generates a URL friendly string from the value passed:.
Instance Method Details
#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>
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 |
# File 'lib/staticmatic/helpers/url_helper.rb', line 13 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 |
#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”
48 49 50 51 52 53 54 55 |
# File 'lib/staticmatic/helpers/url_helper.rb', line 48 def urlify(string) string.tr(" ", "_"). sub("&", "and"). sub("@", "at"). tr("^A-Za-z0-9_", ""). sub(/_{2,}/, "_"). downcase end |