propaganda 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +15 -10
- data/Rakefile +0 -1
- data/VERSION +1 -1
- data/lib/propaganda/fop/bridge.rb +46 -0
- data/lib/propaganda/fop/shell.rb +32 -0
- data/lib/propaganda/renderer.rb +48 -0
- data/lib/propaganda.rb +2 -2
- data/test/fop_test.rb +3 -3
- data/test/samples/sample.pdf +0 -0
- metadata +5 -13
- data/lib/propaganda/fop.rb +0 -63
data/README.rdoc
CHANGED
@@ -3,6 +3,21 @@
|
|
3
3
|
Generate PDFs from HTML. Generate them from Markdown and Textile. Take over
|
4
4
|
the world.
|
5
5
|
|
6
|
+
== Note on Ruby Java Bridge and Invoking the Apache library
|
7
|
+
|
8
|
+
This gem relies on a java binary and Ruby provides a few ways of getting to
|
9
|
+
that. This gem provides support for executing the binary via the Ruby Java
|
10
|
+
Bridge (rjb) which is available on rubygems (http://rubygems.org/gems/rjb).
|
11
|
+
Source is available on Github (http://github.com/arton/rjb). When installing
|
12
|
+
the rjb gem you must have a JAVA_HOME environment variable set. If you install
|
13
|
+
your gems using sudo this can be flummoxing. For OSX:
|
14
|
+
|
15
|
+
sudo env JAVA_HOME=/Library/Java/Home gem install rjb
|
16
|
+
|
17
|
+
If you don't want to use rjb, you don't have to. The gem can invoke the java
|
18
|
+
library via shell commands if it doesn't detect the rjb gem. In order to
|
19
|
+
facilitate this you must have the java application on your path.
|
20
|
+
|
6
21
|
== Installation
|
7
22
|
|
8
23
|
Propaganda is available on Rubygems.org.
|
@@ -53,16 +68,6 @@ keep-together.within-<context>="always" etc. where <context> is line if the
|
|
53
68
|
declaration is found on an inline level element, column if within a table cell
|
54
69
|
otherwise page.
|
55
70
|
|
56
|
-
== Note on Ruby Java Bridge
|
57
|
-
|
58
|
-
For non-jruby usage this gem relies on the Ruby Java Bridge (rjb) which is
|
59
|
-
available on rubygems (http://rubygems.org/gems/rjb). Source is available
|
60
|
-
on Github (http://github.com/arton/rjb). When installing the rjb gem you
|
61
|
-
must have a JAVA_HOME environment variable set. If you install your gems
|
62
|
-
using sudo this can be flummoxing. Just use:
|
63
|
-
|
64
|
-
sudo env JAVA_HOME=/Library/Java/Home gem install rjb
|
65
|
-
|
66
71
|
== Note on Patches/Pull Requests
|
67
72
|
|
68
73
|
* Fork the project.
|
data/Rakefile
CHANGED
@@ -13,7 +13,6 @@ begin
|
|
13
13
|
gem.files = FileList["[A-Z]*", "{bin,java,lib,templates,test}/**/*"]
|
14
14
|
gem.add_dependency "BlueCloth", ">= 1.0.0"
|
15
15
|
gem.add_dependency "RedCloth", ">= 4.1.1"
|
16
|
-
gem.add_dependency "rjb", ">= 1.2.0"
|
17
16
|
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
18
17
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
18
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rjb'
|
2
|
+
|
3
|
+
module Propaganda
|
4
|
+
module Fop
|
5
|
+
class Bridge
|
6
|
+
def initialize(verbose=false)
|
7
|
+
@verbose = verbose
|
8
|
+
end
|
9
|
+
|
10
|
+
def invoke(*args)
|
11
|
+
# When invoking we need to use our own Manager class because the default
|
12
|
+
# cli Main class deletes the file on exit and always calls System.exit
|
13
|
+
# which closes our application. We avoid that and also setup additional
|
14
|
+
# protection against rogue System.exit calls in the library
|
15
|
+
SystemExitManager.disableSystemExitCall
|
16
|
+
Manager._invoke('main', '[Ljava.lang.String;', args)
|
17
|
+
Output.toString
|
18
|
+
rescue Exception => e
|
19
|
+
raise "Could not render document [#{e}] (" + Errors.toString + ")"
|
20
|
+
ensure
|
21
|
+
SystemExitManager.enableSystemExitCall
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def self.classpath
|
27
|
+
path = File.join(File.dirname(__FILE__), '..', '..', '..', 'java')
|
28
|
+
File.expand_path(path)+':'+File.join(path, 'fop.jar')
|
29
|
+
end
|
30
|
+
|
31
|
+
Rjb::load(Bridge.classpath, ['-Djava.awt.headless=true'])
|
32
|
+
SystemExitManager = Rjb::import 'SystemExitManager'
|
33
|
+
Manager = Rjb::import 'org.apache.fop.cli.Manager'
|
34
|
+
ByteArray = Rjb::import 'java.io.ByteArrayOutputStream'
|
35
|
+
PrintStream = Rjb::import 'java.io.PrintStream'
|
36
|
+
|
37
|
+
# Internally fop is very noisy, we have to block all of that if we don't
|
38
|
+
# want to go crazy. To do that we overwrite the default streams,
|
39
|
+
# unfortunately these are globals, so its one size fits all
|
40
|
+
Errors = ByteArray.new
|
41
|
+
Rjb::import('java.lang.System').err = PrintStream.new(Errors)
|
42
|
+
Output = ByteArray.new
|
43
|
+
Rjb::import('java.lang.System').out = PrintStream.new(Output)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'open3'
|
2
|
+
|
3
|
+
module Propaganda
|
4
|
+
module Fop
|
5
|
+
class Shell
|
6
|
+
def initialize(verbose=false)
|
7
|
+
@verbose = verbose
|
8
|
+
end
|
9
|
+
|
10
|
+
def invoke(*args)
|
11
|
+
command = "java -Djava.awt.headless=true -jar #{jarpath} #{args.join(' ')}"
|
12
|
+
if @verbose
|
13
|
+
`#{command}`
|
14
|
+
else
|
15
|
+
stdin, stdout, stderr = Open3.popen3(command)
|
16
|
+
@errors = stderr.read
|
17
|
+
@output = stdout.read
|
18
|
+
raise "Could not invoke: #{@errors}" unless @errors.blank?
|
19
|
+
@output
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def self.jarpath
|
26
|
+
path = File.join(File.dirname(__FILE__), '..', '..', '..', 'java')
|
27
|
+
File.expand_path(File.join(path, 'fop.jar'))
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module Propaganda
|
4
|
+
|
5
|
+
def self.check_dependencies
|
6
|
+
require 'propaganda/fop/shell'
|
7
|
+
require 'propaganda/fop/bridge'
|
8
|
+
end
|
9
|
+
|
10
|
+
MODE = check_dependencies ? 'rjb' : 'shell'
|
11
|
+
|
12
|
+
class Renderer
|
13
|
+
def initialize(verbose=false)
|
14
|
+
@verbose = verbose
|
15
|
+
end
|
16
|
+
|
17
|
+
def version
|
18
|
+
invoke('-v')
|
19
|
+
end
|
20
|
+
|
21
|
+
def render(html, output, template=nil)
|
22
|
+
template ||= 'default'
|
23
|
+
stylesheet = File.join(File.dirname(__FILE__), '..', '..', 'templates', "#{template}.xsl")
|
24
|
+
stylesheet = File.expand_path(stylesheet)
|
25
|
+
tmp = Tempfile.new('fop')
|
26
|
+
tmp << html
|
27
|
+
tmp.flush
|
28
|
+
tmp.close
|
29
|
+
output = File.expand_path(output)
|
30
|
+
invoke('-xml', tmp.path, '-xsl', stylesheet, '-pdf', output)
|
31
|
+
ensure
|
32
|
+
tmp.close rescue nil
|
33
|
+
tmp = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def invoke(*args)
|
39
|
+
if MODE == 'rjb'
|
40
|
+
fop = Propaganda::Fop::Bridge.new(@verbose)
|
41
|
+
fop.invoke(*args)
|
42
|
+
elsif MODE == 'shell'
|
43
|
+
fop = Propaganda::Fop::Shell.new(@verbose)
|
44
|
+
fop.invoke(*args)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/propaganda.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'propaganda/
|
1
|
+
require 'propaganda/renderer'
|
2
2
|
require 'propaganda/formatter'
|
3
3
|
|
4
4
|
module Propaganda
|
@@ -8,7 +8,7 @@ module Propaganda
|
|
8
8
|
text = IO.read(input)
|
9
9
|
formatter = Formatter.new
|
10
10
|
text = formatter.format(text, title, engine)
|
11
|
-
fop =
|
11
|
+
fop = Renderer.new(verbose)
|
12
12
|
fop.render(text, output, template)
|
13
13
|
end
|
14
14
|
|
data/test/fop_test.rb
CHANGED
@@ -5,13 +5,13 @@ class FopTest < Test::Unit::TestCase
|
|
5
5
|
File.delete('test/samples/sample.pdf') rescue nil
|
6
6
|
assert File.exists?('test/samples/sample.html')
|
7
7
|
text = IO.read('test/samples/sample.html')
|
8
|
-
fop = Propaganda::
|
8
|
+
fop = Propaganda::Renderer.new(true)
|
9
9
|
fop.render(text, 'test/samples/sample.pdf')
|
10
|
-
assert File.exists?('test/samples/sample.pdf')
|
10
|
+
assert File.exists?('test/samples/sample.pdf')
|
11
11
|
end
|
12
12
|
|
13
13
|
should "get the version" do
|
14
|
-
fop = Propaganda::
|
14
|
+
fop = Propaganda::Renderer.new
|
15
15
|
assert fop.version
|
16
16
|
end
|
17
17
|
end
|
data/test/samples/sample.pdf
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: propaganda
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Rafter
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-25 00:00:00 -08:00
|
13
13
|
default_executable: propaganda
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -32,16 +32,6 @@ dependencies:
|
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 4.1.1
|
34
34
|
version:
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: rjb
|
37
|
-
type: :runtime
|
38
|
-
version_requirement:
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
40
|
-
requirements:
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: 1.2.0
|
44
|
-
version:
|
45
35
|
- !ruby/object:Gem::Dependency
|
46
36
|
name: thoughtbot-shoulda
|
47
37
|
type: :development
|
@@ -84,8 +74,10 @@ files:
|
|
84
74
|
- java/xml-apis-ext-1.3.04.jar
|
85
75
|
- java/xmlgraphics-commons-1.3.1.jar
|
86
76
|
- lib/propaganda.rb
|
87
|
-
- lib/propaganda/fop.rb
|
77
|
+
- lib/propaganda/fop/bridge.rb
|
78
|
+
- lib/propaganda/fop/shell.rb
|
88
79
|
- lib/propaganda/formatter.rb
|
80
|
+
- lib/propaganda/renderer.rb
|
89
81
|
- templates/clean.xsl
|
90
82
|
- templates/default.xsl
|
91
83
|
- test/fop_test.rb
|
data/lib/propaganda/fop.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
require 'rjb'
|
2
|
-
require 'tempfile'
|
3
|
-
|
4
|
-
module Propaganda
|
5
|
-
class Fop
|
6
|
-
def initialize(verbose=false)
|
7
|
-
unless verbose
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def version
|
12
|
-
invoke('-v')
|
13
|
-
Output.toString
|
14
|
-
end
|
15
|
-
|
16
|
-
def render(html, output, template=nil)
|
17
|
-
template ||= 'default'
|
18
|
-
stylesheet = File.join(File.dirname(__FILE__), '..', '..', 'templates', "#{template}.xsl")
|
19
|
-
stylesheet = File.expand_path(stylesheet)
|
20
|
-
tmp = Tempfile.new('fop')
|
21
|
-
tmp << html
|
22
|
-
tmp.flush
|
23
|
-
tmp.close
|
24
|
-
output = File.expand_path(output)
|
25
|
-
invoke('-xml', tmp.path, '-xsl', stylesheet, '-pdf', output)
|
26
|
-
ensure
|
27
|
-
tmp = nil
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def invoke(*args)
|
33
|
-
# When invoking we need to use our own Manager class because the default
|
34
|
-
# cli Main class deletes the file on exit and always calls System.exit
|
35
|
-
# which closes our application. We avoid that and also setup additional
|
36
|
-
# protection against rogue System.exit calls in the library
|
37
|
-
SystemExitManager.disableSystemExitCall
|
38
|
-
Manager._invoke('main', '[Ljava.lang.String;', args)
|
39
|
-
rescue Exception => e
|
40
|
-
raise "Could not render document [#{e}] (" + Errors.toString + ")"
|
41
|
-
ensure
|
42
|
-
SystemExitManager.enableSystemExitCall
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.classpath
|
46
|
-
path = File.join(File.dirname(__FILE__), '..', '..', 'java')
|
47
|
-
File.expand_path(path+':'+File.join(path, 'fop.jar'))
|
48
|
-
end
|
49
|
-
|
50
|
-
Rjb::load(Fop.classpath, ['-Djava.awt.headless=true'])
|
51
|
-
SystemExitManager = Rjb::import 'SystemExitManager'
|
52
|
-
Manager = Rjb::import 'org.apache.fop.cli.Manager'
|
53
|
-
ByteArray = Rjb::import 'java.io.ByteArrayOutputStream'
|
54
|
-
PrintStream = Rjb::import 'java.io.PrintStream'
|
55
|
-
|
56
|
-
# Internally fop is very noisy, we have to block all of that if we don't
|
57
|
-
# want to go crazy. To do that we overwrite the default streams
|
58
|
-
Errors = ByteArray.new
|
59
|
-
Rjb::import('java.lang.System').err = PrintStream.new(Errors)
|
60
|
-
Output = ByteArray.new
|
61
|
-
Rjb::import('java.lang.System').out = PrintStream.new(Output)
|
62
|
-
end
|
63
|
-
end
|