Module: Lookbook::CodeFormatter

Defined in:
lib/lookbook/code_formatter.rb

Class Method Summary collapse

Class Method Details

.beautify(source, **opts) ⇒ Object



17
18
19
20
21
22
# File 'lib/lookbook/code_formatter.rb', line 17

def beautify(source, **opts)
  language = opts[:language] || "html"
  source = source.strip
  result = language.downcase == "html" ? HtmlBeautifier.beautify(source) : source
  result.strip.html_safe
end

.highlight(source, **opts) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/lookbook/code_formatter.rb', line 8

def highlight(source, **opts)
  coder = HTMLEntities.new
  source = coder.decode source
  language = opts[:language] || "ruby"
  formatter = Formatter.new(**opts)
  lexer = Rouge::Lexer.find(language.to_s) || Rouge::Lexer.find("plaintext")
  formatter.format(lexer.lex(source)).html_safe
end