#!/usr/bin/env ruby require 'fileutils' include FileUtils::Verbose module BSDGroup PDF2HTML="/usr/local/bin/pdftohtml -p -c -noframes" class Flyer attr_writer :slash, :maxdeep, :dotfiles, :docsuffix, :langindex def initialize dir = nil @dir = dir.nil? ? Dir.pwd : dir @slash = '/' @maxdeep = 4 @dotfiles = false @tree = nil @docsuffix = %w( org pdf html ) @langindex = { 'de' => 'deutsch', 'en' => 'english', 'fr' => 'francais', 'nl' => 'xxx', 'es' => 'xxx' } end def get_tree unless @tree @tree = { :path => @dir, :item => File.basename( @dir ), :deep => 0 } get_tree_items( @tree ) end @tree[:tree] end def pdfs2html return self (tree = get_tree).keys.each do |cat| cat = tree[cat] pdfs = false pdfs = 'PDF' if cat[:tree].key? 'PDF' pdfs = 'pdf' if cat[:tree].key? 'pdf' next if ! pdfs pdfs = cat[:tree][pdfs][:tree] html_path = cat[:path] + @slash + 'HTML' Dir.mkdir( html_path ) if ! File.directory?( html_path ) `rm -vrf #{html_path}#{@slash}* > /dev/null 2>&1` pdfs.each_value do |lang| next if lang[:file?] html_lang_path = html_path + @slash + lang[:item] Dir.mkdir(html_lang_path) lang[:tree].each_value do |pdf| pdf_tmp = html_lang_path + @slash + pdf[:item] puts `cp -v '#{pdf[:path]}' '#{pdf_tmp}'` puts `#{PDF2HTML} '#{pdf_tmp}'` `rm '#{pdf_tmp}'` end end end @tree = nil self end def tree2html target = nil, head = nil, foot = nil, replace = nil target = @dir + @slash + 'index.html' unless target replace = {} unless replace html = File.new( target, File::CREAT|File::TRUNC|File::RDWR, 0644 ) File.new( head ).each do |line| replace.each_pair do |k, v| line.gsub!(/#{k}/, v) end html << line end if head tree = get_tree docs = {} tree.keys.each do |cat| tree[cat][:tree].keys.each do |type| next if tree[cat][:tree][type][:file?] tree[cat][:tree][type][:tree].keys.each do |lang| next if tree[cat][:tree][type][:tree][lang][:file?] tree[cat][:tree][type][:tree][lang][:tree].keys.each do |doc| doc = tree[cat][:tree][type][:tree][lang][:tree][doc] next if ! doc[:file?] key = doc[:item].gsub( /\s+/, '-' ).downcase @langindex.each_pair do |k, v| key.gsub!( /#{v}/, k ) key.gsub!( /-#{k}/, '' ) key.sub!( /\.(#{@docsuffix.join('|')})$/, '' ) end docs[cat] = {} unless docs[cat] docs[cat][key] = {} unless docs[cat][key] docs[cat][key][type.downcase] = {} unless docs[cat][key][type.downcase] docs[cat][key][type.downcase][lang.downcase] = doc end end end end tree.keys.sort do |a, b| a.downcase <=> b.downcase end.each do |cat| next if docs[cat].nil? html << "

#{tree[cat][:item]}

\n\n" end File.new( foot ).each do |line| replace.each_pair do |k, v| line.gsub!(/#{k}/, v) end html << line end if foot html.close end private def get_tree_items item item[:tree] = {} Dir.entries( item[:path] ).each do |file| newitem = { :path => item[:path] + @slash + file, :item => file, :deep => item[:deep] + 1, } newitem[:file?] = File.file?( newitem[:path] ) # ignore dotfiles? next if file[0] == 46 && ! @dotfiles # ignore files under deep 2 next if newitem[:file?] && newitem[:deep] < 2 # ignore no docfiles next if newitem[:file?] && ! @docsuffix.include?( File.extname(newitem[:path])[1..-1].downcase ) get_tree_items( newitem ) if newitem[:deep] < @maxdeep && ! newitem[:file?] item[:tree][file] = newitem end end end end # Workdirectory ist this script-directory Dir.chdir( File.dirname( __FILE__ ) ) # create html from pdfs and create htmllist BSDGroup::Flyer.new.pdfs2html.tree2html( 'list.html', '/var/www/bsdgroup.de/bsdgroup_layout/head.tpl', '/var/www/bsdgroup.de/bsdgroup_layout/foot.tpl' ) #EOF