Class: Trevl::HtmlRenderer
- Inherits:
-
Object
- Object
- Trevl::HtmlRenderer
- Defined in:
- lib/trevl/html_renderer.rb
Overview
Renders TREVL YAML to an HTML page that draws its charts with Highcharts.
Highcharts itself is loaded from the CDN by default; configure
highcharts_path to inline a local copy and get a self-contained file
that works offline.
html = Trevl.render_to_html(yaml_string)
File.write("chart.html", html)
For screenshots, open the HTML file with any headless browser:
- Grover gem: Grover.new(html).to_png
- Ferrum: browser.goto("file:///tmp/chart.html"); browser.screenshot
- Playwright MCP: browser_navigate + browser_take_screenshot
- CLI: npx puppeteer screenshot chart.html
Instance Method Summary collapse
-
#initialize(width: 800, height: 400, background: "#ffffff") ⇒ HtmlRenderer
constructor
A new instance of HtmlRenderer.
-
#render(yaml_string, params: {}, data: nil, data_sources: {}) ⇒ Object
Renders TREVL YAML to a standalone HTML string.
-
#render_component(result) ⇒ Object
Renders a single pre-rendered component hash to HTML.
Constructor Details
#initialize(width: 800, height: 400, background: "#ffffff") ⇒ HtmlRenderer
Returns a new instance of HtmlRenderer.
19 20 21 22 23 |
# File 'lib/trevl/html_renderer.rb', line 19 def initialize(width: 800, height: 400, background: "#ffffff") @width = width @height = height @background = background end |
Instance Method Details
#render(yaml_string, params: {}, data: nil, data_sources: {}) ⇒ Object
Renders TREVL YAML to a standalone HTML string. Returns a complete HTML document with all charts embedded.
27 28 29 30 31 32 33 |
# File 'lib/trevl/html_renderer.rb', line 27 def render(yaml_string, params: {}, data: nil, data_sources: {}) results = Trevl.render(yaml_string, params: params, data: data, data_sources: data_sources) charts = results.select { |r| r["type"] == "chart" && r["highchartsData"] } scores = results.select { |r| r["type"] == "score" && r["display"] } build_html(charts, scores) end |
#render_component(result) ⇒ Object
Renders a single pre-rendered component hash to HTML.
36 37 38 39 40 41 42 |
# File 'lib/trevl/html_renderer.rb', line 36 def render_component(result) case result["type"] when "chart" then build_html([result], []) when "score" then build_html([], [result]) else "" end end |