Class: PetitFelix::Worker::TemplatePDFWriter
- Inherits:
-
DefaultPDFWriter
- Object
- Prawn::Document
- DefaultPDFWriter
- PetitFelix::Worker::TemplatePDFWriter
- Defined in:
- lib/worker/templater/font.rb,
lib/worker/templater/error.rb,
lib/worker/templater/methods.rb,
lib/worker/template_pdf_writer.rb,
lib/worker/templater/validation.rb
Constant Summary collapse
- ERROR_CODES =
error count: 16
[ "OK", "Malformed command list.", "No command defined. Use \"com\" or \"command\" to define commands.", "Command \"{{arg}}\" not found.", "Stack overflow.", "Template ID \"{{arg}}\" not found.", "Template method \"{{arg}}\" not found.", "\"{{arg}}\" argument not defined.", "Expression \"{{arg}}\" does not result in boolean result.", "Expression \"{{arg}}\" cannot be evaluated.", "File \"{{arg}}\" not found.", "Not a valid position.", "\"at\" (Array) argument not defined.", "\"{{arg}}\" is not an array.", "Image \"{{arg}}\" not found.", "Template file \"{{arg}}\" not found.", "\"template\" option not defined. No template file can be loaded." ]
- COMMAND =
List of executable commands
{ ## Comment this one out when releasing #:test => -> (obj, args) { obj.com_test args, obj }, :print => -> (obj, args) { obj.com_print args, obj }, :call => -> (obj, args) { obj.com_call args, obj }, :if => -> (obj, args) { obj.com_if args, obj }, :elif => -> (obj, args) { obj.com_elif args, obj }, :each => -> (obj, args) { obj.com_each args, obj }, :times => -> (obj, args) { obj.com_times args, obj }, :set => -> (obj, args) { obj.com_set args, obj }, # Loads Markdown file data into @variables :read_markdown => -> (obj, args) { obj.com_read_markdown args, obj }, :clear_markdown => -> (obj, args) { obj.com_clear_markdown args, obj }, # PDF functions :text => -> (obj, args) { obj.com_text args, obj }, :markdown => -> (obj, args) { obj.com_markdown args, obj }, :text_box => -> (obj, args) { obj.com_text_box args, obj }, :move_down => -> (obj, args) { obj.com_move_down args, obj }, :move_to => -> (obj, args) { obj.com_move_to args, obj }, :move_cursor_to => -> (obj, args) { obj.com_move_cursor_to args, obj }, :copy_page => -> (obj, args) { obj.com_copy_page args, obj }, :paste_page => -> (obj, args) { obj.com_paste_page args, obj }, :delete_page => -> (obj, args) { obj.com_delete_page args, obj }, :start_new_page => -> (obj, args) { obj.com_start_new_page args, obj }, :draw_text => -> (obj, args) { obj.com_draw_text args, obj }, :font => -> (obj, args) { obj.com_font args, obj }, :font_size => -> (obj, args) { obj.com_font_size args, obj }, :go_to_page => -> (obj, args) { obj.com_go_to_page args, obj }, :horizontal_rule => -> (obj, args) { obj.com_horizontal_rule args, obj }, :image => -> (obj, args) { obj.com_image args, obj }, :move_up => -> (obj, args) { obj.com_move_up args, obj }, :number_pages => -> (obj, args) { obj.com_number_pages args, obj }, :bounding_box => -> (obj, args) { obj.com_bounding_box args, obj }, :column_box => -> (obj, args) { obj.com_column_box args, obj }, :indent => -> (obj, args) { obj.com_indent args, obj }, :pad => -> (obj, args) { obj.com_pad args, obj }, :pad_bottom => -> (obj, args) { obj.com_pad_bottom args, obj }, :pad_top => -> (obj, args) { obj.com_pad_top args, obj }, :rotate => -> (obj, args) { obj.com_rotate args, obj }, :scale => -> (obj, args) { obj.com_scale args, obj }, :span => -> (obj, args) { obj.com_span args, obj }, :translate => -> (obj, args) { obj.com_translate args, obj }, :transparent => -> (obj, args) { obj.com_transparent args, obj }, # special custom functions :alternate_pages => -> (obj, args) { obj.com_alternate_pages args, obj }, :set_alternate_pages => -> (obj, args) { obj.com_set_alternate_pages args, obj }, :set_right_to_left => -> (obj, args) { obj.com_set_right_to_left args, obj }, }
- MAX_STACK =
Highest a stack is allowed to be
1024- SYMBOLIZE =
[ :align, :odd_align, :even_align, :valign, :odd_valign, :even_valign, :direction, :mode, :style, :overflow, :vposition, :rotate_around ]
Instance Method Summary collapse
-
#add_font(font, font_name) ⇒ Object
Adds a font to the pdf document.
- #add_fonts ⇒ Object
- #args_correct_hash(hash, type) ⇒ Object
- #args_correct_values(args) ⇒ Object
- #args_has_arr(arg_name, args, type, options = {}) ⇒ Object
- #args_has_file(arg_name, args) ⇒ Object
- #args_has_float(arg_name, args) ⇒ Object
- #args_has_int(arg_name, args) ⇒ Object
-
#args_has_string(arg_name, args) ⇒ Object
variable validation methods.
- #com_alternate_pages(args, obj) ⇒ Object
-
#com_bounding_box(args, obj) ⇒ Object
Prawn commands.
- #com_call(args, obj) ⇒ Object
-
#com_clear_markdown(args, obj) ⇒ Object
This clears the markdown file from memory.
- #com_column_box(args, obj) ⇒ Object
- #com_copy_page(args, obj) ⇒ Object
- #com_delete_page(args, obj) ⇒ Object
- #com_draw_text(args, obj) ⇒ Object
-
#com_each(args, obj) ⇒ Object
Reads from an array of objects and performs a function on all of them populates @variables with the each value.
- #com_elif(args, obj) ⇒ Object
- #com_font(args, obj) ⇒ Object
- #com_font_size(args, obj) ⇒ Object
- #com_go_to_page(args, obj) ⇒ Object
- #com_horizontal_rule(args, obj) ⇒ Object
- #com_if(args, obj) ⇒ Object
- #com_image(args, obj) ⇒ Object
- #com_indent(args, obj) ⇒ Object
-
#com_markdown(args, obj) ⇒ Object
Later - change this to reading a file’s input.
- #com_move_cursor_to(args, obj) ⇒ Object
- #com_move_down(args, obj) ⇒ Object
- #com_move_to(args, obj) ⇒ Object
- #com_move_up(args, obj) ⇒ Object
- #com_number_pages(args, obj) ⇒ Object
- #com_pad(args, obj) ⇒ Object
- #com_pad_bottom(args, obj) ⇒ Object
- #com_pad_top(args, obj) ⇒ Object
- #com_paste_page(args, obj) ⇒ Object
-
#com_print(args, obj) ⇒ Object
Extra commands.
-
#com_read_markdown(args, obj) ⇒ Object
this reads markdown and also adds the metadata.
- #com_rotate(args, obj) ⇒ Object
- #com_scale(args, obj) ⇒ Object
-
#com_set(args, obj) ⇒ Object
sets a variable to a value.
- #com_set_alternate_pages(args, obj) ⇒ Object
- #com_set_right_to_left(args, obj) ⇒ Object
- #com_span(args, obj) ⇒ Object
- #com_start_new_page(args, obj) ⇒ Object
-
#com_test(args, obj) ⇒ Object
Debug test command.
- #com_text(args, obj) ⇒ Object
- #com_text_box(args, obj) ⇒ Object
-
#com_times(args, obj) ⇒ Object
Does an operation a number of times populates @variables with the each value.
- #com_translate(args, obj) ⇒ Object
- #com_transparent(args, obj) ⇒ Object
-
#error_replace_string(error) ⇒ Object
Error display.
- #execute_function(template_id, function_id, obj) ⇒ Object
-
#init_values(options, pdf) ⇒ Object
Functions.
- #print_error(error_code, line) ⇒ Object
-
#read_template ⇒ Object
Reads template from JSON Format: Array of objects where each object has command and set of args.
- #replace_variable(string) ⇒ Object
- #replace_variables(args) ⇒ Object
- #set_variables ⇒ Object
Methods inherited from DefaultPDFWriter
#alternates_pages, #clear_copied_page, #copy_page, #copy_page_data, #initialize_font, #output, #paste_page, #reorder_pages, #reorder_pages_for_2_page, #right_to_left, #set_alternate_pages, #set_options, #set_right_to_left
Instance Method Details
#add_font(font, font_name) ⇒ Object
Adds a font to the pdf document
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/worker/templater/font.rb', line 9 def add_font font, font_name args_has_file :normal, font args_has_file :bold, font args_has_file :italic, font args_has_file :bold_italic, font if font.key?(:normal) if !font.key?(:italic) font[:italic] = font[:normal] end if !font.key?(:bold) font[:bold] = font[:normal] end if !font.key?(:bold_italic) font[:bold_italic] = font[:normal] end font.keys.each do |key| font[key] = replace_variable font[key] end font_families.update(font_name => font) end end |
#add_fonts ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/worker/templater/font.rb', line 48 def add_fonts font_families.clear fonts = Marshal.load(Marshal.dump(@fonts)) fonts.keys.each do |font| add_font fonts[font], font.to_s end end |
#args_correct_hash(hash, type) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/worker/templater/validation.rb', line 156 def args_correct_hash hash, type hash.transform_keys!(&:to_sym) hash.keys.each do |key| if type == :int begin hash[key] = Eqn::Calculator.calc(replace_variable hash[key].to_s).to_i rescue @error_param["arg"] = replace_variable hash[key] return 9 end elsif type == :float begin hash[key] = Eqn::Calculator.calc(replace_variable hash[key].to_s).to_f rescue @error_param["arg"] = replace_variable hash[key] return 9 end else hash[key] = replace_variable hash[key].to_s end end hash end |
#args_correct_values(args) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/worker/templater/validation.rb', line 198 def args_correct_values args args_has_int :width, args args_has_int :height, args SYMBOLIZE.each do |symbol| if args.key? symbol args[symbol] = args[symbol].to_sym end end if args.key? :position if ["left","center","right"].include? args[:position] args[:position] = args[:position].to_sym else args[:position] = args[:position].to_i end end args end |
#args_has_arr(arg_name, args, type, options = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/worker/templater/validation.rb', line 106 def args_has_arr arg_name, args, type, = {} if !args.key? arg_name # text not defined @error_param["arg"] = arg_name.to_s return 7 end if args[arg_name].instance_of? String begin set_variables test = replace_variable args[arg_name] args[arg_name] = JSON.parse(test) rescue => error print "\nError parsing array: #{args[arg_name]}\n" print error end end if args[arg_name].instance_of? Array if type == :float args[arg_name].map! {|item| Eqn::Calculator.calc(replace_variable item.to_s).to_f } elsif type == :int args[arg_name].map! {|item| Eqn::Calculator.calc(replace_variable item.to_s).to_i } elsif type == :hash args[arg_name].map! {|item| args_correct_hash item, [:second_type] } else args[arg_name].map! {|item| replace_variable item.to_s } end end return 0 end |
#args_has_file(arg_name, args) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/worker/templater/validation.rb', line 39 def args_has_file arg_name, args if !args.key? arg_name # text not defined @error_param["arg"] = arg_name.to_s return 7 end file = replace_variable args[arg_name].to_s args[arg_name] = file return 0 end |
#args_has_float(arg_name, args) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/worker/templater/validation.rb', line 82 def args_has_float arg_name, args if args.nil? || !args.key?(arg_name) || args[arg_name].nil? # text not defined @error_param["arg"] = arg_name.to_s return 7 end begin args[arg_name] = Eqn::Calculator.calc(replace_variable args[arg_name]).to_f rescue @error_param["arg"] = replace_variable args[arg_name].to_s return 9 end return 0 end |
#args_has_int(arg_name, args) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/worker/templater/validation.rb', line 57 def args_has_int arg_name, args if args.nil? || !args.key?(arg_name) || args[arg_name].nil? # text not defined @error_param["arg"] = arg_name.to_s return 7 end if args[arg_name].instance_of? String begin args[arg_name] = Eqn::Calculator.calc(replace_variable args[arg_name]).to_i rescue @error_param["arg"] = replace_variable args[arg_name].to_s return 9 end end return 0 end |
#args_has_string(arg_name, args) ⇒ Object
variable validation methods
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/worker/templater/validation.rb', line 25 def args_has_string arg_name, args if !args.key? arg_name # text not defined @error_param["arg"] = arg_name.to_s return 7 end args[arg_name] = replace_variable args[arg_name].to_s return 0 end |
#com_alternate_pages(args, obj) ⇒ Object
1128 1129 1130 1131 1132 1133 1134 |
# File 'lib/worker/templater/methods.rb', line 1128 def com_alternate_pages args, obj obj.reorder_pages_for_2_page return 0 end |
#com_bounding_box(args, obj) ⇒ Object
Prawn commands
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/worker/templater/methods.rb', line 79 def com_bounding_box args, obj validate = args_has_arr :at, args, :int if validate != 0 return validate end validate = args_has_int :width, args if validate != 0 return validate end validate = args_has_string :func, args if validate != 0 return validate end args_has_int :height, args args_has_arr :margin, args, :hash, { :second_type => :int } args[:base_margins] = args[:margin] args = args_correct_values args # Calls the function at "func" obj.bounding_box args[:at], args do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end return 0 end |
#com_call(args, obj) ⇒ Object
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/worker/templater/methods.rb', line 836 def com_call args, obj validate = args_has_string :func, args if validate != 0 return validate end val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end return 0 end |
#com_clear_markdown(args, obj) ⇒ Object
This clears the markdown file from memory
1021 1022 1023 1024 1025 1026 |
# File 'lib/worker/templater/methods.rb', line 1021 def com_clear_markdown args, obj @variables.delete(:markdown_metadata) @variables.delete(:markdown_content) return 0 end |
#com_column_box(args, obj) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/worker/templater/methods.rb', line 121 def com_column_box args, obj validate = args_has_arr :at, args, :int if validate != 0 return validate end validate = args_has_int :width, args if validate != 0 return validate end validate = args_has_string :func, args if validate != 0 return validate end args_has_int :height, args args_has_int :columns, args args_has_float :spacer, args args_has_arr :margin, args, :hash, { :second_type => :int } args[:base_margins] = args[:margin] # Calls the function at "func" obj.column_box args[:at], args do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end return 0 end |
#com_copy_page(args, obj) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/worker/templater/methods.rb', line 163 def com_copy_page args, obj args_has_int :val, args if args.key?(:val) obj.copy_page args[:val] else obj.copy_page end return 0 end |
#com_delete_page(args, obj) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/worker/templater/methods.rb', line 190 def com_delete_page args, obj validate = args_has_int :val, args if validate != 0 return validate end obj.delete_page(args[:val]) return 0 end |
#com_draw_text(args, obj) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/worker/templater/methods.rb', line 202 def com_draw_text args, obj validate = args_has_string :text, args if validate != 0 return validate end validate = args_has_arr :at, args, :int if validate != 0 return validate end args_has_float :size, args args_has_rotate :size, args args = args_correct_values args obj.draw_text args[:text], args return 0 end |
#com_each(args, obj) ⇒ Object
Reads from an array of objects and performs a function on all of them populates @variables with the each value
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 |
# File 'lib/worker/templater/methods.rb', line 1031 def com_each args, obj validate = args_has_string :data, args if validate != 0 return validate end validate = args_has_string :func, args if validate != 0 return validate end arr = PetitFelix::Metadata.new.parse_property args[:data] if arr.instance_of? Array arr.each do |arr_obj| @variables[:each] = arr_obj val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end @variables.delete(:each) else @error_param["arg"] = args[:data].strip return 13 end return 0 end |
#com_elif(args, obj) ⇒ Object
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 |
# File 'lib/worker/templater/methods.rb', line 908 def com_elif args, obj validate = args_has_string :exp, args if validate != 0 return validate end result = false if ["true","false"].include? args[:exp].strip if args[:exp] == "true" result = true else result = false end else begin result = Eqn::Calculator.calc(args[:exp].strip) rescue # expression cannot be evaluated @error_param["arg"] = args[:exp].strip return 9 end end if [true, false].include? result validate = args_has_string :func, args if validate != 0 return validate end validate = args_has_string :func_else, args if validate != 0 return validate end if result val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end else val = obj.execute_function @template_stack[-1], args[:func_else], obj if val[0] != 0 return val[0] end end return 0 end # expression does not return boolean @error_param["arg"] = args[:exp].strip return 8 end |
#com_font(args, obj) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/worker/templater/methods.rb', line 225 def com_font args, obj validate = args_has_string :val, args if validate != 0 return validate end validate = args_has_string :style, args args = args_correct_values args obj.font args[:val] return 0 end |
#com_font_size(args, obj) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/worker/templater/methods.rb', line 241 def com_font_size args, obj validate = args_has_int :val, args if validate != 0 return validate end obj.font_size args[:val] return 0 end |
#com_go_to_page(args, obj) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/worker/templater/methods.rb', line 253 def com_go_to_page args, obj validate = args_has_int :val, args if validate != 0 return validate end obj.go_to_page(args[:val]) return 0 end |
#com_horizontal_rule(args, obj) ⇒ Object
265 266 267 268 269 270 |
# File 'lib/worker/templater/methods.rb', line 265 def com_horizontal_rule args, obj obj.horizontal_rule return 0 end |
#com_if(args, obj) ⇒ Object
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 |
# File 'lib/worker/templater/methods.rb', line 852 def com_if args, obj validate = args_has_string :exp, args if validate != 0 return validate end result = false if ["true","false"].include? args[:exp].strip if args[:exp] == "true" result = true else result = false end else begin result = Eqn::Calculator.calc(args[:exp].strip) rescue # expression cannot be evaluated @error_param["arg"] = replace_variable args[:exp].to_s return 9 end end if [true, false].include? result validate = args_has_string :func, args if validate != 0 return validate end if result val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end return 0 end # expression does not return boolean @error_param["arg"] = args[:exp].strip return 8 end |
#com_image(args, obj) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/worker/templater/methods.rb', line 272 def com_image args, obj validate = args_has_file :file, args if validate != 0 return validate end # if no width defined, use bounds width if !args.key?(:width) && !args.key?(:height) args[:width] = obj.bounds.width end args_has_arr :at, args, :float args_has_float :height, args args_has_float :width, args args_has_float :scale, args args = args_correct_values args if File.file?(args[:file]) obj.image args[:file], args else var = PetitFelix::Metadata.new.get_image_location(@metaoptions["image_dir"], args[:file]) if File.file?(var) obj.image var, args else @error_param["arg"] = args[:file].strip return 14 end end return 0 end |
#com_indent(args, obj) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/worker/templater/methods.rb', line 316 def com_indent args, obj left = 0 if args.key?(:left) right = args[:left].to_i end right = 0 if args.key?(:right) right = args[:right].to_i end validate = args_has_string :func, args if validate != 0 return validate end # Calls the function at "func" obj.indent left, right do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end return 0 end |
#com_markdown(args, obj) ⇒ Object
Later - change this to reading a file’s input.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/worker/templater/methods.rb', line 350 def com_markdown args, obj validate = args_has_string :text, args if validate != 0 return validate end args = args_correct_values args set_variables optio = Marshal.load(Marshal.dump(@variables)) obj.markdown args[:text], options: optio return 0 end |
#com_move_cursor_to(args, obj) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/worker/templater/methods.rb', line 368 def com_move_cursor_to args, obj validate = args_has_int :val, args if validate != 0 return validate end obj.move_cursor_to args[:val] return 0 end |
#com_move_down(args, obj) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/worker/templater/methods.rb', line 380 def com_move_down args, obj validate = args_has_int :val, args if validate != 0 return validate end obj.move_down args[:val] return 0 end |
#com_move_to(args, obj) ⇒ Object
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/worker/templater/methods.rb', line 392 def com_move_to args, obj validate = args_has_arr :pos, args, :float if validate != 0 return validate end if !args[:pos].instance_of? Array || args[:pos].count != 2 # position invalid return 11 end obj.move_to args[:pos] return 0 end |
#com_move_up(args, obj) ⇒ Object
409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/worker/templater/methods.rb', line 409 def com_move_up args, obj validate = args_has_int :val, args if validate != 0 return validate end obj.move_up args[:val] return 0 end |
#com_number_pages(args, obj) ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/worker/templater/methods.rb', line 421 def com_number_pages args, obj validate = args_has_string :text, args if validate != 0 return validate end args_has_string :align_even, args args_has_string :valign_even, args args_has_string :align_odd, args args_has_string :valign_odd, args args_has_int :width, args args_has_arr :at, args, :int args_has_arr :odd_at, args, :int args_has_arr :even_at, args, :int args_has_int :page_finish, args page_mode = :default if args.key?(:page_mode) page_mode = args[:page_mode].to_sym end if !args.key?(:odd_start_count_at) && !args[:start_count_at].nil? args[:odd_start_count_at] = args[:start_count_at] end if !args.key?(:even_start_count_at) && !args[:start_count_at].nil? args[:even_start_count_at] = args[:start_count_at] end args_has_int :start_count_at, args args_has_int :odd_start_count_at, args args_has_int :even_start_count_at, args if !args.key?(:page_finish) args[:page_finish] = -1 end args = args_correct_values args odd_array = { :start_count_at => args[:odd_start_count_at], :at => args[:odd_at], :align => args[:odd_align] } even_array = { :start_count_at => args[:even_start_count_at], :at => args[:even_at], :align => args[:even_align] } args_has_int :page_start, args args_has_int :page_finish, args if !args.key?(:page_start) @variables["paginator_start"] = @metaoptions["paginator_start"] else @variables["paginator_start"] = args[:page_start] end @variables["paginator_end"] = args[:page_finish] if page_mode == :alternate = odd_array.merge(args) = even_array.merge(args) [:start_count_at] += 1 [:page_filter] = ->(pg) { pg > @variables["paginator_start"] && (pg < @variables["paginator_end"] || @variables["paginator_end"] <= -1) && pg % 2 == 1 } [:page_filter] = ->(pg) { pg > @variables["paginator_start"] && (pg < @variables["paginator_end"] || @variables["paginator_end"] <= -1) && pg % 2 == 0 } string = replace_variable args[:text] number_pages string, number_pages string, else odd_array = { :start_count_at => args[:start_count_at], :at => args[:odd_at], :align => args[:align] } = odd_array.merge(args) [:page_filter] = ->(pg) { pg > @variables["paginator_start"] && (pg < @variables["paginator_end"] || @variables["paginator_end"] <= -1) } number_pages args[:text], end return 0 end |
#com_pad(args, obj) ⇒ Object
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/worker/templater/methods.rb', line 523 def com_pad args, obj validate = args_has_int :val, args if validate != 0 return validate end validate = args_has_string :func, args if validate != 0 return validate end # Calls the function at "func" obj.pad args[:val] do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end return 0 end |
#com_pad_bottom(args, obj) ⇒ Object
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/worker/templater/methods.rb', line 550 def com_pad_bottom args, obj validate = args_has_int :val, args if validate != 0 return validate end validate = args_has_string :func, args if validate != 0 return validate end # Calls the function at "func" obj.pad_bottom args[:val] do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end return 0 end |
#com_pad_top(args, obj) ⇒ Object
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'lib/worker/templater/methods.rb', line 578 def com_pad_top args, obj validate = args_has_int :val, args if validate != 0 return validate end validate = args_has_string :func, args if validate != 0 return validate end # Calls the function at "func" obj.pad_top args[:val] do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end return 0 end |
#com_paste_page(args, obj) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/worker/templater/methods.rb', line 176 def com_paste_page args, obj args_has_int :val, args if args.key?(:val) obj.paste_page args[:val] else obj.paste_page end return 0 end |
#com_print(args, obj) ⇒ Object
Extra commands
824 825 826 827 828 829 830 831 832 833 834 |
# File 'lib/worker/templater/methods.rb', line 824 def com_print args, obj validate = args_has_string :text, args if validate != 0 return validate end print args[:text] return 0 end |
#com_read_markdown(args, obj) ⇒ Object
this reads markdown and also adds the metadata
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 |
# File 'lib/worker/templater/methods.rb', line 980 def com_read_markdown args, obj # clears the current markdown file com_clear_markdown args, obj validate = args_has_file :file, args if validate != 0 return validate end if File.file? args[:file] markdown = {} page = File.read(args[:file]) # splits the page into parts for metadata and content # Felix metadata handler = PetitFelix::Metadata.new page_data = .split page @variables[:markdown_metadata] = . page_data[0] @variables[:markdown_content] = page_data[1].strip set_variables add_fonts return 0 else # File not found @error_param["arg"] = args[:file].strip return 10 end end |
#com_rotate(args, obj) ⇒ Object
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
# File 'lib/worker/templater/methods.rb', line 605 def com_rotate args, obj validate = args_has_float :angle, args if validate != 0 return validate end if args.key?(:func) validate = args_has_arr :origin, args, :float if validate != 0 return validate end obj.rotate(args[:angle], args) do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end else obj.rotate args[:angle] end return 0 end |
#com_scale(args, obj) ⇒ Object
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/worker/templater/methods.rb', line 637 def com_scale args, obj validate = args_has_float :factor, args if validate != 0 return validate end if args.key?(:func) validate = args_has_arr :origin, args, :float if validate != 0 return validate end obj.scale(args[:factor], args) do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end else obj.scale args[:factor] end return 0 end |
#com_set(args, obj) ⇒ Object
sets a variable to a value
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 |
# File 'lib/worker/templater/methods.rb', line 1108 def com_set args, obj validate = args_has_int :val, args if validate != 0 return validate end validate = args_has_string :var, args if validate != 0 return validate end @variables[args[:var]] = args[:val] return 0 end |
#com_set_alternate_pages(args, obj) ⇒ Object
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 |
# File 'lib/worker/templater/methods.rb', line 1136 def com_set_alternate_pages args, obj validate = args_has_int :val, args if validate != 0 return validate end obj.set_alternate_pages args[:val] return 0 end |
#com_set_right_to_left(args, obj) ⇒ Object
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 |
# File 'lib/worker/templater/methods.rb', line 1149 def com_set_right_to_left args, obj validate = args_has_int :val, args if validate != 0 return validate end obj.set_right_to_left args[:val] return 0 end |
#com_span(args, obj) ⇒ Object
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
# File 'lib/worker/templater/methods.rb', line 669 def com_span args, obj args = args_correct_values args validate = args_has_float :width, args if validate != 0 return validate end args_has_string :position, args if args.key?(:func) obj.span(args[:width], args) do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end end return 0 end |
#com_start_new_page(args, obj) ⇒ Object
777 778 779 780 781 782 |
# File 'lib/worker/templater/methods.rb', line 777 def com_start_new_page args, obj obj.start_new_page return 0 end |
#com_test(args, obj) ⇒ Object
Debug test command
70 71 72 73 74 75 |
# File 'lib/worker/templater/methods.rb', line 70 def com_test args, obj #obj.reorder_pages [3,2,0,1] obj.reorder_pages_for_2_page return 0 end |
#com_text(args, obj) ⇒ Object
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
# File 'lib/worker/templater/methods.rb', line 696 def com_text args, obj validate = args_has_string :text, args if validate != 0 return validate end args_has_int :size, args args_has_float :character_spacing, args args_has_float :leading, args args_has_string :direction, args args_has_string :align, args args_has_string :valign, args args_has_string :mode, args args = args_correct_values args obj.text args[:text], args return 0 end |
#com_text_box(args, obj) ⇒ Object
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 |
# File 'lib/worker/templater/methods.rb', line 718 def com_text_box args, obj validate = args_has_string :text, args if validate != 0 return validate end args_has_int :size, args args_has_float :width, args args_has_float :height, args args_has_string :direction, args args_has_string :align, args args_has_string :valign, args args_has_float :rotate, args args_has_string :rotate_around, args args_has_float :character_spacing, args args_has_float :leading, args args_has_string :overflow, args args_has_int :min_font_size, args args = args_correct_values args obj.text_box args[:text], args return 0 end |
#com_times(args, obj) ⇒ Object
Does an operation a number of times populates @variables with the each value
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 |
# File 'lib/worker/templater/methods.rb', line 1072 def com_times args, obj validate = args_has_int :val, args if validate != 0 return validate end validate = args_has_string :func, args if validate != 0 return validate end val = args[:val].to_i if val.is_a? Integer val.times do valz = obj.execute_function @template_stack[-1], args[:func], obj if valz[0] != 0 return valz[0] end end else @error_param["arg"] = args[:val].strip return 13 end return 0 end |
#com_translate(args, obj) ⇒ Object
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 |
# File 'lib/worker/templater/methods.rb', line 746 def com_translate args, obj validate = args_has_int :x, args if validate != 0 return validate end validate = args_has_int :y, args if validate != 0 return validate end validate = args_has_string :func, args if validate != 0 return validate end obj.translate(args[:x], args[:y]) do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end return 0 end |
#com_transparent(args, obj) ⇒ Object
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
# File 'lib/worker/templater/methods.rb', line 784 def com_transparent args, obj validate = args_has_float :opacity, args if validate != 0 return validate end stroke_opacity = args[:opacity] if args.key?(:stroke_opacity) stroke_opacity = args[:stroke_opacity].to_f end validate = args_has_string :func, args if validate != 0 return validate end obj.transparent(args[:opacity], stroke_opacity) do val = obj.execute_function @template_stack[-1], args[:func], obj if val[0] != 0 return val[0] end end return 0 end |
#error_replace_string(error) ⇒ Object
Error display
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/worker/templater/error.rb', line 32 def error_replace_string error @error_param.keys.each do |key| error = error.gsub "{{#{key}}}", @error_param[key] end error end |
#execute_function(template_id, function_id, obj) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/worker/template_pdf_writer.rb', line 185 def execute_function template_id, function_id, obj definition = nil if !@template.key? template_id @error_param["arg"] = template_id.to_s # Fails because template ID not found. return [5, -1] else if !@template[template_id].key? function_id.to_sym @error_param["arg"] = function_id.to_s # Fails because template function ID not found. return [6, -1] else definition = Marshal.load(Marshal.dump(@template[template_id][function_id.to_sym])) end end if definition.instance_of? Array counter = 0 @counter_stack.push counter for index in counter ... definition.size # executes the command line = definition[counter] set_variables command = "" if line.key? :cmd command = line[:cmd].to_sym end if line.key? :command command = line[:command].to_sym end args = { "LINE_NUMBER" => counter } if line.key? :args args = line[:args] end begin args = replace_variables(args) rescue => error print "\n" print error print "\n" print "Error rendering variables!\n" end if !command.empty? if COMMAND.keys.include? command @command_stack.push definition if @command_stack.count > 1024 # Failed because of stack overflow return [4, counter] end comm = COMMAND[command].call obj, args @counter_stack[-1] = counter counter += 1 # something about command execution failed if comm != 0 return [comm, counter] end @command_stack.pop else # failed because command not found @error_param["arg"] = command.to_s return [3, counter] end else # failed because no command defined return [2, -1] end end @counter_stack.pop() # Returns 0 if successful return [0, counter] end # Failed because malformed command list return [1, -1] end |
#init_values(options, pdf) ⇒ Object
Functions
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/worker/template_pdf_writer.rb', line 24 def init_values , pdf @variables = {} if @options.nil? || @options.count == 0 @options = Marshal.load(Marshal.dump()) end # Options to use for method calls and stuff # Updated every time a template is loaded @metaoptions = {} @pdf = pdf # stack referencing current running commands @command_stack = [] # stack referencing current program counter @counter_stack = [] # stack referencing current running template # currently unused, will use when stitching # external templates together. @template_stack = [] # Templates @template = {} # prints errors @error_printer = PetitFelix::Error.new # For variables of errors @error_param = {} set_variables end |
#print_error(error_code, line) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/worker/templater/error.rb', line 43 def print_error error_code, line if error_code > -1 && error_code < ERROR_CODES.count @error_printer.print_err "Error reading template. #{error_replace_string( ERROR_CODES[error_code])}" else @error_printer.print_err "Error reading template. General template processing error occured." end print "Error code: #{error_code}" print "\n\n" print "Processing markdown file: #{@metaoptions["filename"]}" print "\n\n" stack_rv = @command_stack.reverse if line >= 0 && !stack_rv.empty? print "\n\nCurrent line:\n" print stack_rv[0][line] print "\n\nCurrent Stack:" test_arr = *(0..[19,@command_stack.count-1].min) test_arr.each do | i | command_obj = stack_rv[i] line_edit = command_obj[0..@counter_stack[i]+1] print "\n" print "Line: #{line_edit.count}: " print line_edit[-1] end end print "\n" return error_code end |
#read_template ⇒ Object
Reads template from JSON Format: Array of objects where each object has command and set of args
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/worker/template_pdf_writer.rb', line 130 def read_template if @options.key? "template" if File.file? @options["template"] obj = JSON.parse(File.read(@options["template"]), symbolize_names: true) = obj[:default_variables].transform_keys!(&:to_s) if .nil? = {} end @fonts = {} @metaoptions = .merge @options set_variables if obj.key? :fonts @fonts = obj[:fonts] end add_fonts @template = { "main" => obj[:definition] } # Runs the main function as entry point of the template @template_stack.push "main" result = execute_function "main", "main", self if result[0] != 0 print_error result[0], result[1] end else @error_param["arg"] = @options["template"].to_s print_error 15, -1 end else print_error 16, -1 end end |
#replace_variable(string) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/worker/template_pdf_writer.rb', line 116 def replace_variable string @variables.keys.each do |key| string = string.gsub("${#{key}}", @variables[key].to_s) end string end |
#replace_variables(args) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/worker/template_pdf_writer.rb', line 101 def replace_variables args args.keys.each do |arg| if args[arg].instance_of? String args[arg] = replace_variable args[arg] end end args end |
#set_variables ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/worker/template_pdf_writer.rb', line 60 def set_variables @metaoptions.keys.each do |key| @variables[key] = @metaoptions[key] end if @variables.key? :markdown_metadata @variables[:markdown_metadata].keys.each do || @variables[] = @variables[:markdown_metadata][] end end @override_options.keys.each do |key| @variables[key] = @override_options[key] end ## add additional functional stuff @variables["cursor"] = @pdf.cursor @variables["bounds_height"] = @pdf.bounds.height @variables["bounds_width"] = @pdf.bounds.width @variables["pages"] = @pdf.page_count # the currently loaded markdown file @variables["loaded_markdown"] = {} if @variables.key?("right_to_left") && @variables["right_to_left"] @right_to_left = @variables["right_to_left"] end if @variables.key?("alternates_pages") && @variables["alternates_pages"] @alternates_pages = @variables["alternates_pages"] end end |