Module: Zenweb::Page::MarkdownHelpers

Defined in:
lib/zenweb/plugins/markdown.rb

Instance Method Summary collapse

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.



92
93
94
95
96
97
# File 'lib/zenweb/plugins/markdown.rb', line 92

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.



102
103
104
# File 'lib/zenweb/plugins/markdown.rb', line 102

def css_class name
  attr ".#{name}"
end

#css_id(name) ⇒ Object

Return a kramdown block-tag for a CSS ID.



109
110
111
# File 'lib/zenweb/plugins/markdown.rb', line 109

def css_id name
  attr "##{name}"
end

#date_sorted_map(a, &b) ⇒ Object

:nodoc:



72
73
74
# File 'lib/zenweb/plugins/markdown.rb', line 72

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:



76
77
78
# File 'lib/zenweb/plugins/markdown.rb', line 76

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.



123
124
125
# File 'lib/zenweb/plugins/markdown.rb', line 123

def image url, alt=url
  "![#{alt}](#{url})"
end

Return a markdown-formatted link for a given url and title.



116
117
118
# File 'lib/zenweb/plugins/markdown.rb', line 116

def link(url, title) # :nodoc:
  "[#{title}](#{url})"
end

#page_sitemap_url(page, depth) ⇒ Object

:nodoc:



68
69
70
# File 'lib/zenweb/plugins/markdown.rb', line 68

def page_sitemap_url page, depth # :nodoc:
  "#{"  " * (depth)}* [#{page.title}](#{page.clean_url})"
end

#sitemapObject

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zenweb/plugins/markdown.rb', line 51

def sitemap
  self.all_subpages.deep_each.chunk { |n, p| n }.map { |depth, a|
    level = (depth-1)/2
    dated, normal = a.map(&:last).partition(&:dated?)

    normal = normal.sort_by(&:url).map { |p| page_sitemap_url p, level }

    dated = dated_map(dated) { |month, ps2|
      date_sorted_map(ps2) { |p|
        page_sitemap_url p, level + 1
      }.unshift "#{"  " * level}* #{month}:"
    }

    normal + dated
  }.join "\n"
end

#tocObject

Convenience function to return a markdown TOC.



83
84
85
# File 'lib/zenweb/plugins/markdown.rb', line 83

def toc
  "* \n{:toc}\n"
end