petit-felix 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 86191af109a9e5c6a646f9a449e00fdf1d24d88238f5b10ecd48ae0de4c8b589
4
+ data.tar.gz: 2719d5612122b24fae5af26bd6367df98a86774d1e4e41dea6abdb3ab642122c
5
+ SHA512:
6
+ metadata.gz: a6b1647e10bf8507b53421b9f0515a87360c73bec8a1f3c67ec94bcbc87af01f9618a69160281b61352a08a31016c60bb41544b826be702576da21059efa9126
7
+ data.tar.gz: 73534102890ef599b1f4baf87b3d0d30f5e7701e12e08ec36cc6c3bb0fc2c5bc7b4ea7455150805e563c6ea8a313d292c63cda6bc35a0c9ea6716921872a351d
@@ -0,0 +1,124 @@
1
+ ## Helper class that handles configs
2
+ require "felix/metadata"
3
+
4
+ module PetitFelix
5
+
6
+ class Config
7
+
8
+ ## Contains all CL arguments already processed
9
+ ## Prevents weird intersection conditions
10
+ @processed_arguments
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
+ }
29
+
30
+ ## Hash for custom command line argument calls
31
+ CL_DATA = {
32
+ "columns" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args).to_i },
33
+ "default_font_size" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args).to_i },
34
+ "header1_size" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args).to_i },
35
+ "header2_size" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args).to_i },
36
+ "header3_size" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args).to_i },
37
+ "header4_size" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args).to_i },
38
+ "header5_size" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args).to_i },
39
+ "header6_size" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args).to_i },
40
+ "margin" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args).to_i },
41
+ "font_normal" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
42
+ "font_italic" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
43
+ "font_bold" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
44
+ "font_bold_italic" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
45
+ "page_layout" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
46
+ "input_files" => -> (command, args, index, cl_args) { cl_add_config(command, args, index, cl_args) },
47
+ }
48
+
49
+ ### Command Line Arguments
50
+
51
+ def self.cl_add_config(command,args, index, cl_args)
52
+ cl_args[command] = args[index + 1]
53
+ end
54
+
55
+ ### Config Loading
56
+
57
+ ## Loads default rendering options and config
58
+ ## Default arguments are loaded first,
59
+ ## then arguments loaded in ./default.cfg,
60
+ ## then command line arguments,
61
+ ## then finally any arguments defined in the metadata.
62
+
63
+ def load_config(args)
64
+ @processed_arguments = []
65
+
66
+ # Felix metadata handler
67
+ metadata = PetitFelix::Metadata.new
68
+
69
+ # load default options
70
+ options = Marshal.load(Marshal.dump(DEFAULT_OPTIONS))
71
+
72
+ # Loads the default configuation in default.cfg
73
+ if File.file?("./default.cfg")
74
+
75
+ config = metadata.get_metadata(File.read("./default.cfg")[0])
76
+ config.keys.each do |key|
77
+ options[key] = config[key]
78
+ end
79
+ end
80
+
81
+ # Loads command line arguments
82
+ cl_args = load_cl_args(args)
83
+
84
+ cl_args.keys.each do |key|
85
+ options[key] = cl_args[key]
86
+ end
87
+
88
+ options
89
+
90
+ end
91
+
92
+ # Loads command line arguments
93
+ def load_cl_args(args)
94
+ cl_args = {}
95
+
96
+ index = 0
97
+
98
+ args.each do |com|
99
+ command = com.downcase
100
+
101
+ if command.start_with?("--")
102
+ command = command[2..]
103
+
104
+ if !@processed_arguments.include?(command)
105
+
106
+ if CL_DATA.key?(command)
107
+
108
+ CL_DATA[command].call(command, args, index, cl_args)
109
+
110
+ end
111
+
112
+ @processed_arguments << command
113
+
114
+ end
115
+ end
116
+
117
+ index+=1
118
+ end
119
+
120
+ cl_args
121
+ end
122
+
123
+ end
124
+ end
@@ -0,0 +1,25 @@
1
+ require "felix/task/basic_pdf_task"
2
+
3
+ module PetitFelix
4
+
5
+ class Generator
6
+
7
+ ## Renders all the files in the given directory.
8
+
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
18
+
19
+ options["output_dir"] = output_path
20
+
21
+ PetitFelix::Task::BasicPDFTask.new.render_files options
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ ## Helper class that handles metadata
2
+
3
+ module PetitFelix
4
+
5
+ class Metadata
6
+
7
+ ## Gets metadata from string into paired hashes
8
+ def get_metadata(input)
9
+ array = input.lines
10
+
11
+ metadata = {}
12
+
13
+ array.each do |set|
14
+ if set.count(":") >= 1
15
+ data = set.split(":")
16
+ index = data[0].strip.downcase
17
+ metadata[index] = data[1].strip
18
+ end
19
+ end
20
+ metadata
21
+ end
22
+
23
+ ## Splits files into array into metadata strings and content
24
+ def split(input)
25
+ input = input.split("---")
26
+ if input.count > 1
27
+ input = input.reject! { |s| s.nil? || s.empty? }
28
+ end
29
+ input
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,171 @@
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
@@ -0,0 +1,42 @@
1
+ module PetitFelix
2
+
3
+ module Task
4
+
5
+ class DefaultTask
6
+
7
+ def render_zine options
8
+ print "render_zine() not implemented\n"
9
+ end
10
+
11
+ def render_files options
12
+ if options.key?("input_files")
13
+ site_list = options["input_files"].split(",")
14
+ end
15
+
16
+ site_list.each do |page|
17
+
18
+ file_list = Dir[page.strip].select { |path| File.file?(path) }
19
+
20
+ if !file_list.empty?
21
+
22
+ file_list.each do |file|
23
+ options["filename"] = file.strip
24
+
25
+ if File.file?(options["filename"])
26
+ render_zine(options)
27
+ end
28
+
29
+ end
30
+
31
+ else
32
+ if File.file?(page)
33
+ render_zine(page)
34
+ end
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ require "felix/generator"
3
+ require "felix/config"
4
+
5
+ module PetitFelix
6
+
7
+ class Output
8
+
9
+ # cl_args - command line arguments passed from CLI
10
+ # options - hash passed by developer containing default rendering options
11
+ def initialize(cl_args: [], options: {})
12
+
13
+ ## Loads options from default values, ./default.cfg
14
+ 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
22
+
23
+ ## Starts producing stuff
24
+ felix_basic = PetitFelix::Generator.new
25
+ felix_basic.render_files loaded_options
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
data/lib/version.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PetitFelix
4
+ module Felix
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,164 @@
1
+ require "prawn"
2
+ require 'fileutils'
3
+ require "prawndown-ext"
4
+ require "felix/metadata"
5
+
6
+ module PetitFelix
7
+ module Worker
8
+
9
+ class BasicPDFWriter < Prawn::Document
10
+
11
+ def page_numbering(page)
12
+
13
+ font_size(12)
14
+
15
+ string = page.title + ": " ' <page>/<total>'
16
+
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
+ end
30
+
31
+ def back_page(pdf, metadata)
32
+
33
+ # draw art
34
+
35
+ box_width = 128
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 = "./assets/images/" + metadata["back_cover_image"]
44
+
45
+ if File.file?(var)
46
+ pdf.image(var,
47
+ width: box_width,
48
+ height: box_width,
49
+ align: :center)
50
+ end
51
+ end
52
+ #transparent(0.5) { pdf.stroke_bounds }
53
+ end
54
+
55
+ # draw name
56
+
57
+ pdf.font_size(20)
58
+ pdf.bounding_box([50, pdf.cursor - 20],
59
+ width: pdf.bounds.width - 100,
60
+ height: 40) do
61
+ if metadata.key?("author_back")
62
+ pdf.text_box(metadata["author_back"],
63
+ align: :center)
64
+ end
65
+ #pdf.transparent(0.5) { pdf.stroke_bounds }
66
+ end
67
+
68
+ end
69
+
70
+ def title_page(pdf, metadata)
71
+ # draw title text
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
85
+
86
+ pdf.font_size(24)
87
+ pdf.bounding_box([50, pdf.cursor],
88
+ width: box_width,
89
+ height: box_width) do
90
+
91
+ if metadata.key?("front_cover_image")
92
+ var = "./assets/images/" + metadata["front_cover_image"]
93
+
94
+ if File.file?(var)
95
+ pdf.image(var,
96
+ width: box_width,
97
+ height: box_width)
98
+ end
99
+ end
100
+ #pdf.transparent(0.5) { pdf.stroke_bounds }
101
+ end
102
+
103
+ # draw name
104
+
105
+ pdf.font_size(24)
106
+ pdf.bounding_box([50, pdf.cursor - 10],
107
+ width: pdf.bounds.width - 100,
108
+ height: 40) do
109
+ if metadata.key?("author")
110
+ pdf.text_box(metadata["author"],
111
+ align: :center)
112
+ end
113
+ #pdf.transparent(0.5) { pdf.stroke_bounds }
114
+ end
115
+
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
+ end
125
+
126
+ def main_block(pdf, options, content)
127
+ margin = 25
128
+ columns = 1
129
+
130
+ if options.key?("margin")
131
+ margin = options["margin"].to_i
132
+ end
133
+
134
+ if options.key?("columns")
135
+ columns = options["columns"].to_i
136
+ end
137
+
138
+ half_margin = (margin * 0.5).floor()
139
+
140
+ # content generation
141
+ pdf.font_size(options["default_font_size"].to_i)
142
+
143
+ if columns == 1
144
+ pdf.bounding_box([half_margin, pdf.cursor - half_margin],
145
+ width: pdf.bounds.width-margin,
146
+ height: pdf.bounds.height - margin) do
147
+
148
+ pdf.markdown(content, options: options)
149
+ end
150
+ else
151
+ pdf.column_box([half_margin, pdf.cursor - half_margin],
152
+ columns: columns,
153
+ width: pdf.bounds.width-margin,
154
+ height: pdf.bounds.height - margin) do
155
+
156
+ pdf.markdown(content, options: options)
157
+ end
158
+ end
159
+ end
160
+
161
+ end
162
+
163
+ end
164
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: petit-felix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - PunishedFelix
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: prawn
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.5'
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 2.5.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '2.5'
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 2.5.0
32
+ - !ruby/object:Gem::Dependency
33
+ name: prawndown-ext
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: 0.1.9
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 0.1.9
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 0.1.9
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 0.1.9
52
+ description: Converts markdown files into PDF documents using options passed in a
53
+ hash.
54
+ email:
55
+ - labadore1844@gmail.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - lib/felix/config.rb
61
+ - lib/felix/generator.rb
62
+ - lib/felix/metadata.rb
63
+ - lib/felix/task/basic_pdf_task.rb
64
+ - lib/felix/task/default_task.rb
65
+ - lib/petit-felix.rb
66
+ - lib/version.rb
67
+ - lib/worker/basic_pdf_writer.rb
68
+ homepage: https://github.com/badgernested/petitfelix
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 3.1.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.6.9
87
+ specification_version: 4
88
+ summary: Ruby gem that creates document files from markdown, such as on Jekyll blogs.
89
+ test_files: []