Module: Landable::TidyService

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

Defined Under Namespace

Classes: Result, TidyError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.liquid_elementsObject

Returns the value of attribute liquid_elements.



7
8
9
# File 'app/services/landable/tidy_service.rb', line 7

def liquid_elements
  @liquid_elements
end

.optionsObject

Returns the value of attribute options.



6
7
8
# File 'app/services/landable/tidy_service.rb', line 6

def options
  @options
end

Class Method Details

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



49
50
51
52
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
# File 'app/services/landable/tidy_service.rb', line 49

def self.call(input, runtime_options = {})
  unless tidyable?
    fail 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 $CHILD_STATUS.exitstatus >= 2 && runtime_options[:raise_on_error]
    fail TidyError, "Tidy exited with status #{$CHILD_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



45
46
47
# File 'app/services/landable/tidy_service.rb', line 45

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

.tidyable?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/services/landable/tidy_service.rb', line 80

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