Class: Latexmath::LatexmlRequirement

Inherits:
Requirement show all
Defined in:
lib/latexmath/latexml_requirement.rb

Instance Method Summary collapse

Constructor Details

#initializeLatexmlRequirement

Returns a new instance of LatexmlRequirement.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/latexmath/latexml_requirement.rb', line 9

def initialize
  version_output, = Open3.capture2e('latexml --VERSION')
  version = version_output&.match(/\d+(.\d+)*/)
  puts version_output
  if version.to_s.empty?
    @error_message = 'LaTeXML is not available. (Or is PATH not setup properly?)'\
      " You must upgrade/install LaTeXML to a version higher than `#{@recommended_version}`"

  elsif Gem::Version.new(version) < Gem::Version.new(@minimal_version)
    @error_message = "Minimal supported LaTeXML version is `#{@minimal_version}` "\
        "Version `#{version}` found; recommended version is `#{@recommended_version}`"

  elsif Gem::Version.new(version) < Gem::Version.new(@recommended_version)
    version = 'unknown' if version.to_s.empty?
    header_msg = "latexmlmath version `#{version}` below `#{@recommended_version}`!"
    suggestion = if Gem.win_platform?
                   'cmd encoding is set to UTF-8 with `chcp 65001`'
                 else
                   'terminal encoding is set to UTF-8 with `export LANG=en_US.UTF-8`'
                 end

    @error_message = "WARNING #{header_msg} Please sure that #{suggestion} command."

    @cmd = 'latexmlmath --strict --preload=amsmath -- -'
    @cmd2 = 'latexmlmath --strict -- -'
  else
    @cmd = 'latexmlmath --strict --preload=amsmath --inputencoding=UTF-8 -- -'
    @cmd2 = 'latexmlmath --strict --inputencoding=UTF-8 -- -'
  end
rescue StandardError
  @error_message = 'LaTeXML is not available. (Or is PATH not setup properly?)'\
      " You must upgrade/install LaTeXML to a version higher than `#{@recommended_version}`"
end

Instance Method Details

#cmdObject



55
56
57
58
59
# File 'lib/latexmath/latexml_requirement.rb', line 55

def cmd
  abort @error_message unless @error_message.nil?

  [@cmd, @cmd2]
end

#convert(input) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/latexmath/latexml_requirement.rb', line 70

def convert(input)
  results = nil
  cmd.each_with_index do |cmd, i|
    warn "Retrying with #{cmd}" if i > 0
    results = run(input, cmd)
    if $CHILD_STATUS.to_i.zero?
      warn 'Success!' if i > 0
      break
    end
  end
  $CHILD_STATUS.to_i.zero? ? results : nil
end

#run(input, command) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/latexmath/latexml_requirement.rb', line 61

def run(input, command)
  puts command
  IO.popen(command, 'r+', external_encoding: 'UTF-8') do |io|
    io.write(input)
    io.close_write
    io.read
  end
end

#satisfied(abort = false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/latexmath/latexml_requirement.rb', line 43

def satisfied(abort = false)
  unless @error_message.nil?
    if abort
      abort @error_message
    else
      warn @error_message
    end
  end

  @error_message.nil?
end