gimli 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -8,11 +8,9 @@ Gimli is a utility for converting markup to pdf files. Useful for reports and su
8
8
  It's a developed version of "textile2pdf":https://github.com/walle/textile2pdf to support multiple markup styles and to get syntax highlighting.
9
9
 
10
10
  It's inspired by the markup convertion in "gollum":https://github.com/github/gollum. The markup code is adapted from gollum.
11
- It works by converting the markup to pdf using "PDFKit":https://github.com/jdpace/PDFKit
11
+ It works by converting the markup to pdf using "wkhtmltopdf":https://github.com/antialize/wkhtmltopdf
12
12
  The markup is converted to html using "github/markup":https://github.com/github/markup
13
13
 
14
- Check out a small presentation here. "http://slides.wallgren.me/gimli":http://slides.wallgren.me/gimli
15
-
16
14
  h3. Markup
17
15
 
18
16
  Markup files may be written in any format supported by GitHub-Markup (except roff).
@@ -51,6 +49,14 @@ Standard behavior is for gimli to output the files in the current directory. To
51
49
 
52
50
  Gimli also plays nice with Jekyll style markup files. You can pass gimli the @-y@ flag to have it remove Jekyll's YAML front matter from the top of your markup files. Allowing you to use gimli & Jekyll together on your Blog/Resume/Catalogue to create nicely formatted versions for online and offline viewing.
53
51
 
52
+ To pass parameters directly to wkhtmltopdf, use the @-w@ flag. eg.
53
+
54
+ bc. $ gimli -f test.md -w '--toc --footer-right "[page]/[toPage]"'
55
+
56
+ This gives a pdf with a table of contents and page numbers in the footer.
57
+
58
+ See the "man page":https://github.com/antialize/wkhtmltopdf/blob/master/README_WKHTMLTOPDF#L80 for wkhtmltopdf for all possible parameters.
59
+
54
60
  Run @gimli -h@ for a full list of options available
55
61
 
56
62
  h2. Syntax highlighting
data/bin/gimli CHANGED
@@ -9,5 +9,16 @@ if ARGV.flags.version?
9
9
  exit
10
10
  end
11
11
 
12
- Gimli.process! ARGV.flags.file, ARGV.flags.recursive?, ARGV.flags.merge?, ARGV.flags.pagenumbers?, ARGV.flags.tableofcontents?, ARGV.flags.removefrontmatter, ARGV.flags.outputfilename, ARGV.flags.outputdir, ARGV.flags.stylesheet
12
+ config = Gimli.configure do |config|
13
+ config.file = ARGV.flags.file
14
+ config.recursive = ARGV.flags.recursive?
15
+ config.merge = ARGV.flags.merge?
16
+ config.wkhtmltopdf_parameters = ARGV.flags.wkhtmltopdfparameters
17
+ config.remove_front_matter = ARGV.flags.removefrontmatter
18
+ config.output_filename = ARGV.flags.outputfilename
19
+ config.output_dir = ARGV.flags.outputdir
20
+ config.stylesheet = ARGV.flags.stylesheet
21
+ end
22
+
23
+ Gimli.process! config
13
24
 
data/lib/gimli.rb CHANGED
@@ -1,19 +1,32 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'gimli/version'
4
+ require 'gimli/config'
4
5
  require 'gimli/setup'
5
6
  require 'gimli/markupfile'
6
7
  require 'gimli/converter'
7
8
  require 'gimli/albino'
8
9
  require 'gimli/path'
10
+ require 'gimli/wkhtmltopdf'
9
11
 
10
12
  module Gimli
11
13
 
12
- # Starts the processing of selected files
13
- def self.process!(file, recursive = false, merge = false, pagenumbers = false, tableofcontents = false, remove_front_matter = false, output_filename = nil, output_dir = nil, stylesheet = nil)
14
-
14
+ # Create a config object
15
+ # @example Example usage
16
+ # config = Gimli.configure |config| do
17
+ # config.file = './test.md'
18
+ # config.output_dir = '/tmp'
19
+ # config.table_of_contents = true
20
+ # end
21
+ def self.configure
22
+ config = Config.new
23
+ yield config
24
+ config
25
+ end
15
26
 
16
- @files = Path.list_valid(file, recursive).map { |file| MarkupFile.new(file) }
17
- Converter.new(@files, merge, pagenumbers, tableofcontents, remove_front_matter, output_filename, output_dir, stylesheet).convert!
27
+ # Starts the processing of selected files
28
+ def self.process!(config)
29
+ @files = Path.list_valid(config.file, config.recursive).map { |file| MarkupFile.new(file) }
30
+ Converter.new(@files, config).convert!
18
31
  end
19
32
  end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Gimli
4
+
5
+ # Class that keeps the config parameters
6
+ class Config
7
+ attr_accessor :file, :recursive, :merge, :wkhtmltopdf_parameters, :remove_front_matter, :output_filename, :output_dir, :stylesheet
8
+
9
+ # Sets default values
10
+ def initialize
11
+ @recursive = false
12
+ @merge = false
13
+ @page_numbers = false
14
+ @table_of_contents = false
15
+ @remove_front_matter = false
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -2,33 +2,27 @@
2
2
 
3
3
  require 'fileutils'
4
4
 
5
- require 'pdfkit'
6
-
7
5
  require 'gimli/markup'
8
6
 
9
7
  module Gimli
10
8
 
11
- # The class that communicates with PDFKit
9
+ # The class that converts the files
12
10
  class Converter
13
11
 
14
12
  # Initialize the converter with a File
15
13
  # @param [Array] files The list of Gimli::MarkupFile to convert (passing a single file will still work)
16
- # @param [Boolean] merge
17
- # @param [Boolean] pagenumbers
18
- # @param [Boolean] tableofcontents
19
- # @param [Boolean] remove_front_matter
20
- # @param [String] output_filename
21
- # @param [String] output_dir
22
- # @param [String] stylesheet
23
- def initialize(files, merge = false, pagenumbers = false, tableofcontents = false, remove_front_matter = false, output_filename = nil, output_dir = nil, stylesheet = nil)
14
+ # @param [Gimli::Config] config
15
+ def initialize(files, config)
24
16
  @files = files
25
- @merge = merge
26
- @pagenumbers = pagenumbers
27
- @tableofcontents = tableofcontents
28
- @remove_front_matter = remove_front_matter
29
- @output_filename = output_filename
30
- @output_dir = output_dir
31
- @stylesheet = stylesheet
17
+ @merge = config.merge
18
+ @wkhtmltopdf_parameters = config.wkhtmltopdf_parameters
19
+ @remove_front_matter = config.remove_front_matter
20
+ @output_filename = config.output_filename
21
+ @output_dir = config.output_dir
22
+ @stylesheet = config.stylesheet
23
+ @stylesheets = []
24
+
25
+ @wkhtmltopdf = Wkhtmltopdf.new @wkhtmltopdf_parameters
32
26
  end
33
27
 
34
28
  # Convert the file and save it as a PDF file
@@ -63,35 +57,35 @@ module Gimli
63
57
  html
64
58
  end
65
59
 
66
- # Load the pdfkit with html
67
- # @param [String] html
68
- # @return [PDFKit]
69
- def pdf_kit(html)
70
- options = {}
71
- options.merge!({ :footer_right => '[page]/[toPage]' }) if @pagenumbers
72
- options.merge!({ :toc => true }) if @tableofcontents
73
- kit = PDFKit.new(html, options)
74
-
75
- load_stylesheets kit
76
-
77
- kit
78
- end
79
-
80
60
  # Create the pdf
81
61
  # @param [String] html the html input
82
62
  # @param [String] filename the name of the output file
83
63
  def output_pdf(html, filename)
84
- kit = pdf_kit(html)
85
- kit.to_file(output_file(filename))
64
+ load_stylesheets
65
+ append_stylesheets html
66
+ @wkhtmltopdf.output_pdf html, output_file(filename)
86
67
  end
87
68
 
88
69
  # Load the stylesheets to pdfkit loads the default and the user selected if any
89
- # @param [PDFKit] kit
90
- def load_stylesheets(kit)
70
+ def load_stylesheets
91
71
  # Load standard stylesheet
92
72
  style = ::File.expand_path("../../../config/style.css", __FILE__)
93
- kit.stylesheets << style
94
- kit.stylesheets << stylesheet if ::File.exists?(stylesheet)
73
+ @stylesheets << style
74
+ @stylesheets << stylesheet if ::File.exists?(stylesheet)
75
+ end
76
+
77
+ def append_stylesheets(html)
78
+ @stylesheets.each do |stylesheet|
79
+ if html.match(/<\/head>/)
80
+ html = html.gsub(/(<\/head>)/, style_tag_for(stylesheet)+'\1')
81
+ else
82
+ html.insert(0, style_tag_for(stylesheet))
83
+ end
84
+ end
85
+ end
86
+
87
+ def style_tag_for(stylesheet)
88
+ "<style>#{File.read(stylesheet)}</style>"
95
89
  end
96
90
 
97
91
  # Returns the selected stylesheet. Defaults to ./gimli.css
data/lib/gimli/setup.rb CHANGED
@@ -20,6 +20,10 @@ module Gimli extend OptiFlagSet
20
20
  description 'Sets the name of the output file. Only used with single file and in merge mode'
21
21
  alternate_forms 'n'
22
22
  end
23
+ optional_flag 'wkhtmltopdfparameters' do
24
+ description 'Parameters to be passed on to wkhtmltopdf. Use "" if more than one parameter. See wkhtmltopdf usage for possible parameters.'
25
+ alternate_forms 'w'
26
+ end
23
27
  optional_switch_flag 'recursive' do
24
28
  description 'Recurse current or target directory and convert all valid markup files'
25
29
  alternate_forms 'r'
@@ -28,14 +32,6 @@ module Gimli extend OptiFlagSet
28
32
  description 'Merge markup files into single pdf file'
29
33
  alternate_forms 'm'
30
34
  end
31
- optional_switch_flag 'pagenumbers' do
32
- description 'Print the page numbers in the lower right corner of all pages in pdf'
33
- alternate_forms 'p'
34
- end
35
- optional_switch_flag 'tableofcontents' do
36
- description 'Insert a table of contents first in the pdf'
37
- alternate_forms 't'
38
- end
39
35
  optional_switch_flag 'removefrontmatter' do
40
36
  description 'Remove yaml frontmatter from your files.'
41
37
  alternate_forms 'y'
data/lib/gimli/version.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Gimli
4
4
 
5
- Version = "0.2.3"
5
+ Version = "0.3.0"
6
6
 
7
7
  end
8
8
 
@@ -0,0 +1,41 @@
1
+ module Gimli
2
+
3
+ # The class that communicates with wkhtmltopdf
4
+ class Wkhtmltopdf
5
+
6
+ # Set up options for wkhtmltopdf
7
+ # @param [String] parameters
8
+ def initialize(parameters = nil)
9
+ @parameters = parameters
10
+ end
11
+
12
+ # Convert the html to pdf and write it to file
13
+ # @param [String] html the html input
14
+ # @param [String] filename the name of the output file
15
+ def output_pdf(html, filename)
16
+ args = command(filename)
17
+ invoke = args.join(' ')
18
+
19
+ result = IO.popen(invoke, "wb+") do |pdf|
20
+ pdf.puts(html)
21
+ pdf.close_write
22
+ pdf.gets(nil)
23
+ end
24
+ end
25
+
26
+ # Assemble the command to run
27
+ # @param [String] filename the outputed pdf's filename
28
+ # @return [Array] a list of strings that make out the call to wkhtmltopdf
29
+ def command(filename)
30
+ [bin, @parameters, '--quiet', '-', "\"#{filename}\""].compact
31
+ end
32
+
33
+ # Find the wkhtmltopdf binary
34
+ # @return [String] the path to the binary
35
+ def bin
36
+ @bin ||= "\"#{(`which wkhtmltopdf`).chomp}\""
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -10,7 +10,7 @@ describe Gimli::Converter do
10
10
  name = 'my_file'
11
11
  mock(file).name { name }
12
12
 
13
- converter = Gimli::Converter.new [file]
13
+ converter = Gimli::Converter.new [file], Gimli::Config.new
14
14
  mock(converter).output_dir { Dir.getwd }
15
15
 
16
16
  converter.output_file.should == File.join(Dir.getwd, "#{name}.pdf")
@@ -21,7 +21,7 @@ describe Gimli::Converter do
21
21
  name = 'my_file'
22
22
  mock(file).name { name }
23
23
 
24
- converter = Gimli::Converter.new [file]
24
+ converter = Gimli::Converter.new [file], Gimli::Config.new
25
25
  mock(converter).output_dir { '/tmp/out' }
26
26
 
27
27
  converter.output_file(file).should == "/tmp/out/#{name}.pdf"
@@ -31,7 +31,11 @@ describe Gimli::Converter do
31
31
  file = Gimli::MarkupFile.new 'fake'
32
32
  output_filename = 'my_file'
33
33
 
34
- converter = Gimli::Converter.new [file], false, false, false, false, output_filename
34
+ config = Gimli.configure do |c|
35
+ c.output_filename = output_filename
36
+ end
37
+
38
+ converter = Gimli::Converter.new [file], config
35
39
  mock(converter).output_dir { Dir.getwd }
36
40
 
37
41
  converter.output_file.should == File.join(Dir.getwd, "#{output_filename}.pdf")
@@ -41,7 +45,7 @@ describe Gimli::Converter do
41
45
  dir = Dir.getwd
42
46
 
43
47
  file = Gimli::MarkupFile.new 'fake'
44
- converter = Gimli::Converter.new file
48
+ converter = Gimli::Converter.new file, Gimli::Config.new
45
49
 
46
50
  converter.output_dir.should == dir
47
51
  end
@@ -50,7 +54,12 @@ describe Gimli::Converter do
50
54
  dir = '/tmp/out'
51
55
 
52
56
  file = Gimli::MarkupFile.new 'fake'
53
- converter = Gimli::Converter.new file, false, false, false, false, nil, dir
57
+
58
+ config = Gimli.configure do |c|
59
+ c.output_dir = dir
60
+ end
61
+
62
+ converter = Gimli::Converter.new file, config
54
63
 
55
64
  mock(File).directory?(dir) { true }
56
65
 
@@ -59,7 +68,7 @@ describe Gimli::Converter do
59
68
 
60
69
  it 'should use default stylesheet if none given' do
61
70
  file = Gimli::MarkupFile.new 'fake'
62
- converter = Gimli::Converter.new file
71
+ converter = Gimli::Converter.new file, Gimli::Config.new
63
72
 
64
73
  converter.stylesheet.should == 'gimli.css'
65
74
  end
@@ -68,7 +77,11 @@ describe Gimli::Converter do
68
77
  file = Gimli::MarkupFile.new 'fake'
69
78
  stylesheet = '/home/me/gimli/my-style.css'
70
79
 
71
- converter = Gimli::Converter.new file, false, false, false, false, nil, nil, stylesheet
80
+ config = Gimli.configure do |c|
81
+ c.stylesheet = stylesheet
82
+ end
83
+
84
+ converter = Gimli::Converter.new file, config
72
85
 
73
86
  converter.stylesheet.should == stylesheet
74
87
  end
@@ -77,7 +90,7 @@ describe Gimli::Converter do
77
90
  file = Gimli::MarkupFile.new 'fake'
78
91
  filename = 'fixtures/fake.textile'
79
92
  dir_string = ::File.dirname(::File.expand_path(filename))
80
- converter = Gimli::Converter.new file
93
+ converter = Gimli::Converter.new file, Gimli::Config.new
81
94
 
82
95
  html = '<p>foo</p><img src="test.jpg" alt="" /><p>bar</p><img src="test2.jpg" alt="" />'
83
96
  valid_html = "<p>foo</p><img src=\"#{File.expand_path('test.jpg', dir_string)}\" alt=\"\" /><p>bar</p><img src=\"#{File.expand_path('test2.jpg', dir_string)}\" alt=\"\" />"
@@ -89,7 +102,7 @@ describe Gimli::Converter do
89
102
  file = Gimli::MarkupFile.new 'fake'
90
103
  filename = '../../fixtures/fake.textile'
91
104
  dir_string = ::File.dirname(::File.expand_path(filename))
92
- converter = Gimli::Converter.new file
105
+ converter = Gimli::Converter.new file, Gimli::Config.new
93
106
 
94
107
  html = '<p>foo</p><img src="https://d3nwyuy0nl342s.cloudfront.net/images/modules/header/logov3-hover.png" alt="" /><p>bar</p>'
95
108
 
@@ -100,7 +113,7 @@ describe Gimli::Converter do
100
113
  file = Gimli::MarkupFile.new 'fake'
101
114
  filename = '../../fixtures/fake.textile'
102
115
  dir_string = ::File.dirname(::File.expand_path(filename))
103
- converter = Gimli::Converter.new file
116
+ converter = Gimli::Converter.new file, Gimli::Config.new
104
117
 
105
118
  html = '<p>foo</p><img src="test.jpg" alt="" /><p>bar</p><img src="/tmp/test2.jpg" alt="" /> <img src="https://d3nwyuy0nl342s.cloudfront.net/images/modules/header/logov3-hover.png" alt="" />'
106
119
  valid_html = "<p>foo</p><img src=\"#{File.expand_path('test.jpg', dir_string)}\" alt=\"\" /><p>bar</p><img src=\"/tmp/test2.jpg\" alt=\"\" /> <img src=\"https://d3nwyuy0nl342s.cloudfront.net/images/modules/header/logov3-hover.png\" alt=\"\" />"
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ require './spec/spec_helper'
4
+
5
+ require './lib/gimli'
6
+
7
+ describe Gimli::Wkhtmltopdf do
8
+
9
+ it 'should assemble correct command' do
10
+ wkhtmltopdf = Gimli::Wkhtmltopdf.new
11
+ mock(wkhtmltopdf).bin { '"wkhtmltopdf"' }
12
+ args = wkhtmltopdf.command('test.pdf')
13
+ args.size.should == 4
14
+ args.should include '"wkhtmltopdf"'
15
+ args.should include '--quiet'
16
+ args.should include '-'
17
+ args.should include '"test.pdf"'
18
+ end
19
+
20
+ end
21
+
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
+ if ENV['COVERAGE']
4
+ require 'simplecov'
5
+ SimpleCov.start
6
+ end
7
+
3
8
  RSpec.configure do |config|
4
9
  config.mock_with :rr
5
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gimli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-07 00:00:00.000000000 Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: github-markup
@@ -155,22 +155,6 @@ dependencies:
155
155
  - - ~>
156
156
  - !ruby/object:Gem::Version
157
157
  version: 0.9.9.1
158
- - !ruby/object:Gem::Dependency
159
- name: pdfkit
160
- requirement: !ruby/object:Gem::Requirement
161
- none: false
162
- requirements:
163
- - - ~>
164
- - !ruby/object:Gem::Version
165
- version: 0.5.2
166
- type: :runtime
167
- prerelease: false
168
- version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
- requirements:
171
- - - ~>
172
- - !ruby/object:Gem::Version
173
- version: 0.5.2
174
158
  - !ruby/object:Gem::Dependency
175
159
  name: optiflag
176
160
  requirement: !ruby/object:Gem::Requirement
@@ -265,8 +249,10 @@ files:
265
249
  - lib/gimli/path.rb
266
250
  - lib/gimli/markupfile.rb
267
251
  - lib/gimli/converter.rb
252
+ - lib/gimli/wkhtmltopdf.rb
268
253
  - lib/gimli/setup.rb
269
254
  - lib/gimli/albino.rb
255
+ - lib/gimli/config.rb
270
256
  - lib/gimli/version.rb
271
257
  - lib/gimli/markup.rb
272
258
  - lib/gimli.rb
@@ -274,6 +260,7 @@ files:
274
260
  - spec/gimli/converter_spec.rb
275
261
  - spec/gimli/markup_spec.rb
276
262
  - spec/gimli/file_spec.rb
263
+ - spec/gimli/wkhtmltopdf_spec.rb
277
264
  - spec/fixtures/code_with_utf8.textile
278
265
  - spec/fixtures/recursion/level2/level2.markdown
279
266
  - spec/fixtures/recursion/level1.textile
@@ -299,7 +286,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
299
286
  version: '0'
300
287
  segments:
301
288
  - 0
302
- hash: -3032383899323034374
289
+ hash: 4505981425751423079
303
290
  required_rubygems_version: !ruby/object:Gem::Requirement
304
291
  none: false
305
292
  requirements:
@@ -308,7 +295,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
308
295
  version: '0'
309
296
  segments:
310
297
  - 0
311
- hash: -3032383899323034374
298
+ hash: 4505981425751423079
312
299
  requirements: []
313
300
  rubyforge_project: gimli
314
301
  rubygems_version: 1.8.24