class ImageParadise::ConfreeGenerator
Constants
- ALPHABET
#¶ ↑
ALPHABET
¶ ↑#¶ ↑
- CREATE_A_HTML_GALLERY
#¶ ↑
CREATE_A_HTML_GALLERY
¶ ↑#¶ ↑
- 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
#¶ ↑
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?¶ ↑
#¶ ↑
# File lib/image_paradise/confree_generator/confree_generator.rb, line 679 def an_error_was_encountered? @an_error_was_encountered end
#¶ ↑
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
(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
#¶ ↑
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
#¶ ↑
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
#¶ ↑
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 (reset tag)¶ ↑
#¶ ↑
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
#¶ ↑
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
#¶ ↑
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
#¶ ↑
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_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
#¶ ↑
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
#¶ ↑
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
#¶ ↑
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?¶ ↑
#¶ ↑
# File lib/image_paradise/confree_generator/confree_generator.rb, line 204 def store_into_this_file? @store_into_this_file end
#¶ ↑
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
#¶ ↑
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
#¶ ↑
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