Module: Philiprehberger::HtmlBuilder
- Defined in:
- lib/philiprehberger/html_builder.rb,
lib/philiprehberger/html_builder/node.rb,
lib/philiprehberger/html_builder/escape.rb,
lib/philiprehberger/html_builder/builder.rb,
lib/philiprehberger/html_builder/version.rb
Defined Under Namespace
Modules: Escape Classes: Builder, DoctypeNode, Error, Node, RawNode
Constant Summary collapse
- VERSION =
'0.6.0'
Class Method Summary collapse
-
.build {|Builder| ... } ⇒ String
Build HTML using a tag DSL (minified output).
-
.build_minified {|Builder| ... } ⇒ String
Build minified HTML (alias for build).
-
.build_pretty(indent_size: 2) {|Builder| ... } ⇒ String
Build pretty-printed HTML using a tag DSL.
-
.document(pretty: false, indent_size: 2) {|Builder| ... } ⇒ String
Build a full HTML5 document: emits ‘<!DOCTYPE html>` followed by the rendered block, separated by a newline.
-
.escape(value) ⇒ String
Escape HTML special characters in a string using the same escaper as the DSL.
-
.merge(*fragments) ⇒ String
Merge multiple HTML fragment strings into one.
Class Method Details
.build {|Builder| ... } ⇒ String
Build HTML using a tag DSL (minified output)
17 18 19 20 21 22 23 |
# File 'lib/philiprehberger/html_builder.rb', line 17 def self.build(&block) raise Error, 'a block is required' unless block builder = Builder.new builder.instance_eval(&block) builder.to_html end |
.build_minified {|Builder| ... } ⇒ String
Build minified HTML (alias for build)
44 45 46 |
# File 'lib/philiprehberger/html_builder.rb', line 44 def self.build_minified(&) build(&) end |
.build_pretty(indent_size: 2) {|Builder| ... } ⇒ String
Build pretty-printed HTML using a tag DSL
31 32 33 34 35 36 37 |
# File 'lib/philiprehberger/html_builder.rb', line 31 def self.build_pretty(indent_size: 2, &block) raise Error, 'a block is required' unless block builder = Builder.new builder.instance_eval(&block) builder.to_pretty_html(indent_size: indent_size) end |
.document(pretty: false, indent_size: 2) {|Builder| ... } ⇒ String
Build a full HTML5 document: emits ‘<!DOCTYPE html>` followed by the rendered block, separated by a newline.
The block is evaluated at the root level exactly like ‘.build` / `.build_pretty`, so the caller decides whether to add an `<html>` wrapper. When `pretty: true`, output is pretty-printed with the given indent size.
60 61 62 63 64 65 66 67 |
# File 'lib/philiprehberger/html_builder.rb', line 60 def self.document(pretty: false, indent_size: 2, &block) raise Error, 'a block is required' unless block builder = Builder.new builder.instance_eval(&block) body = pretty ? builder.to_pretty_html(indent_size: indent_size) : builder.to_html "#{DoctypeNode::DECLARATION}\n#{body}" end |
.escape(value) ⇒ String
Escape HTML special characters in a string using the same escaper as the DSL.
81 82 83 |
# File 'lib/philiprehberger/html_builder.rb', line 81 def self.escape(value) Escape.html(value) end |
.merge(*fragments) ⇒ String
Merge multiple HTML fragment strings into one
73 74 75 |
# File 'lib/philiprehberger/html_builder.rb', line 73 def self.merge(*fragments) fragments.join end |