Class: BlueFeather::CUI

Inherits:
Object
  • Object
show all
Includes:
FormatType
Defined in:
lib/bluefeather/cui.rb

Defined Under Namespace

Modules: FormatType

Constant Summary collapse

FORMAT_TYPE_TABLE =
{
  'document' => DOCUMENT,
  'bfdoc' => DOCUMENT,
  'text' => TEXT,
  'bftext' => TEXT,
}
ENCODING_LIST =
%w(shift-jis euc-jp utf-8 ascii)
HELP =
"bluefeather - Extended Markdown Converter\n\nUsage: bluefeather [options] file1 [file2 file3 ..]\n\nOptions:\n-e, --encoding NAME   parse input files as encoding of NAME.\n                      (s[hift-jis] / e[uc-jp] / u[tf-8] / a[scii]\n                       default: 'utf-8')\n-f, --format TYPE     specify format.\n                      (t[ext]      => text mode\n                       d[ocument]  => document mode)\n    --force           write even if target files have not changed.\n                      (default: only if target files have changed)\n-h, --help            show this help.\n-o, --output DIR      output files to DIR. (default: same as input file)\n-q, --quiet           no output to stderr.\n    --suffix .SUF     specify suffix of output files. (default: '.html')\n-v, --verbose         verbose mode - output detail of operation.\n    --version         show BlueFeather version.\n\nAdvanced Usage:\n* If specify files only '-', bluefeather read from stdin and write to stdout.\n\n\nExample:\nbluefeather *.bftext *.bfdoc\nbluefeather -v --sufix .xhtml -o ../ sample.markdown\nbluefeather -\n\nMore info:\nsee <http://ruby.morphball.net/bluefeather/>\n"

Constants included from FormatType

FormatType::DOCUMENT, FormatType::TEXT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout = $stdout, stderr = $stderr, stdin = $stdin) ⇒ CUI

Returns a new instance of CUI.



71
72
73
# File 'lib/bluefeather/cui.rb', line 71

def initialize(stdout = $stdout, stderr = $stderr, stdin = $stdin)
  @stdout, @stderr, @stdin = stdout, stderr, stdin
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



69
70
71
# File 'lib/bluefeather/cui.rb', line 69

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



69
70
71
# File 'lib/bluefeather/cui.rb', line 69

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



69
70
71
# File 'lib/bluefeather/cui.rb', line 69

def stdout
  @stdout
end

Class Method Details

.run(*args) ⇒ Object



63
64
65
# File 'lib/bluefeather/cui.rb', line 63

def self.run(*args)
  self.new.run(*args)
end

Instance Method Details

#run(argv) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/bluefeather/cui.rb', line 75

def run(argv)
  op = OptionParser.new
  
  verbose = false
  quiet = false
  force = false
  suffix = '.html'
  format = nil
  output_dir_path = nil
  encoding = 'utf-8'
  
  
  message_out = @stderr
  
  
  op.on('-e', '--encoding NAME', ENCODING_LIST){|x| encoding = x}
  op.on('-f', '--format TYPE', FORMAT_TYPE_TABLE){|x| format = x}
  op.on('--force'){|x| force = true}
  op.on('-v', '--verbose'){ verbose = true }
  op.on('-o', '--output DIR', String){|x| output_dir_path = Pathname.new(x)}
  op.on('-q', '--quiet'){ message_out = StringIO.new }
  op.on('--suffix .SUF', String){|x| suffix = x}
  op.on('--version'){
    @stdout.puts "bluefeather #{VERSION_LABEL}"
    return false
  }
  op.on('-h', '--help'){
    @stdout.puts HELP
    return false
  }
  
  args = op.parse(argv)
  
  if args.empty? then
    message_out.puts "ERROR: please text file paths, patterns, or '-' (stdin-mode).\nEx) bluefeather *.bfdoc"
    return false
  end
  
  message_out.puts "default encoding: #{encoding}" if verbose
  
  unless defined?(Encoding) then
    # ruby 1.8 or earlier

    original_kcode = $KCODE
    $KCODE = EncodingType.type_to_kcode(encoding)
  end
  
  begin
    if args == ['-'] then
      if verbose then
        message_out.puts "bluefeather: stdin -> stdout mode." 
        message_out.puts "----"
      end
      src = @stdin.read
      if defined?(Encoding) then
        # ruby 1.9 or later

        src.force_encoding(encoding)
      end
      
      # default: text

      if format == DOCUMENT then
        @stdout.write(BlueFeather.parse_document(src, encoding))
      else
        @stdout.write(BlueFeather.parse_text(src))
      end
    else
      targets = []
      
      args.each do |pattern|
        targets.concat(Pathname.glob(pattern.gsub('\\', '/')))
      end
      
      if targets.empty? then
        message_out.puts "ERROR: targets not found.\n(patterns: #{args.join(' ')})"
        return false
      end
      
      targets.each do |src|
        ext = src.extname
        
        if output_dir_path then
          filename = src.basename.to_s.sub(/#{Regexp.escape(ext)}$/, suffix)
          dest = (output_dir_path + filename).cleanpath
        else
          dest = Pathname.new(src.to_s.sub(/#{Regexp.escape(ext)}$/, suffix)).cleanpath
        end
        
        html = nil
        current_format = format
        
        if ext == suffix then
          message_out.puts "#{src} skipped. (suffix = #{suffix})" if verbose
        elsif not force and dest.exist? and (dest.mtime > src.mtime) then
          message_out.puts "#{src} skipped. (not changed)" if verbose
        else
          # judge by extname if format is not specified

          unless current_format then
            case ext
            when '.bfdoc', '.md'
              current_format = DOCUMENT
            else
              current_format = TEXT
            end
          end
          
          # parse

          parser = BlueFeather::Parser.new
          this_encoding = nil
          parsing_sec = Benchmark.realtime{
            case current_format
            when DOCUMENT
              doc = nil
              open(src, 'r'){|f|
                doc = BlueFeather::Document.parse_io(f, encoding)
              }
              html = parser.document_to_html(doc)
              this_encoding = doc.encoding_type
            when TEXT
              open_mode = (defined?(Encoding) ? "r:#{encoding}" : 'r')
              text = src.open(open_mode){|x| x.read}
              html = parser.parse_text(text)
              this_encoding = encoding
            end
          }
        
          if html then
            open(dest, 'w'){|f|
              f.write(html)
            }
            message_out.puts "#{src} => #{dest} (#{File.size(dest)} byte)"
        
            if verbose then
              message_out.puts "    Format: #{current_format}"
              message_out.puts "    Encoding: #{this_encoding}"
              message_out.puts sprintf('    Parsing Time: %g sec', parsing_sec)
              message_out.puts
            end
          end # if html

        end # if ext == suffix

        
      end # targets.each

    end # if stdin-mode

    
  ensure
    unless defined?(Encoding) then
      # recover original $KCODE

      $KCODE = original_kcode
    end
  
  end # begin

  
  
  return true
end