text2png 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7efbe4e2c0acd34f53c7496a2d60517af963248b
4
+ data.tar.gz: 6875dd09ca4e37c7c8f4aaf5c9a875691a9301aa
5
+ SHA512:
6
+ metadata.gz: 3e2100db39e7df2a2af14ebee484b0bd4edffd12a152d92dfbd9ba06469f6d3addb08185e367e53735927132cc8a73fa8965b00223bbdb5d4c7dc8c4e357cd89
7
+ data.tar.gz: 89bbdf57f3423f9411d4850d1da0f609e41137f96741b0501484be415a86734a88b2b7b597353f00fbda818ec42f6c1ddd137e8cff056854f48c4b20290b13d5
data/.gitignore ADDED
@@ -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
data/LICENSE.txt ADDED
@@ -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.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Text2png
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 "text2png"
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install text2png
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require "text2png"
23
+
24
+ formula = "\\sum_{i = 0}^{i = n} \\frac{i}{2}"
25
+
26
+ converter = Text2png::Converter.new(formula)
27
+
28
+ converter.png {|file| ...do something with 'file'...}
29
+
30
+ converter.data #=> "data:image/png;base64, iVBOR...."
31
+
32
+ converter.png.path #=> "/tmp/text2png/..."
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it ( https://github.com/mindpin/text2png/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
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/text2png ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ #encoding: utf-8
3
+
4
+ require 'text2png'
5
+ require 'optparse'
6
+
7
+ Version = Text2png::VERSION
8
+
9
+ begin
10
+ opts = OptionParser.new do |opt|
11
+ opt.banner = "Usage: text2png text [ --out file.png ]"
12
+ opt.on("--out=file", "Specifie file to write the data. (default: stdout)") do |file|
13
+ $file = File.open(file, 'w')
14
+ end
15
+ opt.on("--debug") do
16
+ $debug = true
17
+ end
18
+ end.parse!
19
+ formula = opts.first
20
+ raise "No text specified" if formula.nil?
21
+ converter = Text2png::Converter.new(formula)
22
+ if $file
23
+ converter.png {|file| $file << file.read}
24
+ else
25
+ puts converter.data
26
+ end
27
+ rescue => err
28
+ puts "Error: #{err.message}"
29
+ puts err.backtrace.join("\n") if $debug
30
+ end
data/lib/text2png.rb ADDED
@@ -0,0 +1,27 @@
1
+ require "text2png/version"
2
+ require "text2png/errors"
3
+ require "text2png/result"
4
+ require "text2png/command"
5
+ require "text2png/converter"
6
+
7
+ module Text2png
8
+ LATEX_PACKAGES = [
9
+ "amssymb", "amsmath", "color", "amsfonts"
10
+ ].freeze
11
+
12
+ TEMP_DIR = "/tmp/text2png"
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 Text2png
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,87 @@
1
+ require "securerandom"
2
+ require "base64"
3
+ require "fileutils"
4
+
5
+ module Text2png
6
+ class Converter
7
+ attr_reader :hash, :formula, :density, :name, :dir, :base
8
+
9
+ def initialize(formula, density: 155)
10
+ @formula = formula
11
+ @density = density
12
+ @hash = SecureRandom.hex(16)
13
+ @base = File.join TEMP_DIR, hash
14
+ end
15
+
16
+ def content
17
+ @content ||= <<-END.gsub(/^ {6}/, "")
18
+ \\documentclass[12pt]{article}
19
+ \\usepackage[utf8]{inputenc}
20
+ \\usepackage{#{LATEX_PACKAGES.join(", ")}}
21
+ \\begin{document}
22
+ \\pagestyle{empty}
23
+ \\begin{displaymath}
24
+ #{formula}
25
+ \\end{displaymath}
26
+ \\end{document}
27
+ END
28
+ end
29
+
30
+ def data
31
+ @data ||= png do |io|
32
+ "data:image/png;base64, #{Base64.encode64(io.read)}"
33
+ end
34
+ end
35
+
36
+ def png(&block)
37
+ @png ||= proc do
38
+ args = [
39
+ "-q -T tight -D #{density}", "-o #{base}.png", "#{base}.dvi"
40
+ ]
41
+
42
+ tex
43
+ dvi
44
+ Text2png::dvipng.run(*args).raise!
45
+ clean!
46
+
47
+ path(:png)
48
+ end.call
49
+
50
+ file(:png, &block)
51
+ end
52
+
53
+ private
54
+
55
+ def tex
56
+ @tex ||= file(:tex, "w") do |file|
57
+ file.write content
58
+ file.path
59
+ end
60
+ end
61
+
62
+ def dvi
63
+ @dvi ||= proc do
64
+ args = [
65
+ "-output-directory=#{TEMP_DIR}", "-interaction=nonstopmode", tex
66
+ ]
67
+
68
+ Text2png::latex.run(*args).raise!
69
+
70
+ path(:dvi)
71
+ end.call
72
+ end
73
+
74
+ def clean!
75
+ FileUtils.rm %W{tex aux log dvi}.map {|ext| path(ext)}
76
+ end
77
+
78
+ def file(ext, mode = "r", &block)
79
+ FileUtils.mkdir TEMP_DIR if !File.exists? TEMP_DIR
80
+ File.open(path(ext), mode, &block)
81
+ end
82
+
83
+ def path(ext)
84
+ "#{base}.#{ext}"
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,11 @@
1
+ module Text2png
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 Text2png
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 Text2png
2
+ VERSION = "0.0.3"
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 include "iVBOR"}
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
data/text2png.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'text2png/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "text2png"
8
+ spec.version = Text2png::VERSION
9
+ spec.authors = ["Kaid", "Nephos"]
10
+ spec.email = ["kaid@kaid.me", "poulet_a@epitech.eu"]
11
+ spec.summary = "Tex literal to png converter"
12
+ spec.description = "Forked from https://github.com/mindpin/tex2png"
13
+ spec.homepage = "https://github.com/Nephos/text2png"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")# + ["bin/text2png"]
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.executables = %w(text2png)
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake", "~> 0"
25
+ spec.add_development_dependency "pry", "~> 0"
26
+ spec.add_development_dependency "rspec", "~> 0"
27
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: text2png
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Kaid
8
+ - Nephos
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-04-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.6'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.6'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: pry
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: Forked from https://github.com/mindpin/tex2png
71
+ email:
72
+ - kaid@kaid.me
73
+ - poulet_a@epitech.eu
74
+ executables:
75
+ - text2png
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/text2png
85
+ - lib/text2png.rb
86
+ - lib/text2png/command.rb
87
+ - lib/text2png/converter.rb
88
+ - lib/text2png/errors.rb
89
+ - lib/text2png/result.rb
90
+ - lib/text2png/version.rb
91
+ - spec/command_spec.rb
92
+ - spec/converter_spec.rb
93
+ - spec/spec_helper.rb
94
+ - spec/support/dummy.rb
95
+ - text2png.gemspec
96
+ homepage: https://github.com/Nephos/text2png
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.5.1
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Tex literal to png converter
120
+ test_files:
121
+ - spec/command_spec.rb
122
+ - spec/converter_spec.rb
123
+ - spec/spec_helper.rb
124
+ - spec/support/dummy.rb