Class: Shellfie::HtmlRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/shellfie/html_renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(config:, theme:) ⇒ HtmlRenderer

Returns a new instance of HtmlRenderer.



8
9
10
11
# File 'lib/shellfie/html_renderer.rb', line 8

def initialize(config:, theme:)
  @config = config
  @svg_renderer = SvgRenderer.new(config: config, theme: theme)
end

Instance Method Details

#render(geometry, output_path, transparent: false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shellfie/html_renderer.rb', line 13

def render(geometry, output_path, transparent: false)
  svg = @svg_renderer.to_svg(geometry, transparent: transparent).sub(/\A<\?xml[^>]+>\s*/, "")
  transcript = geometry[:lines].map { |line| line[:segments].map(&:text).join }.join("\n")
  File.write(output_path, <<~HTML)
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>#{escape(@config.title)}</title>
      <style>
        :root { color-scheme: light dark; }
        :root[data-theme="light"] { color-scheme: light; }
        :root[data-theme="dark"] { color-scheme: dark; }
        body { margin: 0; display: grid; min-height: 100vh; place-items: center; background: Canvas; }
        main { max-width: 100%; overflow: auto; }
        button { position: fixed; inset: 1rem 1rem auto auto; font: inherit; }
        svg { display: block; max-width: 100%; height: auto; }
        .transcript { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: pre; }
      </style>
    </head>
    <body>
      <button type="button" aria-label="Toggle color theme">Theme</button>
      <main aria-label="#{escape(@config.title)}">#{svg}<pre class="transcript">#{escape(transcript)}</pre></main>
      <script>
        document.querySelector('button').addEventListener('click', () => {
          const root = document.documentElement;
          root.dataset.theme = root.dataset.theme === 'dark' ? 'light' : 'dark';
        });
      </script>
    </body>
    </html>
  HTML
end