Class: LivetextASTToHTML

Inherits:
Object show all
Defined in:
lib/livetext/ast_to_html.rb

Overview

AST to HTML Converter Converts LivetextAST output back to HTML

Class Method Summary collapse

Class Method Details

.convert(ast) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/livetext/ast_to_html.rb', line 5

def self.convert(ast)
  case ast
  when Array
    if ast.empty?
      ""  # empty array maps to empty string
    elsif ast.first == :text
      # [:text, "Hello ", [:bold, "world"], "!"]
      ast[1..-1].map { |part| convert(part) }.join
    elsif ast.length == 2
      # [:bold, "content"], [:italic, "content"], etc.
      format_type, content = ast
      case format_type
      when :bold then "<b>#{content}</b>"
      when :italic then "<i>#{content}</i>"
      when :code then "<tt>#{content}</tt>"
      when :strike then "<strike>#{content}</strike>"
      else content  # fallback
      end
    else
      ast.to_s  # fallback
    end
  when String
    ast
  else
    ast.to_s
  end
end