class Epuber::Epubcheck

Constants

Problem
Report

Public Class Methods

_parse_json(string) click to toggle source

Parse json from epubcheck

@param [String] string json string @return [Report]

# File lib/epuber/epubcheck.rb, line 65
def _parse_json(string)
  json = JSON.parse(string)
  messages = json['messages']
  problems = messages
             .map { |msg| _parse_locations(msg) }
             .flatten

  Report.new(problems: problems)
end
_parse_locations(json) click to toggle source

Parse all problems from single message

@param [Hash] json @return [Array<Problem>]

# File lib/epuber/epubcheck.rb, line 80
def _parse_locations(json)
  json['locations'].map do |json_location|
    location = Epuber::Location.new(
      path: json_location['path'],
      lineno: json_location['line'],
      column: json_location['column'],
    )

    Problem.new(
      level: json['severity'].downcase.to_sym,
      code: json['ID'],
      message: json['message'],
      location: location,
    )
  end
end
check(path) click to toggle source

@param [String] path path to file

@return [Report] report of the epubcheck

# File lib/epuber/epubcheck.rb, line 46
def check(path)
  report = nil

  Dir.mktmpdir('epubcheck-') do |tmpdir|
    json_path = File.join(tmpdir, 'epubcheck.json')
    Open3.popen2('epubcheck', path, '--json', json_path) do |_stdin, _stdout, wait_thr|
      wait_thr.value # wait for the process to finish
      report = _parse_json(File.read(json_path))
    end
  end

  report
end