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.



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

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.



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

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

#css_id(name) ⇒ Object

Return a kramdown block-tag for a CSS ID.



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

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

#date_sorted_map(a, &b) ⇒ Object

:nodoc:



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

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:



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

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.



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

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

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



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

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

#page_sitemap_url(page, depth) ⇒ Object

:nodoc:



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

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.



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

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.



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

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