Class: Codnar::Formatter
- Inherits:
-
Object
- Object
- Codnar::Formatter
- Defined in:
- lib/codnar/formatter.rb
Overview
Format chunks into HTML.
Instance Method Summary collapse
-
#initialize(errors, formatters) ⇒ Formatter
constructor
Construct a Formatter based on a mapping from a classified line kind, to a Ruby expression, that converts an array of classified lines of that kind, into an array of lines of another kind.
-
#lines_to_html(lines) ⇒ Object
Repeatedly process an array of classified lines of arbitrary kinds until we obtain a single classified “line” containing a unified final HTML presentation of the original classified lines.
Constructor Details
#initialize(errors, formatters) ⇒ Formatter
Construct a Formatter based on a mapping from a classified line kind, to a Ruby expression, that converts an array of classified lines of that kind, into an array of lines of another kind. This expression is simply eval-ed, and is expected to make use of a variable called lines that contains an array of classified lines, as produced by a Scanner. The result of evaluating the expressions is expected to be an array of any number of classified lines of any kind.
Formatting repeatedly applies these formatting expressions, until the result is an array containing a single classified line, which has the kind html and whose payload field contains the unified final HTML presentation of the original classified lines. In each processing round, all consecutive lines of the same kind are formated together. This allows for properly formating line kinds that use a multi-line notation such as Markdown.
The default formatting expression for the kind html simply joins all the payloads of all the classified lines into a single html, and returns a single “line” containing this joined HTML. All other line kinds need to have a formatting expression explicitly specified in the formatters mapping.
If no formatting expression is specified for some classified line kind, an error is reported and the classified lines are wrapped in a pre HTML element with a missing_formatter CSS class. Similarly, if a formatting expression fails (raises an exception), an error is reported and the lines are wrapped in a pre HTML element with a failed_formatter CSS class.
34 35 36 37 |
# File 'lib/codnar/formatter.rb', line 34 def initialize(errors, formatters) @errors = errors @formatters = { "html" => "Formatter.merge_html_lines(lines)" }.merge(formatters) end |
Instance Method Details
#lines_to_html(lines) ⇒ Object
Repeatedly process an array of classified lines of arbitrary kinds until we obtain a single classified “line” containing a unified final HTML presentation of the original classified lines.
42 43 44 45 46 47 |
# File 'lib/codnar/formatter.rb', line 42 def lines_to_html(lines) until Formatter.single_html_line?(lines) lines = Grouper.lines_to_groups(lines).map { |group| process_lines_group(group) }.flatten end return lines.last.andand.payload.to_s end |