Module: Zenweb::Page::MarkdownHelpers

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.page_url(page) ⇒ Object



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

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.



104
105
106
107
108
109
# File 'lib/zenweb/plugins/markdown.rb', line 104

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.



114
115
116
# File 'lib/zenweb/plugins/markdown.rb', line 114

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

#css_id(name) ⇒ Object

Return a kramdown block-tag for a CSS ID.



121
122
123
# File 'lib/zenweb/plugins/markdown.rb', line 121

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

#date_sorted_map(a, &b) ⇒ Object

:nodoc:



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

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:



88
89
90
# File 'lib/zenweb/plugins/markdown.rb', line 88

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.



135
136
137
# File 'lib/zenweb/plugins/markdown.rb', line 135

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

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



128
129
130
# File 'lib/zenweb/plugins/markdown.rb', line 128

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

#page_sitemap_url(page, depth) ⇒ Object

:nodoc:



80
81
82
# File 'lib/zenweb/plugins/markdown.rb', line 80

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.



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

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(&:url).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

#tocObject

Convenience function to return a markdown TOC.



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

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