Module: Lookbook::CodeFormatter

Defined in:
lib/lookbook/code_formatter.rb

Class Method Summary collapse

Class Method Details

.beautify(source, language = "html") ⇒ Object



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

def beautify(source, language = "html")
  source = source.strip
  result = language.downcase == "html" ? HtmlBeautifier.beautify(source) : source
  result.strip.html_safe
end

.highlight(source, language, opts = {}) ⇒ Object



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

def highlight(source, language, opts = {})
  source&.strip! unless opts[:strip] == false
  source&.gsub!("&gt;", ">")&.gsub!("&lt;", "<")
  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