rutex 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a109437a1fab0035da570f512d6280fab45ed5da7ff418c0362672c1fed7c8d
4
- data.tar.gz: 531719ff61028adc2afde56dbeff66a7ec94310a72fb742cc312b87522471023
3
+ metadata.gz: 2d1778f9c254e131282f0cb5edb930ac63e860341206494d0fcf7bc7f46c9846
4
+ data.tar.gz: affe81dd3e2e234127477b301f8f721735cadd870e7b25e45c911f4db3d6598c
5
5
  SHA512:
6
- metadata.gz: '01063887af0845f41b5b2467465924cc690b826950f4f3940d205b996b0670d05112f8ec6040ad7d26d45627542492bd96216c8e7dc8faaddbd7805a37fe6ae1'
7
- data.tar.gz: dc16f7aa28e8b802bdb06cecef112a32abf91c43a078722f3938491849172345d8d28f68ebaff46ded8dace212361e3a0852310ae350eb7518992e539a3f625a
6
+ metadata.gz: 16e40a9985a176feb0d32e9d46b1a5f4b2dadb27ffa5b84f518aa1b14b1ea732d4450df968a00b504e1eb65623509f82d2a5c4d7c5a8a676d6d11a619bb548ea
7
+ data.tar.gz: 9af7dddadca679980a796f9b941add10ea33e21649325adcdfb95ace2fe7887ae4f3d0b36b933ffd444ea7ae416937e45ae372c46602fbc877652c4eff65ca9b
data/.gitignore CHANGED
@@ -10,3 +10,5 @@
10
10
  TODO.md
11
11
  .rake*
12
12
  *.swp
13
+ *.tex
14
+ *.png
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rutex (0.1.3)
4
+ rutex (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/exe/rutex CHANGED
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+
3
5
  require "rutex"
4
6
 
5
7
  Rutex.entry(ARGV)
data/lib/rutex/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rutex
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/rutex.rb CHANGED
@@ -1,99 +1,113 @@
1
+ $LOAD_PATH.unshift __dir__
1
2
  require 'rutex/version'
2
3
  require 'tempfile'
3
4
 
4
- class RutexError < StandardError; end
5
- class PdfError < RutexError; end
6
- class ConvertError < RutexError; end
5
+ class RutexError < StandardError; end # :nodoc:
6
+ class PdfError < RutexError; end # :nodoc:
7
+ class PackageError < RutexError; end # :nodoc:
7
8
 
8
9
  module Rutex
9
10
  extend self
10
11
 
12
+ attr_accessor :color, :equation, :args, :output_file
13
+
11
14
  def entry(args)
12
15
  @args=args
13
16
 
14
- if args.size<1 or !(args & %w(-h --help)).empty?
15
- help
17
+ unless pdflatex
18
+ raise PdfError "Missing pdflatex binary from texlive package"
16
19
  end
17
20
 
18
- parse_options
21
+ help if (args.include?('-h') or args.include?('--help'))
22
+
23
+ version if args.include?('-v')
19
24
 
25
+ if args.include?('-f')
26
+ _, @output_file = args.slice!(args.index('-f'),2)
27
+ end
28
+ @output_file||=Tempfile.new(['equation','.png']).path
29
+
30
+ if args.include?('-c')
31
+ _, @color = args.slice!(args.index('-c'),2)
32
+ end
20
33
  @color||='black'
21
- @eqn=args.join
22
- tex_file=make_tex
23
- @outfile||=(tex_file.sub %r(.*/),'')+'.png'
24
- puts "Generating #{outfile}"
25
- convert_tex(tex_file)
34
+
35
+ help if args.empty?
36
+
37
+ @equation=args.join
38
+ make_tex_file
39
+ puts "Generating #{output_file}"
40
+ pdflatex_command
41
+ #convert_tex(tex_file)
26
42
  end
27
43
 
28
- def parse_options
29
- opts=%w(c f)
44
+ def version
45
+ puts "rutex #{VERSION}"
46
+ exit
47
+ end
30
48
 
31
- opts_idx=args.map.with_index do |e,i|
32
- e.match(/^-[#{opts}.join]/) ? i : nil
33
- end.compact.reverse
49
+ def pdflatex
50
+ %x(type -p pdflatex)
51
+ $?.exitstatus==0 ? true : false
52
+ end
34
53
 
35
- return if opts_idx.empty?
54
+ def pdflatex_options
55
+ "-halt-on-error -interaction=nonstopmode -output-directory=#{output_dir}"
56
+ end
36
57
 
37
- opts_idx.each do |i|
38
- opt,value=args.slice!(i,2)
39
- if opt=='-f'
40
- @outfile=value
41
- elsif opt=='-c'
42
- @color=value
43
- end
44
- end
58
+ def pdflatex_command
59
+ %x(pdflatex #{pdflatex_options} #{tex_file})
60
+ raise PdfError "Error logged in #{output_dir}" unless $?.exitstatus==0
61
+ rescue PdfError => e
62
+ puts "PdfError (#{e.message})"
63
+ exit
64
+ end
65
+
66
+ def install_standalone_package
67
+ 'http://mirrors.ctan.org/macros/latex/contrib/standalone.zip'
45
68
  end
46
69
 
47
70
  def help
48
71
  puts <<~eof
49
- Usage: rutex <equation> [options]
72
+ Usage:
73
+ rutex <equation> [options]
50
74
 
51
75
  Options:
52
- -c --color
53
- set color of equation (default black)
54
- -f --file
55
- set output file
76
+ -c, --color set color of equation (default black)
77
+ -f, --file set output file
56
78
 
57
79
  Examples:
58
80
  rutex E = mc^2
59
81
  rutex '2\\neq\\sqrt{9}'
60
- rutex 3=\\\\sqrt{9} -c white -f myeqn.png
82
+ rutex 3=\\\\sqrt{9} -c white -f myequation.png
61
83
  eof
62
84
  exit
63
85
  end
64
86
 
65
- def texstr
66
- <<~eof
87
+ def tex_string
88
+ end
89
+
90
+ def output_dir
91
+ File.dirname(File.expand_path(output_file))
92
+ end
93
+
94
+ def make_tex_file
95
+ tex_string = <<~str
67
96
  \\documentclass[preview]{standalone}
68
97
  \\usepackage{xcolor}
69
98
  \\begin{document}
70
- $\\color{#{color}}#{eqn}$
99
+ $\\color{#{color}}#{equation}$
71
100
  \\end{document}
72
- eof
101
+ str
102
+ File.write(tex_file, tex_string)
73
103
  end
74
104
 
75
- def make_tex
76
- tmp=Tempfile.new('eqn')
77
- tmp.write(texstr)
78
- path=tmp.path
79
- dir=path.sub(%r(.*\K/.*),'')
80
- tmp.close
81
-
82
- unless has_pdflatex
83
- "Cannot proceed: RuTeX depends on pdflatex"
84
- exit
85
- end
86
-
87
- %x(pdflatex --halt-on-error --output-directory=#{dir} #{path})
88
- raise PdfError unless $?.exitstatus==0
89
- path
90
- rescue => e
91
- puts "#{e.message}:\nError logged in #{path}.log"
92
- exit
105
+ def tex_file
106
+ "#{output_dir}/#{File.basename(output_file,File.extname(output_file))}.tex"
93
107
  end
94
108
 
95
109
  def convert_tex(tex_file)
96
- %x(convert -density 300 #{tex_file}.pdf #{outfile} 2>/dev/null)
110
+ %x(convert -density 300 #{tex_file}.pdf #{output_file} 2>/dev/null)
97
111
  raise ConvertError unless $?.exitstatus==0
98
112
  rescue => e
99
113
  puts "#{e.message}"
@@ -101,22 +115,6 @@ module Rutex
101
115
  end
102
116
 
103
117
  def has_pdflatex
104
- system('type','-p', 'pdflatex', out: File::NULL, err: File::NULL)
105
- end
106
-
107
- def color
108
- @color
109
- end
110
-
111
- def eqn
112
- @eqn
113
- end
114
-
115
- def args
116
- @args
117
- end
118
-
119
- def outfile
120
- @outfile
118
+ system('type','-p', 'pdflatex', [:out, :err] => File::NULL)
121
119
  end
122
120
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rutex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - sergioro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-13 00:00:00.000000000 Z
11
+ date: 2020-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug