Class: EscapeCode::HtmlFormatter
- Inherits:
-
Object
- Object
- EscapeCode::HtmlFormatter
- Defined in:
- lib/escape_code/html_formatter.rb
Defined Under Namespace
Classes: ColorScheme
Instance Method Summary collapse
- #generate ⇒ Object
- #generate_page ⇒ Object
- #generate_stylesheet ⇒ Object
-
#initialize(text, prefix = '', color_scheme: EscapeCode::HtmlFormatter::ColorScheme.default) ⇒ HtmlFormatter
constructor
A new instance of HtmlFormatter.
Constructor Details
#initialize(text, prefix = '', color_scheme: EscapeCode::HtmlFormatter::ColorScheme.default) ⇒ HtmlFormatter
Returns a new instance of HtmlFormatter.
4 5 6 7 8 |
# File 'lib/escape_code/html_formatter.rb', line 4 def initialize(text, prefix = '', color_scheme: EscapeCode::HtmlFormatter::ColorScheme.default) @prefix = prefix @color_scheme = color_scheme @scanner = EscapeCode::Scanner.new(text) end |
Instance Method Details
#generate ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/escape_code/html_formatter.rb', line 14 def generate state = EscapeCode::SgrState.new @scanner.scan.map do |thing| case thing when EscapeCode::Code state.ingest(thing) when String classes = compute_classes(state) if classes.empty? CGI.escapeHTML(thing) else "<span class='#{compute_classes(state)}'>#{CGI.escapeHTML(thing)}</span>" end end end.compact.join end |
#generate_page ⇒ Object
32 33 34 |
# File 'lib/escape_code/html_formatter.rb', line 32 def generate_page "<html><head><style>#{generate_stylesheet}</style></head><body><pre>#{generate}</pre></body></html>" end |
#generate_stylesheet ⇒ Object
10 11 12 |
# File 'lib/escape_code/html_formatter.rb', line 10 def generate_stylesheet @color_scheme.generate_stylesheet end |