Class: Cyberweb::PdfToHtml

Inherits:
ObtainFilesAndDirectoriesFromThisPath show all
Defined in:
lib/cyberweb/utility_scripts/pdf_to_html/pdf_to_html.rb

Overview

Cyberweb::PdfToHtml

Constant Summary collapse

REMOTE_PATH_TO_THE_PDF_LOGO =
#
#
'https://i.imgur.com/dNvPea4.png'
RIGHT_ARROW =
#

RIGHT_ARROW

#
''
ENGLISH_TITLE =
#

ENGLISH_TITLE

#
'PDF documents'
GERMAN_TITLE =
#

GERMAN_TITLE

#
'PDF Dokumente'
'font-size: 1.1em; margin-left: 1em; '\
'padding: 8px; '\
'color: saddlebrown; font-weight: bold;'
TARGET_TO_INGRID =
#

TARGET_TO_INGRID

#
'C:\\ingrid\\dokumente_von_ingrid_k****nz_und_peter_k****nz_vor_dem_jahr_2015\\'.
squeeze('*').
tr('*','u')

Constants inherited from ObtainFilesAndDirectoriesFromThisPath

ObtainFilesAndDirectoriesFromThisPath::ARRAY_IMAGES, ObtainFilesAndDirectoriesFromThisPath::BE_VERBOSE, ObtainFilesAndDirectoriesFromThisPath::CSS_STYLE_TO_USE_FOR_THE_TOP_HEADER, ObtainFilesAndDirectoriesFromThisPath::DEFAULT_CSS_STYLE_FOR_HYPERLINKS, ObtainFilesAndDirectoriesFromThisPath::FONT_SIZE_TO_USE, ObtainFilesAndDirectoriesFromThisPath::HTML, ObtainFilesAndDirectoriesFromThisPath::IMG_CSS_STYLE, ObtainFilesAndDirectoriesFromThisPath::INFO_DIRECTORY, ObtainFilesAndDirectoriesFromThisPath::SHALL_WE_DEBUG, ObtainFilesAndDirectoriesFromThisPath::THESE_CSS_CLASSES_ARE_KNOWN_TO_THE_RENDERED_TEMPLATE

Constants inherited from Base

Base::HOME_DIRECTORY_OF_THE_USER_X, Base::NAMESPACE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ObtainFilesAndDirectoriesFromThisPath

#add, #add_date_in_the_lower_part_of_the_webpage?, #add_hyperlinks_to_subdirectories?, #add_the_footer_with_hyperlinks, #addnl, #append, #append_to_local_css_style, #be_verbose?, #body_css_style?, #check_whether_at_the_least_one_argument_was_given_and_then_act_accordingly, #clear_directories, #clear_files, #colour_for_pdf_files, #consider_appending_the_jquery_string, #content_of_the_HTML_page?, #directory_containing_all_files?, #display_the_top_header?, #do_debug, #do_include_subdirectories, #do_make_use_of_a_go_down_one_level_link, #do_not_include_subdirectories, #do_not_use_HTML_table_layout, #do_open_the_webpage_if_we_are_on_a_roebe_system, #do_process_all_entries_from_this_directory, #do_store_the_content, #do_traverse_into_subdirectories?, #do_use_HTML_table_layout, #files?, #filter_for_these_files, #gold, #include_subdirectories?, #make_use_of_a_go_down_one_level_link?, #mediumorchid, #mediumseagreen, #menu, #n_rows?, #olivedrab, #paleturquoise, #registered_ids?, #report_not_registered_commandline_argument, #report_scanning_directory, #return_early_and_late_year_from_this_array, #return_jquery, #return_the_code_for_going_down_one_level, #return_the_pretty_name_of_the_webpage, #royalblue, #save_into_this_local_file?, #set_background, #set_body_css_style, #set_content_of_the_html_page, #set_directory_containing_all_files, #set_files, #set_save_into_this_local_file, #shall_we_debug?, #show_help, #show_year_in_the_header?, #sort_how?, #use_HTML_table_layout?, #use_german?, #use_jquery?, #use_no_size_value?, #work_on_video_files_only?

Methods inherited from Base

#be_verbose, #be_verbose?, #cascading_style_sheets_directory?, #cd, #commandline_mode?, #copyright?, #css_comment, #delete_directory, #do_toggle, #e, #echo_raw, #filename?, #full_path_to_image_directory?, #html_colours?, #html_image, #htmlentities, #img_dir?, #infer_the_namespace, #initialize_the_configuration, #is_a_file?, #is_a_video_file?, #is_an_image_file?, #is_on_roebe?, #last_tag_used?, #last_tag_used_first_element?, #last_tag_used_id?, #mkdir, #mode?, #namespace?, #open_in_browser, #opne, #opnn, #remove_comments_from_this_string_but_preserve_CSS_rules, #remove_html, #return_all_directories, #return_date, #return_program_name, #return_pwd, #ruby_header?, #server_base_directory?, #set_commandline_mode, #set_last_tag_used, #update_the_image_directory, #use_this_relative_directory_for_custom_images?, #www_mode?

Methods included from BaseModule::FileRelatedFunctionality

#copy_this_file, #cpr, #delete_file, #filename_without_extension?, #read_file_via_utf8_encoding

Methods included from BaseModule

#attach_these_constants, #base_dir?, #beautiful_url, #cd, #create_the_internal_hash, #doctype?, #ee, #ensure_main_encoding, #esystem, #html_templates, #initialize_the_config_if_the_config_hash_is_empty, #internal_hash?, #log_dir?, #no_http, #random_alphabet_characters, #rarrow?, #rds, #remove_comments_from_this_string, #remove_numbers, #require_the_html_templates, #require_these, #return_file_size_in_kb_of, #return_html_comment, #ruby_sitelib_dir?, #sanitize_this_id, #server_base_directory?, #string_remote_image, #today?, #try_to_require_rack, #try_to_require_the_open_gem, #write_what_into

Methods included from BaseModule::ContentType

#content_type_is_html, #content_type_is_jpeg, #content_type_is_json, #content_type_is_plain_text

Methods included from BaseModule::CommandlineArguments

#append_to_the_commandline_arguments, #commandline_arguments?, #first_argument?, #parse_these_commandline_arguments, #second_argument?, #set_commandline_arguments

Methods included from BaseModule::Colours

#all_html_colours, #lightgreen, #rev, #sdir, #sfancy, #sfile, #simp, #steelblue, #tomato

Constructor Details

#initialize(commandline_arguments = ARGV, run_already = true) ⇒ PdfToHtml

#

initialize

#


66
67
68
69
70
71
72
73
74
75
# File 'lib/cyberweb/utility_scripts/pdf_to_html/pdf_to_html.rb', line 66

def initialize(
    commandline_arguments = ARGV,
    run_already           = true
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  run if run_already
end

Class Method Details

.[](i = ARGV) ⇒ Object

#

Cyberweb::PdfToHtml[]

#


288
289
290
# File 'lib/cyberweb/utility_scripts/pdf_to_html/pdf_to_html.rb', line 288

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

Instance Method Details

#resetObject

#

reset (reset tag)

#


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cyberweb/utility_scripts/pdf_to_html/pdf_to_html.rb', line 80

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @title_to_use
  # ======================================================================= #
  @title_to_use = GERMAN_TITLE
  # ======================================================================= #
  # Use a white background next:
  # ======================================================================= #
  set_body_css_style :default_for_white_background
end

#runObject

#

run (run tag)

#


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
# File 'lib/cyberweb/utility_scripts/pdf_to_html/pdf_to_html.rb', line 103

def run
  menu(commandline_arguments?) # Must come first.
  do_obtain_all_files(:pwd)
  do_filter_for(:pdf)
  files = files?
  append_to_local_css_style(
    '#div_footer_id {
      color: lightgreen;
      background-color: black;
      padding: 8px;
    }
    .NO_BLOCK {
      display: inline;
    }
    '
  )
  # ======================================================================= #
  # Next we will sort them. This can lead to errors, such as wrong
  # encoding, so we have to be mindful of this.
  # ======================================================================= #
  files = ::Cyberweb.sort_this_array_by_time(files).reverse # Reverse-sort them by time, always.
  consider_appending_the_jquery_string
  addnl('<div style="color: lightgreen; margin: 8px">')
  possible_name_for_the_html_file_below = ''.dup
  possible_results = Dir['../*.html']
  unless possible_results.empty?
    possible_name_for_the_html_file_below = possible_results.first.to_s
  end
  # ======================================================================= #
  # Next add the top-title of the page:
  # ======================================================================= #
  addnl(
    HtmlTags.h1( # The hyperlink was added in February 2024.
      '<a href="'+possible_name_for_the_html_file_below+'">
      <img src="'+REMOTE_PATH_TO_THE_PDF_LOGO+'" style="margin-right: 0.35em">
      </a>'+
      title_to_use?+' ('+files.size.to_s+' in total) - <span class="smaller lightblue">'+
      return_the_pretty_name_of_the_webpage+'</span>',
      'lightgreen pad12px BG_Black rounded_border'
    )
  )
  addnl('</div>')
  # ======================================================================= #
  # First, check if we want to use a more complex HTML Table layout:
  # ======================================================================= #
  if use_HTML_table_layout?
    _ = ''.dup
    #_ << '<script src="https://www.kryogenix.org/code/browser/sorttable/sorttable.js"></script>'
    _ << '<table class="sortable" id="table_that_can_be_sorted" style="border-radius: 14px; '\
         'border: 4px dotted royalblue; margin: 0.25em; padding: 0.3em; font-size: 1.2em;">'.dup
    _ << "\n"+
         '<tr style="font-size: 1.5em">'\
         '<th>Index</th>'\
         '<th>File size</th>'\
         '<th>n pages</th>'\
         '<th>Date</th>'\
         '</tr>'+"\n"
    files.each_with_index {|this_file, index| index += 1
      date = Cyberweb.extract_date(this_file)
      # target_to_use = TARGET_TO_INGRID+
      #                File.basename(this_file)
      target_to_use = File.basename(this_file)
      file_size = (File.size(this_file) / 1024).to_s if File.file?(this_file)
      _ << '<tr><td style="background-color: black; padding: 4px">'+
           gold(' # '+index.to_s)+'</td>'\
           '<td style="background-color: black; padding: 4px">'+             # Index
           paleturquoise(' file size: '+file_size.to_s+' kb')+ # file size
           '</td>'+
           '<td style="background-color: black; padding: 4px">'+colour_for_pdf_files(
           ' n pages: '+PdfParadise.n_pages?(this_file).to_s)+
           '</td>'+                                         # n pages
           '<td style="font-weight: bold; background-color: black; padding: 4px; color: lightgreen">'+
           date+       # Date
           '</td>'+
           '</tr>'+
           "\n"
      shortened_description = File.basename(target_to_use)
      if shortened_description.include? '\\'
        shortened_description = shortened_description.split('\\').last
      end
      shortened_description = shortened_description.
                              delete_suffix('.pdf').
                              tr('_',' ')
      # =================================================================== #
      # Shorten the content if it exceeds a certain threshold:
      # =================================================================== #
      threshold_to_use = 92
      if shortened_description.size > threshold_to_use
        shortened_description = shortened_description[0 .. threshold_to_use]+' [...]'
      end
      # =================================================================== #
      # Add the <href> tag next:
      # =================================================================== #
      _ << '<tr><td colspan="4">'+HtmlTags.a(
          target_to_use,
          css_style: USE_THIS_CSS_STYLE_FOR_THE_HREF_LINK,
          description: RIGHT_ARROW+' '+
                       shortened_description
        )+'</td></tr>'+"\n"
    }
    _ << '</table><br><br>'
    addnl(_) # And finally add it here.
  else
    files.each_with_index {|this_file, index| index += 1
      puts ::Colours.rev+'Going through '+sfile(this_file)+
           ::Colours.rev+' (index: '+index.to_s+')'
      target_to_use = TARGET_TO_INGRID+
                      File.basename(this_file)
      if return_pwd.include?('/home/x/data/')
        target_to_use = this_file
      end
      # Add the HTML-link next:
      addnl(
        HtmlTags.a(
          target_to_use,
          css_style: 'font-size: 1.1em; color: brown; font-weight: bold;'
        )
      )
      addnl('<br>')
      # ===================================================================== #
      # Next we can determine the title of the .pdf file at hand.
      # ===================================================================== #
      if Object.const_defined? :PdfParadise
        _title_of_the_pdf_file = PdfParadise.return_title_of_this_pdf_file(this_file).to_s
      else
        _title_of_the_pdf_file = 'title is unknown'
      end
      # ===================================================================== #
      # Next the content in green colour, aka olivedrab, specifically:
      # ===================================================================== #
      basename = File.basename(this_file)
      if return_pwd.include?('/home/x/data/personal/roebe/data/gehalt/')
        basename = this_file.delete_suffix(
          '/home/x/data/personal/roebe/data/gehalt/'
        )
      end
      file_size = (File.size(this_file) / 1024).to_s if File.file?(this_file)
      # ===================================================================== #
      # Build up the "floating"-div next; it actually is no longer floating.
      # ===================================================================== #
      floating_div = '<div class="NO_BLOCK" style="margin-left: 14px; border-radius: 12px; padding: 8px; background-color: black;">'+
                       gold(' # '+index.to_s)+
                       mediumseagreen(
                         ' file size: '+file_size+' kb'
                       )+'; '+
                       colour_for_pdf_files(
                         ' n pages: '+PdfParadise.n_pages?(this_file).to_s
                       )+
                       '</div>'
      addnl(
        '<br>'+ # Next the hyperlink.
        HtmlTags.a(
          basename,
          description: "#{RIGHT_ARROW} "+
                       File.basename(this_file).delete_suffix('.pdf').
                       tr('_',' ')+
                       floating_div,
          css_class: 'BOLD',
          css_style: 'font-size: 1.1em;
                      margin-left: 0.8em;
                      font-weight: bold;
                      color: darkblue;',
          open_new_tab: true
        )
      )
      addnl('<br><br>')
    }
  end
  if add_hyperlinks_to_subdirectories?
    # ===================================================================== #
    # Yes, we need .html files here. As of December 2023 we will also
    # count the .pdf files in that subdirectory.
    # ===================================================================== #
    add_the_footer_with_hyperlinks(
      '.html',
      :default,
      :count_the_pdf_files
    )
  end
  do_store_the_content
end

#title_to_use?Boolean

#

title_to_use?

#

Returns:

  • (Boolean)


96
97
98
# File 'lib/cyberweb/utility_scripts/pdf_to_html/pdf_to_html.rb', line 96

def title_to_use?
  @title_to_use
end