class ImageParadise::ConfreeGenerator

Constants

ALPHABET
#

ALPHABET

#
#
#
DEFAULT_HEIGHT
#

DEFAULT_HEIGHT

#
DEFAULT_WIDTH
#

DEFAULT_WIDTH

#
DIRECTORY_CONTAINING_THE_CFDG_FILES
#

DIRECTORY_CONTAINING_THE_CFDG_FILES

#
DIRECTORY_FOR_THE_TEMPORARY_IMAGES
#

DIRECTORY_FOR_THE_TEMPORARY_IMAGES

#
MAY_WE_GENERATE_THE_IMAGE
#

MAY_WE_GENERATE_THE_IMAGE

If this constant is false, then we will NOT generate any images.

But we will auto-generate the html parts still.

#
NAMESPACE
#

NAMESPACE

#
NAME_OF_OUTPUT_FILE
#

NAME_OF_OUTPUT_FILE

#
OPTIMIZE_THE_PNG_FILES
#

OPTIMIZE_THE_PNG_FILES

If this constant is set to true, then we will try to run “pngquant” on the generated .png file.

#
TRY_TO_OPEN_THE_GENERATED_IMAGE_IN_THE_BROWSER
#

TRY_TO_OPEN_THE_GENERATED_IMAGE_IN_THE_BROWSER

#
USE_THIS_WIDTH_AND_HEIGHT
#

USE_THIS_WIDTH_AND_HEIGHT

#
YOUR_FAVOURITE_EDITOR
#

YOUR_FAVOURITE_EDITOR

Your favourite editor comes here. Perhaps you may prefer vim or emacs.

#

Public Class Methods

[](i = '') click to toggle source
#

ImageParadise::ConfreeGenerator[]

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 735
def self.[](i = '')
  new(i)
end
batch_generate_all_images() click to toggle source
#

ImageParadise::ConfreeGenerator.batch_generate_all_images

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 742
def self.batch_generate_all_images
  ::ImageParadise::ConfreeGenerator.new('--batchmode')
end
new( commandline_arguments = nil, run_already = true ) { || ... } click to toggle source
#

initialize

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 37
def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  register_sigint
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  # ======================================================================= #
  # === Handle blocks given to this class next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :do_not_open_via_the_browser
    # ===================================================================== #
    when :do_not_open_via_the_browser
      @try_to_open_the_generated_image_in_the_browser = false
    else
      # =================================================================== #
      # === Handle Hashes next
      # =================================================================== #
      if yielded.is_a? Hash
        # ================================================================= #
        # === :open_in_browser_tab
        # ================================================================= #
        if yielded.has_key? :open_in_browser_tab
          set_open_in_browser_tab(
            yielded[:open_in_browser_tab]
          )
        end
        # ================================================================= #
        # === :output_location
        # ================================================================= #
        if yielded.has_key? :output_location
          output_location = yielded[:output_location]
          set_location_of_output_file(output_location)
          @may_we_modify_the_location_of_the_output_file = false
        end
        # ================================================================= #
        # === :width
        # ================================================================= #
        if yielded.has_key? :width
          set_width(yielded[:width])
        end
        # ================================================================= #
        # === :height
        # ================================================================= #
        if yielded.has_key? :height
          set_height(yielded[:height])
        end
      end
    end
  end
  run if run_already
end

Public Instance Methods

an_error_was_encountered?() click to toggle source
#

an_error_was_encountered?

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 679
def an_error_was_encountered?
  @an_error_was_encountered
end
Also aliased as: error?, was_an_error_encountered?
br() click to toggle source
#

br

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 312
def br
  '<br>'
end
change_flag()
check_whether_cfdg_is_installed() click to toggle source
#

check_whether_cfdg_is_installed

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 261
def check_whether_cfdg_is_installed
  _ = `cfdg 2>&1` # => "sh: cfdg: command not found\n"
  if _.include? 'cfdg: command not found'
    e 'Note that you do not seem to have cfdg installed.'
    e 'We will still continue nonetheless.'
  end
end
create_a_html_page( into = @store_into_this_file ) click to toggle source
#

create_a_html_page (html tag)

This may typically store into a file such as ‘/Depot/Images/index.html’.

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 544
def create_a_html_page(
    into = @store_into_this_file
  ) 
  require 'save_file'
  require 'cyberweb'
  mkdir(File.dirname(into))
  opnn; e "We will save into `#{sfile(into)}`."
  all_image_files = Dir[DIRECTORY_FOR_THE_TEMPORARY_IMAGES+'*.png'].sort
  # ======================================================================= #
  # Store onto the _ variable.
  # ======================================================================= #
  _ = '<html><head><title>Confree-Images (CFDG Image Collection)</title></head><body>'.dup
  all_image_files.each_with_index {|this_file, index|
    @n_image_files += 1
    # ===================================================================== #
    # Add the <img> tag here.
    # ===================================================================== #
    id_for_this_image_file = File.basename(this_file).
                             sub(/#{File.extname(this_file)}$/,'')
    _ << '<div>'
    _ << Cyberweb.span(
           'The CFDG-File Name is:','','','font-size:0.95em'
         ).dup+
         '<br>'+N
    _ << '<b>'+this_file+'</b>'
    _ << "<img src=\"#{File.basename(this_file)}\" "\
         "id=\"#{id_for_this_image_file}\" "\
         "style=\"margin:5px; border:1px solid black\">"
    _ << Cyberweb.standalone_drag_and_drop_this_image(id_for_this_image_file)+
         N # img tag here.
    _ << '</div>'
    case index % 3
    when 0
      _ << "<br>\n"
    end if index > 0
  }
  _ << br+' '+@n_image_files.to_s+' images files are handled in this .html file.'+N+br+br
  _ << br+'Find more examples '
  _ << '<a href="https://www.contextfreeart.org/downloads.html"><b>here</b></a>.<br>'
  _ << br+'</body></html>'
  File.delete(into) if File.exist? into
  SaveFile.write_what_into(_, into)
  if File.exist? into
    require 'open'
    opnn; e "Finished appending into `#{sfile(into)}`."
    ::Open.in_browser(into)
  end
end
create_image_from_cfdg( i = first_argument_without_hyphen?, may_we_generate_the_image = @may_we_generate_the_image )
do_not_open_in_the_browser_tab() click to toggle source
#

do_not_open_in_the_browser_tab

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 190
def do_not_open_in_the_browser_tab
  @try_to_open_the_generated_image_in_the_browser = false
end
error?()
get_random_cfdg( i = DIRECTORY_WITH_CFDG_IMAGES )
html_file?()
let_the_cfdg_binary_work_on_this_file( this_file = first_argument?, may_we_modify_the_location_of_the_output_file = @may_we_modify_the_location_of_the_output_file ) click to toggle source
#

let_the_cfdg_binary_work_on_this_file

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 647
def let_the_cfdg_binary_work_on_this_file(
    this_file                                     = first_argument?,
    may_we_modify_the_location_of_the_output_file = @may_we_modify_the_location_of_the_output_file
  )
  # ======================================================================= #
  # Notify the user on which .cfdg file this class will work on next.
  # ======================================================================= #
  notify_the_user_that_we_are_working_on_this_file_next(this_file)
  target = return_output_image_file_for(this_file)
  set_output_file(target)
  if may_we_modify_the_location_of_the_output_file
    # ===================================================================== #
    # This is done because the ruby-gtk3 GUI wrapper needs to determine
    # its own full path to the generated .png file at hand.
    # ===================================================================== #
    set_location_of_output_file(target)
  end
  reset_the_error_tracker
  try_to_invoke_the_cfdg_binary_if_an_argument_was_given(this_file)
  try_to_open_the_file_in_a_browser_tab_if_it_exists_locally
end
may_we_generate_the_image?() click to toggle source
#

may_we_generate_the_image?

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 197
def may_we_generate_the_image?
  @may_we_generate_the_image
end
menu( i = commandline_arguments? ) click to toggle source
#

menu (menu tag)

Usage examples:

confgen change_flag

Regular use:

rfree $CFDG/spiral_star.cfdg
rfree $CFDG/star.cfdg
#
name_of_output_file?() click to toggle source
#

name_of_output_file?

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 254
def name_of_output_file?
  NAME_OF_OUTPUT_FILE
end
notify_the_user_that_we_are_working_on_this_file_next(this_file) click to toggle source
#

notify_the_user_that_we_are_working_on_this_file_next

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 375
def notify_the_user_that_we_are_working_on_this_file_next(this_file)
  if File.exist? this_file
    opnn; e rev+'=> Working on the file '+sfile(this_file)+' next.'
  end
end
opnn() click to toggle source
#

opnn

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 240
def opnn
  Opn.opn(namespace: NAMESPACE)
end
random_name(i) click to toggle source
#

random_name

This method will return a string which consists of the input-string and 10 more random characters. We assume that for 10 random characters, there will be no double names.

The result of this method is, for instance:

"tube_map_jblhelce.png"

Note that this may not be as important as it once used to be, past February 2021.

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 330
def random_name(i)
  copy = i.to_s.dup
  alphabet = ALPHABET # Build up the alphabet.
  copy.gsub!(/\.cfdg/,'') # Don't like file ending here.
  copy = File.basename(copy).dup
  copy << '_'
  10.times { copy << alphabet.sample }
  return copy
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method ImageParadise::Base#reset
# File lib/image_paradise/confree_generator/confree_generator.rb, line 99
def reset
  super()
  set_width_then_height(
    DEFAULT_WIDTH,
    DEFAULT_HEIGHT
  )
  # ======================================================================= #
  # === @may_we_generate_the_image
  #
  # The next flag can be true or false.
  #
  # By default it should be true. If it is true, we will generate the
  # image in question. Sometimes though, we may only want to generate
  # the HTML code - that is why this variable exists.
  # ======================================================================= #
  @may_we_generate_the_image = MAY_WE_GENERATE_THE_IMAGE
  # ======================================================================= #
  # === @store_into_this_file
  #
  # This variable will keep track into which local .html file we will
  # generate into.
  #
  # By default it will point at:
  #
  #   /Depot/Images/index.html
  #
  # ======================================================================= #
  @store_into_this_file =
    "#{DIRECTORY_FOR_THE_TEMPORARY_IMAGES}index.html"
  # ======================================================================= #
  # === @create_a_html_gallery
  #
  # If true, we create a html gallery. This is usually the default
  # mode for the --batch commandline flag.
  # ======================================================================= #
  @create_a_html_gallery = CREATE_A_HTML_GALLERY
  # ======================================================================= #
  # === @operate_in_this_mode
  #
  # This variable can be:
  #
  #   :commandline
  #   :batch
  #   :GUI
  #
  # ======================================================================= #
  @operate_in_this_mode = :commandline
  # ======================================================================= #
  # === @try_to_open_the_generated_image_in_the_browser
  # ======================================================================= #
  @try_to_open_the_generated_image_in_the_browser =
    TRY_TO_OPEN_THE_GENERATED_IMAGE_IN_THE_BROWSER
  # ======================================================================= #
  # === @n_image_files
  #
  # This variable will keep track of the current index of image files.
  # ======================================================================= #
  @n_image_files = 0
  # ======================================================================= #
  # === @output_file
  #
  # Designate the main output-file, an image file, here.
  # ======================================================================= #
  @output_file = NAME_OF_OUTPUT_FILE
  # ======================================================================= #
  # === @location_of_output_file
  #
  # This will either be nil, or contain the absolute path to the
  # generated image file.
  # ======================================================================= #
  @location_of_output_file = nil
  # ======================================================================= #
  # === @may_we_modify_the_location_of_the_output_file
  # ======================================================================= #
  @may_we_modify_the_location_of_the_output_file = true
  # ======================================================================= #
  # === @an_error_was_encountered
  # ======================================================================= #
  @an_error_was_encountered = false
end
reset_the_error_tracker() click to toggle source
#

reset_the_error_tracker

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 672
def reset_the_error_tracker
  @an_error_was_encountered = false
end
return_output_image_file_for( this_file = @output_file ) click to toggle source
#

return_output_image_file_for

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 230
def return_output_image_file_for(
    this_file = @output_file
  )
  DIRECTORY_FOR_THE_TEMPORARY_IMAGES+
  File.basename(this_file)+'.png' # Or use random_name(this_file)+'.png'
end
return_random_cfdg_file( i = DIRECTORY_WITH_CFDG_IMAGES ) click to toggle source
#

return_random_cfdg_file

This method will obtain one random .cfdg file.

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 288
def return_random_cfdg_file(
    i = DIRECTORY_WITH_CFDG_IMAGES
  )
  get_all_cfdg_files(i).sample
end
Also aliased as: get_random_cfdg
run() click to toggle source
#

run (run tag)

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 637
def run
  check_whether_cfdg_is_installed
  show_the_welcome_message
  menu
  run_in_commandline_mode_or_in_batch_mode
end
run_in_batch_mode() click to toggle source
#

run_in_batch_mode (batch tag)

Invocation example:

confreegen --batch
#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 601
def run_in_batch_mode
  e "#{rev}Running in batch-mode next."
  @operate_in_this_mode = :batch
  do_not_open_in_the_browser_tab
  if @may_we_generate_the_image
    @all_cfdg_files = Dir[DIRECTORY_CONTAINING_THE_CFDG_FILES+'*.cfdg']
    @all_cfdg_files.each_with_index {|this_file, index| index += 1
      # =================================================================== #
      # Keep track of the current iteration number (index):
      # =================================================================== #
      opnn; e 'Iteration number: '+
               ::Colours.lightblue("(#{index})").rjust(4)
      opnn; e
      opnn; e "  #{::Colours.sfile(this_file)}"
      opnn; e
      set_width_then_height(USE_THIS_WIDTH_AND_HEIGHT) # This is the same size for both.
      let_the_cfdg_binary_work_on_this_file(this_file)
    }
  end
  create_a_html_page
end
run_in_commandline_mode_for_a_single_file( this_file = first_argument?, may_we_modify_the_location_of_the_output_file = @may_we_modify_the_location_of_the_output_file )
run_in_commandline_mode_or_in_batch_mode() click to toggle source
#

run_in_commandline_mode_or_in_batch_mode

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 391
def run_in_commandline_mode_or_in_batch_mode
  case @operate_in_this_mode
  when :commandline
    run_in_commandline_mode_for_a_single_file
  when :batch
    run_in_batch_mode
  end
end
save_files_where?() click to toggle source
#

save_files_where?

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 247
def save_files_where?
  DIRECTORY_FOR_THE_TEMPORARY_IMAGES
end
scan_this_file_for_width_and_height_values(i) click to toggle source
#

scan_this_file_for_width_and_height_values

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 211
def scan_this_file_for_width_and_height_values(i)
  File.readlines(i).each {|line|
    if    line.include? ' WIDTH: '
      # =================================================================== #
      # Set the width next:
      # =================================================================== #
      set_width(line.delete('#:').strip.sub(/WIDTH/,''))
    elsif line.include? ' HEIGHT: '
      # =================================================================== #
      # Set the height next:
      # =================================================================== #
      set_height(line.delete('#:').strip.sub(/HEIGHT/,''))
    end
  }
end
set_height(i) click to toggle source
#

set_height

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 423
def set_height(i)
  if i.is_a? String
    i = i.dup if i.frozen?
    i.strip!
  end
  @height = i.to_i
end
set_location_of_output_file( i = :default ) click to toggle source
#

set_location_of_output_file

This tells us where we store the output file.

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 405
def set_location_of_output_file(
    i = :default
  )
  case i
  when :default
    i = DIRECTORY_FOR_THE_TEMPORARY_IMAGES+
        @output_file
  end
  unless i.include? '/'
    i = File.absolute_path(i)
  end
  @location_of_output_file = i
end
Also aliased as: set_output_location, set_output
set_open_in_browser_tab(i) click to toggle source
#

set_open_in_browser_tab

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 183
def set_open_in_browser_tab(i)
  @try_to_open_the_generated_image_in_the_browser = i
end
set_output( i = :default )
set_output_file(i) click to toggle source
#

set_output_file

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 384
def set_output_file(i)
  @output_file = File.basename(i)
end
Also aliased as: set_output_location
set_output_location(i)
Alias for: set_output_file
set_width(i) click to toggle source
#

set_width

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 434
def set_width(i)
  if i.is_a? String
    i = i.dup if i.frozen?
    i.strip!
  end
  @width = i.to_i
end
set_width_then_height( width = 55, height = nil ) click to toggle source
#

set_width_then_height

This method will first set the width, then the height.

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 274
def set_width_then_height(
    width  = 55,
    height = nil
  )
  height = width if height.nil?
  set_width(width)
  set_height(height)
end
show_help() click to toggle source
#

show_help (help tag)

To invoke this help menu, do:

congen HELP
#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 348
def show_help
  e
  e ::Colours::YEL+"Currently this script has this usage:#{rev}"
  e
  eparse '  confreegen name_of_cfdg_file.cfdg'
  e
  eparse '  --toggle       # Toggle whether we want to create the image or not.'
  eparse '  --batch        # run everything in batch mode (batch '\
         'processing) before creating a .html file'
  eparse '  --html         # an alias to the ^^ above'
  eparse '  --gallery_only # generate only the gallery'
  eparse '  --gui          # start the GUI component of this project'
  e
end
show_the_welcome_message() click to toggle source
#

show_the_welcome_message

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 366
def show_the_welcome_message
  opnn; e ::Colours.steelblue('Welcome!')
  opnn; efancy ' => Handling data through class '\
               'ImageParadise::ConfreeGenerator next.'
end
start_gui() click to toggle source
#

start_gui (gui tag)

This method will start the GUI component.

To start this, try:

confree_generator --gui
#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 452
def start_gui
  require 'image_paradise/gui/gtk3/cfdg_widget/cfdg_widget.rb'
  @gtk_confreegenerator = ImageParadise::GUI::Gtk::CfdgWidget.run
end
store_into_this_file?() click to toggle source
#

store_into_this_file?

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 204
def store_into_this_file? 
  @store_into_this_file
end
Also aliased as: html_file?
toggle_may_we_generate_the_image() click to toggle source
#

toggle_may_we_generate_the_image

This toggle switches between when we want to generate the image, and when we don’t.

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 300
def toggle_may_we_generate_the_image
  if generate_the_image?
    e 'Setting to false - dont create images.'
  else
    e 'Setting to true - do create images.'
  end
  @may_we_generate_the_image = !@may_we_generate_the_image
end
Also aliased as: change_flag
try_to_invoke_the_cfdg_binary_if_an_argument_was_given( i = first_argument_without_hyphen?, may_we_generate_the_image = @may_we_generate_the_image ) click to toggle source
#

try_to_invoke_the_cfdg_binary_if_an_argument_was_given (system tag)

This method will actually create the image-file, by shelling out to the “cfdg” binary. Additionally we may also optimize the generated .png file, via “pngquant”, to minimize its size.

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 691
def try_to_invoke_the_cfdg_binary_if_an_argument_was_given(
    i                         = first_argument_without_hyphen?,
    may_we_generate_the_image = @may_we_generate_the_image
  )
  if i and File.exist?(i)
    scan_this_file_for_width_and_height_values(i)
    # ===================================================================== #
    # We depend on @location_of_output_file being correctly set next.
    # ===================================================================== #
    _ = "cfdg #{i}".dup
    _ << ' --bordersize=1' # This means border size. For now this is hardcoded.
    _ << " -w #{@height}"
    _ << " -h #{@width}"
    # _ << '-a 10'
    _ << " #{@location_of_output_file}"
    e _
    if may_we_generate_the_image
      result = `#{_} 2>&1`
      if result.include? 'error: syntax error'
        @an_error_was_encountered = true
      end
    end
    # ===================================================================== #
    # === Optimize via pngquant
    # ===================================================================== #
    if OPTIMIZE_THE_PNG_FILES and File.exist?(@output_file)
      File.delete('output-fs8.png') if File.exist? 'output-fs8.png'
      # =================================================================== #
      # Optimize the generated .png file.
      # =================================================================== #
      optimize_string = 'pngquant -f --ext .png '+@location_of_output_file
      e "#{rev}Next running #{sfancy('pngquant')} to reduce the size "\
        "of the .png file at hand."
      e
      e sfancy("  #{optimize_string}")
      e
      system(optimize_string)
    end
  end
end
Also aliased as: create_image_from_cfdg
try_to_open_the_file_in_a_browser_tab_if_it_exists_locally( i = @location_of_output_file ) click to toggle source
#

try_to_open_the_file_in_a_browser_tab_if_it_exists_locally

#
# File lib/image_paradise/confree_generator/confree_generator.rb, line 626
def try_to_open_the_file_in_a_browser_tab_if_it_exists_locally(
    i = @location_of_output_file
  )
  if i and @try_to_open_the_generated_image_in_the_browser and File.exist?(i)
    Open.in_browser(i)
  end
end
was_an_error_encountered?()