Class: Dictum::HtmlHelpers

Inherits:
Object
  • Object
show all
Defined in:
lib/dictum/html_helpers.rb

Overview

rubocop:disable ClassLength

Constant Summary collapse

BOOTSTRAP_JS =
'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js'.freeze
BOOTSTRAP_CSS =
'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'.freeze
JQUERY =
'https://code.jquery.com/jquery-1.12.2.min.js'.freeze
PRETTIFY =
'https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js'.freeze

Class Method Summary collapse

Class Method Details

.build {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



10
11
12
# File 'lib/dictum/html_helpers.rb', line 10

def build
  yield self
end

.button(text, glyphicon = 'glyphicon-menu-left') ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/dictum/html_helpers.rb', line 81

def button(text, glyphicon = 'glyphicon-menu-left')
  span = tag('span', nil, class: "glyphicon #{glyphicon}", 'aria-hidden' => 'true')
  paragraph = paragraph(text)
  button_content = span.to_s + paragraph.to_s
  button = tag('button', button_content, 'type' => 'button',
                                         'class' => 'btn btn-primary back dictum-button',
                                         'aria-label' => 'Left Align')
  tag('a', button.to_s, href: 'index.html')
end

.code(json) ⇒ Object



97
98
99
100
# File 'lib/dictum/html_helpers.rb', line 97

def code(json)
  return '' unless json
  tag('pre', json, class: 'prettyprint')
end

.code_block(title, json) ⇒ Object



91
92
93
94
95
# File 'lib/dictum/html_helpers.rb', line 91

def code_block(title, json)
  return '' unless json
  return code(json) unless title
  "#{sub_subtitle(title)}#{code(json)}"
end

.container(content) ⇒ Object



20
21
22
# File 'lib/dictum/html_helpers.rb', line 20

def container(content)
  tag('div', content.to_s, class: 'container-fluid')
end

.external_css(css_path) ⇒ Object



34
35
36
37
# File 'lib/dictum/html_helpers.rb', line 34

def external_css(css_path)
  return '' unless css_path
  "<link rel='stylesheet' href='#{css_path}' />"
end

.html_header(title, body_content, inline_css = nil) ⇒ Object



14
15
16
17
18
# File 'lib/dictum/html_helpers.rb', line 14

def html_header(title, body_content, inline_css = nil)
  "<!DOCTYPE html><html><head><title>#{title}</title>#{external_css(BOOTSTRAP_CSS)}"\
  "#{inline_css(inline_css)}</head><body>#{body_content}" \
  "#{script(JQUERY)}#{script(BOOTSTRAP_JS)}#{script(PRETTIFY)}</body></html>"
end

.inline_css(style) ⇒ Object



39
40
41
42
# File 'lib/dictum/html_helpers.rb', line 39

def inline_css(style)
  return '' unless style
  "<style>#{style}</style>"
end

.jumbotron(content) ⇒ Object



102
103
104
105
# File 'lib/dictum/html_helpers.rb', line 102

def jumbotron(content)
  return '' unless content
  tag('div', content, class: 'jumbotron')
end


57
58
59
# File 'lib/dictum/html_helpers.rb', line 57

def link(href, content)
  tag('a', content, href: './' + href)
end

.list_item(content) ⇒ Object



53
54
55
# File 'lib/dictum/html_helpers.rb', line 53

def list_item(content)
  tag('li', content)
end

.paragraph(text, html_class = nil) ⇒ Object



76
77
78
79
# File 'lib/dictum/html_helpers.rb', line 76

def paragraph(text, html_class = nil)
  return "<p>#{text}</p>" unless html_class
  tag('p', text, class: html_class)
end

.row(content) ⇒ Object



24
25
26
27
# File 'lib/dictum/html_helpers.rb', line 24

def row(content)
  internal_div = tag('div', content.to_s, class: 'col-md-8 col-md-offset-2')
  tag('div', internal_div.to_s, class: 'row')
end

.script(script_path) ⇒ Object



29
30
31
32
# File 'lib/dictum/html_helpers.rb', line 29

def script(script_path)
  return '' unless script_path
  tag('script', nil, src: script_path)
end

.sub_subtitle(text, html_class = nil) ⇒ Object



71
72
73
74
# File 'lib/dictum/html_helpers.rb', line 71

def sub_subtitle(text, html_class = nil)
  return "<h4>#{text}</h4>" unless html_class
  tag('h4', text, class: html_class)
end

.subtitle(text, html_class = nil) ⇒ Object



66
67
68
69
# File 'lib/dictum/html_helpers.rb', line 66

def subtitle(text, html_class = nil)
  return "<h3>#{text}</h3>" unless html_class
  tag('h3', text, class: html_class)
end

.table(headers, rows) ⇒ Object



116
117
118
119
120
121
# File 'lib/dictum/html_helpers.rb', line 116

def table(headers, rows)
  return '' unless headers
  answer = table_headers(headers)
  answer += table_rows(rows)
  tag('table', answer, class: 'table')
end

.tag(name, content, attributes = {}) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/dictum/html_helpers.rb', line 107

def tag(name, content, attributes = {})
  return '' unless name
  answer = "<#{name}"
  attributes.each do |key, value|
    answer += " #{key}='#{value}'"
  end
  answer += ">#{content}</#{name}>"
end

.title(text, html_class = nil) ⇒ Object



61
62
63
64
# File 'lib/dictum/html_helpers.rb', line 61

def title(text, html_class = nil)
  return "<h1>#{text}</h1>" unless html_class
  tag('h1', text, class: html_class)
end

.unordered_list(elements) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/dictum/html_helpers.rb', line 44

def unordered_list(elements)
  return '<ul></ul>' unless elements
  answer = '<ul>'
  elements.each do |element|
    answer += list_item(link("#{element.downcase}.html", element))
  end
  answer += '</ul>'
end