Module: Megatron::DocsHelper
- Defined in:
- app/helpers/megatron/docs_helper.rb
Constant Summary collapse
- DEMO_DEFAULTS =
{ type: :slim, class: 'demo', tag: :div }
Instance Method Summary collapse
- #code(lang, options = {}, &block) ⇒ Object
- #demo(title = nil, options = {}, &block) ⇒ Object
- #demo_box(title, options = {}, &block) ⇒ Object
- #demo_description(&block) ⇒ Object
- #extract_code(block, marker) ⇒ Object
-
#heading(*args) ⇒ Object
Ouptut a linkable heading.
- #heading_id(title) ⇒ Object
- #markdown(&block) ⇒ Object
- #markdown_table(classname = 'doc-table', &block) ⇒ Object
- #spaces_at_beginning(str) ⇒ Object
- #strip_description(lines) ⇒ Object
Instance Method Details
#code(lang, options = {}, &block) ⇒ Object
80 81 82 83 84 |
# File 'app/helpers/megatron/docs_helper.rb', line 80 def code(lang, = {}, &block) classes = ["language-#{lang}"] classes << .delete(:class) if [:class] content_tag(:pre, .merge(class: classes), &block) end |
#demo(title = nil, options = {}, &block) ⇒ Object
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 |
# File 'app/helpers/megatron/docs_helper.rb', line 10 def demo(title=nil, ={}, &block) if title.is_a? Hash = title title = nil end = DEMO_DEFAULTS.merge() content_tag [:tag], class: [:class] do rand = SecureRandom.hex(5) concat content_tag(:header, class: 'demo-header') { if title concat content_tag(:h3, class: 'demo-heading', id: heading_id(title)){ title } end if [:toggle] concat content_tag(:a, href: '#', class: 'demo-source-toggle', 'data-toggle' => "#source-#{rand}, #demo-#{rand}"){ 'source' } else concat content_tag(:a, href: '#', class: 'demo-source-toggle', 'data-toggle' => "#source-#{rand}"){ 'source' } end } concat code([:type], id: "source-#{rand}", class: 'hidden') { lines = extract_code(block, 'demo_box') } concat content_tag(:div, id: "demo-#{rand}", &block) end end |
#demo_box(title, options = {}, &block) ⇒ Object
40 41 42 43 44 45 |
# File 'app/helpers/megatron/docs_helper.rb', line 40 def demo_box(title, ={}, &block) [:class] = "#{[:class]} demo-box" [:class] << ' padded' unless [:padded] == false [:toggle] = true demo(title, , &block) end |
#demo_description(&block) ⇒ Object
76 77 78 |
# File 'app/helpers/megatron/docs_helper.rb', line 76 def demo_description(&block) content_tag :div, class: 'demo-description', &block end |
#extract_code(block, marker) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/helpers/megatron/docs_helper.rb', line 96 def extract_code(block, marker) filename, start_line = block.source_location lines = File.readlines(filename) start_line -= 1 until lines[start_line] =~ /#{marker}/ start_line += 1 end spaces = spaces_at_beginning(lines[start_line]) lines = lines[start_line + 1 .. -1] lines = lines.take_while { |line| spaces_at_beginning(line) > spaces || line.match(/^\n\s*$/) } .map { |line| line.sub(%r{^\s{#{spaces + 2}}}, '') } strip_description(lines).join("") end |
#heading(*args) ⇒ Object
Ouptut a linkable heading
62 63 64 65 66 67 68 69 70 |
# File 'app/helpers/megatron/docs_helper.rb', line 62 def heading(*args) content = args.pop # Default to h3 tag = args.pop || :h3 content_tag(tag.to_sym, class: 'link-heading', id: heading_id(content)) do concat content end end |
#heading_id(title) ⇒ Object
72 73 74 |
# File 'app/helpers/megatron/docs_helper.rb', line 72 def heading_id(title) title.gsub(/\W/, '-').downcase end |
#markdown(&block) ⇒ Object
86 87 88 89 90 |
# File 'app/helpers/megatron/docs_helper.rb', line 86 def markdown(&block) text = extract_code(block, 'markdown') #text = content_tag('div', {}, &block).gsub('<div>','') Kramdown::Document.new(text).to_html.html_safe end |
#markdown_table(classname = 'doc-table', &block) ⇒ Object
92 93 94 |
# File 'app/helpers/megatron/docs_helper.rb', line 92 def markdown_table(classname='doc-table', &block) markdown(&block).gsub(/<table/, "<table class='#{classname}'").html_safe end |
#spaces_at_beginning(str) ⇒ Object
117 118 119 |
# File 'app/helpers/megatron/docs_helper.rb', line 117 def spaces_at_beginning(str) str[/\A */].size end |
#strip_description(lines) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/helpers/megatron/docs_helper.rb', line 47 def strip_description(lines) start = lines.find_index{|x| x=~/=\s*demo_description/} if start spaces = spaces_at_beginning(lines[start]) count = lines[start+1 .. -1].take_while { |line| spaces_at_beginning(line) > spaces }.size lines.slice!(start..start+count) end lines end |