gimli 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,15 +13,9 @@ module Gimli
13
13
  # @param [Array] files The list of Gimli::MarkupFile to convert (passing a single file will still work)
14
14
  # @param [Gimli::Config] config
15
15
  def initialize(files, config)
16
- @files = files
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 = []
16
+ @files, @config = files, config
24
17
 
18
+ @stylesheets = []
25
19
  @wkhtmltopdf = Wkhtmltopdf.new @wkhtmltopdf_parameters
26
20
  end
27
21
 
@@ -29,9 +23,9 @@ module Gimli
29
23
  def convert!
30
24
  merged_contents = []
31
25
  @files.each do |file|
32
- markup = Markup::Renderer.new file, @remove_front_matter
26
+ markup = Markup::Renderer.new file, @config.remove_front_matter
33
27
  html = convert_image_urls markup.render, file.filename
34
- if @merge
28
+ if @config.merge
35
29
  html = "<div class=\"page-break\"></div>#{html}" unless merged_contents.empty?
36
30
  merged_contents << html
37
31
  else
@@ -76,11 +70,7 @@ module Gimli
76
70
 
77
71
  def append_stylesheets(html)
78
72
  @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
73
+ html.insert(0, style_tag_for(stylesheet))
84
74
  end
85
75
  end
86
76
 
@@ -91,13 +81,13 @@ module Gimli
91
81
  # Returns the selected stylesheet. Defaults to ./gimli.css
92
82
  # @return [String]
93
83
  def stylesheet
94
- @stylesheet.nil? ? 'gimli.css' : @stylesheet
84
+ @config.stylesheet.nil? ? 'gimli.css' : @config.stylesheet
95
85
  end
96
86
 
97
87
  # Returns the directory where to save the output. Defaults to ./
98
88
  # @return [String]
99
89
  def output_dir
100
- output_dir = @output_dir.nil? ? Dir.getwd : @output_dir
90
+ output_dir = @config.output_dir.nil? ? Dir.getwd : @config.output_dir
101
91
  FileUtils.mkdir_p(output_dir) unless ::File.directory?(output_dir)
102
92
  output_dir
103
93
  end
@@ -108,13 +98,13 @@ module Gimli
108
98
  def output_file(file = nil)
109
99
  if file
110
100
  output_filename = file.name
111
- if !@output_filename.nil? && @files.length == 1
112
- output_filename = @output_filename
101
+ if !@config.output_filename.nil? && @files.length == 1
102
+ output_filename = @config.output_filename
113
103
  end
114
104
  else
115
105
  output_filename = Time.now.to_s.split(' ').join('_')
116
- output_filename = @files.last.name if @files.length == 1 || @merge
117
- output_filename = @output_filename unless @output_filename.nil?
106
+ output_filename = @files.last.name if @files.length == 1 || @config.merge
107
+ output_filename = @config.output_filename unless @config.output_filename.nil?
118
108
  end
119
109
 
120
110
  ::File.join(output_dir, "#{output_filename}.pdf")
@@ -14,6 +14,8 @@ module Gimli
14
14
  @id, @language, @code = id, language, code
15
15
  end
16
16
 
17
+ # Returns the code with syntax highlightning
18
+ # @return [String]
17
19
  def highlighted
18
20
  if @language
19
21
  CodeRay.scan(@code, @language).html(:line_numbers => :table)
@@ -12,10 +12,12 @@ module Gimli
12
12
  # @param [Boolean] do_remove_yaml_front_matter Should we remove the front matter?
13
13
  # @return [Gimli::Markup]
14
14
  def initialize(file, do_remove_yaml_front_matter = false)
15
- @filename = file.filename
16
- @name = file.name
17
- @data = file.data
15
+ @file = file
18
16
  @do_remove_yaml_front_matter = do_remove_yaml_front_matter
17
+
18
+ @data = file.data
19
+ @code = Code.new
20
+ @yaml_frontmatter_remover = YamlFrontmatterRemover.new
19
21
  end
20
22
 
21
23
  # Render the content with Gollum wiki syntax on top of the file's own
@@ -23,32 +25,40 @@ module Gimli
23
25
  #
24
26
  # @return [String] The formatted data
25
27
  def render
26
- data = @data.dup
27
- @code = Code.new
28
- @yaml_frontmatter_remover = YamlFrontmatterRemover.new
29
- data = @yaml_frontmatter_remover.process(data) if @do_remove_yaml_front_matter
30
- data = @code.extract(data)
28
+ prepare_data
29
+ render_data
30
+ post_process_data
31
+
32
+ return @data
33
+ end
34
+
35
+ private
36
+
37
+ # Prepare data for rendering
38
+ def prepare_data
39
+ @data = @yaml_frontmatter_remover.process(@data) if @do_remove_yaml_front_matter
40
+ @data = @code.extract(@data)
41
+ end
42
+
43
+ # Do the markup to html rendering
44
+ def render_data
31
45
  begin
32
- data = data.force_encoding('utf-8') if data.respond_to? :force_encoding
33
- data = GitHub::Markup.render(@filename, data)
34
- if data.nil?
35
- raise "There was an error converting #{@name} to HTML."
46
+ @data = @data.force_encoding('utf-8') if @data.respond_to? :force_encoding
47
+ @data = GitHub::Markup.render(@file.filename, @data)
48
+ if @data.nil?
49
+ raise "There was an error converting #{@file.name} to HTML."
36
50
  end
37
51
  rescue Object => e
38
- data = %{<p class="gimli-error">#{e.message}</p>}
52
+ @data = %{<p class="gimli-error">#{e.message}</p>}
39
53
  end
40
- data = @code.process(data)
41
-
42
- doc = Nokogiri::HTML::DocumentFragment.parse(data, 'UTF-8')
43
- yield doc if block_given?
44
- data = doc_to_html(doc)
45
-
46
- data.gsub!(/<p><\/p>/, '')
47
- data
48
54
  end
49
55
 
50
- def doc_to_html(doc)
51
- doc.to_xhtml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XHTML, :encoding => 'UTF-8')
56
+ # Do post processing on data
57
+ def post_process_data
58
+ @data = @code.process(@data)
59
+ doc = Nokogiri::HTML::DocumentFragment.parse(@data, 'UTF-8')
60
+ @data = doc.to_xhtml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XHTML, :encoding => 'UTF-8')
61
+ @data.gsub!(/<p><\/p>/, '')
52
62
  end
53
63
  end
54
64
  end
data/lib/gimli/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Gimli
4
- VERSION = "0.3.2"
4
+ VERSION = "0.4.0"
5
5
  end
6
6
 
@@ -38,7 +38,7 @@ describe Gimli::Converter do
38
38
  converter = Gimli::Converter.new [file], config
39
39
  mock(converter).output_dir { Dir.getwd }
40
40
 
41
- converter.output_file.should == File.join(Dir.getwd, "#{output_filename}.pdf")
41
+ converter.output_file(file).should == File.join(Dir.getwd, "#{output_filename}.pdf")
42
42
  end
43
43
 
44
44
  it 'should give the correct output_dir when none given' do
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.3.2
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,54 +59,6 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: 4.2.7
62
- - !ruby/object:Gem::Dependency
63
- name: org-ruby
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ~>
68
- - !ruby/object:Gem::Version
69
- version: 0.7.1
70
- type: :runtime
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ~>
76
- - !ruby/object:Gem::Version
77
- version: 0.7.1
78
- - !ruby/object:Gem::Dependency
79
- name: creole
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ~>
84
- - !ruby/object:Gem::Version
85
- version: 0.4.2
86
- type: :runtime
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ~>
92
- - !ruby/object:Gem::Version
93
- version: 0.4.2
94
- - !ruby/object:Gem::Dependency
95
- name: wikicloth
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ~>
100
- - !ruby/object:Gem::Version
101
- version: 0.8.0
102
- type: :runtime
103
- prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ~>
108
- - !ruby/object:Gem::Version
109
- version: 0.8.0
110
62
  - !ruby/object:Gem::Dependency
111
63
  name: coderay
112
64
  requirement: !ruby/object:Gem::Requirement
@@ -296,7 +248,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
296
248
  version: '0'
297
249
  segments:
298
250
  - 0
299
- hash: -3276508745148089455
251
+ hash: 2603706137914885458
300
252
  required_rubygems_version: !ruby/object:Gem::Requirement
301
253
  none: false
302
254
  requirements:
@@ -305,7 +257,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
305
257
  version: '0'
306
258
  segments:
307
259
  - 0
308
- hash: -3276508745148089455
260
+ hash: 2603706137914885458
309
261
  requirements: []
310
262
  rubyforge_project: gimli
311
263
  rubygems_version: 1.8.24