Module: Transmuter::CLI::Thor::InstanceMethods

Defined in:
lib/transmuter/cli/thor.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/transmuter/cli/thor.rb', line 13

def self.included(base)
  base.class_eval <<-END, __FILE__, __LINE__ + 1
    desc "Transmute one file format into another."

    class_option :input_format,
      type: :string,
      required: false,
      aliases: "-f",
      desc: "The input format."

    class_option :output_format,
      type: :string,
      required: false,
      aliases: "-t",
      default: "pdf",
      desc: "The output format."

    class_option :stylesheets,
      type: :array,
      required: false,
      aliases: "-s",
      default: [DEFAULT_THEME],
      desc: "The stylesheets."

    argument :input,
      type: :string,
      required: true,
      aliases: "-i",
      desc: "The input file name."

    argument :output,
      type: :string,
      required: false,
      aliases: "-o",
      desc: "The output file name."

    def set_input_filename
      @input_filename = input
    end

    def set_input_fileformat
      @input_fileformat = options[:input_format] || input_format
    end

    def set_output_fileformat
      @output_fileformat = options[:output_format]
    end

    def set_output_filename
      if output.blank? && options[:output_format].blank?
        raise ArgumentError, "Either output or output_format should be given,"
      end

      @output_filename = output || output_file
    end

    def set_stylesheets
      @stylesheets = options[:stylesheets]
    end

    def transmute_input_to_output
      transmute!
    end

    protected

      def input_format
        case @input.split('.').last
        when /^(md|markdown)$/
          "markdown"
        when /^textile/
          "textile"
        when /^(html|htm)/
          "html"
        else
          raise ArgumentError, "No format was given and format could not be parsed from the file name"
        end
      end

      def output_file
        output = @input_filename.dup
        output.gsub(/^(.+)\\.[^.]*$/, "\\\\1.\#{@output_fileformat}")
      end
  END
end