Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # vim: set ts=2 sw=2 et:
  3.  
  4. require 'find'
  5. require 'colorize'
  6. require 'fileutils'
  7.  
  8. class Merger
  9. def initialize
  10. @ind = 0
  11. end
  12.  
  13. def merge_pdfs where
  14. pdf_basename = File.basename(File.expand_path(where))
  15. pdf_path = File.join(File.dirname(where), pdf_basename + '.pdf')
  16. outpdf_path = output_of pdf_path
  17. outpdf_dir = File.dirname(outpdf_path)
  18. FileUtils.mkdir_p outpdf_dir
  19. FileUtils.mkdir_p (output_of where)
  20.  
  21. sub_pdfs = []
  22. Dir.chdir(where) do
  23. sub_pdfs.concat Dir['*.pdf']
  24. end
  25. Dir.chdir(output_of where) do
  26. sub_pdfs.concat Dir['*.pdf']
  27. end
  28. sub_pdfs.sort!
  29.  
  30. puts "#{indent} #{where.yellow} merge PDF"
  31. sub_pdfs.each do |file|
  32. puts "#{indent} #{where.yellow} src = %s" % file
  33. end
  34. if sub_pdfs.size == 0 then
  35. puts "#{indent} #{where.yellow}" + " no PDF found in subtree !".red + " skipping merge."
  36. else
  37. pdfs = sub_pdfs.map do |file|
  38. if File.exist? File.join(where, file) then File.join(where, file)
  39. else File.join(output_of(where), file)
  40. end
  41. end
  42. puts "#{indent} #{pdf_path.green} merged"
  43. system "pdftk", *pdfs, "cat", "output", outpdf_path
  44. end
  45. end
  46.  
  47. def recurse where
  48. puts "#{indent} #{where.yellow} begin recursing_dir"
  49. indent_more
  50. Find.find(where) do |path|
  51. next unless FileTest.directory?(path)
  52. next if File.basename(path) == "_build"
  53. next if path == where
  54. next if path == "."
  55. recurse path
  56. Find.prune
  57. end
  58. indent_less
  59. puts "#{indent} #{where.yellow} end recursing_dir"
  60. puts "#{indent} #{where.yellow} begin merging_pdfs"
  61. merge_pdfs where
  62. puts "#{indent} #{where.yellow} end merging_pdfs"
  63. end
  64.  
  65. def run where
  66. FileUtils.rm_rf "_build"
  67. recurse where
  68. end
  69.  
  70. private
  71.  
  72. def output_of where
  73. File.join('_build', where)
  74. end
  75.  
  76. def indent
  77. (" " * @ind)
  78. end
  79. def indent_more
  80. @ind += 1
  81. end
  82.  
  83. def indent_less
  84. @ind -= 1
  85. end
  86.  
  87. end
  88.  
  89. app = Merger.new
  90. if ARGV.size > 0 and File.directory? ARGV[0] then
  91. app.run ARGV[0]
  92. else
  93. puts "Usage: #{$0} directory"
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement