Class: Jekyll::TidyJSON::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/tidy_json.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Processor

Returns a new instance of Processor.



9
10
11
# File 'lib/jekyll/tidy_json.rb', line 9

def initialize(config)
  parse_plugin_config config.fetch("tidy_json", {})
end

Instance Method Details

#continue_on_error?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/jekyll/tidy_json.rb', line 40

def continue_on_error?
  !!@continue_on_error
end

#enabled?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/jekyll/tidy_json.rb', line 48

def enabled?
  !!@enabled
end

#pretty?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/jekyll/tidy_json.rb', line 44

def pretty?
  !!@pretty
end

#should_tidy?(path) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/jekyll/tidy_json.rb', line 13

def should_tidy?(path)
  enabled? && File.extname(path).downcase == ".json"
end

#tidy_page_or_document(page_or_document) ⇒ Object



17
18
19
20
21
# File 'lib/jekyll/tidy_json.rb', line 17

def tidy_page_or_document(page_or_document)
  if should_tidy?(page_or_document.relative_path)
    tidy_page_or_document!(page_or_document)
  end
end

#tidy_page_or_document!(page_or_document) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/jekyll/tidy_json.rb', line 23

def tidy_page_or_document!(page_or_document)
  path = page_or_document.relative_path

  Jekyll.logger.debug("Tidy up JSON:", path)
  page_or_document.output = tidy_string(page_or_document.output)
rescue JSON::ParserError
  print_parse_error(!continue_on_error?, $!, path)
  raise unless continue_on_error?
end

#tidy_string(string) ⇒ Object



33
34
35
36
37
38
# File 'lib/jekyll/tidy_json.rb', line 33

def tidy_string(string)
  json = JSON.parse(string)
  meth = self.pretty? ? :pretty_generate : :fast_generate

  JSON.public_send meth, json
end