Class: Lookbook::CodeBeautifier

Inherits:
Service
  • Object
show all
Defined in:
lib/lookbook/services/code/code_beautifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Service

call

Constructor Details

#initialize(source, opts = {}) ⇒ CodeBeautifier

Returns a new instance of CodeBeautifier.



7
8
9
10
# File 'lib/lookbook/services/code/code_beautifier.rb', line 7

def initialize(source, opts = {})
  @source = source.to_s
  @opts = opts
end

Instance Attribute Details

#optsObject (readonly)



5
6
7
# File 'lib/lookbook/services/code/code_beautifier.rb', line 5

def opts
  @opts
end

#sourceObject (readonly)



5
6
7
# File 'lib/lookbook/services/code/code_beautifier.rb', line 5

def source
  @source
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
# File 'lib/lookbook/services/code/code_beautifier.rb', line 12

def call
  language = opts.fetch(:language, "html")
  stripped_source = source.strip
  result = (language.downcase == "html") ? HtmlBeautifier.beautify(stripped_source, opts) : stripped_source
  result.to_s.strip.html_safe
rescue
  source.strip.html_safe
end