rails-latex 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +21 -0
- data/README.rdoc +57 -0
- data/lib/erb_latex.rb +18 -0
- data/lib/latex_to_pdf.rb +57 -0
- data/lib/rails-latex.rb +1 -0
- data/test/helper.rb +8 -0
- data/test/test_doc.tex +5 -0
- data/test/test_latex_to_pdf.rb +23 -0
- metadata +89 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2010 Geoff Jacobsen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
= Rails-LaTeX
|
2
|
+
|
3
|
+
* Git: http://github.com/jacott/rails-latex
|
4
|
+
* Author: Geoff Jacobsen
|
5
|
+
* Copyright: 2009-2010
|
6
|
+
* License: MIT-LICENSE
|
7
|
+
|
8
|
+
== Description
|
9
|
+
|
10
|
+
rails-latex is a renderer for rails 3 which allows tex files with erb to be turned into an inline pdf.
|
11
|
+
|
12
|
+
== Synopsis
|
13
|
+
|
14
|
+
app/helpers/application_helper.rb:
|
15
|
+
def lesc(text)
|
16
|
+
LatexToPdf.escape_latex(text)
|
17
|
+
end
|
18
|
+
|
19
|
+
app/views/stories/show.html.erb:
|
20
|
+
...
|
21
|
+
<%= link_to "print", story_path(@story,:format => :pdf) %>
|
22
|
+
|
23
|
+
|
24
|
+
app/views/stories/show.pdf.erb:
|
25
|
+
...
|
26
|
+
<%= lesc @story.name % >
|
27
|
+
|
28
|
+
app/views/layouts/application.pdf.erbtex:
|
29
|
+
\documentclass[12pt,a4paper,sloppy,< %= @landscape % >]{article}
|
30
|
+
...
|
31
|
+
< %= yield % >
|
32
|
+
\end{document}
|
33
|
+
|
34
|
+
|
35
|
+
config/initializers/mime_types.rb:
|
36
|
+
...
|
37
|
+
Mime::Type.register "application/pdf", :pdf, ['text/pdf'], ['pdf']
|
38
|
+
|
39
|
+
|
40
|
+
Only the file containing the \\documentclass should be of type: .pdf.erbtex . Partials and views (when there is a layout)
|
41
|
+
should be of type .pdf.erb .
|
42
|
+
|
43
|
+
== Requirements
|
44
|
+
|
45
|
+
* ruby 1.8 or 1.9
|
46
|
+
* rails 3
|
47
|
+
|
48
|
+
== Install
|
49
|
+
|
50
|
+
* gem install rails-latex
|
51
|
+
|
52
|
+
== Development
|
53
|
+
|
54
|
+
Developing rails-latex requires jeweler
|
55
|
+
|
56
|
+
* rake test
|
57
|
+
* rake build
|
data/lib/erb_latex.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'latex_to_pdf'
|
3
|
+
require 'action_view'
|
4
|
+
|
5
|
+
module ActionView # :nodoc: all
|
6
|
+
module Template::Handlers
|
7
|
+
class ERBLatex < ERB
|
8
|
+
def compile(template)
|
9
|
+
erb = "<% __in_erb_template=true %>#{template.source}"
|
10
|
+
out=self.class.erb_implementation.new(erb, :trim=>(self.class.erb_trim_mode == "-")).src
|
11
|
+
out + ";LatexToPdf.generate_pdf(@output_buffer.to_s)"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
Template.register_template_handler :erbtex, Template::Handlers::ERBLatex
|
16
|
+
end
|
17
|
+
|
18
|
+
|
data/lib/latex_to_pdf.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
class LatexToPdf
|
2
|
+
# Converts a string of LaTeX +code+ into a binary string of PDF.
|
3
|
+
#
|
4
|
+
# pdflatex is used to convert the file and creates the directory +#{Rails.root}/tmp/rails-latex+ to store intermediate
|
5
|
+
# files.
|
6
|
+
def self.generate_pdf(code)
|
7
|
+
dir=File.join(Rails.root,'tmp','rails-latex',"#{Process.pid}-#{Thread.current.hash}")
|
8
|
+
input=File.join(dir,'input.tex')
|
9
|
+
FileUtils.mkdir_p(dir)
|
10
|
+
File.open(input,'wb') {|io| io.write(code) }
|
11
|
+
system('pdflatex','-output-directory',dir,'-interaction','batchmode',input,
|
12
|
+
:umask => 7,:out => :close, :err => :close, :in => :close)
|
13
|
+
result=File.read(input.sub(/\.tex$/,'.pdf'))
|
14
|
+
FileUtils.rm_rf(dir)
|
15
|
+
result
|
16
|
+
end
|
17
|
+
|
18
|
+
# Escapes LaTex special characters in text so that they wont be interpreted as LaTex commands.
|
19
|
+
#
|
20
|
+
# This method will use RedCloth to do the escaping if available.
|
21
|
+
def self.escape_latex(text)
|
22
|
+
# :stopdoc:
|
23
|
+
unless @latex_escaper
|
24
|
+
if defined?(RedCloth::Formatters::LATEX)
|
25
|
+
class << (@latex_escaper=RedCloth.new(''))
|
26
|
+
include RedCloth::Formatters::LATEX
|
27
|
+
end
|
28
|
+
else
|
29
|
+
class << (@latex_escaper=Object.new)
|
30
|
+
ESCAPE_RE=/([{}_$&%#])|([\\^~|<>])/
|
31
|
+
ESC_MAP={
|
32
|
+
'\\' => 'backlash',
|
33
|
+
'^' => 'asciicircum',
|
34
|
+
'~' => 'asciitilde',
|
35
|
+
'|' => 'bar',
|
36
|
+
'<' => 'less',
|
37
|
+
'>' => 'greater',
|
38
|
+
}
|
39
|
+
|
40
|
+
def latex_esc(text) # :nodoc:
|
41
|
+
text.gsub(ESCAPE_RE) {|m|
|
42
|
+
if $1
|
43
|
+
"\\#{m}"
|
44
|
+
else
|
45
|
+
"\\text#{ESC_MAP[m]}{}"
|
46
|
+
end
|
47
|
+
}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
# :startdoc:
|
52
|
+
end
|
53
|
+
|
54
|
+
@latex_escaper.latex_esc(text.to_s).html_safe
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/lib/rails-latex.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'erb_latex'
|
data/test/helper.rb
ADDED
data/test/test_doc.tex
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'erb_latex'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
Rails=OpenStruct.new(:root => File.dirname(TMP_DIR=File.expand_path(File.dirname(__FILE__),'tmp')))
|
6
|
+
|
7
|
+
class TestLatexToPdf < Test::Unit::TestCase
|
8
|
+
def test_escape
|
9
|
+
assert_equal "dsf \\textless{} \\textgreater{} \\& ! @ \\# \\$ \\% \\textasciicircum{} \\textasciitilde{} \\textbacklash{} fds", LatexToPdf.escape_latex('dsf < > & ! @ # $ % ^ ~ \\ fds')
|
10
|
+
LatexToPdf.instance_eval{@latex_escaper=nil}
|
11
|
+
require 'redcloth'
|
12
|
+
assert_equal "dsf \\textless{} \\textgreater{} \\& ! @ \\# \\$ \\% \\^{} \\~{} \\textbackslash{} fds", LatexToPdf.escape_latex('dsf < > & ! @ # $ % ^ ~ \\ fds')
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_generate_pdf
|
16
|
+
FileUtils.mkdir_p(TMP_DIR)
|
17
|
+
File.open(pdf_file=File.join(TMP_DIR,'out.pdf'),'wb') do |wio|
|
18
|
+
wio.write(LatexToPdf.generate_pdf(IO.read(File.expand_path(File.dirname(__FILE__),'test_doc.tex'))))
|
19
|
+
end
|
20
|
+
assert_equal "hello world\n\n1\n\n\f", `pdftotext #{pdf_file} -`
|
21
|
+
assert_equal [], Dir["#{TMP_DIR}/rails-latex/*"]
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-latex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Geoff Jacobsen
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-07 00:00:00 +12:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rails
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
- beta3
|
33
|
+
version: 3.0.0.beta3
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: rails-latex is a renderer for rails 3 which allows tex files with erb to be turned into an inline pdf.
|
37
|
+
email: geoffjacobsen@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- MIT-LICENSE
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- MIT-LICENSE
|
47
|
+
- lib/erb_latex.rb
|
48
|
+
- lib/latex_to_pdf.rb
|
49
|
+
- lib/rails-latex.rb
|
50
|
+
- test/helper.rb
|
51
|
+
- test/test_doc.tex
|
52
|
+
- test/test_latex_to_pdf.rb
|
53
|
+
- README.rdoc
|
54
|
+
has_rdoc: true
|
55
|
+
homepage:
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --charset=UTF-8
|
61
|
+
- --main=README.rdoc
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: A LaTeX to pdf rails 3 renderer.
|
87
|
+
test_files:
|
88
|
+
- test/test_latex_to_pdf.rb
|
89
|
+
- test/helper.rb
|