texrack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MmE1MTFhNzg2NzBkMTRkNmYyYjMzZmM0MWM5NmMxN2RlNTRiYzM3ZA==
5
+ data.tar.gz: !binary |-
6
+ NmI3OTA3YjRhNDhlN2ExZmFmOWJiYmVjOGQyYmZmZmFkMTBkOTU5Yw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZmI3ZDQ2MjcwYWVlYjQzNTJhNWEwYmYxYWQ0Y2I5YWZjYmQwYzI2ZTdlYzNl
10
+ ZjFhODc2ZDI2NjNmYTIxYWI4NjBlNjRiYzI1OGVkMDU0ODc1ZmJmMGFlYzEy
11
+ OGZlMzdlMmRkOGU0MDg2NDc1YmE0OWViMjk3ZTg2NTI5MDJlZTg=
12
+ data.tar.gz: !binary |-
13
+ NDE1YmZkYjY5ZTE4MDUwZDkyYTBiOTZjOTI0OWFmYmJkMWY4N2I0YjA4ZmI3
14
+ OGExOTUzYTQ2MDM0NGRhZGNmMmI1YzA0MzI3ZGE2MjIxODA5NGNjMGU1MWY2
15
+ MzZhMjc2YzNkNDJhN2M0ZGM2OWM4NDRjZmI0MGZhNTk2NmVmNjM=
checksums.yaml.gz.asc ADDED
@@ -0,0 +1,18 @@
1
+ -----BEGIN PGP SIGNATURE-----
2
+ Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
3
+ Comment: GPGTools - http://gpgtools.org
4
+
5
+ iQIcBAABAgAGBQJSbpGjAAoJEH1ncb0Txu7XVnoP/1lZdSrAUwVvNYSrb/bpggKa
6
+ 2C4VKplgsB204dKV8HoLgLEThqaq7ljengC8bS/YHPAKS8DTclesPN9zZ7UzvLmF
7
+ IvLYasZnTDC6zAgMUHdhAz3Y+epUOdMPjTid25i1C/Zs19ZQaD2E9SizsoqUf6rm
8
+ 2M4bBioMoYhxK8VzZvaehl84iXAg9fkWmkBYBITPf4Sr+luMoyXPBVeQMMXXneuE
9
+ TfBWAYLH0C1dYk8/6Ys/ZIBVdNYkMO4iz9icE/mTBhTKHPt4BtEjas8/gt4Pvjkj
10
+ Qqme7XKXMBdwp0Y95HWsfKrCVfM08S7RcbxsgNV6xuBHZQy9k9LU2i6z4aZD08fz
11
+ kujdSnfOc7fO/GqukiYLHzHXiYJ6/BtHPRFQpo9O7sM1GlOktuIckyOlDsAKzfH8
12
+ Bp6YcApw39QmoH+NBkXgQQOg3TSBhlzXapjLInRv4dtAsDFNVMkQQa9YXtdKOKik
13
+ g9H7Q6grwx3ZRv8BypDFqhUoQnD1DY7dQu0A0+1QRAo4dJcECjSWLM5oyywVVKO3
14
+ ctYvj4nD9Ma9QxNR2oL25pogjaRbx+CUMQaZYD2lnbpVt+BoEgO27AIkGVYYzsC1
15
+ elN9CvkxDCrJhgRygz8NHDQ2Rfm6vYCx7gnJrp7+JpesDQRs+uTExurEEAlkSLFW
16
+ zOzAeMo2w49TQcgl1cFW
17
+ =1Kx8
18
+ -----END PGP SIGNATURE-----
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Per Christian B. Viken <perchr@northblue.org>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # texrack
2
+
3
+ Simple Rack middleware to render LaTeX to PNGs on the fly.
4
+
5
+ ## Requirements
6
+
7
+ * `pdflatex` must exist in `$PATH`
8
+ * RMagick does the conversion from PDF to PNG, so `ImageMagick` must be
9
+ installed and available.
10
+
11
+ ## Configuration
12
+
13
+ ## Usage
14
+
15
+ Either POST or GET to the configured URL with `data` set to the LaTeX source to
16
+ compile.
17
+
18
+ If you `GET /` without setting `data`, you get a simple form.
19
+
20
+ ### Display mode
21
+ Everything is rendered inside a displaymath block (`\[` or `$$`) by default.
22
+ To disable the displaymath block, pass `math=0`.
23
+
24
+ ### Packages
25
+ The only included package is `amsmath`.
26
+ To add more, send a pipe-separated string as `packages`.
27
+ Arguments can be prepended.
28
+
29
+ For example: `packages="[usenames,dvipsnames,svgnames,table]xcolor|amssymb"`
30
+ turns into
31
+ ```latex
32
+ \usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
33
+ \usepackage{amssymb}
34
+ ```
data/config.ru ADDED
@@ -0,0 +1,4 @@
1
+ $:.unshift 'lib'
2
+ require 'texrack'
3
+
4
+ run Texrack::Endpoint
@@ -0,0 +1,45 @@
1
+ require 'sinatra/base'
2
+ require 'erb'
3
+
4
+ module Texrack
5
+ class Endpoint < Sinatra::Base
6
+ enable :logging
7
+
8
+ get '/' do
9
+ if params[:data].to_s.strip != ""
10
+ render_png(params[:data])
11
+ else
12
+ erb :form
13
+ end
14
+ end
15
+
16
+ post '/' do
17
+ render_png(params[:data])
18
+ end
19
+
20
+ def render_png(source)
21
+ content_type 'image/png'
22
+ @data = source
23
+ pdf_source = erb :latex
24
+ pdf = Texrack::LatexToPdf.new(pdf_source, logger).generate_pdf
25
+ tmp = Tempfile.new(["texrack-output", ".png"])
26
+ png = Texrack::PdfToPng.new(pdf).to_file(tmp)
27
+ png
28
+ end
29
+
30
+ helpers do
31
+ def math_mode?
32
+ params[:math] != "0"
33
+ end
34
+
35
+ def packages
36
+ found = {}
37
+ params[:packages].to_s.split("|").each do |package|
38
+ details = package.match /(?:\[([^\]]+)\])?([A-Za-z]+)/
39
+ found[details[2]] = details[1]
40
+ end
41
+ found
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,55 @@
1
+ class Texrack::LatexToPdf
2
+ def self.config
3
+ @config ||= {command: 'pdflatex', arguments: [], parse_twice: false}
4
+ end
5
+
6
+ attr_reader :config, :latex, :logger
7
+
8
+ def initialize(latex, logger=Logger.new(STDOUT), overrides = {})
9
+ @latex = latex
10
+ @logger = logger
11
+ @config = self.class.config.merge(overrides)
12
+ end
13
+
14
+ # Converts a string of LaTeX code into a binary string of PDF.
15
+ #
16
+ # pdflatex is used to convert the file.
17
+ def generate_pdf
18
+ logger.debug "Generating PDF from #{latex}"
19
+ input = Tempfile.new(["texrack-input", ".tex"])
20
+ input.binmode
21
+ input.write(latex)
22
+ input.flush
23
+
24
+ Process.waitpid(
25
+ fork do
26
+ begin
27
+ Dir.chdir File.dirname(input)
28
+ args = config[:arguments] + %w[-shell-escape -interaction batchmode -halt-on-error] + [input.path]
29
+
30
+ if config[:parse_twice]
31
+ logger.debug("Texrack executing (parse twice): #{config[:command]} -draftmode #{args}")
32
+ system config[:command], '-draftmode', *args
33
+ end
34
+
35
+ logger.debug "Texrack executing: #{config[:command]} #{args}"
36
+ exec config[:command], *args
37
+ rescue
38
+ logger.error "#{$!.message}:\n#{$!.backtrace.join("\n")}\n"
39
+ ensure
40
+ Process.exit! 1
41
+ end
42
+ end)
43
+
44
+ output = input.path.sub(/\.tex$/, '.pdf')
45
+ if File.exist?(output)
46
+ result = File.read(output)
47
+ else
48
+ logger.warn "Unable to find file #{output}"
49
+ raise "pdflatex failed"
50
+ end
51
+
52
+ input.close
53
+ result
54
+ end
55
+ end
@@ -0,0 +1,15 @@
1
+ require 'RMagick'
2
+
3
+ class Texrack::PdfToPng
4
+ def initialize(blob)
5
+ @img = Magick::Image.from_blob(blob) do |info|
6
+ info.density = "300x300"
7
+ info.quality = 90
8
+ end[0]
9
+ end
10
+
11
+ def to_file(file)
12
+ @img.write('png:' + file.path)
13
+ file
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Texrack
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Texrack</title>
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
7
+ </head>
8
+ <body>
9
+ <div class="container">
10
+ <h1 class="page-header">Render <img src="/?data=\LaTeX&math=0" alt="LaTeX"> as a PNG</h1>
11
+
12
+ <form role="form" action="/" method="POST">
13
+ <div class="form-group">
14
+ <label for="data">LaTeX to render</label>
15
+ <textarea class="form-control" id="data" name="data" rows="5"></textarea>
16
+ </div>
17
+ <div class="checkbox">
18
+ <label>
19
+ <input type="hidden" name="math" value="0">
20
+ <input type="checkbox" checked name="math" value="1"> Render in math mode?
21
+ </label>
22
+ </div>
23
+ <div class="form-group">
24
+ <label for="packages">Extra packages to include</label>
25
+ <input type="text" class="form-control" id="packages" name="packages">
26
+ <p class="help-block">
27
+ Separate packages with a pipe (|).
28
+ Arguments are also supported. Example: "[english]babel|amsmath"
29
+ </p>
30
+ </div>
31
+
32
+ <div class="form-actions">
33
+ <input type="submit" class="btn btn-primary" value="Render PNG">
34
+ </div>
35
+ </form>
36
+ </div>
37
+ </body>
38
+ </html>
@@ -0,0 +1,12 @@
1
+ \documentclass[border=2pt,varwidth]{standalone}
2
+ \usepackage{standalone}
3
+ <% packages.each_pair do |package, arguments| %>
4
+ \usepackage<% if arguments %>[<%= arguments.to_s %>]<% end %>{<%= package %>}
5
+ <% end %>
6
+ \usepackage{amsmath}
7
+ \begin{document}
8
+ <% if math_mode? %>\[<% end %>
9
+ <%= @data %>
10
+ <% if math_mode? %>\]<% end %>
11
+ \end{document}
12
+
data/lib/texrack.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'texrack/version'
2
+ require 'texrack/latex_to_pdf'
3
+ require 'texrack/pdf_to_png'
4
+ require 'texrack/endpoint'
data/texrack.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "texrack/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "texrack"
7
+ s.version = Texrack::VERSION
8
+ s.authors = ["Per Christian B. Viken"]
9
+ s.email = ["perchr@northblue.org"]
10
+ s.homepage = "https://github.com/PerfectlyNormal/texrack"
11
+ s.summary = %q{Rack microservice to render LaTeX as PNG}
12
+ s.description = %q{texrack is a microservice that renders LaTeX as PNG files. Can be mounted in a Rack app}
13
+ s.licenses = "MIT"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+
19
+ s.rdoc_options = [%q{--main=README.md}]
20
+
21
+ s.extra_rdoc_files = [
22
+ "MIT-LICENSE",
23
+ "README.md"
24
+ ]
25
+
26
+ s.add_dependency 'sinatra', '~> 1.4.4'
27
+ s.add_dependency 'rmagick', '~> 2.13.2'
28
+ end
data.tar.gz.asc ADDED
@@ -0,0 +1,18 @@
1
+ -----BEGIN PGP SIGNATURE-----
2
+ Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
3
+ Comment: GPGTools - http://gpgtools.org
4
+
5
+ iQIcBAABAgAGBQJSbpGiAAoJEH1ncb0Txu7XPisP/R45LFwRbSbSsqUgfE4hTNda
6
+ 3XZhBDwrno1Rc/i3i29klT3/YmxhMSzNosDQReqrJA2rhC7eu/1lq8Ndt2gih/Pu
7
+ 9f0nOVauXAz1LwQgAn5M3jelqCt9PSUZfJVxsV1k48zFh0Vbl+oogZ25VRqcurN7
8
+ bfMb3QdtXv3iIi9s1x5OwjVtpHqzMFVI4JpvAUTAOXacnaN1tnlTaUY9avoDrZc1
9
+ Av0pk8Pu0w+G1IPEb3GRicXdq4Bl8dD/qlgSIXO/KWvgmVHqjoJTWHBnF92Ehist
10
+ 0SsZvEwY6VTZ19hYhCWKRpIbh37wR18e3rcRRia91d3iU1ggCJCSX8pBqkxsozFN
11
+ h34jKTR45bIZOSEVV87Gdksea2PlgtG5uw1qBFXCEDkAQoMfKIom8bLvpssTNczf
12
+ hg9wazU0CmJHddFqFA6SqtWf6BwZoJ0O/GXOso7MeBJ6y2Pl5IWyr8tBbqZf5NpF
13
+ FIP7uPKLMO7KE28DYtrmcEKvA2jXmnQtK5ddztJkIUlZwie4zvS3WVSwKBjxOhFs
14
+ XzYPV1q9bxiuVES2AGWA0yq/m4RBpKfJT+N47jm5zXMgvphJiQpHbPO3EA5RE+dQ
15
+ TLmGAqZ1lM77WnN3qDCvfHNm+ovh2H9KixfOwnlahVL1WEhWA+XEki3sFemx1BB0
16
+ OWV9BKLJ6NO1SAw/Tb5l
17
+ =Vq2I
18
+ -----END PGP SIGNATURE-----
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: texrack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Per Christian B. Viken
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ~>
17
+ - !ruby/object:Gem::Version
18
+ version: 1.4.4
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.4.4
24
+ type: :runtime
25
+ prerelease: false
26
+ name: sinatra
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.13.2
33
+ version_requirements: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.13.2
38
+ type: :runtime
39
+ prerelease: false
40
+ name: rmagick
41
+ description: texrack is a microservice that renders LaTeX as PNG files. Can be mounted
42
+ in a Rack app
43
+ email:
44
+ - perchr@northblue.org
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ files:
51
+ - MIT-LICENSE
52
+ - README.md
53
+ - config.ru
54
+ - lib/texrack.rb
55
+ - lib/texrack/endpoint.rb
56
+ - lib/texrack/latex_to_pdf.rb
57
+ - lib/texrack/pdf_to_png.rb
58
+ - lib/texrack/version.rb
59
+ - lib/texrack/views/form.erb
60
+ - lib/texrack/views/latex.erb
61
+ - texrack.gemspec
62
+ homepage: https://github.com/PerfectlyNormal/texrack
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --main=README.md
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.0.3
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Rack microservice to render LaTeX as PNG
87
+ test_files: []
88
+ has_rdoc:
metadata.gz.asc ADDED
@@ -0,0 +1,18 @@
1
+ -----BEGIN PGP SIGNATURE-----
2
+ Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
3
+ Comment: GPGTools - http://gpgtools.org
4
+
5
+ iQIcBAABAgAGBQJSbpGiAAoJEH1ncb0Txu7X2UwP/1PwVQwNgtK6Q9fSQOiyDuyK
6
+ teRGNeHN+AzddQ5z0YeA++fa8xUPRHbkc5VScZspypHoRVzrzbLsmrNHjRf95qNF
7
+ ZcIoOXePuYSpUNLsVekUruB9EUC6LuXbgBxSuDkwP/pCpUnXARADP6qCVXDDJCwr
8
+ xzwzvIQRkM3WkrxwaNDx8GKkwCuRu+bTPFg98XDHdqM4eK8faQKozzchFfFgGutB
9
+ 73VISK3SZFf9ciXH9HfeTi21SoyW3bQFO/Nzx+rFtOI/6A9XFvQ3yL6AWuBOnru3
10
+ 2J657sWu3N59spiuepU3V6b6esUiS6Oe1sJPIiUgVk5UFsWQ252baz6jIXaIBs7M
11
+ E8eC/OUp2zGFUYv+gpr0wWQ9rnSG4KmHD7YSVQ2E+DrFKI+aNekVvrKvGkp+6qwa
12
+ lReG7URHF7x6OXdC4WIZMYBPWzkpUF9TFdTGUyRYguItsOO0Gk0r1Amyl7E+SN1v
13
+ wkaQ8GFO4pM3UxrEbtzKaSd0utGB1GeuHEnXfPeuEWG2vfRoGNJMB6y4GzpB3uLf
14
+ jprL4cmUQgCMZz5+MPopneP5y1UK1i0fZLUCn0dcMoNpPa4Ga0zP+3y5VibzfsYW
15
+ CHMkXPDstJAzmXjNngYz/CtlePt2X2nZSrfnocq0rr7/U2icqyMi78HaNLTF1bkC
16
+ syu+g/0vyVyWXNEI84Uf
17
+ =c1X7
18
+ -----END PGP SIGNATURE-----