Module: HtmlSlice

Defined in:
lib/html_slice.rb,
lib/html_slice/rails.rb,
lib/html_slice/version.rb

Defined Under Namespace

Modules: Rails Classes: Error

Constant Summary collapse

TAGS =
%i[
  div title embed meta br a em b i ul ol li img table tbody thead tr th td strong
  form input button link h1 h2 h3 h4 h5 h6 hr span label iframe template main
  footer aside source section small nav area
].freeze
EMPTY_TAGS =
%i[
  area br embed hr img input link meta source
].freeze
TAGS_WITHOUT_HTML_ESCAPE =
%i[
  style script
].freeze
DEFAULT_SLICE =
:default
ATTRIBUTE_KEY_CACHE =
{}
STR =
{
  empty: "",
  space: " ",
  underline: "_",
  dash: "-",
  attr_equal: "='",
  single_quote: "'",
  doctype_html: "<!DOCTYPE html><html>",
  end_html: "</html>",
  gt: ">",
  gt_slash: "/>",
  lt: "<",
  lt_slash: "</",
}.freeze
VERSION =
"0.2.7"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.layout(slice_id = DEFAULT_SLICE, &block) ⇒ Object



48
49
50
51
# File 'lib/html_slice.rb', line 48

def self.layout(slice_id = DEFAULT_SLICE, &block)
  @class ||= Class.new { include HtmlSlice }.new
  @class.html_layout(slice_id, &block)
end

.slice(slice_id = DEFAULT_SLICE, wrap: [STR[:empty], STR[:empty]], &block) ⇒ Object



43
44
45
46
# File 'lib/html_slice.rb', line 43

def self.slice(slice_id = DEFAULT_SLICE, wrap: [STR[:empty], STR[:empty]], &block)
  @class ||= Class.new { include HtmlSlice }.new
  @class.html_slice(slice_id, wrap: wrap, &block)
end

Instance Method Details

#_(text) ⇒ Object



93
94
95
# File 'lib/html_slice.rb', line 93

def _(text)
  @html_slice[@html_slice_current_id] << text.to_s
end

#html_layout(slice_id = DEFAULT_SLICE, &block) ⇒ Object



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

def html_layout(slice_id = DEFAULT_SLICE, &block)
  html_slice(slice_id, wrap: [STR[:doctype_html], STR[:end_html]], &block)
end

#html_slice(slice_id = DEFAULT_SLICE, wrap: [STR[:empty], STR[:empty]], &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/html_slice.rb', line 57

def html_slice(slice_id = DEFAULT_SLICE, wrap: [STR[:empty], STR[:empty]], &block)
  @html_slice_current_id = slice_id
  @html_slice ||= {}

  if block_given?
    buffer = +STR[:empty]
    buffer << wrap[0]
    @html_slice[slice_id] = buffer
    instance_eval(&block)
    buffer << wrap[1]
  end

  @html_slice[slice_id].to_s
end

#tag(tag_name, *args, &block) ⇒ Object



88
89
90
91
# File 'lib/html_slice.rb', line 88

def tag(tag_name, *args, &block)
  content, attrs = parse_args(args, escape: true)
  render_tag(tag_name.to_s, tag_name, content, attrs, &block)
end