bookify 0.0.1 → 0.0.2

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: aa13a293e82b3202654bbe56325b76825c2541a1
4
- data.tar.gz: fe89d5dda1fd7bcbecab8a2a9874f96baaa66b83
3
+ metadata.gz: 0f4dd5811e1a0f1d4f058f95138a107d69cf3053
4
+ data.tar.gz: 11e37153bbd3ae1cb527105974defdc69178dc44
5
5
  SHA512:
6
- metadata.gz: 870ca47b16755a18020dfc53521e9e3061a25d4ee7621b79d41419d778dc175c0efd818094827798abc2c5aeff69354810d07b39196fed123fdd88cfb9488cde
7
- data.tar.gz: 46668570fe964cdca30e1cb3b0b0365d012db03637a1c8d882e0ea230410206dae39bd047a1de27ac58c59cbacc45e26021a0648a7fb23cd2a67db7a31da3a6c
6
+ metadata.gz: fc5227a7e1f3d1f8756585eaa0e7d7ada00125b5a89fa3d4cd3dadc202763e235f724735270c71fb1c9c06d239f32f3e271547d0fdb42aaf8605719717e707c0
7
+ data.tar.gz: dc60adac9294fb5b9990cab5a795ed49d6dfdb546f7bb9d21f9a399e82a9e399b590caf2721800d9b1df3ef4ff4ae356617b42bb5801d45793978981b8bb7065
Binary file
@@ -8,7 +8,7 @@ require "redcarpet"
8
8
  module Bookify
9
9
  end
10
10
 
11
- Dir["./lib/bookify/node/*.rb"].each { |f| require f }
11
+ Dir["#{File.dirname(__FILE__)}/bookify/node/*.rb"].each { |f| require f }
12
12
 
13
- require "./lib/bookify/node"
14
- require "./lib/bookify/renderer"
13
+ require_relative "bookify/node"
14
+ require_relative "bookify/renderer"
@@ -0,0 +1,24 @@
1
+ module Bookify::Node
2
+ SUBCLASSES = {
3
+ blockquote: Blockquote,
4
+ h1: H1,
5
+ h2: H2,
6
+ h3: H3,
7
+ h4: H4,
8
+ h5: H4,
9
+ h6: H4,
10
+ hr: Hr,
11
+ img: Image,
12
+ ol: List,
13
+ p: Paragraph,
14
+ ul: List
15
+ }
16
+
17
+ def self.render(node, pdf)
18
+ if subclass = SUBCLASSES[node.name.to_sym]
19
+ subclass.render(node, pdf)
20
+ else
21
+ node.children.each { |c| render(c, pdf) }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ module Bookify::Node
2
+ class Base
3
+ attr_accessor :node, :pdf
4
+
5
+ FONTS = {
6
+ h1: ["Book Antiqua", size: 16, style: :bold],
7
+ h2: ["Book Antiqua", size: 12, style: :bold],
8
+ h3: ["Book Antiqua", size: 11, style: :bold],
9
+ h4: ["Book Antiqua", size: 10, style: :bold],
10
+ primary: ["Book Antiqua", size: 10]
11
+ }
12
+
13
+ def self.render(node, pdf)
14
+ new(node, pdf).render
15
+ end
16
+
17
+ def method_missing(method, *args, &block)
18
+ pdf.send(method, *args, &block)
19
+ end
20
+
21
+ def initialize(node, pdf)
22
+ self.node = node
23
+ self.pdf = pdf
24
+ end
25
+
26
+ def html_classes(element = node)
27
+ (element.attr(:class) || "").split(" ")
28
+ end
29
+
30
+ def font(name)
31
+ pdf.font *FONTS[name]
32
+ end
33
+
34
+ def clean_html(html)
35
+ html.gsub(/\n/, "").gsub(/\s+/, " ")
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ module Bookify::Node
2
+ class Blockquote < Base
3
+ def render
4
+ font :primary
5
+
6
+ bounding_box [bounds.left + 10, cursor], width: bounds.width - 10 do
7
+ node.css("p").each do |p|
8
+ text p.inner_html.strip, inline_format: true
9
+ move_down 10
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module Bookify::Node
2
+ class H1 < Base
3
+ def render
4
+ font :h1
5
+ text node.inner_html.strip, align: :center
6
+ move_down 10
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Bookify::Node
2
+ class H2 < Base
3
+ def render
4
+ font :h2
5
+ move_down 5
6
+ text node.inner_html.strip
7
+ move_down 1
8
+ stroke { horizontal_rule }
9
+ move_down 5
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ module Bookify::Node
2
+ class H3 < Base
3
+ def render
4
+ font :h3
5
+ move_down 5
6
+ text node.inner_html.strip
7
+ move_down 5
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Bookify::Node
2
+ class H4 < Base
3
+ def render
4
+ move_down 5
5
+ font :h4
6
+ text node.inner_html.strip
7
+ move_down 5
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module Bookify::Node
2
+ class Hr < Base
3
+ def render
4
+ stroke { horizontal_rule }
5
+ move_down 10
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ module Bookify::Node
2
+ class Image < Base
3
+ def render
4
+ options = {
5
+ position: html_classes.include?("center") ? :center : :left,
6
+ height: node.attr(:height).to_f
7
+ }
8
+
9
+ image path, options
10
+ move_down 15
11
+ end
12
+
13
+ def path
14
+ "src/#{node.attr(:src)}"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ module Bookify::Node
2
+ class List < Base
3
+ def render
4
+ font :primary
5
+
6
+ move_up 5
7
+
8
+ data = node.css("li").each_with_index.map do |li, i|
9
+ [bullet(i), li.inner_html.strip]
10
+ end
11
+
12
+ table(data) do
13
+ cells.inline_format = true
14
+ cells.borders = []
15
+ cells.padding = 5
16
+ end
17
+
18
+ move_down 5
19
+ end
20
+
21
+ def bullet(index)
22
+ if node.name == "ul"
23
+ "•" # is there a better way to render this?
24
+ else
25
+ "#{index + 1}."
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ module Bookify::Node
2
+ class Paragraph < Base
3
+ def render
4
+ html = clean_html(node.inner_html)
5
+ font :primary
6
+ text html, inline_format: true
7
+ move_down 10
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,49 @@
1
+ class Bookify::Renderer
2
+ MARKDOWN_CONVERTER = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
3
+
4
+ attr_accessor :filename
5
+
6
+ def initialize(filename)
7
+ self.filename = filename
8
+ end
9
+
10
+ def render
11
+ Prawn::Document.generate(pdf_path) do |pdf|
12
+ font_path = "#{File.dirname(__FILE__)}/../../fonts"
13
+
14
+ pdf.font_families["Book Antiqua"] = {
15
+ normal: { file: "#{font_path}/BookAntiqua.ttf" },
16
+ bold: { file: "#{font_path}/BookAntiqua-Bold.ttf" },
17
+ italic: { file: "#{font_path}/BookAntiqua-Italic.ttf" },
18
+ bold_italic: { file: "#{font_path}/BookAntiqua-BoldItalic.ttf" }
19
+ }
20
+
21
+ pdf.fill_color "000000"
22
+ pdf.stroke_color "333333"
23
+ pdf.line_width(0.5)
24
+ pdf.default_leading 0.5
25
+
26
+ pdf.column_box [0, pdf.cursor], columns: 2, width: pdf.bounds.width do
27
+ doc.children.each { |c| Bookify::Node.render(c, pdf) }
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def markdown
35
+ @markdown ||= File.read(filename)
36
+ end
37
+
38
+ def doc
39
+ @doc ||= Nokogiri::HTML(html)
40
+ end
41
+
42
+ def html
43
+ @html ||= MARKDOWN_CONVERTER.render(markdown)
44
+ end
45
+
46
+ def pdf_path
47
+ filename.gsub(/\.\w+/, ".pdf")
48
+ end
49
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bookify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joey Schoblaska
@@ -74,7 +74,23 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - bin/bookify
77
+ - fonts/BookAntiqua-Bold.ttf
78
+ - fonts/BookAntiqua-BoldItalic.ttf
79
+ - fonts/BookAntiqua-Italic.ttf
80
+ - fonts/BookAntiqua.ttf
77
81
  - lib/bookify.rb
82
+ - lib/bookify/node.rb
83
+ - lib/bookify/node/base.rb
84
+ - lib/bookify/node/blockquote.rb
85
+ - lib/bookify/node/h1.rb
86
+ - lib/bookify/node/h2.rb
87
+ - lib/bookify/node/h3.rb
88
+ - lib/bookify/node/h4.rb
89
+ - lib/bookify/node/hr.rb
90
+ - lib/bookify/node/image.rb
91
+ - lib/bookify/node/list.rb
92
+ - lib/bookify/node/paragraph.rb
93
+ - lib/bookify/renderer.rb
78
94
  homepage: https://github.com/joeyschoblaska/bookify
79
95
  licenses:
80
96
  - MIT