diff options
author | Achilleas Pipinellis <axilleas@axilleas.me> | 2017-09-12 19:30:40 +0300 |
---|---|---|
committer | Achilleas Pipinellis <axilleas@axilleas.me> | 2017-09-12 19:44:14 +0300 |
commit | 450b2c51ad74ea531104b5a578ccb9a869b252ce (patch) | |
tree | dae7e12f04246cefcbc7e988fea3f36df4080b6c /lib | |
parent | 0fbbc80434f5f966326abc6e0c5e3e714ac49328 (diff) |
Add admonition styles for notes, tips, warnings, etc.
There are currently 4 styles: INFO, TIP, CAUTION, DANGER
In a doc they are used like:
NOTE: This is a note
Diffstat (limited to 'lib')
-rw-r--r-- | lib/filters/admonition.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/filters/admonition.rb b/lib/filters/admonition.rb new file mode 100644 index 00000000..4bf5a080 --- /dev/null +++ b/lib/filters/admonition.rb @@ -0,0 +1,42 @@ +# encoding: utf-8 + +# Adapted from the admonition code on http://nanoc.ws/ +class AdmonitionFilter < Nanoc::Filter + + identifier :admonition + + BOOTSTRAP_MAPPING = { + 'tip' => 'success', + 'note' => 'info', + 'caution' => 'warning', + 'danger' => 'danger', + } + + FONT_AWESOME_MAPPING = { + 'note' => 'pencil', + 'tip' => 'info-circle', + 'warning' => 'exclamation-triangle', + 'danger' => 'bolt', + } + + def run(content, params = {}) + # `#dup` is necessary because `.fragment` modifies the incoming string. Ew! + # See https://github.com/sparklemotion/nokogiri/issues/1077 + doc = Nokogiri::HTML.fragment(content.dup) + doc.css('p').each do |para| + content = para.inner_html + next if content !~ /\A(TIP|NOTE|CAUTION|DANGER): (.*)\Z/m + new_content = generate($1.downcase, $2) + para.replace(new_content) + end + doc.to_s + end + + def generate(kind, content) + %[<div class="admonition-wrapper #{kind}">] + + %[<div class="admonition alert alert-#{BOOTSTRAP_MAPPING[kind]}">] + + content + + %[</div></div>] + end + +end |