tex2png 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e22eb7193e7e4e5abb25267b76ae4d75a5be7dc
4
+ data.tar.gz: 57c91a516b2352498c38eea0a0ac4c7f85bda1c9
5
+ SHA512:
6
+ metadata.gz: 84ada4a9c6cdf76a15856ac96def76db5b9864f8f2b2ea89bc73eb4323b2ef0f578eebd58d6b243465306a8585fbf1b3ca2575caf0a83d52a3dcfde04b7d32a3
7
+ data.tar.gz: e89b73fde05d4ad9af0a3ab84781711773bf16b08f97aaf894109e0f22a92b9247ac2f2597cdea23bf8f47fe358184b6865e7dc28923e2830445291974614734
@@ -0,0 +1,28 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .\#*
24
+ \#*
25
+ *~
26
+ *.swp
27
+ .DS*
28
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tex2png.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kaid
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Tex2png
2
+
3
+ A tex to png converter for ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem "tex2png"
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tex2png
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require "text2png"
23
+
24
+ formula = "\\sum_{i = 0}^{i = n} \\frac{i}{2}"
25
+
26
+ converter = Text2png::Convert(formula)
27
+
28
+ converter.png #=> "/path/to/file.png"
29
+
30
+ converter.png {|file| ...do something with 'file'...}
31
+
32
+ converter.data #=> "data:image/png;base64, iVBOR...."
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it ( https://github.com/mindpin/tex2png/fork )
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ require "tex2png/version"
2
+ require "tex2png/errors"
3
+ require "tex2png/result"
4
+ require "tex2png/command"
5
+ require "tex2png/converter"
6
+
7
+ module Tex2png
8
+ LATEX_PACKAGES = [
9
+ "amssymb", "amsmath", "color", "amsfonts"
10
+ ].freeze
11
+
12
+ TEMP_DIR = "/tmp/tex2png"
13
+
14
+ class << self
15
+ def latex
16
+ @latex ||= Command.new(:latex)
17
+ end
18
+
19
+ def dvipng
20
+ @dvipng ||= Command.new(:dvipng)
21
+ end
22
+ end
23
+
24
+ def Convert(formula, density: 155)
25
+ Converter.new(formula, density: density)
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module Tex2png
2
+ class Command
3
+ attr_reader :path, :out, :name
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ @path = IO.popen("which #{name}").read.chomp
8
+ CommandNotFound.raise!(name) {path.empty?}
9
+ end
10
+
11
+ def run(*args)
12
+ Result.new(name, IO.popen(%Q|#{path} #{args.join(" ")}|, :err => [:child, :out]))
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,83 @@
1
+ require "securerandom"
2
+
3
+ module Tex2png
4
+ class Converter
5
+ attr_reader :hash, :formula, :density, :name, :dir, :base
6
+
7
+ def initialize(formula, density: 155)
8
+ @formula = formula
9
+ @density = density
10
+ @hash = SecureRandom.hex(16)
11
+ @base = File.join TEMP_DIR, hash
12
+ end
13
+
14
+ def content
15
+ @content ||= <<-END.gsub(/^ {6}/, "")
16
+ \\documentclass[12pt]{article}
17
+ \\usepackage[utf8]{inputenc}
18
+ \\usepackage{#{LATEX_PACKAGES.join(", ")}}
19
+ \\begin{document}
20
+ \\pagestyle{empty}
21
+ \\begin{displaymath}
22
+ #{formula}
23
+ \\end{displaymath}
24
+ \\end{document}
25
+ END
26
+ end
27
+
28
+ def data
29
+ png {|io| "data:image/png;base64, #{io.read}"}
30
+ end
31
+
32
+ def png(&block)
33
+ @png ||= proc do
34
+ args = [
35
+ "-q -T tight -D #{density}", "-o #{base}.png", "#{base}.dvi"
36
+ ]
37
+
38
+ tex
39
+ dvi
40
+ Tex2png::dvipng.run(*args).raise!
41
+ clean!
42
+
43
+ path(:png)
44
+ end.call
45
+
46
+ file(:png, &block)
47
+ end
48
+
49
+ private
50
+
51
+ def tex
52
+ @tex ||= file(:tex, "w") do |file|
53
+ file.write content
54
+ file.path
55
+ end
56
+ end
57
+
58
+ def dvi
59
+ @dvi ||= proc do
60
+ args = [
61
+ "-output-directory=#{TEMP_DIR}", "-interaction=nonstopmode", tex
62
+ ]
63
+
64
+ Tex2png::latex.run(*args).raise!
65
+
66
+ path(:dvi)
67
+ end.call
68
+ end
69
+
70
+ def clean!
71
+ FileUtils.rm %W{tex aux log dvi}.map {|ext| path(ext)}
72
+ end
73
+
74
+ def file(ext, mode = "r", &block)
75
+ FileUtils.mkdir TEMP_DIR if !File.exists? TEMP_DIR
76
+ File.open(path(ext), mode, &block)
77
+ end
78
+
79
+ def path(ext)
80
+ "#{base}.#{ext}"
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,11 @@
1
+ module Tex2png
2
+ class Error < Exception
3
+ def self.raise!(msg = nil, &cond)
4
+ raise self.new(msg) if !cond || cond.call
5
+ end
6
+ end
7
+
8
+ AbstractCommandError = Class.new(Error)
9
+ CommandNotFound = Class.new(Error)
10
+ ExcutionError = Class.new(Error)
11
+ end
@@ -0,0 +1,22 @@
1
+ module Tex2png
2
+ class Result
3
+ attr_reader :raw, :output, :cmd
4
+
5
+ def initialize(cmd, io)
6
+ @cmd = cmd
7
+ @raw = io
8
+ end
9
+
10
+ def output
11
+ @output ||= raw.read
12
+ end
13
+
14
+ def success?
15
+ @success ||= Process.wait2(raw.pid)[1].success?
16
+ end
17
+
18
+ def raise!
19
+ ExcutionError.raise!("#{cmd} - #{output}") {!success?}
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Tex2png
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ module Tex2png
4
+ describe Command do
5
+ let(:sym1) {:ruby}
6
+ let(:sym2) {:a2f39sdY}
7
+ let(:cmd1) {Command.new(sym1)}
8
+ let(:cmd2) {Command.new(sym2)}
9
+ let(:dummy) {"./spec/support/dummy.rb"}
10
+ let(:arg1) {"dummy!"}
11
+ let(:arg2) {"yummy!"}
12
+
13
+ context "when cmd exist" do
14
+ subject {cmd1}
15
+
16
+ it {is_expected.to be_a Command}
17
+
18
+ describe Result do
19
+ let(:res1) {cmd1.run([dummy, arg1])}
20
+ let(:res2) {cmd1.run([dummy, arg2])}
21
+
22
+ it {expect(res2).to be_a Result}
23
+
24
+ context "when cmd succeeds" do
25
+ it {expect(res1).to be_a Result}
26
+ it {expect(res1.success?).to be true}
27
+ it {expect(res1.output).to be_empty}
28
+ end
29
+
30
+ context "when cmd fails" do
31
+ it {expect(res2).to be_a Result}
32
+ it {expect(res2.success?).to be false}
33
+ it {expect(res2.output).to eq "failed!\n"}
34
+ end
35
+ end
36
+ end
37
+
38
+ context "when cmd not exist" do
39
+ it {expect{cmd2}.to raise_error(CommandNotFound, sym2.to_s)}
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ module Tex2png
4
+ describe Converter do
5
+ let(:formula) {"\\sum_{i = 0}^{i = n} \\frac{i}{2}"}
6
+ let(:converter) {Converter.new(formula)}
7
+
8
+ it {expect(converter.png {|io| io}).to be_an IO}
9
+ it {expect(converter.data).to be_a String}
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ require "rspec"
2
+ require "tex2png"
3
+ require "pry"
@@ -0,0 +1,5 @@
1
+ status = ARGV[0] == "dummy!" ? 0 : 444
2
+
3
+ $stderr.write "failed!\n" if status != 0
4
+
5
+ exit status
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tex2png/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tex2png"
8
+ spec.version = Tex2png::VERSION
9
+ spec.authors = ["Kaid"]
10
+ spec.email = ["kaid@kaid.me"]
11
+ spec.summary = %q{Tex literal to png converter.}
12
+ spec.description = %q{Tex literal to png converter.}
13
+ spec.homepage = "https://github.com/mindpin/tex2png"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tex2png
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kaid
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
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'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Tex literal to png converter.
70
+ email:
71
+ - kaid@kaid.me
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/tex2png.rb
82
+ - lib/tex2png/command.rb
83
+ - lib/tex2png/converter.rb
84
+ - lib/tex2png/errors.rb
85
+ - lib/tex2png/result.rb
86
+ - lib/tex2png/version.rb
87
+ - spec/command_spec.rb
88
+ - spec/converter_spec.rb
89
+ - spec/spec_helper.rb
90
+ - spec/support/dummy.rb
91
+ - tex2png.gemspec
92
+ homepage: https://github.com/mindpin/tex2png
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.3.0
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Tex literal to png converter.
116
+ test_files:
117
+ - spec/command_spec.rb
118
+ - spec/converter_spec.rb
119
+ - spec/spec_helper.rb
120
+ - spec/support/dummy.rb
121
+ has_rdoc: