Class: GamesAndRpgParadise::GenerateHtmlGamebook

Inherits:
Base
  • Object
show all
Includes:
Base::ExtensionsForAGamebook
Defined in:
lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb

Overview

GamesAndRpgParadise::GenerateHtmlGamebook

Constant Summary collapse

CSS_RULES_PER_PAGE =
#

CSS_RULES_PER_PAGE

If you want to use different CSS rules for each page, then this is the constant that you could modify.

#
'
  a, a:link { text-decoration: none;      }
  a:hover   { text-decoration: underline; }
  body {
    font-size: 1.6em;
  }

.circle {
background: lightblue;
border-radius: 80%;
width:  105px;
height: 105px;
text-align: center;
}


'
CSS_FOR_EMBEDDED_IMAGES =
#

CSS_FOR_EMBEDDED_IMAGES

#
'margin-left: 4em;
border: 2px solid black;
border-radius: 12px;'
BR =
#

BR

#
'<br>'

Constants included from Base::ExtensionsForAGamebook

Base::ExtensionsForAGamebook::DIRECTORY_FOR_THE_GAMEBOOKS

Constants included from CommonExtensions

CommonExtensions::CONTROL_C_CODE, CommonExtensions::N

Constants included from Base::Extensions::Colours

Base::Extensions::Colours::ARRAY_AVAILABLE_KONSOLE_COLOURS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::ExtensionsForAGamebook

#available_gamebooks?, #io_download, #roll_this_die, use_gtk?, #use_gtk?, #wget, #yaml_gamebooks_dir?

Methods included from Base::Extensions::CommandlineArguments

#commandline_arguments?, #filter_away_commandline_arguments, #first_argument?, #first_non_hyphened_argument?, #set_commandline_arguments

Methods included from CommonExtensions

#cat, #cd, #cliner, #copy_file, #delete, #dirname_but_retains_the_trailing_slash, #disable_colours, #ensure_that_the_log_directory_exists, #esystem, #get_user_input, #infer_the_namespace, #is_on_roebe?, #log_dir?, #mkdir, #mkdir_then_cd_into_it, #mv, #namespace?, #opne, #opnn, #project_base_directory?, #project_image_directory?, #project_yaml_directory?, #rds, #register_sigint, #remove_this_directory, #rename_file, #reset_the_internal_hash, #return_pwd, #return_today, #touch_file, #wrap, #write_what_into

Methods included from Base::Extensions::Colours

ecomment, #efancy, #eparse, #forestgreen, #gold, #grey, #lightblue, #mediumseagreen, #mediumslateblue, #peru, #rev, sdir, sfancy, #sfile, simp, #teal, #yellow

Constructor Details

#initialize(commandline_arguments = ARGV, run_already = true, &block) ⇒ GenerateHtmlGamebook

#

initialize

#


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 73

def initialize(
    commandline_arguments = ARGV,
    run_already           = true,
    &block
  )
  reset
  if commandline_arguments.is_a?(Array) and commandline_arguments.empty?
    commandline_arguments << '1' # Default value in this case.
  end
  set_commandline_arguments(
    commandline_arguments
  )
  # ======================================================================= #
  # === Handle blocks given to this class next
  # ======================================================================= #
  if block_given?
    yielded = yield
    set_generate_how(yielded) # For { :single } specifically.
  end
  run if run_already
end

Class Method Details

.[](i = ARGV) ⇒ Object

#

GamesAndRpgParadise::GenerateHtmlGamebook[]

#


537
538
539
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 537

def self.[](i = ARGV)
  new(i)
end

Instance Method Details

#consider_opening_this_html_file_in_the_browser(into) ⇒ Object

#

consider_opening_this_html_file_in_the_browse

#


175
176
177
178
179
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 175

def consider_opening_this_html_file_in_the_browser(into)
  if File.exist?(into) and is_on_roebe?
    Open.in_browser(into) # Open it in the browser if we are on a roebe-system.
  end
end

#generate_how?Boolean

#

generate_how?

#

Returns:

  • (Boolean)


243
244
245
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 243

def generate_how?
  @generate_how
end

#header_for_the_subpage(number_of_the_subpage) ⇒ Object

#

header_for_the_subpage

This method will return the code for a subpage-header, as a <h2> tag.

#


253
254
255
256
257
258
259
260
261
262
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 253

def header_for_the_subpage(number_of_the_subpage)
  self_link = HtmlTags.a(
    href:    '#'+number_of_the_subpage.to_s, 
    content: number_of_the_subpage.to_s
  )
  return '<div style="margin-left: 40%; margin-right: auto;">
          <h2 class="circle" style="font-weight: bold; padding-top: 25px;">'+
         self_link+
         "</h2></div>\n"
end

#iterate_over_the_dataset_and_then_generate_a_html_fileObject

#

iterate_over_the_dataset_and_then_generate_a_html_file

There are two ways to generate HTML page/pages holding the dataset to an oldschool gamebook. The first way is to simply generate one big .html file, as-is. This is the default; it is very simple.

The alternative to that is to generate an individual .html file for each subpage. This can be enabled from the commandline via –multiple-HTML-files or simply –multiple.

#


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
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 275

def iterate_over_the_dataset_and_then_generate_a_html_file
  require 'cyberweb/requires/require_the_html_template.rb'
  if @dataset

    use_this_title = @title
    e "#{rev}As title we will be using #{steelblue(use_this_title)}."
    _ = ''.dup
    last_element = @dataset.last
    file_config = File.dirname(last_element)+'/config.yml'
    # ===================================================================== #
    # First we check whether the key :book_cover_image has been
    # defined in the file config.yml. If so then we will
    # embed that image.
    # ===================================================================== #
    if File.exist? file_config
      dataset_from_the_file_config_yaml = YAML.load_file(file_config)
      if dataset_from_the_file_config_yaml.has_key?(:book_cover_image)
        _ << HtmlTags.img(
               dataset_from_the_file_config_yaml[:book_cover_image],
               css_style: 'margin: 1em; margin-left: 3em; border: 3px solid black; border-radius: 10px;'
             )
      end
    end
    case generate_how?
    # ===================================================================== #
    # === :multiple_HTML_pages
    #
    # This entry point can be used to generate multiple different
    # .html files. For this to work we must create a subdirectory.
    #
    # Invocation example:
    #
    #   gengame 7 --multiple
    #
    # ===================================================================== #
    when :multiple_HTML_pages
      target_dir = log_dir?+'autogenerated_html_'+
                   use_this_title.downcase.tr(' ','_')+
                   '/'
      e grey('Autogenerating into `')+
        sdir(target_dir)+
        grey('`.')
      mkdir(target_dir) unless File.directory?(target_dir)
      cd(target_dir)
      current_working_directory = return_pwd
      if (current_working_directory+'/').include? target_dir
        # Ok, proceed now.
        @dataset.each {|number_of_the_subpage, inner_hash|
          _ = ''.dup # Always reset anew here.
          exits   = inner_hash['exits']
          image   = inner_hash['image'].to_s
          content = inner_hash['content'].to_s.dup # Refer to the content.
          if content.include? 'Random Number Table'
            content.gsub!(/Random Number Table/, '<b>Random Number Table</b>')
          end
          if content.include? 'COMBAT SKILL: '
            content.gsub!(/COMBAT SKILL: /,'<b>COMBAT SKILL: </b>')
          end
          if content.include? 'ENDURANCE: '
            content.gsub!(/ENDURANCE: /,'<b>ENDURANCE: </b>')
          end
          content.rstrip!
          _ << '<div style="width: 1200px; margin: 2.5em; padding: 0.75em; '\
               'border: 3px dotted darkblue;
                border-radius: 12px;" '\
               'id="'+number_of_the_subpage.to_s+'">'
          _ << header_for_the_subpage(number_of_the_subpage)
          content = content.gsub(/\n/,"<br>\n")
          # =============================================================== #
          # Append the content next:
          # =============================================================== #
          _ << content+"\n"
          if image and !image.empty?
            _ << BR+BR+
                 HtmlTags.img(
                   image,
                   css_style: CSS_FOR_EMBEDDED_IMAGES
                 )+BR
          end
          if exits
            _ << BR
            exits.each {|number, description|
              description = description.to_s
              modified_description = ''+description
              _ << BR
              if description.include? 'has come to an end.'
                _ << '<b style="margin-left: 2.5em">'+description+'</b>'
              else
                _ << ::HtmlTags.a(
                       number.to_s+'.html',
                       content:   modified_description,
                       css_style: 'margin-left: 2.5em; font-weight: bold;'
                     )
              end
              _ << BR
            }
            _ << BR
          end
          _ << "</div>\n\n"
          # ===================================================================== #
          # Assign this to a standalone page next, captured in the variable
          # called html_content.
          # ===================================================================== #
          html_content = Cyberweb.html_template(
            "<h2 style=\"margin:3px\">#{use_this_title}</h2>\n"+
            _
          ) {{
            tite:     use_this_title,
            main_css: CSS_RULES_PER_PAGE
          }}
          what = html_content
          into = target_dir+
                 number_of_the_subpage.to_s+
                 '.html'
          notify_the_user_where_the_file_will_be_stored_at(into) { :omit_the_title_though }
          write_what_into(what, into)
          # consider_opening_this_html_file_in_the_browser(into)
        }
      else
        e 'No directory exists at '+sdir(_)+' exist.'
      end
    # ===================================================================== #
    # === :a_single_HTML_page
    #
    # Note that this is the default working mode for this class.
    # ===================================================================== #
    when :a_single_HTML_page,
         :default
      crossed_swords_icon = '<img style="margin: 1em; margin-left: 3em;" '\
                            'src="../../'+BeautifulUrl[:crossed_swords_icon].
                            sub(/home\/x/,'')+'"><br>'
      dataset = @dataset
      if dataset.is_a? Array
        dataset = dataset.first
      end
      dataset.each {|number_of_the_subpage, inner_hash|
        if inner_hash.nil?
          e tomato('Problem with the subpage '+number_of_the_subpage.to_s)
          exit
        end
        exits   = inner_hash['exits']
        image   = inner_hash['image'].to_s
        content = inner_hash['content'].to_s.dup
        if content.include? 'Random Number Table'
          content.gsub!(/Random Number Table/, '<b>Random Number Table</b>')
        end
        content.rstrip!
        if content.include?('')
          # Use italic for these.
          regex_to_use = /„([a-zA-Z0-9,üäöß\.\!\?\s*]*)“/ # See: https://rubular.com/r/FT0S8hj1jciS3V
          content.gsub!(regex_to_use, '„<i>\1</i>“')
        end
        _ << '<div style="width: 1200px; margin: 2.5em; padding: 0.75em; '\
             'border: 2px dotted darkblue;" '\
             'id="'+number_of_the_subpage.to_s+'">'
        _ << header_for_the_subpage(number_of_the_subpage)
        content = content.gsub(/\n/,"<br>\n")
        if content.include?('COMBAT SKILL: ')
            # ============================================================= #
            # Add a small weapon-icon and indent to the right side. The
            # regex for ENDURANCE can be seen here: https://rubular.com/r/BHCuvV0WgqAj55
            # ============================================================= #
            _.gsub!(
              /^(\s+)(ENDURANCE:\s*\d{1,3})$/,
                '<span style="font-family: monospace; font-weight: bold; white-space: pre; margin-left: 1.5em">\2</span>'+"\n"
            )
            _.gsub!(
              /  (COMBAT SKILL:.+)$/,
              crossed_swords_icon+"\n"+
                '<span style="font-family: monospace; font-weight: bold; white-space: pre; margin-left: 1.5em">\1</span>'
            )
        end
        _ << content+"\n"
        if image and !image.empty?
          # =============================================================== #
          # Also put the image into the correct location since as of
          # March 2023:
          # =============================================================== #
          real_local_location = GamesAndRpgParadise.project_base_directory?+
                                'images/gamebooks/'+image
          new_target = log_dir?+image
          if image.start_with?('http')
            new_target = log_dir?+File.basename(image)
          end
          mkdir(File.dirname(new_target))
          # =============================================================== #
          # Download the image or make it available otherwise, as-is:
          # =============================================================== #
          if image.start_with?('https') and !File.file?(new_target)
            esystem 'wget '+image+' -O '+new_target 
          elsif File.file?(real_local_location) and !File.file?(new_target)
            copy_file(real_local_location, new_target)
          end
          _ << BR+BR+
               HtmlTags.img(
                 image,
                 css_style: CSS_FOR_EMBEDDED_IMAGES
               )+BR
        end
        if exits
          _ << BR 
          exits.each {|number, description|
            description = description.to_s
            modified_description = ''+description
            _ << BR
            if description.include? 'has come to an end.'
              _ << '<b style="margin-left: 2.5em">'+description+'</b>'
            else
              _ << ::HtmlTags.a(
                     '#'+number.to_s,
                     content:   modified_description,
                     css_style: 'margin-left: 2.5em; font-weight: bold;'
                   )
            end
            _ << BR
          }
          _ << BR
        end
        _ << "</div>\n\n"
      }
      # ===================================================================== #
      # Assign this to a standalone page next, captured in the variable
      # called html_content.
      # ===================================================================== #
      html_content = Cyberweb.html_template(
        "<h2 style=\"margin:3px\">#{use_this_title}</h2>\n"+
        _
      ) {{
        tite:     use_this_title,
        main_css: CSS_RULES_PER_PAGE
      }}
      what = html_content
      into = return_assumed_path_to_the_generated_HTML_page
      notify_the_user_where_the_file_will_be_stored_at(into)
      write_what_into(what, into)
      consider_opening_this_html_file_in_the_browser(into)
    else
      opnn; e 'No dataset has been assigned yet.'
    end
  end
end
#

menu (menu tag)

#


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
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 184

def menu(
    i = commandline_arguments?
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i.to_s # We need to work on Strings past that point. (case tag, menu tag)
    # ===================================================================== #
    # === gengame --help
    # ===================================================================== #
    when /^-?-?-help$/i
      show_help
      exit
    # ===================================================================== #
    # === generate_html_gamebook 1
    #
    # This entry point is used to select the correct gamebook.
    #
    # Usage example:
    #
    #   gengame 3
    #
    # ===================================================================== #
    when /^-?-?gamebook(\d+)$/, # === $1
         /^(\d{1,5})$/ # This is for support of "raw numbers".
      set_use_this_gamebook($1.to_s.dup)
    # ===================================================================== #
    # === gengame --multiple
    # ===================================================================== #
    when /^-?-?multiple$/i,
         /^-?-?multiple(-|_)?HTML(-|_)?files?$/i
      set_generate_how :multiple_HTML_pages
    # ===================================================================== #
    # === gengame --single
    # ===================================================================== #
    when /^-?-?single$/i,
         /^-?-?single(-|_)?HTML(-|_)?files?$/i
      set_generate_how :a_single_HTML_page
    end
  end
end

#notify_the_user_where_the_file_will_be_stored_at(into, &block) ⇒ Object

#

notify_the_user_where_the_file_will_be_stored_at

#


157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 157

def notify_the_user_where_the_file_will_be_stored_at(into, &block)
  result = grey('Saving into the file `')+
           sfile(into)
  if block_given?
    yielded = yield
    case yielded
    when :omit_the_title_though
      e result
      return
    end
  end
  e result+grey("`.\nThe title will be: ")+
           lightgreen(title?)
end

#resetObject

#

reset (reset tag)

#


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 98

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @dataset
  # ======================================================================= #
  @dataset = nil
  # ======================================================================= #
  # === @generate_how
  #
  # The following variable has two valid values only:
  #
  #   - :a_single_HTML_page
  #   - :multiple_HTML_pages
  #
  # ======================================================================= #
  @generate_how = :a_single_HTML_page
end

#return_assumed_path_to_the_generated_HTML_pageObject

#

return_assumed_path_to_the_generated_HTML_page

#


520
521
522
523
524
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 520

def return_assumed_path_to_the_generated_HTML_page
   log_dir?+
   title?.to_s.downcase.tr(' ','_')+
   '.html'
end

#runObject

#

run (run tag)

#


529
530
531
532
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 529

def run
  menu
  iterate_over_the_dataset_and_then_generate_a_html_file
end

#set_generate_how(i) ⇒ Object

#

set_generate_how

#


229
230
231
232
233
234
235
236
237
238
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 229

def set_generate_how(i)
  case i
  # ======================================================================= #
  # === :single
  # ======================================================================= #
  when :single
    i = :a_single_HTML_page
  end
  @generate_how = i
end

#set_use_this_gamebook(i) ⇒ Object

#

set_use_this_gamebook

#


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 131

def set_use_this_gamebook(i)
  i = i.to_i
  # ======================================================================= #
  # === @dataset
  #
  # @dataset will contain the whole gamebook.
  # ======================================================================= #
  @dataset = GamesAndRpgParadise::Gamebooks.load_dataset(i)
  # ======================================================================= #
  # === @title
  # ======================================================================= #
  @title = GamesAndRpgParadise::Gamebooks.infer_title_of_this_gamebook_from_the_file_path(
    GamesAndRpgParadise::Gamebooks.map_number_to_gamebook_file(i)
  )
end

#show_helpObject

#

show_help (help tag)

#


120
121
122
123
124
125
126
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 120

def show_help
  e
  e '  gamebook=2  # generate the second gamebook'
  e '  --single    # generate a single .html file'
  e '  --multiple  # generate multiple .html files'
  e
end

#title?Boolean

#

title?

#

Returns:

  • (Boolean)


150
151
152
# File 'lib/games_and_rpg_paradise/gamebooks/generate_html_gamebook.rb', line 150

def title?
  @title
end