Sha256: 4852cfde662c5e9cfb86c1f6854719afca0f2da110a6b4b9e2c962a54b604b97

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require "rabbit_viewer/version"
require "rabbit/command/rabbit"
require "tempfile"
require "uri"

module RabbitViewer
  module Command
    class RabbitViewer

      class << self
        def run(*arguments)
          new.run(arguments)
        end
      end

      def initialize
      end

      def run(arguments)
        Tempfile.open(["rabbit_viewer", ".rab"]) do |tempfile|
          tempfile.puts(title)
        
          arguments.each do |viewfile_relative|
            viewfile = File.expand_path(viewfile_relative)
            next unless File.file?(viewfile)

            begin
              URI.parse(viewfile)
            rescue URI::InvalidURIError
              STDERR.puts($!.message)
              STDERR.puts("Sorry, don't support multibyte file name as yet.")
              next
            end

            case viewfile
            when /\.(svg|png|jpe?g|gif|eps|dia|xcf)$/i
              page = image_page(viewfile)
            else
              page = text_page(viewfile)
            end

            unless page
              page = undefind_page(viewfile)
            end

            tempfile.puts(page)
          end
        
          tempfile.flush
          Rabbit::Command::Rabbit.run(tempfile.path)
        end
      end

      private
      def title
        <<-EOT
= RabbitViewer
: date
   version #{::RabbitViewer::VERSION}
        EOT
      end

      def image_page(viewfile)
        <<-EOT
= #{File.basename(viewfile)}
  # image
  # src = #{viewfile}
        EOT
      end

      def text_page(viewfile)
        page = "= #{File.basename(viewfile)}\n"
        File.open(viewfile) do |f|
          f.each_line.to_a[0..9].each_with_index do |line, i|
            return nil unless line.valid_encoding?
            page += "    #{i + 1}: #{line}\n"
          end
        end
        page
      end

      def undefind_page(viewfile)
        page = "= #{File.basename(viewfile)}\n"
        page += "Sorry, this file is don't support format...\n"
        page
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rabbit_viewer-0.1.2 lib/rabbit_viewer/command/rabbit_viewer.rb