/
helpers.rb
57 lines (50 loc) · 1.24 KB
/
helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: false
module Kitabu
module Helpers
def image_tag(path, _attributes = {})
%[<img alt="" src="assets/images/#{path}" />]
end
def escape_html(content)
CGI.escape_html(content.to_s)
end
alias h escape_html
def note(class_name = :info, &block)
content = block_content(block)
type = class_name.to_s
title = I18n.t(
type,
scope: :notes,
default: type.titleize
)
output << format(
'<div class="note %{type}">',
type: escape_html(class_name)
)
output << format(
'<p class="note--title">%{title}</p>',
title: escape_html(title)
)
output << markdown(content)
output << "</div>"
end
def block_content(block)
output = @_output.dup
@_output = ""
content = block.call
@_output = output
content
end
def markdown(content, deindent_content: true)
content = deindent(content) if deindent_content
Markdown.render(content)
end
def deindent(content)
content = content.to_s
indent = (content.scan(/^[ \t]*(?=\S)/) || []).size
content.gsub(/^[ \t]{#{indent}}/, "")
end
def output
@_output
end
end
end