latex_to_png 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ec7822ae92fb0f98e532fa72737330791bc9a24
4
- data.tar.gz: 9e3f2a3593321336d06d88e70ff675a789a007bc
3
+ metadata.gz: 2343b421ba8305388e395e8ec3f072ad0d36005d
4
+ data.tar.gz: 5ed7548a0e922fcca88a2d2f8c86d32b025c460a
5
5
  SHA512:
6
- metadata.gz: b6e673221153c9fc1142e2dccfa91e44014f7a4515c6f57b978330e6f00ba0c33c45b05a45d1331c0d34eb4853e02540cc17c4677e490ea8d9bfcb817e846cc1
7
- data.tar.gz: 925fa8fea6b554c5148fd8099a77e996f19bca10c7f15e8195bc4608998daf21933983f094c545f6ac71a6f04ed0072f95da41b58050d93392f355776a749d60
6
+ metadata.gz: 5ba388cc7b822b3849cfbbed26e5db5881181d40d2042b4ac2abfd90d349fdb9b9edde12949a10d05444fb03d3d8a8284ed5221253f5f4e21c29a64ba1d2fdcf
7
+ data.tar.gz: bb8a3588cb7b6cb329b5b5af9eba84fe3ccb88108969787cff9c9d9dfd7c84ab26f0dd6804521160ee1ed6d77c50f80504112405f3356f6d32556d1e3bdd63b8
data/Gemfile CHANGED
@@ -3,6 +3,6 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in latex_to_png.gemspec
4
4
  gemspec
5
5
 
6
-
7
- gem 'byebug'
8
- gem 'rspec'
6
+ platforms :ruby do
7
+ # gem 'byebug'
8
+ end
data/README.md CHANGED
@@ -19,7 +19,15 @@ Or install it yourself as:
19
19
  ## External Dependencies
20
20
 
21
21
  sudo apt-get install imagemagick libmagickcore-dev
22
-
22
+
23
23
  sudo apt-get install texlive texlive-latex-extra
24
-
25
- sudo apt-get install texinfo
24
+
25
+ sudo apt-get install texinfo
26
+
27
+
28
+ ## From source
29
+
30
+ ```
31
+ $ cd latex_to_png
32
+ $ gem build
33
+ ```
data/latex_to_png.gemspec CHANGED
@@ -24,4 +24,6 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.6"
26
26
  spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+
27
29
  end
@@ -1,3 +1,3 @@
1
1
  module LatexToPng
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/latex_to_png.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  require "latex_to_png/version"
2
2
  require 'tempfile'
3
3
  require "erb"
4
- require 'ostruct'
5
-
6
4
 
7
5
 
8
6
  #precisa do programa texi2dvi
@@ -23,85 +21,103 @@ begin
23
21
  end
24
22
  rescue Exception => e
25
23
  puts e
26
- # raise Gem::Installer::ExtensionBuildError
24
+ # raise Gem::Installer::ExtensionBuildError
27
25
  end
28
26
 
29
27
  require "latex_to_png/sizes"
30
28
 
31
29
  module LatexToPng
32
-
33
-
34
-
35
- ROOT_LIB = File.dirname __FILE__
36
30
 
31
+ Formula = Struct.new(:formula)
37
32
 
38
33
  class Convert
39
34
 
40
- attr_accessor :dirname , :filename, :basename, :png_file, :template_path
35
+ attr_accessor :filepath
41
36
 
42
- def size_in_points size_in_pixels
43
- size = Sizes.invert[size_in_pixels]
44
- if size.nil?
45
- size_in_points "#{size_in_pixels.to_i + 1}px"
37
+ class << self
46
38
 
47
- else
48
- size
39
+ def template
40
+ return @doc if @doc
41
+ @doc = ERB.new(File.read("#{ File.dirname __FILE__}/templates/equation.erb"))
42
+ @doc
49
43
  end
44
+
50
45
  end
51
46
 
52
- def initialize opts={ filename: nil, formula: nil, template_path: nil, size: nil }
53
-
54
- @filename = opts[:filename] if opts[:filename]
55
- @basename = File.basename opts[:filename].split(".")[0] if opts[:filename]
56
- @dirname = File.dirname opts[:filename] if opts[:filename]
47
+ #or formula or filepath
48
+ def initialize opts={ filepath: nil, formula: nil, size: nil }
57
49
 
50
+ @filepath = opts[:filepath] if opts[:filepath]
58
51
  @size = (opts[:size] || 10).to_i
59
- @template_path = opts[:template_path] if opts[:template_path]
60
52
  @formula = opts[:formula] if opts[:formula]
53
+
61
54
  end
62
55
 
63
- def to_png
64
- if @formula
65
- doc = ERB.new(File.read("#{ROOT_LIB}/templates/equation.erb"))
66
- infos = OpenStruct.new({formula: @formula })
67
- doc = doc.result(infos.instance_eval { binding })
68
-
69
- tmp_file = Tempfile.new("formula")
70
- tmp_file.write doc
71
- tmp_file.close
72
- # debugger
73
- @filename = tmp_file.path
74
- else
75
- @filename
76
- end
56
+ def size_in_points size_in_pixels
57
+ size = Sizes.invert[size_in_pixels]
58
+ return (size.nil?)? size_in_points("#{size_in_pixels.to_i + 1}px") : size
59
+ end
77
60
 
78
- name = @filename.split("/").last.split(".").first
79
- dirname = File.dirname @filename
80
- basename = @filename.gsub( /.tex$/,'')
81
- density = ((300/10)*@size).to_i
61
+ def from_formula formula, size
82
62
 
83
- %x(cd #{dirname}; latex -halt-on-error #{@filename} >> convert_#{name}.log)
84
- %x(cd #{dirname}; dvips -q* -E #{name}.dvi >> convert_#{name}.log)
85
- %x(cd #{dirname}; convert -density #{density}x#{density} #{name}.ps #{name}.png >> convert_#{name}.log)
86
- %x(cd #{dirname}; rm -f #{name}.dvi #{name}.log #{name}.aux #{name}.ps)
87
- png_path = "#{@filename.gsub(/.tex$/,"")}.png"
88
63
 
89
- if File.exist? png_path
90
- %x(cd #{dirname}; rm -f convert_#{name}.log)
91
- @png_file = open(png_path)
92
- else
93
- %x(cp #{tmp_file.path} #{dirname}/origin_#{name}.log)
94
- raise StandardError
95
- end
64
+ tmp_file = Tempfile.new("formula")
65
+ tmp_file.write mount_tex(formula)
66
+ tmp_file.close
67
+ # debugger
68
+ filepath = tmp_file.path
96
69
 
97
- end
70
+ convert(filepath,size )
98
71
 
99
- private
100
-
101
- def move_to_dir
102
- "cd #{@dirname}"
103
- end
72
+ end
73
+
74
+ def mount_tex formula
75
+ self.class.template.result(Formula.new( formula ).instance_eval { binding })
76
+ end
77
+
78
+
79
+ def from_estatic filepath, size
80
+
81
+ convert(filepath, size )
82
+ end
83
+
84
+ def convert filepath, size,
85
+ name = filepath.split("/").last.split(".").first
86
+ dirname = File.dirname filepath
87
+ density = ((300/10)*size).to_i
88
+ # debugger
89
+ #convert for .dvi
90
+ # dvi to .ps
91
+ # .ps to .png "q*" option is to run quietly
92
+
93
+ %x(
94
+ cd #{dirname}; latex -halt-on-error #{filepath} &&
95
+ dvips -q* -E #{name}.dvi &&
96
+ convert -density #{density}x#{density} #{name}.ps #{name}.png 1>&2 > /dev/null
97
+ )
98
+
99
+ Thread.new {
100
+ %x(cd #{dirname}; rm -f #{name}.dvi #{name}.log #{name}.aux #{name}.ps &)
101
+ }.run()
102
+
103
+ png_path = "#{filepath.gsub(/.tex$/,"")}.png"
104
+
105
+ if File.exist?(png_path)
106
+ return open(png_path)
107
+ else
108
+ raise StandardError("Image not generated")
109
+ end
110
+ end
111
+
112
+
113
+ def to_png
114
+ if @formula
115
+ from_formula @formula, @size
116
+
117
+ else
118
+ from_estatic @filepath, @size
119
+ end
120
+ end
104
121
 
105
122
  end
106
123
  end
107
-
@@ -1,11 +1,13 @@
1
- \documentclass{article}
1
+ \documentclass{standalone}
2
+ \usepackage[active,tightpage]{preview}
2
3
  \usepackage{amsmath}
3
4
  \usepackage{cancel}
4
5
  \usepackage{amsfonts}
5
6
  \usepackage{amssymb}
6
7
  \usepackage{amsthm}
7
8
  \usepackage{mathtools}
8
- \pagestyle{empty}
9
9
  \begin{document}
10
+ \begin{preview}
10
11
  $$<%= formula %>$$
12
+ \end{preview}
11
13
  \end{document}
data/spec/convert_spec.rb CHANGED
@@ -1,24 +1,24 @@
1
1
  require "spec_helper"
2
2
 
3
- describe "" do
3
+ describe "" do
4
4
 
5
5
  after(:each) do
6
- # %x(rm #{ROOT_DIR_SPEC}/support/*.png)
6
+ %x(rm -Rf #{ROOT_DIR_SPEC}/support/*.png)
7
7
  end
8
8
 
9
9
  context "convert.size_in_points" do
10
- it "com valor exato" do
11
-
12
- image = LatexToPng::Convert.new(filename: "#{ROOT_DIR_SPEC}/support/flux.tex")
13
-
10
+ it "com valor exato" do
11
+
12
+ image = LatexToPng::Convert.new(filepath: "#{ROOT_DIR_SPEC}/support/flux.tex")
13
+
14
14
  expect(image.size_in_points("12px")).to eq "9pt"
15
15
 
16
16
  end
17
17
 
18
- it "com valor inexato pontos arredondadndo" do
19
-
20
- image = LatexToPng::Convert.new(filename: "#{ROOT_DIR_SPEC}/support/flux.tex")
21
-
18
+ it "com valor inexato pontos arredondadndo" do
19
+
20
+ image = LatexToPng::Convert.new(filepath: "#{ROOT_DIR_SPEC}/support/flux.tex")
21
+
22
22
  expect(image.size_in_points("18px")).to eq "14pt"
23
23
 
24
24
  end
@@ -27,97 +27,80 @@ describe "" do
27
27
  end
28
28
 
29
29
 
30
- it "dirname do arquivo" do
31
-
32
- image = LatexToPng::Convert.new(filename: "#{ROOT_DIR_SPEC}/support/flux.tex")
33
-
34
- expect(image.dirname).to eq "#{ROOT_DIR_SPEC}/support"
35
- expect(image.basename).to eq "flux"
30
+ it "filepath flux.tex" do
36
31
 
37
-
38
- end
39
-
40
- it "dirname do arquivo" do
41
-
42
- image = LatexToPng::Convert.new(filename: "#{ROOT_DIR_SPEC}/support/flux.tex")
32
+ image = LatexToPng::Convert.new(filepath: "#{ROOT_DIR_SPEC}/support/flux.tex")
43
33
  image = image.to_png
44
34
  expect(image.class).to eq File
45
35
 
46
- File.delete image.path
47
36
  end
48
37
 
49
- it "dirname do arquivo com \\cancel" do
50
-
51
- image = LatexToPng::Convert.new(filename: "#{ROOT_DIR_SPEC}/support/cancel_example.tex")
38
+ it "filepath cancel_example.tex" do
39
+
40
+ image = LatexToPng::Convert.new(filepath: "#{ROOT_DIR_SPEC}/support/cancel_example.tex")
52
41
  image = image.to_png
53
42
 
54
43
  expect(image.class).to eq File
55
- File.delete image.path
56
44
 
57
45
  end
58
-
59
- context "formula com usando template" do
60
46
 
61
- it "fração simples" do
62
-
47
+ context "formula usando template" do
48
+
49
+ it "fração simples" do
50
+
63
51
  image = LatexToPng::Convert.new(formula: "\\frac{a}{b}")
64
52
  image = image.to_png
65
53
 
66
54
  expect(image.class).to eq File
67
55
  expect(File.exist? image).to eq true
68
56
 
69
- File.delete image.path
70
57
  end
71
58
 
72
- it "fração com \\cdot" do
73
-
59
+ it "fração com \\cdot" do
60
+
74
61
  image = LatexToPng::Convert.new(formula: "\\frac{a}{b}\\cdot\\frac{b}{a}")
75
62
  image = image.to_png
76
63
 
77
64
  expect(image.class).to eq File
78
65
  expect(File.exist? image).to eq true
79
66
 
80
- File.delete image.path
81
67
  end
82
68
 
83
- it "fração com \\cdot e espaços" do
84
-
69
+ it "fração com \\cdot e espaços" do
70
+
85
71
  image = LatexToPng::Convert.new(formula: "\\frac{a}{b} \\cdot \\frac{b}{a}")
86
72
  image = image.to_png
87
73
 
88
74
  expect(image.class).to eq File
89
75
  expect(File.exist? image).to eq true
90
76
 
91
- File.delete image.path
92
77
  end
93
78
 
94
- it "fração com \\cdot e espaços e tab" do
95
-
79
+ it "fração com \\cdot e espaços e tab" do
80
+
96
81
  image = LatexToPng::Convert.new(formula: "\\frac{a}{b} \\cdot \\frac{b}{a}")
97
82
  image = image.to_png
98
83
 
99
84
  expect(image.class).to eq File
100
85
  expect(File.exist? image).to eq true
101
86
 
102
- File.delete image.path
103
87
  end
104
88
 
105
- it "fração com tamanho de fonte" do
106
-
89
+ it "fração com tamanho de fonte" do
90
+
107
91
  image = LatexToPng::Convert.new(formula: "\\frac{a}{b}", size_in_pixels: "19px")
108
92
  image = image.to_png
109
93
 
110
94
  expect(image.class).to eq File
111
95
  expect(File.exist? image).to eq true
112
96
 
113
- # File.delete image.path
114
97
  end
115
98
 
116
- it "insstrução inexistente como \\blabla" do
117
-
99
+ it "insstrução inexistente como \\blabla" do
100
+
118
101
  image = LatexToPng::Convert.new(formula: "\\blabla")
119
- expect{image.to_png}.to raise_error
102
+ expect{image.to_png}.to raise_error(StandardError)
120
103
  end
121
104
 
122
105
  end
123
- end
106
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "latex_to_png"
2
- require "byebug"
2
+ # require "byebug"
3
3
  require 'bundler/setup'
4
4
 
5
5
  Bundler.setup
@@ -9,4 +9,4 @@ ROOT_DIR_SPEC = File.dirname __FILE__
9
9
  RSpec.configure do |config|
10
10
 
11
11
 
12
- end
12
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: latex_to_png
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - luizamboni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-10 00:00:00.000000000 Z
11
+ date: 2016-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: Conversor de documentos Latex em imagens, via shell script
42
56
  email:
43
57
  - luizamboni2002@hotmail.com
@@ -88,7 +102,7 @@ requirements:
88
102
  - dvips
89
103
  - imageMagik
90
104
  rubyforge_project:
91
- rubygems_version: 2.4.6
105
+ rubygems_version: 2.4.8
92
106
  signing_key:
93
107
  specification_version: 4
94
108
  summary: Conversor de documentos Latex em imagens