Module: Zenweb::Page::MarkdownHelpers
- Defined in:
- lib/zenweb/plugins/markdown.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#attr(h_or_name) ⇒ Object
Return a kramdown block-tag to add attributes to the following (or preceding… kramdown is a bit crazy) block.
-
#css_class(name) ⇒ Object
Return a kramdown block-tag for a CSS class.
-
#css_id(name) ⇒ Object
Return a kramdown block-tag for a CSS ID.
-
#date_sorted_map(a, &b) ⇒ Object
:nodoc:.
-
#dated_map(a, &b) ⇒ Object
:nodoc:.
-
#image(url, alt = url) ⇒ Object
Return a markdown-formatted image for a given url and an optional alt.
-
#link(url, title) ⇒ Object
Return a markdown-formatted link for a given url and title.
-
#page_sitemap_url(page, depth) ⇒ Object
:nodoc:.
-
#sitemap(title_dated = true, demote = 0) ⇒ Object
Returns a markdown formatted sitemap for the given pages or the current page’s subpages.
-
#toc ⇒ Object
Convenience function to return a markdown TOC.
Class Method Details
.page_url(page) ⇒ Object
80 81 82 |
# File 'lib/zenweb/plugins/markdown.rb', line 80 def page_url page "[#{page.title}](#{page.clean_url})" end |
Instance Method Details
#attr(h_or_name) ⇒ Object
Return a kramdown block-tag to add attributes to the following (or preceding… kramdown is a bit crazy) block. Attributes can either be a simple name or a hash of key/value pairs.
110 111 112 113 114 115 |
# File 'lib/zenweb/plugins/markdown.rb', line 110 def attr h_or_name h_or_name = h_or_name.map { |k,v| "#{k}=\"#{v}\"" }.join " " if Hash === h_or_name "{:#{h_or_name}}" end |
#css_class(name) ⇒ Object
Return a kramdown block-tag for a CSS class.
120 121 122 |
# File 'lib/zenweb/plugins/markdown.rb', line 120 def css_class name attr ".#{name}" end |
#css_id(name) ⇒ Object
Return a kramdown block-tag for a CSS ID.
127 128 129 |
# File 'lib/zenweb/plugins/markdown.rb', line 127 def css_id name attr "##{name}" end |
#date_sorted_map(a, &b) ⇒ Object
:nodoc:
90 91 92 |
# File 'lib/zenweb/plugins/markdown.rb', line 90 def date_sorted_map a, &b # :nodoc: a.sort_by { |p| [-p.date.to_i, p.url] }.map(&b) end |
#dated_map(a, &b) ⇒ Object
:nodoc:
94 95 96 |
# File 'lib/zenweb/plugins/markdown.rb', line 94 def dated_map a, &b # :nodoc: a.group_by(&:date_str).sort.reverse.map(&b) end |
#image(url, alt = url) ⇒ Object
Return a markdown-formatted image for a given url and an optional alt.
141 142 143 |
# File 'lib/zenweb/plugins/markdown.rb', line 141 def image url, alt=url "![#{alt}](#{url})" end |
#link(url, title) ⇒ Object
Return a markdown-formatted link for a given url and title.
134 135 136 |
# File 'lib/zenweb/plugins/markdown.rb', line 134 def link(url, title) # :nodoc: "[#{title}](#{url})" end |
#page_sitemap_url(page, depth) ⇒ Object
:nodoc:
86 87 88 |
# File 'lib/zenweb/plugins/markdown.rb', line 86 def page_sitemap_url page, depth # :nodoc: "#{" " * (depth)}* #{page_url page}" end |
#sitemap(title_dated = true, demote = 0) ⇒ Object
Returns a markdown formatted sitemap for the given pages or the current page’s subpages. This intelligently composes a sitemap whether the pages are ordered (dated) or not or a combination of the two.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/zenweb/plugins/markdown.rb', line 58 def sitemap title_dated = true, demote = 0 self.all_subpages_by_level(true).chunk { |n, p| n }.map { |level, a| level -= demote level = 0 if level < 0 dated, normal = a.map(&:last).reject(&:no_index?).partition(&:dated?) normal = normal.sort_by { |p| p.url.downcase }.map { |p| page_sitemap_url p, level } dated = dated_map(dated) { |month, ps2| x = date_sorted_map(ps2) { |p| page_sitemap_url p, level + (title_dated ? 1 : 0) } x.unshift "#{" " * level}* #{month}:" if title_dated x } normal + dated }.join "\n" end |
#toc ⇒ Object
Convenience function to return a markdown TOC.
101 102 103 |
# File 'lib/zenweb/plugins/markdown.rb', line 101 def toc "* \n{:toc}\n" end |