petit-felix 0.1.1 → 0.1.3
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 +4 -4
- data/lib/felix/config.rb +58 -24
- data/lib/felix/error.rb +9 -0
- data/lib/felix/generator.rb +8 -12
- data/lib/felix/task_manager.rb +74 -0
- data/lib/petit-felix.rb +6 -9
- data/lib/task/basic_pdf_task.rb +133 -0
- data/lib/version.rb +1 -1
- data/lib/worker/basic_pdf_writer.rb +327 -106
- metadata +5 -9
- data/lib/felix/task/basic_pdf_task.rb +0 -171
- /data/lib/{felix/task → task}/default_task.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b82f613c07ac215219d350be885afff66876e67b094d3358d5f2598785b370e9
|
4
|
+
data.tar.gz: 53dc26d5fa3834c38317f6632c16d8c99bae7ab8af77660c77fdb420be625f41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c054afc66ad63700938463e5d081deba54f3a9b064c9fdd82201d3a315b99c338cc70390873b557f780ea20e832c01f46e7d689830e9eb07d0340a64f42c2fb5
|
7
|
+
data.tar.gz: ef7cd3f1724ac05f70fd9d73675b72a2246bfcd2978ea0329ed4c5d73c92bda02d56a4f5dacb5bde2a3804f2001375ca752887dce029edf805521b2528201236
|
data/lib/felix/config.rb
CHANGED
@@ -9,23 +9,12 @@ module PetitFelix
|
|
9
9
|
## Prevents weird intersection conditions
|
10
10
|
@processed_arguments
|
11
11
|
|
12
|
-
|
12
|
+
# Global defaults
|
13
13
|
DEFAULT_OPTIONS = {
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
"header3_size" => 20,
|
19
|
-
"header4_size" => 18,
|
20
|
-
"header5_size" => 16,
|
21
|
-
"header6_size" => 14,
|
22
|
-
"quote_size" => 14,
|
23
|
-
"margin" => 40,
|
24
|
-
"font_normal" => "./assets/font/Unageo-Regular.ttf",
|
25
|
-
"font_italic"=> "./assets/font/Unageo-Regular-Italic.ttf",
|
26
|
-
"font_bold"=> "./assets/font/Unageo-ExtraBold.ttf",
|
27
|
-
"font_bold_italic" => "./assets/font/Unageo-ExtraBold-Italic.ttf",
|
28
|
-
"image_dir" => "./assets/images",
|
14
|
+
"image_dir" => "./assets/images",
|
15
|
+
"input_files" => "./md/*",
|
16
|
+
"output_dir" => "./output",
|
17
|
+
"worker" => "basic_pdf",
|
29
18
|
}
|
30
19
|
|
31
20
|
## Hash for custom command line argument calls
|
@@ -62,33 +51,78 @@ module PetitFelix
|
|
62
51
|
## then command line arguments,
|
63
52
|
## then finally any arguments defined in the metadata.
|
64
53
|
|
65
|
-
def load_config(args)
|
54
|
+
def load_config(wm, passed_args, args)
|
66
55
|
@processed_arguments = []
|
67
56
|
|
68
57
|
# Felix metadata handler
|
69
58
|
metadata = PetitFelix::Metadata.new
|
70
59
|
|
71
|
-
# load default options
|
72
|
-
|
60
|
+
# load global default options
|
61
|
+
default_options = Marshal.load(Marshal.dump(DEFAULT_OPTIONS))
|
73
62
|
|
74
63
|
# Loads the default configuation in default.cfg
|
64
|
+
|
65
|
+
default_config = {}
|
66
|
+
|
75
67
|
if File.file?("./default.cfg")
|
76
68
|
|
77
|
-
|
78
|
-
config.keys.each do |key|
|
79
|
-
options[key] = config[key]
|
80
|
-
end
|
69
|
+
default_config = metadata.get_metadata(File.read("./default.cfg")[0])
|
81
70
|
end
|
82
71
|
|
83
72
|
# Loads command line arguments
|
84
73
|
cl_args = load_cl_args(args)
|
85
74
|
|
75
|
+
# Loads default worker options
|
76
|
+
worker = ""
|
77
|
+
worker_options = nil
|
78
|
+
|
79
|
+
if default_options.key?("worker")
|
80
|
+
worker = default_options["worker"]
|
81
|
+
end
|
82
|
+
|
83
|
+
if default_config.key?("worker")
|
84
|
+
worker = default_config["worker"]
|
85
|
+
end
|
86
|
+
|
87
|
+
if cl_args.key?("worker")
|
88
|
+
worker = cl_args["worker"]
|
89
|
+
end
|
90
|
+
|
91
|
+
if passed_args.key?("worker")
|
92
|
+
worker = passed_args["worker"]
|
93
|
+
end
|
94
|
+
|
95
|
+
if worker != ""
|
96
|
+
worker_options = Marshal.load(Marshal.dump(wm.get_task_options worker))
|
97
|
+
end
|
98
|
+
|
99
|
+
# Now, assemble the options in order
|
100
|
+
# First, default options
|
101
|
+
options = default_options
|
102
|
+
|
103
|
+
# Then loading default config file
|
104
|
+
default_config.keys.each do |key|
|
105
|
+
options[key] = default_config[key]
|
106
|
+
end
|
107
|
+
|
108
|
+
# Then loading default worker args
|
109
|
+
if !worker_options.nil?
|
110
|
+
worker_options.keys.each do |key|
|
111
|
+
options[key] = worker_options[key]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# Then loading CLI arguments
|
86
116
|
cl_args.keys.each do |key|
|
87
117
|
options[key] = cl_args[key]
|
88
118
|
end
|
119
|
+
|
120
|
+
# Then loading passed arguments
|
121
|
+
passed_args.keys.each do |key|
|
122
|
+
options[key] = passed_args[key]
|
123
|
+
end
|
89
124
|
|
90
125
|
options
|
91
|
-
|
92
126
|
end
|
93
127
|
|
94
128
|
# Loads command line arguments
|
data/lib/felix/error.rb
ADDED
data/lib/felix/generator.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require "
|
1
|
+
require "task/basic_pdf_task"
|
2
|
+
require "felix/task_manager"
|
2
3
|
|
3
4
|
module PetitFelix
|
4
5
|
|
@@ -6,19 +7,14 @@ module PetitFelix
|
|
6
7
|
|
7
8
|
## Renders all the files in the given directory.
|
8
9
|
|
9
|
-
def render_files(options)
|
10
|
-
|
11
|
-
## debugging section
|
12
|
-
input_path = "./md/*"
|
13
|
-
output_path ="./output"
|
14
|
-
|
15
|
-
if !options.key?("input_files")
|
16
|
-
options["input_files"] = input_path
|
17
|
-
end
|
10
|
+
def render_files(wm, options)
|
18
11
|
|
19
|
-
options["
|
12
|
+
task = wm.get_task options["worker"]
|
20
13
|
|
21
|
-
|
14
|
+
if !task.nil?
|
15
|
+
task.render_files options
|
16
|
+
end
|
17
|
+
|
22
18
|
end
|
23
19
|
|
24
20
|
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
## This class maintains all of the available tasks in the system.
|
2
|
+
require "felix/error"
|
3
|
+
|
4
|
+
module PetitFelix
|
5
|
+
class TaskManager
|
6
|
+
|
7
|
+
# Gets the list of tasks loaded
|
8
|
+
def initialize
|
9
|
+
@error_printer = PetitFelix::Error.new
|
10
|
+
@task_list = {}
|
11
|
+
|
12
|
+
task_list = PetitFelix::Task.constants.select {|c| PetitFelix::Task.const_get(c).is_a? Class}
|
13
|
+
|
14
|
+
task_list.delete(:DefaultTask)
|
15
|
+
|
16
|
+
task_list.each do |task|
|
17
|
+
task_instance = PetitFelix::Task.const_get(task)
|
18
|
+
|
19
|
+
task_obj = {}
|
20
|
+
task_obj["id"] = task
|
21
|
+
task_obj["options"] = task_instance::DEFAULT_OPTIONS
|
22
|
+
|
23
|
+
name = task_instance::NAME
|
24
|
+
|
25
|
+
@task_list[name] = task_obj
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
# Gets an instance of a task
|
31
|
+
def get_task name
|
32
|
+
|
33
|
+
if @task_list.include?(name)
|
34
|
+
return PetitFelix::Task.const_get(@task_list[name]["id"]).new
|
35
|
+
else
|
36
|
+
if name.nil?
|
37
|
+
name = "[UNDEFINED]"
|
38
|
+
end
|
39
|
+
|
40
|
+
err_no_task_found name, additional_text: "Unable to find task " + name + ":\n"
|
41
|
+
end
|
42
|
+
|
43
|
+
return nil
|
44
|
+
end
|
45
|
+
|
46
|
+
# Gets options of a task
|
47
|
+
def get_task_options name
|
48
|
+
|
49
|
+
if @task_list.include?(name)
|
50
|
+
return @task_list[name]["options"]
|
51
|
+
else
|
52
|
+
if name.nil?
|
53
|
+
name = "[UNDEFINED]"
|
54
|
+
end
|
55
|
+
|
56
|
+
err_no_task_found name, additional_text: "Unable to get options for Worker " + name + ":\n"
|
57
|
+
end
|
58
|
+
|
59
|
+
return nil
|
60
|
+
end
|
61
|
+
|
62
|
+
# No task found error
|
63
|
+
def err_no_task_found task, additional_text: ""
|
64
|
+
text = "Worker " + task.downcase + " not found. Make sure the variable \"worker\" is set correctly in your configuration settings. Available Workers: "
|
65
|
+
|
66
|
+
@task_list.keys.each do |key|
|
67
|
+
text += "\n " + key
|
68
|
+
end
|
69
|
+
|
70
|
+
@error_printer.print_err text
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
data/lib/petit-felix.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require "felix/generator"
|
3
3
|
require "felix/config"
|
4
|
+
require "felix/task_manager"
|
4
5
|
|
5
6
|
module PetitFelix
|
6
7
|
|
@@ -10,19 +11,15 @@ module PetitFelix
|
|
10
11
|
# options - hash passed by developer containing default rendering options
|
11
12
|
def initialize(cl_args: [], options: {})
|
12
13
|
|
14
|
+
# Creates a new worker manager, which has all the worker stuff
|
15
|
+
wm = PetitFelix::TaskManager.new
|
16
|
+
|
13
17
|
## Loads options from default values, ./default.cfg
|
14
18
|
config = PetitFelix::Config.new
|
15
|
-
loaded_options = config.load_config cl_args
|
16
|
-
|
17
|
-
## Makes sure stuff passed by options variable overrides
|
18
|
-
## any previous config loading
|
19
|
-
options.keys.each do |option|
|
20
|
-
loaded_options[option] = options[option]
|
21
|
-
end
|
19
|
+
loaded_options = config.load_config wm, options, cl_args
|
22
20
|
|
23
21
|
## Starts producing stuff
|
24
|
-
|
25
|
-
felix_basic.render_files loaded_options
|
22
|
+
PetitFelix::Generator.new.render_files wm, loaded_options
|
26
23
|
|
27
24
|
end
|
28
25
|
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require "task/default_task"
|
2
|
+
require "worker/basic_pdf_writer"
|
3
|
+
|
4
|
+
module PetitFelix
|
5
|
+
|
6
|
+
module Task
|
7
|
+
|
8
|
+
class BasicPDFTask < PetitFelix::Task::DefaultTask
|
9
|
+
|
10
|
+
NAME = "basic_pdf"
|
11
|
+
|
12
|
+
## Default options of the application
|
13
|
+
DEFAULT_OPTIONS = {
|
14
|
+
"columns" => 1,
|
15
|
+
"default_font_size" => 12,
|
16
|
+
"header1_size" => 32,
|
17
|
+
"header2_size" => 28,
|
18
|
+
"header3_size" => 20,
|
19
|
+
"header4_size" => 18,
|
20
|
+
"header5_size" => 16,
|
21
|
+
"header6_size" => 14,
|
22
|
+
"quote_size" => 14,
|
23
|
+
"margin" => 40,
|
24
|
+
"font_normal" => "./assets/font/Unageo-Regular.ttf",
|
25
|
+
"font_italic"=> "./assets/font/Unageo-Regular-Italic.ttf",
|
26
|
+
"font_bold"=> "./assets/font/Unageo-ExtraBold.ttf",
|
27
|
+
"font_bold_italic" => "./assets/font/Unageo-ExtraBold-Italic.ttf",
|
28
|
+
"paginator_start_count" => 1,
|
29
|
+
"paginator_start" => 1,
|
30
|
+
"paginator" => true,
|
31
|
+
"paginator_alternate" => false,
|
32
|
+
"back_text" => "",
|
33
|
+
"back_text_margin" => 0,
|
34
|
+
"back_text_size" => 14,
|
35
|
+
"front_title_size"=> 32,
|
36
|
+
"front_author_size" => 18,
|
37
|
+
"front_date_size" => 14,
|
38
|
+
"back_publisher" => "",
|
39
|
+
"back_publisher_size" => 14,
|
40
|
+
"front_publisher" => "",
|
41
|
+
"front_publisher_size" => 14,
|
42
|
+
"paginator_size" => 12,
|
43
|
+
"back_author" => "",
|
44
|
+
"back_author_size" => 16,
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
def render_zine options
|
49
|
+
|
50
|
+
page = File.read(options["filename"])
|
51
|
+
|
52
|
+
# splits the page into parts for metadata and content
|
53
|
+
|
54
|
+
# Felix metadata handler
|
55
|
+
metadata_helper = PetitFelix::Metadata.new
|
56
|
+
|
57
|
+
page_data = metadata_helper.split page
|
58
|
+
|
59
|
+
metadata = metadata_helper.get_metadata page_data[0]
|
60
|
+
|
61
|
+
content = page_data[1]
|
62
|
+
|
63
|
+
# stores options + metadata. metadata overrides options.
|
64
|
+
metaoptions = {}
|
65
|
+
|
66
|
+
options.keys.each do |key|
|
67
|
+
metaoptions[key] = options[key]
|
68
|
+
end
|
69
|
+
|
70
|
+
metadata.keys.each do |key|
|
71
|
+
metaoptions[key] = metadata[key]
|
72
|
+
end
|
73
|
+
|
74
|
+
# Only continue if metadata has a title
|
75
|
+
|
76
|
+
if metadata.key?("title")
|
77
|
+
|
78
|
+
# Parameters
|
79
|
+
|
80
|
+
page_layout = :portrait
|
81
|
+
print_scaling = :none
|
82
|
+
|
83
|
+
if metaoptions.key?("page_layout")
|
84
|
+
page_layout = metaoptions["page_layout"]
|
85
|
+
|
86
|
+
if page_layout.is_a? String
|
87
|
+
if page_layout.include?("portrait")
|
88
|
+
page_layout = :portrait
|
89
|
+
else
|
90
|
+
if page_layout.include?("landscape")
|
91
|
+
page_layout = :landscape
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Generates PDF
|
98
|
+
|
99
|
+
pdf = PetitFelix::Worker::BasicPDFWriter.new(
|
100
|
+
page_layout: page_layout,
|
101
|
+
print_scaling: print_scaling)
|
102
|
+
|
103
|
+
pdf.set_options metaoptions
|
104
|
+
|
105
|
+
# Adds extra fonts
|
106
|
+
|
107
|
+
pdf.initialize_font
|
108
|
+
|
109
|
+
# Title page generation
|
110
|
+
|
111
|
+
pdf.title_page
|
112
|
+
|
113
|
+
# Does the main content
|
114
|
+
pdf.main_block content
|
115
|
+
|
116
|
+
# Does page numbering
|
117
|
+
pdf.page_numbering
|
118
|
+
|
119
|
+
# Back page generation
|
120
|
+
pdf.back_page
|
121
|
+
|
122
|
+
# Outputs to file
|
123
|
+
pdf.output
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
data/lib/version.rb
CHANGED
@@ -7,156 +7,377 @@ module PetitFelix
|
|
7
7
|
module Worker
|
8
8
|
|
9
9
|
class BasicPDFWriter < Prawn::Document
|
10
|
-
|
11
|
-
def
|
10
|
+
|
11
|
+
def set_options metaoptions
|
12
|
+
@options = metaoptions
|
13
|
+
end
|
14
|
+
|
15
|
+
# Initializes fonts
|
16
|
+
def initialize_font
|
17
|
+
# Fonts that must always be defined
|
18
|
+
fonts = [
|
19
|
+
"quote",
|
20
|
+
"header1",
|
21
|
+
"header2",
|
22
|
+
"header3",
|
23
|
+
"header4",
|
24
|
+
"header5",
|
25
|
+
"header6",
|
26
|
+
"front_default", # Default title text
|
27
|
+
"back_default", # Default title text
|
28
|
+
"paginator",
|
29
|
+
]
|
30
|
+
|
31
|
+
fonts_special_default = {
|
32
|
+
"front_title" => "front_default", # Title Text
|
33
|
+
"front_author" => "front_default", # Author Text
|
34
|
+
"front_date" => "front_default", # Date Text
|
35
|
+
"front_publish" => "front_default", # Publication Text
|
36
|
+
"back_author" => "back_default", # Publication Text
|
37
|
+
"back_publish" => "back_default", # Publication Text
|
38
|
+
}
|
12
39
|
|
13
|
-
|
40
|
+
add_font "default"
|
14
41
|
|
15
|
-
|
42
|
+
# Adds the fonts in the options
|
43
|
+
fonts.each do |key|
|
44
|
+
add_font key
|
45
|
+
@options[key +"_font"] = key
|
46
|
+
end
|
47
|
+
|
48
|
+
# Adds special fonts with custom default fonts
|
49
|
+
fonts_special_default.keys.each do |key|
|
50
|
+
add_font key, default_font: fonts_special_default[key]
|
51
|
+
@options[key +"_font"] = key
|
52
|
+
end
|
16
53
|
|
17
|
-
# Green page numbers 1 to 7
|
18
|
-
options = {
|
19
|
-
at: [0, 0],
|
20
|
-
width: bounds.width,
|
21
|
-
align: :right,
|
22
|
-
start_count_at: 1,
|
23
|
-
color: '000000'
|
24
|
-
}
|
25
|
-
options[:page_filter] = ->(pg) { pg > 1 }
|
26
|
-
|
27
|
-
number_pages string, options
|
28
|
-
|
29
54
|
end
|
30
55
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
pdf.font_size(24)
|
38
|
-
pdf.bounding_box([pdf.bounds.width * 0.5 - 64, pdf.cursor - 500],
|
39
|
-
width: pdf.bounds.width - 100 - box_width,
|
40
|
-
height: box_width) do
|
41
|
-
|
42
|
-
if metadata.key?("back_cover_image")
|
43
|
-
var = PetitFelix::Metadata.new.get_image_location(metadata["image_dir"], metadata["back_cover_image"])
|
56
|
+
# Adds a font to the pdf document
|
57
|
+
def add_font (type,
|
58
|
+
default_font: "default")
|
59
|
+
|
60
|
+
if @options.key?(type + "_font_normal")
|
44
61
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
62
|
+
font_normal = type + "_font_normal"
|
63
|
+
font_italic = type + "_font_normal"
|
64
|
+
font_bold = type + "_font_normal"
|
65
|
+
font_bold_italic = type + "_font_normal"
|
66
|
+
|
67
|
+
if @options.key?(type + "_font_italic")
|
68
|
+
font_italic = type + "_font_italic"
|
69
|
+
end
|
70
|
+
if @options.key?(type + "_font_bold")
|
71
|
+
font_bold = type + "_font_bold"
|
72
|
+
end
|
73
|
+
if @options.key?(type + "_font_bold_italic")
|
74
|
+
font_bold_italic = type + "_font_bold_italic"
|
75
|
+
end
|
76
|
+
|
77
|
+
font_families.update(type => {
|
78
|
+
:normal => @options[font_normal],
|
79
|
+
:italic => @options[font_italic],
|
80
|
+
:bold => @options[font_bold],
|
81
|
+
:bold_italic => @options[font_bold_italic]
|
82
|
+
})
|
83
|
+
|
84
|
+
else
|
85
|
+
if font_families.key?(default_font)
|
86
|
+
font_families.update(type => {
|
87
|
+
:normal => font_families[default_font][:normal],
|
88
|
+
:italic => font_families[default_font][:italic],
|
89
|
+
:bold => font_families[default_font][:bold],
|
90
|
+
:bold_italic => font_families[default_font][:bold_italic]
|
91
|
+
})
|
92
|
+
|
93
|
+
else
|
94
|
+
font_families.update(type => {
|
95
|
+
:normal => @options["font_normal"],
|
96
|
+
:italic => @options["font_italic"],
|
97
|
+
:bold => @options["font_bold"],
|
98
|
+
:bold_italic => @options["font_bold_italic"]
|
99
|
+
})
|
100
|
+
end
|
101
|
+
|
50
102
|
end
|
51
|
-
end
|
52
|
-
#transparent(0.5) { pdf.stroke_bounds }
|
53
103
|
end
|
104
|
+
|
105
|
+
# Draws page numbering
|
54
106
|
|
55
|
-
|
107
|
+
def page_numbering
|
56
108
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
109
|
+
if @options.key?("paginator")
|
110
|
+
if @options["paginator"]
|
111
|
+
|
112
|
+
font "paginator"
|
113
|
+
|
114
|
+
font_size(@options["paginator_size"].to_i)
|
115
|
+
|
116
|
+
string = @options["title"] + ": " ' <page>'
|
117
|
+
|
118
|
+
page_start_count = @options["paginator_start_count"].to_i
|
119
|
+
page_start = @options["paginator_start"].to_i
|
120
|
+
|
121
|
+
align_odd = :left
|
122
|
+
align_even = :right
|
123
|
+
|
124
|
+
if @options["front_extra_page"]
|
125
|
+
page_start += 1
|
126
|
+
align_odd = :right
|
127
|
+
align_even = :left
|
128
|
+
end
|
129
|
+
|
130
|
+
if @options["paginator_alternate"]
|
131
|
+
|
132
|
+
odd_options = {
|
133
|
+
at: [0, 0],
|
134
|
+
width: bounds.width,
|
135
|
+
align: align_odd,
|
136
|
+
start_count_at: page_start_count,
|
137
|
+
}
|
138
|
+
|
139
|
+
odd_options[:page_filter] = ->(pg) { pg > page_start && pg % 2 == 1 }
|
140
|
+
|
141
|
+
even_options = {
|
142
|
+
at: [0, bounds.left],
|
143
|
+
width: bounds.width,
|
144
|
+
align: align_even,
|
145
|
+
start_count_at: page_start_count + 1,
|
146
|
+
}
|
147
|
+
|
148
|
+
even_options[:page_filter] = ->(pg) { pg > page_start && pg % 2 == 0 }
|
149
|
+
|
150
|
+
number_pages string, odd_options
|
151
|
+
number_pages string, even_options
|
152
|
+
|
153
|
+
else
|
154
|
+
|
155
|
+
options = {
|
156
|
+
at: [0, 0],
|
157
|
+
width: bounds.width,
|
158
|
+
align: :right,
|
159
|
+
start_count_at: page_start_count,
|
160
|
+
color: '000000'
|
161
|
+
}
|
162
|
+
options[:page_filter] = ->(pg) { pg > page_start }
|
163
|
+
|
164
|
+
number_pages string, options
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
64
169
|
end
|
65
|
-
#pdf.transparent(0.5) { pdf.stroke_bounds }
|
66
|
-
end
|
67
|
-
|
68
170
|
end
|
69
171
|
|
70
|
-
|
71
|
-
|
72
|
-
pdf.font_size(36)
|
73
|
-
pdf.bounding_box([25, pdf.cursor - 20],
|
74
|
-
width: pdf.bounds.width - 50,
|
75
|
-
height: 150) do
|
76
|
-
pdf.text_box(metadata["title"],
|
77
|
-
align: :center,
|
78
|
-
style: :bold)
|
79
|
-
#pdf.transparent(0.5) { pdf.stroke_bounds }
|
80
|
-
end
|
81
|
-
|
82
|
-
# draw art
|
83
|
-
|
84
|
-
box_width = pdf.bounds.width - 100
|
172
|
+
# Draws back page
|
173
|
+
def back_page
|
85
174
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
175
|
+
if @options.key?("back_cover")
|
176
|
+
if @options["back_cover"]
|
177
|
+
if @options.key?("back_extra_page") && @options["back_extra_page"]
|
178
|
+
start_new_page()
|
179
|
+
end
|
180
|
+
|
181
|
+
start_new_page()
|
90
182
|
|
91
|
-
|
92
|
-
|
183
|
+
font "back_default"
|
184
|
+
|
185
|
+
# draw end text, can be markdown
|
186
|
+
font_size(@options["back_text_size"].to_i)
|
187
|
+
|
188
|
+
margin = @options["back_text_margin"].to_i
|
189
|
+
|
190
|
+
half_margin = (margin * 0.5).floor
|
191
|
+
|
192
|
+
bounding_box([half_margin, cursor - 20 - half_margin],
|
193
|
+
width: bounds.width - margin,
|
194
|
+
height: 300 - margin) do
|
195
|
+
markdown(@options["back_text"], options: @options)
|
196
|
+
#transparent(0.5) { stroke_bounds }
|
197
|
+
end
|
93
198
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
199
|
+
# draw art
|
200
|
+
|
201
|
+
box_width = 128
|
202
|
+
|
203
|
+
bounding_box([bounds.width * 0.5 - 64, cursor - 180],
|
204
|
+
width: bounds.width - 100 - box_width,
|
205
|
+
height: box_width) do
|
206
|
+
|
207
|
+
if @options.key?("back_cover_image")
|
208
|
+
var = PetitFelix::Metadata.new.get_image_location(@options["image_dir"], @options["back_cover_image"])
|
209
|
+
|
210
|
+
if File.file?(var)
|
211
|
+
image(var,
|
212
|
+
width: box_width,
|
213
|
+
height: box_width,
|
214
|
+
align: :center)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
#transparent(0.5) { stroke_bounds }
|
218
|
+
end
|
219
|
+
|
220
|
+
# draw name
|
221
|
+
|
222
|
+
font "back_author"
|
223
|
+
|
224
|
+
font_size(@options["back_author_size"].to_i)
|
225
|
+
bounding_box([50, cursor - 20],
|
226
|
+
width: bounds.width - 100,
|
227
|
+
height: 40) do
|
228
|
+
if @options.key?("back_author")
|
229
|
+
text_box(@options["back_author"],
|
230
|
+
align: :center)
|
231
|
+
end
|
232
|
+
#transparent(0.5) { stroke_bounds }
|
233
|
+
end
|
234
|
+
|
235
|
+
font "back_publish"
|
236
|
+
|
237
|
+
font_size(@options["back_publisher_size"].to_i)
|
238
|
+
bounding_box([25, cursor - 60],
|
239
|
+
width: bounds.width - 50,
|
240
|
+
height: 32) do
|
241
|
+
|
242
|
+
text_box(@options["back_publisher"],
|
243
|
+
align: :center)
|
244
|
+
#transparent(0.5) { stroke_bounds }
|
245
|
+
end
|
246
|
+
|
98
247
|
end
|
99
248
|
end
|
100
|
-
#pdf.transparent(0.5) { pdf.stroke_bounds }
|
101
249
|
end
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
250
|
+
|
251
|
+
|
252
|
+
# Draws title page
|
253
|
+
def title_page
|
254
|
+
|
255
|
+
if @options.key?("front_cover")
|
256
|
+
if @options["front_cover"]
|
257
|
+
|
258
|
+
font "front_title"
|
259
|
+
|
260
|
+
# draw title text
|
261
|
+
font_size(@options["front_title_size"].to_i)
|
262
|
+
bounding_box([25, cursor ],
|
263
|
+
width: bounds.width - 50,
|
264
|
+
height: 250) do
|
265
|
+
text_box(@options["title"],
|
266
|
+
align: :center,
|
267
|
+
style: :bold)
|
268
|
+
#transparent(0.5) { stroke_bounds }
|
269
|
+
end
|
270
|
+
|
271
|
+
# draw art
|
272
|
+
|
273
|
+
box_width = bounds.width - 150
|
274
|
+
|
275
|
+
bounding_box([50, cursor + 10],
|
276
|
+
width: box_width,
|
277
|
+
height: box_width) do
|
278
|
+
|
279
|
+
if @options.key?("front_cover_image")
|
280
|
+
var = PetitFelix::Metadata.new.get_image_location(@options["image_dir"], @options["front_cover_image"])
|
281
|
+
|
282
|
+
if File.file?(var)
|
283
|
+
image(var,
|
284
|
+
width: box_width,
|
285
|
+
height: box_width)
|
286
|
+
end
|
287
|
+
end
|
288
|
+
#transparent(0.5) { stroke_bounds }
|
289
|
+
end
|
290
|
+
|
291
|
+
font "front_author"
|
292
|
+
|
293
|
+
# draw author
|
294
|
+
|
295
|
+
font_size(@options["front_author_size"].to_i)
|
296
|
+
bounding_box([50, cursor - 10],
|
297
|
+
width: bounds.width - 100,
|
298
|
+
height: 30) do
|
299
|
+
if @options.key?("author")
|
300
|
+
text_box(@options["author"],
|
301
|
+
align: :center)
|
302
|
+
end
|
303
|
+
#transparent(0.5) { stroke_bounds }
|
304
|
+
end
|
305
|
+
|
306
|
+
font "front_date"
|
307
|
+
|
308
|
+
font_size(@options["front_date_size"].to_i)
|
309
|
+
bounding_box([50, cursor],
|
310
|
+
width: bounds.width - 100,
|
311
|
+
height: 30) do
|
312
|
+
text_box("Original Publication: " + @options["date"],#.strftime('%e %B, %Y'),
|
313
|
+
align: :center)
|
314
|
+
#transparent(0.5) { stroke_bounds }
|
315
|
+
end
|
316
|
+
|
317
|
+
font "front_publish"
|
318
|
+
|
319
|
+
font_size(@options["front_publisher_size"].to_i)
|
320
|
+
bounding_box([25, cursor - 20],
|
321
|
+
width: bounds.width - 50,
|
322
|
+
height: 32) do
|
323
|
+
|
324
|
+
text_box(@options["front_publisher"],
|
325
|
+
align: :center)
|
326
|
+
#transparent(0.5) { stroke_bounds }
|
327
|
+
end
|
328
|
+
|
329
|
+
start_new_page()
|
330
|
+
if @options.key?("front_extra_page") && @options["front_extra_page"]
|
331
|
+
start_new_page()
|
332
|
+
end
|
333
|
+
|
112
334
|
end
|
113
|
-
#pdf.transparent(0.5) { pdf.stroke_bounds }
|
114
335
|
end
|
115
336
|
|
116
|
-
pdf.font_size(16)
|
117
|
-
pdf.bounding_box([50, pdf.cursor],
|
118
|
-
width: pdf.bounds.width - 100,
|
119
|
-
height: 80) do
|
120
|
-
pdf.text_box("Original Publication: " + metadata["date"],#.strftime('%e %B, %Y'),
|
121
|
-
align: :center)
|
122
|
-
#pdf.transparent(0.5) { pdf.stroke_bounds }
|
123
|
-
end
|
124
337
|
end
|
125
338
|
|
126
|
-
|
339
|
+
# Draws the main block of content
|
340
|
+
def main_block(content)
|
341
|
+
font "default"
|
342
|
+
|
127
343
|
margin = 25
|
128
344
|
columns = 1
|
129
345
|
|
130
|
-
if options.key?("margin")
|
131
|
-
margin = options["margin"].to_i
|
346
|
+
if @options.key?("margin")
|
347
|
+
margin = @options["margin"].to_i
|
132
348
|
end
|
133
349
|
|
134
|
-
if options.key?("columns")
|
135
|
-
columns = options["columns"].to_i
|
350
|
+
if @options.key?("columns")
|
351
|
+
columns = @options["columns"].to_i
|
136
352
|
end
|
137
353
|
|
138
354
|
half_margin = (margin * 0.5).floor()
|
139
355
|
|
140
356
|
# content generation
|
141
|
-
|
357
|
+
font_size(@options["default_font_size"].to_i)
|
142
358
|
|
143
359
|
if columns == 1
|
144
|
-
|
145
|
-
width:
|
146
|
-
height:
|
360
|
+
bounding_box([half_margin, cursor - half_margin],
|
361
|
+
width: bounds.width-margin,
|
362
|
+
height: [bounds.height - 20 - margin, bounds.height - margin].min) do
|
147
363
|
|
148
|
-
|
364
|
+
markdown(content, options: @options)
|
149
365
|
end
|
150
366
|
else
|
151
|
-
|
367
|
+
column_box([half_margin, cursor - half_margin],
|
152
368
|
columns: columns,
|
153
|
-
width:
|
154
|
-
height:
|
369
|
+
width: bounds.width-margin,
|
370
|
+
height: [bounds.height - 20 - margin, bounds.height - margin].min) do
|
155
371
|
|
156
|
-
|
372
|
+
markdown(content, options: @options)
|
157
373
|
end
|
158
374
|
end
|
159
375
|
end
|
376
|
+
|
377
|
+
def output
|
378
|
+
FileUtils.mkdir_p @options["output_dir"]
|
379
|
+
render_file(@options["output_dir"] + "/" + @options["title"].gsub(/[^\w\s]/, '').tr(" ", "_") + '.pdf')
|
380
|
+
end
|
160
381
|
|
161
382
|
end
|
162
383
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: petit-felix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- PunishedFelix
|
@@ -36,9 +36,6 @@ dependencies:
|
|
36
36
|
- - "~>"
|
37
37
|
- !ruby/object:Gem::Version
|
38
38
|
version: 0.1.10
|
39
|
-
- - ">="
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
version: 0.1.9
|
42
39
|
type: :runtime
|
43
40
|
prerelease: false
|
44
41
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -46,9 +43,6 @@ dependencies:
|
|
46
43
|
- - "~>"
|
47
44
|
- !ruby/object:Gem::Version
|
48
45
|
version: 0.1.10
|
49
|
-
- - ">="
|
50
|
-
- !ruby/object:Gem::Version
|
51
|
-
version: 0.1.9
|
52
46
|
description: Converts markdown files into PDF documents using options passed in a
|
53
47
|
hash.
|
54
48
|
email:
|
@@ -58,11 +52,13 @@ extensions: []
|
|
58
52
|
extra_rdoc_files: []
|
59
53
|
files:
|
60
54
|
- lib/felix/config.rb
|
55
|
+
- lib/felix/error.rb
|
61
56
|
- lib/felix/generator.rb
|
62
57
|
- lib/felix/metadata.rb
|
63
|
-
- lib/felix/
|
64
|
-
- lib/felix/task/default_task.rb
|
58
|
+
- lib/felix/task_manager.rb
|
65
59
|
- lib/petit-felix.rb
|
60
|
+
- lib/task/basic_pdf_task.rb
|
61
|
+
- lib/task/default_task.rb
|
66
62
|
- lib/version.rb
|
67
63
|
- lib/worker/basic_pdf_writer.rb
|
68
64
|
homepage: https://github.com/badgernested/petitfelix
|
@@ -1,171 +0,0 @@
|
|
1
|
-
require "felix/task/default_task"
|
2
|
-
require "worker/basic_pdf_writer"
|
3
|
-
|
4
|
-
module PetitFelix
|
5
|
-
|
6
|
-
module Task
|
7
|
-
|
8
|
-
class BasicPDFTask < PetitFelix::Task::DefaultTask
|
9
|
-
|
10
|
-
# Adds a font to the pdf document
|
11
|
-
def add_font pdf, metaoptions, type
|
12
|
-
|
13
|
-
if metaoptions.key?(type + "_font_normal")
|
14
|
-
font_normal = type + "_font_normal"
|
15
|
-
font_italic = type + "_font_normal"
|
16
|
-
font_bold = type + "_font_normal"
|
17
|
-
font_bold_italic = type + "_font_normal"
|
18
|
-
|
19
|
-
if metaoptions.key?(type + "_font_italic")
|
20
|
-
font_italic = type + "_font_italic"
|
21
|
-
end
|
22
|
-
if metaoptions.key?(type + "_font_bold")
|
23
|
-
font_bold = type + "_font_bold"
|
24
|
-
end
|
25
|
-
if metaoptions.key?(type + "_font_bold_italic")
|
26
|
-
font_bold_italic = type + "_font_bold_italic"
|
27
|
-
end
|
28
|
-
|
29
|
-
pdf.font_families.update(type => {
|
30
|
-
:normal => metaoptions[font_normal],
|
31
|
-
:italic => metaoptions[font_italic],
|
32
|
-
:bold => metaoptions[font_bold],
|
33
|
-
:bold_italic => metaoptions[font_bold_italic]
|
34
|
-
})
|
35
|
-
|
36
|
-
else
|
37
|
-
|
38
|
-
pdf.font_families.update(type => {
|
39
|
-
:normal => metaoptions["font_normal"],
|
40
|
-
:italic => metaoptions["font_italic"],
|
41
|
-
:bold => metaoptions["font_bold"],
|
42
|
-
:bold_italic => metaoptions["font_bold_italic"]
|
43
|
-
})
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def render_zine options
|
49
|
-
|
50
|
-
# Options that are always added for this task
|
51
|
-
options["quote_font"] = "quote"
|
52
|
-
options["header1_font"] = "header1"
|
53
|
-
options["header2_font"] = "header2"
|
54
|
-
options["header3_font"] = "header3"
|
55
|
-
options["header4_font"] = "header4"
|
56
|
-
options["header5_font"] = "header5"
|
57
|
-
options["header6_font"] = "header6"
|
58
|
-
|
59
|
-
dir = options["output_dir"]
|
60
|
-
|
61
|
-
|
62
|
-
page = File.read(options["filename"])
|
63
|
-
|
64
|
-
# splits the page into parts for metadata and content
|
65
|
-
|
66
|
-
# Felix metadata handler
|
67
|
-
metadata_helper = PetitFelix::Metadata.new
|
68
|
-
|
69
|
-
page_data = metadata_helper.split page
|
70
|
-
|
71
|
-
metadata = metadata_helper.get_metadata page_data[0]
|
72
|
-
|
73
|
-
content = page_data[1]
|
74
|
-
|
75
|
-
# stores options + metadata. metadata overrides options.
|
76
|
-
metaoptions = {}
|
77
|
-
|
78
|
-
options.keys.each do |key|
|
79
|
-
metaoptions[key] = options[key]
|
80
|
-
end
|
81
|
-
|
82
|
-
metadata.keys.each do |key|
|
83
|
-
metaoptions[key] = metadata[key]
|
84
|
-
end
|
85
|
-
|
86
|
-
# Only continue if metadata has a title
|
87
|
-
|
88
|
-
if metadata.key?("title")
|
89
|
-
|
90
|
-
# Parameters
|
91
|
-
|
92
|
-
page_layout = :portrait
|
93
|
-
print_scaling = :none
|
94
|
-
|
95
|
-
if metaoptions.key?("page_layout")
|
96
|
-
page_layout = metaoptions["page_layout"]
|
97
|
-
|
98
|
-
if page_layout.is_a? String
|
99
|
-
if page_layout.include?("portrait")
|
100
|
-
page_layout = :portrait
|
101
|
-
else
|
102
|
-
if page_layout.include?("landscape")
|
103
|
-
page_layout = :landscape
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
# Generates PDF
|
110
|
-
|
111
|
-
pdf = PetitFelix::Worker::BasicPDFWriter.new(
|
112
|
-
page_layout: page_layout,
|
113
|
-
print_scaling: print_scaling)
|
114
|
-
|
115
|
-
pdf.font_families.update("OpenSans" => {
|
116
|
-
:normal => metaoptions["font_normal"],
|
117
|
-
:italic => metaoptions["font_italic"],
|
118
|
-
:bold => metaoptions["font_bold"],
|
119
|
-
:bold_italic => metaoptions["font_bold_italic"]
|
120
|
-
})
|
121
|
-
|
122
|
-
add_font pdf, metaoptions, "quote"
|
123
|
-
|
124
|
-
add_font pdf, metaoptions, "header1"
|
125
|
-
add_font pdf, metaoptions, "header2"
|
126
|
-
add_font pdf, metaoptions, "header3"
|
127
|
-
add_font pdf, metaoptions, "header4"
|
128
|
-
add_font pdf, metaoptions, "header5"
|
129
|
-
add_font pdf, metaoptions, "header6"
|
130
|
-
|
131
|
-
# End quote font
|
132
|
-
|
133
|
-
pdf.font "OpenSans"
|
134
|
-
|
135
|
-
# If the title is generated
|
136
|
-
if metaoptions.key?("front_cover")
|
137
|
-
if metaoptions["front_cover"]
|
138
|
-
pdf.title_page(pdf, metaoptions)
|
139
|
-
pdf.start_new_page()
|
140
|
-
if metaoptions.key?("front_extra_page") && metaoptions["front_extra_page"]
|
141
|
-
pdf.start_new_page()
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
# Does the main content
|
147
|
-
pdf.main_block(pdf, metaoptions, content)
|
148
|
-
|
149
|
-
# If the back is generated
|
150
|
-
if metaoptions.key?("back_cover")
|
151
|
-
if metaoptions["back_cover"]
|
152
|
-
if metaoptions.key?("back_extra_page") && metaoptions["back_extra_page"]
|
153
|
-
pdf.start_new_page()
|
154
|
-
end
|
155
|
-
|
156
|
-
pdf.start_new_page()
|
157
|
-
pdf.back_page(pdf, metaoptions)
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
|
162
|
-
FileUtils.mkdir_p dir
|
163
|
-
pdf.render_file(dir + "/" + metadata["title"].gsub(/[^\w\s]/, '').tr(" ", "_") + '.pdf')
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
end
|
168
|
-
|
169
|
-
end
|
170
|
-
|
171
|
-
end
|
File without changes
|