-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.rb
821 lines (691 loc) · 25.4 KB
/
generator.rb
1
2
3
4
5
6
7
8
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
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
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
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
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
184
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
311
312
313
314
315
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
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
522
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
549
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
576
577
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
604
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
636
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
668
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
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
745
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
776
777
778
779
780
781
782
783
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
814
815
816
817
818
819
820
821
#!/usr/bin/env ruby
# #######################################################################
#
# Cards Against Humanity Generator
# --------------------------------
#
# A card generator for Cards Against Humanity
#
# https://dev.bloomgogo.com/projects/cah-generator
#
# Created by Miguel Ángel Fernández Gutiérrez (@mianfg)
# https://mianfg.bloomgogo.com/
#
# Based on Bigger, Blacker Cards
# https://github.com/bbcards/bbcards
#
# #######################################################################
require "prawn"
require "prawn/measurement_extensions"
MM_PER_INCH=25.4
PAPER_NAME = "LETTER"
PAPER_HEIGHT = (MM_PER_INCH*11.0).mm;
PAPER_WIDTH = (MM_PER_INCH*8.5).mm;
def get_card_geometry(card_width_inches=2.0, card_height_inches=2.0, rounded_corners=false, one_card_per_page=false)
card_geometry = Hash.new
card_geometry["card_width"] = (MM_PER_INCH*card_width_inches).mm
card_geometry["card_height"] = (MM_PER_INCH*card_height_inches).mm
card_geometry["rounded_corners"] = rounded_corners == true ? ((1.0/8.0)*MM_PER_INCH).mm : rounded_corners
card_geometry["one_card_per_page"] = one_card_per_page
if card_geometry["one_card_per_page"]
card_geometry["paper_width"] = card_geometry["card_width"]
card_geometry["paper_height"] = card_geometry["card_height"]
else
card_geometry["paper_width"] = PAPER_WIDTH
card_geometry["paper_height"] = PAPER_HEIGHT
end
card_geometry["cards_across"] = (card_geometry["paper_width"] / card_geometry["card_width"]).floor
card_geometry["cards_high"] = (card_geometry["paper_height"] / card_geometry["card_height"]).floor
card_geometry["page_width"] = card_geometry["card_width"] * card_geometry["cards_across"]
card_geometry["page_height"] = card_geometry["card_height"] * card_geometry["cards_high"]
card_geometry["margin_left"] = (card_geometry["paper_width"] - card_geometry["page_width"] ) / 2
card_geometry["margin_top"] = (card_geometry["paper_height"] - card_geometry["page_height"] ) / 2
return card_geometry;
end
def draw_grid(pdf, card_geometry)
pdf.stroke do
if card_geometry["rounded_corners"] == false
#Draw vertical lines
0.upto(card_geometry["cards_across"]) do |i|
pdf.line(
[card_geometry["card_width"]*i, 0],
[card_geometry["card_width"]*i, card_geometry["page_height"]]
)
end
#Draw horizontal lines
0.upto(card_geometry["cards_high"]) do |i|
pdf.line(
[0, card_geometry["card_height"]*i],
[card_geometry["page_width"], card_geometry["card_height"]*i]
)
end
else
0.upto(card_geometry["cards_across"]-1) do |i|
0.upto(card_geometry["cards_high"]-1) do |j|
#rectangle bounded by upper left corner, horizontal measured from the left, vertical measured from the bottom
pdf.rounded_rectangle(
[i*card_geometry["card_width"], card_geometry["card_height"]+(j*card_geometry["card_height"])],
card_geometry["card_width"],
card_geometry["card_height"],
card_geometry["rounded_corners"]
)
end
end
end
end
end
def box(pdf, card_geometry, index, &blck)
# Determine row + column number
column = index%card_geometry["cards_across"]
row = card_geometry["cards_high"] - index/card_geometry["cards_across"]
# Margin: 10pt
x = card_geometry["card_width"] * column + 10
y = card_geometry["card_height"] * row - 10
pdf.bounding_box([x,y], width: card_geometry["card_width"]-20, height: card_geometry["card_height"]-10, &blck)
end
def draw_logos(pdf, card_geometry, icon)
idx=0
while idx < card_geometry["cards_across"] * card_geometry["cards_high"]
box(pdf, card_geometry, idx) do
logo_max_height = 15
logo_max_width = card_geometry["card_width"]/2
pdf.image icon, fit: [logo_max_width,logo_max_height], at: [pdf.bounds.left,pdf.bounds.bottom+25]
end
idx = idx + 1
end
end
def render_card_page(pdf, card_geometry, icon, statements, is_black, game_info)
pdf.font "Helvetica", :style => :normal
pdf.font_size = 14
pdf.line_width(0.5);
if(is_black)
pdf.canvas do
pdf.rectangle(pdf.bounds.top_left,pdf.bounds.width, pdf.bounds.height)
end
pdf.fill_and_stroke(:fill_color=>"000000", :stroke_color=>"000000") do
pdf.canvas do
pdf.rectangle(pdf.bounds.top_left,pdf.bounds.width, pdf.bounds.height)
end
end
pdf.stroke_color "ffffff"
pdf.fill_color "ffffff"
else
pdf.stroke_color "000000"
pdf.fill_color "000000"
end
draw_grid(pdf, card_geometry)
draw_logos(pdf, card_geometry, icon)
statements.each_with_index do |line, idx|
box(pdf, card_geometry, idx) do
line_parts = line.split(/\t/)
card_text = line_parts.shift
card_text = card_text.gsub(/\\n */, "\n")
card_text = card_text.gsub(/\\t/, "\t")
card_text = card_text.gsub("<b>", "[[[b]]]")
card_text = card_text.gsub("<i>", "[[[i]]]")
card_text = card_text.gsub("<u>", "[[[u]]]")
card_text = card_text.gsub("<strikethrough>", "[[[strikethrough]]]")
card_text = card_text.gsub("<sub>", "[[[sub]]]")
card_text = card_text.gsub("<sup>", "[[[sup]]]")
card_text = card_text.gsub("<font", "[[[font")
card_text = card_text.gsub("<color", "[[[color")
card_text = card_text.gsub("<br>", "[[[br/]]]")
card_text = card_text.gsub("<br/>", "[[[br/]]]")
card_text = card_text.gsub("<br />", "[[[br/]]]")
card_text = card_text.gsub("</b>", "[[[/b]]]")
card_text = card_text.gsub("</i>", "[[[/i]]]")
card_text = card_text.gsub("</u>", "[[[/u]]]")
card_text = card_text.gsub("</strikethrough>", "[[[/strikethrough]]]")
card_text = card_text.gsub("</sub>", "[[[/sub]]]")
card_text = card_text.gsub("</sup>", "[[[/sup]]]")
card_text = card_text.gsub("</font>", "[[[/font]]]")
card_text = card_text.gsub("</color>", "[[[/color]]]")
card_text = card_text.gsub(/</, "<");
card_text = card_text.gsub("\[\[\[b\]\]\]", "<b>")
card_text = card_text.gsub("\[\[\[i\]\]\]", "<i>")
card_text = card_text.gsub("\[\[\[u\]\]\]", "<u>")
card_text = card_text.gsub("\[\[\[strikethrough\]\]\]", "<strikethrough>")
card_text = card_text.gsub("\[\[\[sub\]\]\]", "<sub>")
card_text = card_text.gsub("\[\[\[sup\]\]\]", "<sup>")
card_text = card_text.gsub("\[\[\[font", "<font")
card_text = card_text.gsub("\[\[\[color", "<color")
card_text = card_text.gsub("\[\[\[br/\]\]\]", "<br/>")
card_text = card_text.gsub("\[\[\[/b\]\]\]", "</b>")
card_text = card_text.gsub("\[\[\[/i\]\]\]", "</i>")
card_text = card_text.gsub("\[\[\[/u\]\]\]", "</u>")
card_text = card_text.gsub("\[\[\[/strikethrough\]\]\]", "</strikethrough>")
card_text = card_text.gsub("\[\[\[/sub\]\]\]", "</sub>")
card_text = card_text.gsub("\[\[\[/sup\]\]\]", "</sup>")
card_text = card_text.gsub("\[\[\[/font\]\]\]", "</font>")
card_text = card_text.gsub("\[\[\[/color\]\]\]", "</color>")
parts = card_text.split(/\[\[/)
card_text = ""
first = true
previous_matches = false
parts.each do |p|
n = p
this_matches=false
if p.match(/\]\]/)
s = p.split(/\]\]/)
line_parts.push(s[0])
if s.length > 1
n = s[1]
else
n = ""
end
this_matches=true
end
if first
card_text = n.to_s
elsif this_matches
card_text = card_text + n
else
card_text = card_text + "[[" + n
end
first = false
end
card_text = card_text.gsub(/^[\t ]*/, "")
card_text = card_text.gsub(/[\t ]*$/, "")
is_pick2 = false
is_pick3 = false
if is_black
pick_num = line_parts.shift
if pick_num.nil? or pick_num == ""
tmpline = "a" + card_text.to_s + "a"
parts = tmpline.split(/__+/)
if parts.length == 3
is_pick2 = true
elsif parts.length >= 4
is_pick3 = true
end
elsif pick_num == "2"
is_pick2 = true
elsif pick_num == "3"
is_pick3 = true
end
end
is_warn = false
warn_text = ""
if card_text.include? "((" and card_text.include? "))"
is_warn = true
warn_text = card_text[/\(\((.*?)\)\)/, 1]
card_text.sub!("(("+warn_text+"))", "")
end
picknum = "0"
if is_pick2
picknum = "2"
elsif is_pick3
picknum = "3"
elsif is_black
picknum = "1"
end
statements[idx] = [card_text,picknum]
#by default cards should be bold
card_text = "<b>" + card_text + "</b>"
# Text
pdf.font "Helvetica", :style => :normal
if is_pick3 and !is_warn
pdf.text_box card_text.to_s, :overflow => :shrink_to_fit, :height =>card_geometry["card_height"]-55, :inline_format => true
else
pdf.text_box card_text.to_s, :overflow => :shrink_to_fit, :height =>card_geometry["card_height"]-35, :inline_format => true
end
pdf.font "Helvetica", :style => :bold
#pick 2
if is_pick2 and !is_warn
pdf.text_box "PICK", size:11, align: :right, width:30, at: [pdf.bounds.right-50,pdf.bounds.bottom+20+1]
pdf.fill_and_stroke(:fill_color=>"ffffff", :stroke_color=>"ffffff") do
pdf.circle([pdf.bounds.right-10+4,pdf.bounds.bottom+15.5+2],7.5)
end
pdf.stroke_color '000000'
pdf.fill_color '000000'
pdf.text_box "2", color:"000000", size:14, width:8, align: :center, at:[pdf.bounds.right-14+4,pdf.bounds.bottom+21+1.5]
pdf.stroke_color "ffffff"
pdf.fill_color "ffffff"
end
#pick 3
if is_pick3 and !is_warn
pdf.text_box "PICK", size:11, align: :right, width:30, at: [pdf.bounds.right-50,pdf.bounds.bottom+20+1]
pdf.fill_and_stroke(:fill_color=>"ffffff", :stroke_color=>"ffffff") do
pdf.circle([pdf.bounds.right-10+4,pdf.bounds.bottom+15.5+2],7.5)
end
pdf.stroke_color '000000'
pdf.fill_color '000000'
pdf.text_box "3", color:"000000", size:14, width:8, align: :center, at:[pdf.bounds.right-14+4,pdf.bounds.bottom+21+1.5]
pdf.stroke_color "ffffff"
pdf.fill_color "ffffff"
pdf.text_box "DRAW", size:11, align: :right, width:35, at: [pdf.bounds.right-55,pdf.bounds.bottom+40+1]
pdf.fill_and_stroke(:fill_color=>"ffffff", :stroke_color=>"ffffff") do
pdf.circle([pdf.bounds.right-10+4,pdf.bounds.bottom+35.5+2],7.5)
end
pdf.stroke_color '000000'
pdf.fill_color '000000'
pdf.text_box "2", color:"000000", size:14, width:8, align: :center, at:[pdf.bounds.right-14+4,pdf.bounds.bottom+41+1.5]
pdf.stroke_color "ffffff"
pdf.fill_color "ffffff"
end
if is_warn
if is_black
stroke_previous = pdf.stroke_color
fill_previous = pdf.fill_color
pdf.fill_and_stroke(:fill_color=>"ffffff", :stroke_color=>"ffffff") do
pdf.circle([pdf.bounds.right-10+4,pdf.bounds.bottom+15.5+2],7.5)
end
pdf.stroke_color '000000'
pdf.fill_color '000000'
pdf.text_box warn_text, color:"000000", size:14, width:12, align: :center, at:[pdf.bounds.right-14+4-2,pdf.bounds.bottom+21+1.5]
pdf.stroke_color stroke_previous
pdf.fill_color fill_previous
else
stroke_previous = pdf.stroke_color
fill_previous = pdf.fill_color
pdf.fill_and_stroke(:fill_color=>"000000", :stroke_color=>"000000") do
pdf.circle([pdf.bounds.right-10+4,pdf.bounds.bottom+15.5+2],7.5)
end
pdf.stroke_color 'ffffff'
pdf.fill_color 'ffffff'
pdf.text_box warn_text, color:"ffffff", size:14, width:12, align: :center, at:[pdf.bounds.right-14+4-2,pdf.bounds.bottom+21+1.5]
pdf.stroke_color stroke_previous
pdf.fill_color fill_previous
end
end
# print text
text = game_info[:game_name]
version = game_info[:game_version]
if is_pick2 or is_pick3
text = game_info[:game_name_abbr]
end
if is_black
text_color = "ffffff"
else
text_color = "000000"
end
stroke_previous = pdf.stroke_color
fill_previous = pdf.fill_color
pdf.stroke_color '000000'
pdf.fill_color '000000'
pdf.text_box version, color:"000000", size:6, width:20, rotate: -20, align: :center, at:[pdf.bounds.left+6,pdf.bounds.bottom+22] # WIP text
pdf.stroke_color text_color
pdf.fill_color text_color
pdf.text_box text, color:"ffffff", size:6, width:80, align: :left, at:[pdf.bounds.left+24,pdf.bounds.bottom+20] # WIP text
pdf.stroke_color = stroke_previous
pdf.fill_color = fill_previous
end
end
pdf.stroke_color "000000"
pdf.fill_color "000000"
end
def load_pages_from_lines(lines, card_geometry)
pages = []
non_empty_lines = []
lines.each do |line|
line = line.gsub(/^[\t\n\r]*/, "")
line = line.gsub(/[\t\n\r]*$/, "")
if line != ""
non_empty_lines.push(line)
end
end
lines = non_empty_lines
cards_per_page = card_geometry["cards_high"] * card_geometry["cards_across"]
num_pages = (lines.length.to_f/cards_per_page.to_f).ceil
0.upto(num_pages - 1) do |pn|
pages << lines[pn*cards_per_page,cards_per_page]
end
return pages
end
def load_info(file)
if File.exist?(file)
lines = IO.readlines(file)
end
if !lines.nil?
if lines.size > 0
name = lines[0]
else
name = ""
end
if lines.size > 1
name_abbr = name.scan(/\b[a-z]/i).join
version = lines[1]
elsif lines.size > 2
name_abbr = lines[1]
version = lines[2]
else
name_abbr = ""
version = ""
end
end
return { :game_name => name, :game_name_abbr => name_abbr, :game_version => version }
end
def load_pages_from_string(string, card_geometry)
lines = string.split(/[\r\n]+/)
pages = load_pages_from_lines(lines, card_geometry)
return pages
end
def load_pages_from_file(file, card_geometry)
pages = []
if File.exist?(file)
lines = IO.readlines(file)
pages = load_pages_from_lines(lines, card_geometry);
end
return pages
end
def load_ttf_fonts(font_dir, font_families)
if font_dir.nil?
return
elsif (not Dir.exist?(font_dir)) or (font_families.nil?)
return
end
font_files = Hash.new
ttf_files=Dir.glob(font_dir + "/*.ttf")
ttf_files.each do |ttf|
full_name = ttf.gsub(/^.*\//, "")
full_name = full_name.gsub(/\.ttf$/, "")
style = "normal"
name = full_name
if name.match(/_Bold_Italic$/)
style = "bold_italic"
name = name.gsub(/_Bold_Italic$/, "")
elsif name.match(/_Italic$/)
style = "italic"
name = name.gsub(/_Italic$/, "")
elsif name.match(/_Bold$/)
style = "bold"
name = name.gsub(/_Bold$/, "")
end
name = name.gsub(/_/, " ");
if not (font_files.has_key? name)
font_files[name] = Hash.new
end
font_files[name][style] = ttf
end
font_files.each_pair do |name, ttf_files|
if (ttf_files.has_key? "normal" ) and (not font_families.has_key? "name" )
normal = ttf_files["normal"]
italic = (ttf_files.has_key? "italic") ? ttf_files["italic"] : normal
bold = (ttf_files.has_key? "bold" ) ? ttf_files["bold"] : normal
bold_italic = normal
if ttf_files.has_key? 'bold_italic'
bold_italic = ttf_files["bold_italic"]
elsif ttf_files.has_key? 'italic'
bold_italic = italic
elsif ttf_files.has_key? 'bold'
bold_italic = bold
end
font_families.update(name => {
:normal => normal,
:italic => italic,
:bold => bold,
:bold_italic => bold_italic
})
end
end
end
# Renders cards
# @param directory [String] :
# @param info_file [String] :
# @param white_file [String] :
# @param black_file [String] :
# @param icon_file [String] :
# @param output_file [String] :
# @param input_files_are_absolute [Bool] :
# @param output_file_name_from_directory [Bool] :
# @param recurse [Bool] :
def render_cards(directory=".", info_file="info.txt", white_file="white.txt", black_file="black.txt", icon_file="icon.png", output_file="cards.pdf", input_files_are_absolute=false, output_file_name_from_directory=true, recurse=true, card_geometry=get_card_geometry, white_string="", black_string="", output_to_stdout=false, title=nil)
original_white_file = white_file
original_black_file = black_file
original_icon_file = icon_file
original_info_file = info_file
if not input_files_are_absolute
white_file = directory + File::Separator + white_file
black_file = directory + File::Separator + black_file
icon_file = directory + File::Separator + icon_file
info_file = directory + File::Separator + info_file
end
if not File.exist? icon_file
icon_file = "./icon.png"
end
if not directory.nil?
if File.exist?(directory) and directory != "." and output_file_name_from_directory
output_file = directory.split(File::Separator).pop + ".pdf"
end
end
if output_to_stdout and title.nil?
title = "Bloomgogo CAH"
elsif title.nil? and output_file != "cards.pdf"
title = output_file.split(File::Separator).pop.gsub(/.pdf$/, "")
end
white_pages = []
black_pages = []
if white_file == nil and black_file == nil and white_string == "" and black_string == ""
white_string = " "
black_string = " "
end
if white_string != "" || white_file == nil
white_pages = load_pages_from_string(white_string, card_geometry)
else
white_pages = load_pages_from_file(white_file, card_geometry)
end
if black_string != "" || black_file == nil
black_pages = load_pages_from_string(black_string, card_geometry)
else
black_pages = load_pages_from_file(black_file, card_geometry)
end
if white_pages.length > 0 or black_pages.length > 0
pdf = Prawn::Document.new(
page_size: [card_geometry["paper_width"], card_geometry["paper_height"]],
left_margin: card_geometry["margin_left"],
right_margin: card_geometry["margin_left"],
top_margin: card_geometry["margin_top"],
bottom_margin: card_geometry["margin_top"],
info: { :Title => title, :CreationDate => Time.now, :Producer => "Bigger, Blacker Cards", :Creator=>"Bigger, Blacker Cards" }
)
load_ttf_fonts("/usr/share/fonts/truetype/msttcorefonts", pdf.font_families)
info = load_info(info_file)
white_pages.each_with_index do |statements, page|
render_card_page(pdf, card_geometry, icon_file, statements, false, info)
pdf.start_new_page unless page >= white_pages.length-1
end
pdf.start_new_page unless white_pages.length == 0 || black_pages.length == 0
black_pages.each_with_index do |statements, page|
render_card_page(pdf, card_geometry, icon_file, statements, true, info)
pdf.start_new_page unless page >= black_pages.length-1
end
if output_to_stdout
puts "Content-Type: application/pdf"
puts ""
print pdf.render
else
pdf.render_file(output_file)
end
end
if (not input_files_are_absolute) and recurse
files_in_dir =Dir.glob(directory + File::Separator + "*")
files_in_dir.each do |subdir|
if File.directory? subdir
render_cards(subdir, original_info_file, original_white_file, original_black_file, original_icon_file, "irrelevant", false, true, true, card_geometry )
end
end
end
end
# Parse arguments
# @param variables [Hash] :
# @param flags [Hash] :
# @param save_orphaned [Bool] :
# @param argv [Array<>] :
def parse_args(variables=Hash.new, flags=Hash.new, save_orphaned=false, argv=ARGV)
parsed_args = Hash.new
orphaned = Array.new
new_argv=Array.new
while argv.length > 0
next_arg = argv.shift
if variables.has_key? next_arg
arg_name = variables[next_arg]
parsed_args[arg_name] = argv.shift
elsif flags.has_key? next_arg
flag_name = flags[next_arg]
parsed_args[flag_name] = true
else
orphaned.push next_arg
end
new_argv.push next_arg
end
if save_orphaned
parsed_args["ORPHANED_ARGUMENT_ARRAY"] = orphaned
end
while new_argv.length > 0
argv.push new_argv.shift
end
return parsed_args
end
# Prints help message
def print_help
puts "\n . :+ysmd.
`.``ohhys+-`.m
`.`-+ohsy-yhyyyyyyyyo:o/-/oosssyy.
`--:+hsso:.` mMMMMMMMMN/NMMMNdhyyydm-
dm+/-` mMMMMMMMModMMMMMMMMMMmdy+:.`
+N+ mMMMMMMMyyMMMMMMMMMMMMMMMMNmhs+:.
`/N: mMMMMMMd+MMMMMMMMMMMMMMMMMMMMMMMm.
.om. mMMMMMN/NMMMMMMMMMMMMMMMMMMMMMMM+
-yh` mMMMMM+mMMMMMMMMMMMMMMMMMMMMMMMy
:do mMMMMsyMMMMMMMMMMMMMMMMMMMMMMMm`
`:N: mMMMhoMMMMMMMMMMMMMMMMMMMMMMMN-
`/N. mMMm/NMMMMMMMMMMMMMMMMMMMMMMM/
.sd` mMM+ymMMMMMMMMMMMMMMMMMMMMMMs
:hs +yy+-.-/oydNMMMMMMMMMMMMMMMd`
-m/-/oosys-.. ``-/ohmNMMMMMMMMN.
.+s++/. ``-+shmMMM/
`::
"
puts "\n CARDS AGAINST HUMANITY GENERATOR"
puts ""
puts "-----------------------------------------------------------"
puts "USAGE:"
puts "\tgenerator --directory [CARD_FILE_DIRECTORY]"
puts "\tOR"
puts "\tgenerator --white [WHITE_CARD_FILE]"
puts "\t --black [BLACK_CARD_FILE]"
puts "\t --info [INFO_FILE] --output [OUTPUT_FILE]"
puts "-----------------------------------------------------------"
puts ""
puts "1. Routes"
puts "-----------------------------------------------------------"
puts "This generator expects you to specify EITHER a directory or"
puts "specify a path to black/white card files. If both are"
puts "specified, it will ignore the indifidual files and generate"
puts "cards from the directory."
puts ""
puts "If you specify a directory, white cards will be loaded from"
puts "a file white.txt in that directory and black cards from"
puts "black.txt. If info.txt file exists in that directory, every"
puts "card will contain the information provided."
puts "The output will be a pdf file with the same name as the"
puts "directory you specified in the current working directory."
puts "This generator will descend recursively into any directory"
puts "you specify, generating a separate pdf for every directory"
puts "that contains black.txt, white.txt or both."
puts ""
puts "2. Info file"
puts "-----------------------------------------------------------"
puts "The info file contains the name of the game. It must have"
puts "from one to three lines."
puts " - One line:"
puts " line 1 - game name"
puts " (name abbreviation will be automatically generated)"
puts " - Two lines:"
puts " line 1 - game name"
puts " line 2 - game version"
puts " (name abbreviation will be automatically generated)"
puts " - Three lines:"
puts " line 1 - game name"
puts " line 2 - abbreviated game name"
puts " line 3 - game version"
puts ""
puts "3. White and black text files"
puts "-----------------------------------------------------------"
puts "Each card must be in one line. Zero-length lines will be"
puts "ignored, but lines containing spaces will be turned into"
puts "blank cards."
puts ""
puts "Inserting ((_)) will generate a special card, that has as"
puts "icon the character _ (i.e., for warning cards, put ((!))"
puts ""
puts "The generator will detect PICK 2 and PICK 3, but you can"
puts "manually insert them by adding [[2]] or [[3]] at the"
puts "beginning or the end of the line."
puts ""
puts "Card text can be formatted using HTML-like tags. The"
puts "list of supported tags is as follows:"
puts ""
puts "\t<b></b> - bold text"
puts "\t<i></i> - italic text"
puts "\t<u></u> - underlined text"
puts "\t<strikethrough></strikethrough>"
puts "\t<sub></sub> - subscript text"
puts "\t<sup></sup> - superscript text"
puts "\t<br> - line break"
puts "\t<color rgb=\"#0000ff\"></color>"
puts "\t<font name=\"Font Name\"></font>"
puts ""
puts "3. Card sizes"
puts "-----------------------------------------------------------"
puts "You may specify the card size by passing either the --small"
puts " or --large flag. If you pass the --small flag then small"
puts "cards of size 2\"x2\" will be produced. If you pass the"
puts "--large flag larger cards of size 2.5\"x3.5\" will be"
puts "produced. Small cards are produced by default."
puts ""
puts "All flags"
puts "-----------------------------------------------------------"
puts "\t-b,--black\tBlack card file"
puts "\t-d,--dir\tDirectory to search for card files"
puts "\t-h,--help\tPrint this Help message"
puts "\t-i,--info\tInfo file"
puts "\t-l,--large\tGenerate large 2.5\"x3.5\" cards"
puts "\t-o,--output\tOutput file, will be a .pdf file"
puts "\t-s,--small\tGenerate small 2\"x2\" cards"
puts "\t-w,--white\tWhite card file"
puts ""
puts "-----------------------------------------------------------"
puts ""
puts "https://dev.bloomgogo.com/projects/cah-generator"
puts ""
end
# Main function
def main
arg_defs = Hash.new
flag_defs = Hash.new
arg_defs["-b"] = "black"
arg_defs["--black"] = "black"
arg_defs["-w"] = "white"
arg_defs["--white"] = "white"
arg_defs["--info"] = "info"
arg_defs["-i"] = "info"
arg_defs["-d"] = "dir"
arg_defs["--directory"] = "dir"
arg_defs["-o"] = "output"
arg_defs["-output"] = "output"
flag_defs["-s"] = "small"
flag_defs["--small"] = "small"
flag_defs["-l"] = "large"
flag_defs["--large"] = "large"
flag_defs["-r"] = "rounded"
flag_defs["--rounded"] = "rounded"
flag_defs["-p"] = "oneperpage"
flag_defs["--oneperpage"] = "oneperpage"
flag_defs["-h"] = "help"
flag_defs["--help"] = "help"
args = parse_args(arg_defs, flag_defs)
card_geometry = get_card_geometry(2.0,2.0, !(args["rounded"]).nil?, !(args["oneperpage"]).nil? )
if args.has_key? "large"
card_geometry = get_card_geometry(2.5,3.5, (not (args["rounded"]).nil?), (not (args["oneperpage"]).nil? ))
end
if args.has_key? "help" or args.length == 0 or ( (not args.has_key? "white") and (not args.has_key? "black") and (not args.has_key? "dir") )
print_help
elsif args.has_key? "dir"
render_cards args["dir"], "info.txt", "white.txt", "black.txt", "icon.png", "cards.pdf", false, true, true, card_geometry, "", "", false
else
render_cards nil, args["info"], args["white"], args["black"], "icon.png", args["output"], true, false, false, card_geometry, "", "", false
end
end
main
exit