Module: Middleman::Tansu::Helpers
- Defined in:
- lib/middleman-tansu/helpers.rb
Overview
Tansu Helpers
Instance Method Summary collapse
- #base_title ⇒ Object
- #breadcrumbs(klass = 'breadcrumbs', root = 'Top') ⇒ Object
- #children_pages(key = :date, order_by = :asc) ⇒ Object
- #exclude ⇒ Object
- #exclude?(path) ⇒ Boolean
- #heading ⇒ Object
- #index? ⇒ Boolean
- #page_name(page) ⇒ Object
- #page_title(path) ⇒ Object
- #page_title_or_path(path) ⇒ Object
- #page_url(page) ⇒ Object
- #path_list(current_path) ⇒ Object
- #title(splitter = ' - ') ⇒ Object
Instance Method Details
#base_title ⇒ Object
65 66 67 |
# File 'lib/middleman-tansu/helpers.rb', line 65 def base_title config[:site_title] || 'Middleman-Tansu' end |
#breadcrumbs(klass = 'breadcrumbs', root = 'Top') ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/middleman-tansu/helpers.rb', line 18 def (klass = 'breadcrumbs', root = 'Top') li = [] root = page_title('/') || root li.push("<li class=\"root\">#{link_to(root, '/')}</li>") paths = path_list(current_resource.path) paths.each do |path| name = page_title(path[:path]) || path[:name] if path == paths.last li.push("<li class=\"current\">#{h(name)}</li>") else li.push("<li>#{link_to(name, path[:path])}</li>") end end "<ul class=\"#{klass}\">\n#{li.join("\n")}\n</ul>" end |
#children_pages(key = :date, order_by = :asc) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/middleman-tansu/helpers.rb', line 74 def children_pages(key = :date, order_by = :asc) dirs = [] pages = [] current_resource.children.each do |page| unless exclude?(page.path) if /index\.html$/ =~ page.path dirs.push(page) else pages.push(page) end end next end # Sorting pages and dirs if order_by == :desc pages = pages.sort do |a, b| b.data[key] <=> a.data[key] end dirs = dirs.sort do |a, b| b.path <=> a.path end else pages = pages.sort do |a, b| a.data[key] <=> b.data[key] end dirs = dirs.sort do |a, b| a.path <=> b.path end end dirs | pages end |
#exclude ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/middleman-tansu/helpers.rb', line 114 def exclude default = [ config.images_dir, config.js_dir, config.css_dir, config.layouts_dir, config.tansu[:templates_dir] ] default | config.tansu[:exclude_path] end |
#exclude?(path) ⇒ Boolean
109 110 111 112 |
# File 'lib/middleman-tansu/helpers.rb', line 109 def exclude?(path) regex = Regexp.new("^(#{exclude.join('|')})") regex =~ path end |
#heading ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/middleman-tansu/helpers.rb', line 56 def heading page_title = page_title_or_path(current_resource.path) if !page_title.empty? page_title else base_title end end |
#index? ⇒ Boolean
69 70 71 72 |
# File 'lib/middleman-tansu/helpers.rb', line 69 def index? regex = Regexp.new("#{config.tansu[:default_document]}$") regex =~ current_resource.path || '/' == current_resource.path end |
#page_name(page) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/middleman-tansu/helpers.rb', line 125 def page_name(page) if page.data.title paths = page.path.split('/') paths.pop File.join(paths, page.data.title).gsub(/^\//, '') else page.path.gsub(/(\/index)?\.html$/, '') end end |
#page_title(path) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/middleman-tansu/helpers.rb', line 36 def page_title(path) if /\.html$/ !~ path path = File.join(path, config.tansu[:default_document]) end sitemap.find_resource_by_path(path).data.title end |
#page_title_or_path(path) ⇒ Object
43 44 45 |
# File 'lib/middleman-tansu/helpers.rb', line 43 def page_title_or_path(path) page_title(path) || path.gsub(/(index)?\.html$/, '') end |
#page_url(page) ⇒ Object
135 136 137 |
# File 'lib/middleman-tansu/helpers.rb', line 135 def page_url(page) File.join('/', page.path.gsub(/index\.html$/, '')) end |
#path_list(current_path) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/middleman-tansu/helpers.rb', line 5 def path_list(current_path) paths = [] splited = current_path.split('/') splited.each_with_index do |val, index| path = File.join('/', splited[0..index]) path += '/' unless /\.html$/ =~ path val = val.sub('.html', '') if /\.html$/ =~ val paths.push(path: path, name: val) unless /^index$/ =~ val end paths end |
#title(splitter = ' - ') ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/middleman-tansu/helpers.rb', line 47 def title(splitter = ' - ') page_title = page_title_or_path(current_resource.path) if !page_title.empty? "#{page_title}#{splitter}#{base_title}" else base_title end end |