petit-felix 0.1.4 → 0.1.6

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
  SHA256:
3
- metadata.gz: 97cba85bae835c95a94b3143517e5e7276772f287ad58a41047f5562dd6d746b
4
- data.tar.gz: 69d0d789487e81046db93826ed0481f613b4fa70d3e13fdc6fde5e063422f973
3
+ metadata.gz: 8520f2febc5661d5cc6c5be58d294a6a92e03996be301164da8be5c8a2cbfa32
4
+ data.tar.gz: ee3505a8ed5f1dc356c8b052e95b4eea6332e4e313fe20872cf5a092d35a6211
5
5
  SHA512:
6
- metadata.gz: cbbce50a26a2351b24c17d092995fa9d5c7bfa5dac8afa51b5e93eb88c9e83182d1b5770a1a793c2967d2623f5c93a6abfa2965b710c042a94a6c513ea34653b
7
- data.tar.gz: 289b9746a5c0129fa8b8aab09c38b06fdb05bd9e3e279371edac4f7d9ad927ab34225e8946c165de6b898e52d9024b54e39aaea055dde03b66ee1ff093f79617
6
+ metadata.gz: 403e8b620ea5a35712ccd6ef83cc37e8ceadc05ae1ae1dde46771e016834579f3936f2dd0884716079a251cecda4a8b1e2c394dc3d5951dd339ac78b212da9e4
7
+ data.tar.gz: 8d69e16b180af01aeee3d81b60fbf3343d655b0933c9970cb2574f2a02d622aa3d7d7975267bae1f8e2c384fbad332603f0ed58d27fce7f619dc70d491a6ae06
data/lib/felix/config.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  ## Helper class that handles configs
2
2
  require "felix/metadata"
3
3
 
4
+ ## Processes loaded options. Doesn't actually store them, though.
5
+
4
6
  module PetitFelix
5
7
 
6
8
  class Config
@@ -14,7 +16,7 @@ module PetitFelix
14
16
  "image_dir" => "./assets/images",
15
17
  "input_files" => "./md/*",
16
18
  "output_dir" => "./output",
17
- "worker" => "basic_pdf",
19
+ "task" => "pdf-single",
18
20
  }
19
21
 
20
22
  ## Hash for custom command line argument calls
@@ -35,11 +37,13 @@ module PetitFelix
35
37
  "page_layout" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
36
38
  "input_files" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
37
39
  "image_dir" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
40
+ "task" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
41
+ "template" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
38
42
  }
39
43
 
40
44
  ### Command Line Arguments
41
45
 
42
- def self.cl_add_config(command,args, index, cl_args)
46
+ def self.cl_add_config command,args, index, cl_args
43
47
  cl_args[command] = args[index + 1]
44
48
  end
45
49
 
@@ -51,7 +55,8 @@ module PetitFelix
51
55
  ## then command line arguments,
52
56
  ## then finally any arguments defined in the metadata.
53
57
 
54
- def load_config(wm, passed_args, args)
58
+ def load_config wm, passed_args, args
59
+
55
60
  @processed_arguments = []
56
61
 
57
62
  # Felix metadata handler
@@ -64,34 +69,38 @@ module PetitFelix
64
69
 
65
70
  default_config = {}
66
71
 
67
- if File.file?("./default.cfg")
72
+ if File.file? "./default.cfg"
68
73
 
69
- default_config = metadata.get_metadata(File.read("./default.cfg")[0])
74
+ default_config = metadata.get_metadata(File.read "./default.cfg")
70
75
  end
71
76
 
72
77
  # Loads command line arguments
73
- cl_args = load_cl_args(args)
78
+ cl_args = load_cl_args args
74
79
 
75
80
  # Loads default worker options
76
81
  worker = ""
77
82
  worker_options = nil
78
83
 
79
- if default_options.key?("worker")
80
- worker = default_options["worker"]
84
+ # Gets the task to run.
85
+
86
+ if default_options.key? "task"
87
+ worker = default_options["task"]
81
88
  end
82
89
 
83
- if default_config.key?("worker")
84
- worker = default_config["worker"]
90
+ if default_config.key? "task"
91
+ worker = default_config["task"]
85
92
  end
86
93
 
87
- if cl_args.key?("worker")
88
- worker = cl_args["worker"]
94
+ if cl_args.key? "task"
95
+ worker = cl_args["task"]
89
96
  end
90
97
 
91
- if passed_args.key?("worker")
92
- worker = passed_args["worker"]
98
+ if passed_args.key? "task"
99
+ worker = passed_args["task"]
93
100
  end
94
101
 
102
+ # Loads the worker based on the task.
103
+
95
104
  if worker != ""
96
105
  worker_options = Marshal.load(Marshal.dump(wm.get_task_options worker))
97
106
  end
@@ -100,11 +109,6 @@ module PetitFelix
100
109
  # First, default options
101
110
  options = default_options
102
111
 
103
- # Then loading default config file
104
- default_config.keys.each do |key|
105
- options[key] = default_config[key]
106
- end
107
-
108
112
  # Then loading default worker args
109
113
  if !worker_options.nil?
110
114
  worker_options.keys.each do |key|
@@ -112,6 +116,11 @@ module PetitFelix
112
116
  end
113
117
  end
114
118
 
119
+ # Then loading default config file
120
+ default_config.keys.each do |key|
121
+ options[key] = default_config[key]
122
+ end
123
+
115
124
  # Then loading CLI arguments
116
125
  cl_args.keys.each do |key|
117
126
  options[key] = cl_args[key]
@@ -126,22 +135,23 @@ module PetitFelix
126
135
  end
127
136
 
128
137
  # Loads command line arguments
129
- def load_cl_args(args)
138
+ def load_cl_args args
130
139
  cl_args = {}
131
140
 
132
141
  index = 0
133
142
 
134
143
  args.each do |com|
144
+
135
145
  command = com.downcase
136
146
 
137
- if command.start_with?("--")
147
+ if command.start_with? "--"
138
148
  command = command[2..]
139
149
 
140
- if !@processed_arguments.include?(command)
150
+ if !@processed_arguments.include? command
141
151
 
142
- if CL_DATA.key?(command)
152
+ if CL_DATA.key? command
143
153
 
144
- CL_DATA[command].call(command, args, index, cl_args)
154
+ CL_DATA[command].call command, args, index, cl_args
145
155
 
146
156
  end
147
157
 
data/lib/felix/error.rb CHANGED
@@ -1,8 +1,10 @@
1
+ ## Prints an error message.
2
+
1
3
  module PetitFelix
2
4
  class Error
3
5
 
4
- def print_err(error)
5
- print "\n\nError: " + error + "\n"
6
+ def print_err error
7
+ print "\n\nError: #{error}\n"
6
8
  end
7
9
 
8
10
  end
@@ -1,4 +1,3 @@
1
- require "task/basic_pdf_task"
2
1
  require "felix/task_manager"
3
2
 
4
3
  module PetitFelix
@@ -9,10 +8,14 @@ module PetitFelix
9
8
 
10
9
  def render_files(wm, options)
11
10
 
12
- task = wm.get_task options["worker"]
11
+ task = wm.get_task options["task"]
13
12
 
13
+ default_options = wm.get_task_options options["task"]
14
+
15
+ default_options = default_options.merge(options)
16
+
14
17
  if !task.nil?
15
- task.render_files options
18
+ task.render_files default_options
16
19
  end
17
20
 
18
21
  end
@@ -6,38 +6,67 @@ module PetitFelix
6
6
 
7
7
  ## Gets image from path and location
8
8
  def get_image_location img_dir, filename
9
- file = img_dir + "/" + filename
9
+
10
+ file = "#{img_dir}/#{filename}"
10
11
 
11
12
  if !File.file?(file)
12
- file = "." + img_dir + "/" + filename
13
+
14
+ file = ".#{img_dir}/#{filename}"
15
+
13
16
  end
14
17
 
15
18
  file
19
+
16
20
  end
17
21
 
18
22
  ## Gets metadata from string into paired hashes
19
23
  def get_metadata(input)
24
+
20
25
  array = input.lines
21
26
 
22
27
  metadata = {}
23
-
28
+
24
29
  array.each do |set|
30
+
25
31
  if set.count(":") >= 1
26
- data = set.split(":")
32
+
33
+ data = set.split(":", 2)
27
34
  index = data[0].strip.downcase
28
35
  metadata[index] = data[1].strip.gsub("\\\"", "\"")
36
+
29
37
  end
30
38
  end
39
+
31
40
  metadata
32
41
  end
33
42
 
34
43
  ## Splits files into array into metadata strings and content
35
44
  def split(input)
45
+
36
46
  input = input.split("---")
47
+
37
48
  if input.count > 1
49
+
38
50
  input = input.reject! { |s| s.nil? || s.empty? }
51
+
39
52
  end
53
+
40
54
  input
55
+
56
+ end
57
+
58
+ ## Transforms string into JSON parsed object
59
+ def parse_property string, base="${x}"
60
+
61
+ begin
62
+
63
+ return JSON.parse(base.sub("${x}", string), symbolize_names: true)
64
+
65
+ rescue
66
+
67
+ return parse_property
68
+
69
+ end
41
70
  end
42
71
 
43
72
  end
@@ -4,13 +4,22 @@ require "felix/error"
4
4
  module PetitFelix
5
5
  class TaskManager
6
6
 
7
+ def load_task filename
8
+ load filename
9
+ end
10
+
11
+
7
12
  # Gets the list of tasks loaded
8
13
  def initialize
9
14
  @error_printer = PetitFelix::Error.new
10
15
  @task_list = {}
16
+
17
+ load_task "task/template_pdf_task.rb"
18
+ load_task "task/pdf_single_task.rb"
19
+ load_task "task/basic_pdf_classic_task.rb"
11
20
 
12
21
  task_list = PetitFelix::Task.constants.select {|c| PetitFelix::Task.const_get(c).is_a? Class}
13
-
22
+
14
23
  task_list.delete(:DefaultTask)
15
24
 
16
25
  task_list.each do |task|
@@ -18,9 +27,10 @@ module PetitFelix
18
27
 
19
28
  task_obj = {}
20
29
  task_obj["id"] = task
21
- task_obj["options"] = task_instance::DEFAULT_OPTIONS
22
30
 
23
- name = task_instance::NAME
31
+ task_obj["options"] = task_instance.default_options
32
+
33
+ name = task_instance.name
24
34
 
25
35
  @task_list[name] = task_obj
26
36
  end
@@ -37,7 +47,7 @@ module PetitFelix
37
47
  name = "[UNDEFINED]"
38
48
  end
39
49
 
40
- err_no_task_found name, additional_text: "Unable to find task " + name + ":\n"
50
+ err_no_task_found name, additional_text: "Unable to find task #{name}:\n"
41
51
  end
42
52
 
43
53
  return nil
@@ -53,7 +63,7 @@ module PetitFelix
53
63
  name = "[UNDEFINED]"
54
64
  end
55
65
 
56
- err_no_task_found name, additional_text: "Unable to get options for Worker " + name + ":\n"
66
+ err_no_task_found name, additional_text: "Unable to get options for Task #{name}:\n"
57
67
  end
58
68
 
59
69
  return nil
@@ -61,10 +71,10 @@ module PetitFelix
61
71
 
62
72
  # No task found error
63
73
  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: "
74
+ text = "Task #{task.downcase} not found. Make sure the variable \"task\" is set correctly in your configuration settings. Available Tasks: "
65
75
 
66
76
  @task_list.keys.each do |key|
67
- text += "\n " + key
77
+ text += "\n #{key}"
68
78
  end
69
79
 
70
80
  @error_printer.print_err text
data/lib/petit-felix.rb CHANGED
@@ -3,6 +3,8 @@ require "felix/generator"
3
3
  require "felix/config"
4
4
  require "felix/task_manager"
5
5
 
6
+ # Main entry point of the program for batch outputting files
7
+
6
8
  module PetitFelix
7
9
 
8
10
  class Output
@@ -10,7 +12,7 @@ module PetitFelix
10
12
  # cl_args - command line arguments passed from CLI
11
13
  # options - hash passed by developer containing default rendering options
12
14
  def initialize(cl_args: [], options: {})
13
-
15
+
14
16
  # Creates a new worker manager, which has all the worker stuff
15
17
  wm = PetitFelix::TaskManager.new
16
18
 
@@ -0,0 +1,182 @@
1
+ require "task/default_task"
2
+ require "worker/basic_pdf_writer_classic"
3
+
4
+ module PetitFelix
5
+
6
+ module Task
7
+
8
+ class BasicPDFTask < PetitFelix::Task::DefaultTask
9
+
10
+ def self.name
11
+
12
+ "basic-pdf-classic"
13
+
14
+ end
15
+
16
+ ## Default options of the application
17
+ def self.default_options
18
+
19
+ return {
20
+ "date" => "",
21
+ "columns" => 1,
22
+ "default_font_size" => 12,
23
+ "header1_size" => 32,
24
+ "header2_size" => 28,
25
+ "header3_size" => 20,
26
+ "header4_size" => 18,
27
+ "header5_size" => 16,
28
+ "header6_size" => 14,
29
+ "quote_size" => 14,
30
+ "margin" => 40,
31
+ "font_normal" => "./assets/font/Unageo-Regular.ttf",
32
+ "font_italic"=> "./assets/font/Unageo-Regular-Italic.ttf",
33
+ "font_bold"=> "./assets/font/Unageo-ExtraBold.ttf",
34
+ "font_bold_italic" => "./assets/font/Unageo-ExtraBold-Italic.ttf",
35
+ "paginator_start_count" => 1,
36
+ "paginator_start" => 1,
37
+ "paginator" => true,
38
+ "paginator_alternate" => false,
39
+ "back_text" => "",
40
+ "back_text_margin" => 0,
41
+ "back_text_size" => 14,
42
+ "front_title_size"=> 32,
43
+ "front_author_size" => 18,
44
+ "front_date_size" => 14,
45
+ "back_publisher" => "",
46
+ "back_publisher_size" => 14,
47
+ "front_publisher" => "",
48
+ "front_publisher_size" => 14,
49
+ "paginator_size" => 12,
50
+ "back_author" => "",
51
+ "back_author_size" => 16,
52
+ }
53
+
54
+ end
55
+
56
+ def render_zine options
57
+
58
+ page = File.read options["filename"]
59
+
60
+ # splits the page into parts for metadata and content
61
+
62
+ # Felix metadata handler
63
+ metadata_helper = PetitFelix::Metadata.new
64
+
65
+ page_data = metadata_helper.split page
66
+
67
+ @metadata = metadata_helper.get_metadata page_data[0]
68
+
69
+ @content = page_data[1]
70
+
71
+ prepare_options options
72
+
73
+ # Only continue if metadata has a title
74
+
75
+ if @metadata.key? "title"
76
+
77
+ # Parameters
78
+
79
+ page_layout = :portrait
80
+ print_scaling = :none
81
+
82
+ if @metaoptions.key? "page_layout"
83
+
84
+ page_layout = @metaoptions["page_layout"].to_sym
85
+
86
+ end
87
+
88
+ # Generates PDF
89
+
90
+ pdf = PetitFelix::Worker::BasicPDFWriter.new(
91
+ page_layout: page_layout,
92
+ print_scaling: print_scaling)
93
+
94
+
95
+ pdf.set_options @metaoptions
96
+
97
+ # Adds extra fonts
98
+
99
+ pdf.initialize_font
100
+
101
+ # Title page generation
102
+
103
+ pdf.title_page
104
+
105
+ # Does the main content
106
+ pdf.main_block @content
107
+
108
+ # Does page numbering
109
+ pdf.page_numbering
110
+
111
+ pdf.go_to_page pdf.page_count
112
+
113
+ # Back page generation
114
+ pdf.back_page
115
+
116
+ # Outputs to file
117
+ pdf.output
118
+
119
+ end
120
+ end
121
+
122
+ def prepare_options options
123
+
124
+ # stores options + metadata. metadata overrides options.
125
+ @metaoptions = {}
126
+
127
+ options.keys.each do |key|
128
+
129
+ @metaoptions[key] = options[key]
130
+
131
+ end
132
+
133
+ @metadata.keys.each do |key|
134
+
135
+ @metaoptions[key] = @metadata[key]
136
+
137
+ end
138
+
139
+ # Loads proper values from strings for certain params
140
+ page_layout = :portrait
141
+ print_scaling = :none
142
+
143
+ if @metaoptions.key? "page_layout"
144
+ page_layout = @metaoptions["page_layout"].to_sym
145
+ end
146
+
147
+ end
148
+
149
+ def render_files options
150
+
151
+ if options.key? "input_files"
152
+
153
+ site_list = options["input_files"].split(",")
154
+
155
+ end
156
+
157
+ site_list.each do |page|
158
+
159
+ file_list = Dir[page.strip].select { |path| File.file?(path) }
160
+
161
+ if !file_list.empty?
162
+
163
+ file_list.each do |file|
164
+
165
+ options["filename"] = file.strip
166
+
167
+ if File.file? options["filename"]
168
+ render_zine options
169
+ end
170
+
171
+ end
172
+
173
+ end
174
+ end
175
+
176
+ end
177
+
178
+ end
179
+
180
+ end
181
+
182
+ end
@@ -1,18 +1,63 @@
1
+ require "felix/metadata"
2
+
1
3
  module PetitFelix
2
4
 
3
5
  module Task
4
6
 
5
7
  class DefaultTask
6
8
 
7
- def render_zine options
9
+ ## Name of the task as visible to the Cli and "task" variable
10
+ def self.name
11
+ ""
12
+ end
13
+
14
+ ## Default options of the application
15
+ def self.default_options
16
+
17
+ return {}
18
+
19
+ end
20
+
21
+ # prepares the task's options from metadata
22
+ def prepare_options options
23
+
24
+ # stores options + metadata. metadata overrides options.
25
+ @metaoptions = {}
26
+
27
+ options.keys.each do |key|
28
+
29
+ @metaoptions[key] = options[key]
30
+
31
+ end
32
+
33
+ # Loads proper values from strings for certain params
34
+ page_layout = :portrait
35
+ print_scaling = :none
36
+
37
+ if @metaoptions.key? "page_layout"
38
+
39
+ page_layout = @metaoptions["page_layout"].to_sym
40
+
41
+ end
42
+
43
+ end
44
+
45
+ # renders the task
46
+ def render_zine
47
+
8
48
  print "render_zine() not implemented\n"
49
+
9
50
  end
10
51
 
52
+ # gets the files from input files and renders them.
11
53
  def render_files options
12
- if options.key?("input_files")
54
+
55
+ if options.key? "input_files"
56
+
13
57
  site_list = options["input_files"].split(",")
58
+
14
59
  end
15
-
60
+
16
61
  site_list.each do |page|
17
62
 
18
63
  file_list = Dir[page.strip].select { |path| File.file?(path) }
@@ -20,18 +65,18 @@ module PetitFelix
20
65
  if !file_list.empty?
21
66
 
22
67
  file_list.each do |file|
68
+
23
69
  options["filename"] = file.strip
24
70
 
25
- if File.file?(options["filename"])
26
- render_zine(options)
71
+ if File.file? options["filename"]
72
+
73
+ prepare_options options
74
+ render_zine
75
+
27
76
  end
28
77
 
29
78
  end
30
79
 
31
- else
32
- if File.file?(page)
33
- render_zine(page)
34
- end
35
80
  end
36
81
  end
37
82
 
@@ -0,0 +1,74 @@
1
+ require "task/template_pdf_task"
2
+ require "worker/template_pdf_writer"
3
+
4
+ module PetitFelix
5
+
6
+ module Task
7
+
8
+ class SinglePDFTask < PetitFelix::Task::DefaultTask
9
+
10
+ # Task name
11
+ def self.name
12
+
13
+ "pdf-single"
14
+
15
+ end
16
+
17
+ ## Default options of the application
18
+ def self.default_options
19
+
20
+ return {
21
+ "pdf" => "false"
22
+ }
23
+
24
+ end
25
+
26
+ # Renders zine
27
+ def render_zine
28
+
29
+ # Only continue if metadata has a title
30
+ # Generates PDF
31
+
32
+ @metaoptions["pdf"] = false
33
+
34
+ page = File.read(@metaoptions["filename"])
35
+
36
+ # splits the page into parts for metadata and content
37
+
38
+ # Felix metadata handler
39
+ metadata_helper = PetitFelix::Metadata.new
40
+
41
+ page_data = metadata_helper.split page
42
+
43
+ metadata = @metaoptions.merge(metadata_helper.get_metadata(page_data[0]))
44
+
45
+ # Always forces you to use this template
46
+ @metaoptions["template"] = File.dirname(__FILE__) + "/../../templates/zine-single.json"
47
+
48
+ if metadata["pdf"] == "true"
49
+
50
+ pdf = PetitFelix::Worker::TemplatePDFWriter.new(
51
+ page_layout: @metaoptions["page_layout"],
52
+ print_scaling: @metaoptions["print_scaling"])
53
+
54
+ @metaoptions["output_file"] = File.basename(File.basename(@metaoptions["filename"], ".md"), ".markdown") + ".pdf"
55
+
56
+ pdf.set_options @metaoptions
57
+
58
+ pdf.init_values @metaoptions, pdf
59
+
60
+ pdf.read_template
61
+
62
+ # Adds extra fonts
63
+ #pdf.initialize_font
64
+
65
+ # Outputs to file
66
+ pdf.output
67
+
68
+ end
69
+ end
70
+
71
+
72
+ end
73
+ end
74
+ end