lib/hexapdf/cli/command.rb in hexapdf-0.6.0 vs lib/hexapdf/cli/command.rb in hexapdf-0.7.0

- old
+ new

@@ -1,12 +1,12 @@ -# -*- encoding: utf-8 -*- +# -*- encoding: utf-8; frozen_string_literal: true -*- # #-- # This file is part of HexaPDF. # # HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby -# Copyright (C) 2014-2017 Thomas Leitner +# Copyright (C) 2014-2018 Thomas Leitner # # HexaPDF is free software: you can redistribute it and/or modify it # under the terms of the GNU Affero General Public License version 3 as # published by the Free Software Foundation with the addition of the # following permission added to Section 15 as permitted in Section 7(a): @@ -45,12 +45,12 @@ class Command < CmdParse::Command module Extensions #:nodoc: def help_banner #:nodoc: "hexapdf #{HexaPDF::VERSION} - Versatile PDF Manipulation Tool\n" \ - "Copyright (c) 2014-2017 Thomas Leitner; licensed under the AGPLv3\n\n" << - format(usage, indent: 7) << "\n\n" + "Copyright (c) 2014-2017 Thomas Leitner; licensed under the AGPLv3\n\n" \ + "#{format(usage, indent: 7)}\n\n" end end include Extensions @@ -241,21 +241,21 @@ }.freeze # Applies the chosen stream mode to the given object. def optimize_stream(obj) return if @out_options.streams == :preserve || !obj.respond_to?(:set_filter) || - Array(obj[:Filter]).any? {|f| IGNORED_FILTERS[f]} + Array(obj[:Filter]).any? {|f| IGNORED_FILTERS[f] } obj.set_filter(@out_options.streams == :compress ? :FlateDecode : nil) end # Optimize the object if it is a font object. def optimize_font(obj) return unless @out_options.optimize_fonts && obj.kind_of?(HexaPDF::Type::Font) && - (obj[:Subtype] == :TrueType || - (obj[:Subtype] == :Type0 && obj.descendant_font[:Subtype] == :CIDFontType2)) && - obj.embedded? + (obj[:Subtype] == :TrueType || + (obj[:Subtype] == :Type0 && obj.descendant_font[:Subtype] == :CIDFontType2)) && + obj.embedded? font = HexaPDF::Font::TrueType::Font.new(StringIO.new(obj.font_file.stream)) data = HexaPDF::Font::TrueType::Optimizer.build_for_pdf(font) obj.font_file.stream = data obj.font_file[:Length1] = data.size @@ -266,20 +266,20 @@ # See: #define_encryption_options def apply_encryption_options(doc) if @out_options.encryption == :add doc.encrypt(algorithm: @out_options.enc_algorithm, key_length: @out_options.enc_key_length, - force_V4: @out_options.enc_force_v4, + force_v4: @out_options.enc_force_v4, permissions: @out_options.enc_permissions, owner_password: @out_options.enc_owner_pwd, user_password: @out_options.enc_user_pwd) elsif @out_options.encryption == :remove doc.encrypt(name: nil) end end - PAGE_NUMBER_SPEC = "([1-9]\\d*|e)".freeze #:nodoc: + PAGE_NUMBER_SPEC = "([1-9]\\d*|e)" #:nodoc: ROTATE_MAP = {'l' => -90, 'r' => 90, 'd' => 180, 'n' => :none}.freeze #:nodoc: # Parses the pages specification string and returns an array of tuples containing a page # number and a rotation value (either -90, 90, 180, :none or +nil+ where an integer means # adding a rotation by that number of degrees, :none means removing any set rotation value and @@ -293,16 +293,16 @@ case str when /\A#{PAGE_NUMBER_SPEC}(l|r|d|n)?\z/o page_num = ($1 == 'e' ? count : str.to_i) next if page_num > count arr << [page_num - 1, ROTATE_MAP[$2]] - when /\A#{PAGE_NUMBER_SPEC}-#{PAGE_NUMBER_SPEC}(?:\/([1-9]\d*))?(l|r|d|n)?\z/ + when /\A#{PAGE_NUMBER_SPEC}-#{PAGE_NUMBER_SPEC}(?:\/([1-9]\d*))?(l|r|d|n)?\z/o start_nr = ($1 == 'e' ? count : [$1.to_i, count].min) - 1 end_nr = ($2 == 'e' ? count : [$2.to_i, count].min) - 1 step = ($3 ? $3.to_i : 1) * (start_nr > end_nr ? -1 : 1) rotation = ROTATE_MAP[$4] - start_nr.step(to: end_nr, by: step) {|n| arr << [n, rotation]} + start_nr.step(to: end_nr, by: step) {|n| arr << [n, rotation] } else raise OptionParser::InvalidArgument, "invalid page range format: #{str}" end end end @@ -313,19 +313,17 @@ # console. def read_password(prompt = "Password") if $stdin.tty? read_from_console(prompt) else - pwd = $stdin.gets - pwd = read_from_console(prompt) unless pwd - pwd.chomp + ($stdin.gets || read_from_console(prompt)).chomp end end # Removes unused pages and page tree nodes from the document. def remove_unused_pages(doc) - retained = doc.pages.each_with_object({}) {|page, h| h[page.data] = true} + retained = doc.pages.each_with_object({}) {|page, h| h[page.data] = true } retained[doc.pages.root.data] = true doc.each(current: false) do |obj| next unless obj.kind_of?(HexaPDF::Dictionary) if (obj.type == :Pages || obj.type == :Page) && !retained.key?(obj.data) doc.delete(obj) @@ -336,10 +334,10 @@ private # Displays the given prompt, reads from the console without echo and returns the read string. def read_from_console(prompt) IO.console.write("#{prompt}: ") - str = IO.console.noecho {|io| io.gets.chomp} + str = IO.console.noecho {|io| io.gets.chomp } puts str end end