any2pdf 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05ce3ca115826a2f7bb50ed4fc0f57995fc1d540
4
- data.tar.gz: 21af605e0c4312d2274813e5ade3c84d9e3a82db
3
+ metadata.gz: 4d78a031dfe4f63afa1d0b33c4770b1ea769704a
4
+ data.tar.gz: bd3975264c73cf68f6ed59b4f40add219f1521c8
5
5
  SHA512:
6
- metadata.gz: 459358330747938fbe49f720c7b1dac3ee7b0fb76b38be3d0163efcf9bb1eeb710c24621d7ca9d98923ccdcb71278134a36f92b13f5b81bd211fff44ded24fba
7
- data.tar.gz: 6d6a0eaf6e6e2c0d8443d2cf4ef51a6818e37352b5b077e8a48b844569f07be1217ee62f7a1167c455e714686bf35d84093c8883da36c823739e74bce2aca5bd
6
+ metadata.gz: dd7cf986a1f429c1733cc61c817cdbe1fa55b3b7b3690d68fdc8faf96eed2c9aedadeeba62f9b97a7a616295e826873e2ffee4fedd3de7b5eb34a0b4e7e4e238
7
+ data.tar.gz: a71db6d57213764152e53f0e2013ea2ca9f60f85b0cd18122f5f39bfba117ed700873f062d39832231a1fd205c4a692ac6d558c6c9542365792415d7ba19acd6
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  Any2Pdf
2
2
  ========
3
3
 
4
+ [![RubyGems][gem_version_badge]][ruby_gems]
5
+
4
6
  Convert things to PDF. This is a quick and dirty collection of scripts I have grown accustomed to coupled with an inherent lazyness to enter tedious commands. Remember: it is dirty by design.
5
7
 
6
8
  Currently supports:
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'any2pdf'
4
- s.version = '1.0.2'
4
+ s.version = '1.1.0'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.summary = "convert anything to pdf (via pandoc and pdfkit)"
7
7
  s.description = s.summary
@@ -9,6 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = "dominik.richter@gmail.com"
10
10
 
11
11
  s.add_dependency "pdfkit"
12
+ s.add_dependency "css_parser"
12
13
  s.add_dependency "trollop"
13
14
  s.add_dependency "nokogiri"
14
15
  s.add_dependency "fileutils"
@@ -4,6 +4,7 @@ require 'pdfkit'
4
4
  require 'trollop'
5
5
  require 'fileutils'
6
6
  require 'nokogiri'
7
+ require 'css_parser'
7
8
  require 'uri'
8
9
  require 'erb'
9
10
  require 'zlog'
@@ -108,7 +109,7 @@ def md2html(file, opts, stylefile)
108
109
  ].join("\n\n")
109
110
 
110
111
  # create the html
111
- `pandoc "#{file}" -o "#{htmlfile_raw}"`
112
+ `pandoc "#{file}" -o "#{htmlfile_raw}" --template #{rel_path}/pandoc-template.html --toc`
112
113
  raw_html = File::read htmlfile_raw
113
114
  FileUtils::rm_rf htmlfile_raw
114
115
 
@@ -125,10 +126,57 @@ def md2html(file, opts, stylefile)
125
126
  htmlfile
126
127
  end
127
128
 
129
+ def get_style_options style_file
130
+ return {} unless File::file? style_file
131
+
132
+ declarations = []
133
+
134
+ css = CssParser::Parser.new
135
+ css.load_file! style_file
136
+ css.each_rule_set do |rule|
137
+ if rule.selectors.include? '#pdf'
138
+ rule.each_declaration do |id, params|
139
+ declarations.push [id, params]
140
+ end
141
+ end
142
+ end
143
+
144
+ # check all rules
145
+ opts = {}
146
+ declarations.each do |field,value|
147
+ if field == 'margin'
148
+ vals = value.split(' ');
149
+ if vals.length == 4
150
+ opts[:margin_top] = vals[0]
151
+ opts[:margin_right] = vals[1]
152
+ opts[:margin_bottom] = vals[2]
153
+ opts[:margin_left] = vals[3]
154
+ end
155
+ if vals.length == 2
156
+ opts[:margin_top] = vals[0]
157
+ opts[:margin_right] = vals[1]
158
+ opts[:margin_bottom] = vals[0]
159
+ opts[:margin_left] = vals[1]
160
+ end
161
+ if vals.length == 1
162
+ opts[:margin_top] = vals[0]
163
+ opts[:margin_right] = vals[0]
164
+ opts[:margin_bottom] = vals[0]
165
+ opts[:margin_left] = vals[0]
166
+ end
167
+ end
168
+
169
+ end
170
+
171
+ opts
172
+ end
173
+
128
174
  def pdfkit_html2pdf( file, outfile, opts )
129
175
  styles = []
130
176
  styles << opts.stylesheet
131
177
 
178
+ style_opts = get_style_options opts.stylesheet
179
+
132
180
  # find css files
133
181
  dom = Nokogiri::parse( File::open(file).read() )
134
182
  dom.css("link").each do |e|
@@ -144,10 +192,10 @@ def pdfkit_html2pdf( file, outfile, opts )
144
192
  footer_right: "[page]/[topage]",
145
193
  orientation: (( opts[:landscape] ) ? "landscape" : "portrait" ),
146
194
  page_size: 'A4',
147
- }
195
+ }.merge(style_opts)
148
196
  html = dom.to_s
149
197
  kit = PDFKit.new(html, options)
150
-
198
+
151
199
  styles.compact.each do |style|
152
200
  kit.stylesheets << style if File::exists?(style)
153
201
  end
@@ -0,0 +1,30 @@
1
+ $for(include-before)$
2
+ $include-before$
3
+ $endfor$
4
+
5
+ $if(title)$
6
+ <header>
7
+ <h1 class="title">$title$</h1>
8
+ $if(subtitle)$
9
+ <h1 class="subtitle">$subtitle$</h1>
10
+ $endif$
11
+ $for(author)$
12
+ <h2 class="author">$author$</h2>
13
+ $endfor$
14
+ $if(date)$
15
+ <h3 class="date">$date$</h3>
16
+ $endif$
17
+ </header>
18
+ $endif$
19
+
20
+ $if(toc)$
21
+ <nav id="$idprefix$TOC">
22
+ $toc$
23
+ </nav>
24
+ $endif$
25
+
26
+ $body$
27
+
28
+ $for(include-after)$
29
+ $include-after$
30
+ $endfor$
@@ -37,4 +37,8 @@ code, pre{
37
37
 
38
38
  table{
39
39
  border-top-color: #e2007f;
40
+ }
41
+
42
+ header {
43
+ page-break-after: always;
40
44
  }
@@ -12,8 +12,10 @@
12
12
  up to level <%= toc_level %>*/
13
13
  for( var lvl=1; lvl <= <%= toc_level %>; lvl++){
14
14
  $("h"+lvl).addClass("in_toc")
15
+ $("header h"+lvl).removeClass("in_toc")
15
16
  <% if number_headings %>
16
17
  $("h"+lvl).addClass("numbering")
18
+ $("header h"+lvl).removeClass("numbering")
17
19
  <% end %>
18
20
  }
19
21
  window.gen_toc();
@@ -22,7 +24,6 @@
22
24
  </script>
23
25
  </head>
24
26
  <body id="home">
25
- <div id="toc" ></div>
26
27
  <%= raw_html %>
27
28
  </body>
28
29
  </html>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: any2pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominik Richter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-15 00:00:00.000000000 Z
11
+ date: 2014-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pdfkit
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: css_parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: trollop
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -99,6 +113,7 @@ files:
99
113
  - data/boilerplate.css
100
114
  - data/default.css
101
115
  - data/jquery-1.9.0.min.js
116
+ - data/pandoc-template.html
102
117
  - data/print.css
103
118
  - data/telekom.css
104
119
  - data/template.html.erb