Module: Landable::TidyService

Defined in:
app/services/landable/tidy_service.rb

Defined Under Namespace

Classes: Result, TidyError

Constant Summary collapse

@@options =
[
  # is what we have
  '-utf8',
   # two-space soft indents
  '-indent',
   # no wrapping
  '--wrap 0',
   # make some guesses about how the code should look
  '--clean true',
   # kill microsoft word crap
  '--bare true',
   # quote 'em up
  '--quote-ampersand true',
   # whitespace niceness
  '--break-before-br true',
   # allow <div ...><div ...></div></div>
  '--merge-divs false',
   # silence will fall
  '--quiet true',
  '--show-warnings false',
]
@@liquid_elements =
[
  'template',
  'title_tag',
  'meta_tags',
  'img_tag',
]

Class Method Summary collapse

Class Method Details

.call(input, runtime_options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/services/landable/tidy_service.rb', line 53

def self.call input, runtime_options={}
  if not tidyable?
    raise TidyError, 'Your system doesn\'t seem to have tidy installed. Please see https://github.com/w3c/tidy-html5.'
  end

  # wrapping known liquid in a span to allow tidy to format them nicely
  input = wrap_liquid input

  # off to tidy
  output = IO.popen("tidy #{options.join(' ')}", 'r+') do |io|
    io.puts input
    io.close_write
    io.read
  end

  # 0: success
  # 1: warning
  # 2: error
  # 3: ???
  # 4: profit
  if $?.exitstatus >= 2 and runtime_options[:raise_on_error]
    raise TidyError, "Tidy exited with status #{$?} - check stderr."
  end

  # unnwrapping the liquid that we wrapped earlier
  output = unwrap_liquid output

  # create and return a Result, allowing access to specific bits of the output
  Result.new output
end

.call!(input) ⇒ Object



49
50
51
# File 'app/services/landable/tidy_service.rb', line 49

def self.call! input
  self.call input, raise_on_error: true
end

.tidyable?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/services/landable/tidy_service.rb', line 84

def self.tidyable?
  @@is_tidyable ||= Kernel.system('which tidy > /dev/null')
end