Class: BlueFeather::CUI
- Inherits:
-
Object
- Object
- BlueFeather::CUI
- 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 =
<<-EOS bluefeather - Extended Markdown Converter Usage: bluefeather [options] file1 [file2 file3 ..] Options: -e, --encoding NAME parse input files as encoding of NAME. (s[hift-jis] / e[uc-jp] / u[tf-8] / a[scii] default: 'utf-8') -f, --format TYPE specify format. (t[ext] => text mode d[ocument] => document mode) --force write even if target files have not changed. (default: only if target files have changed) -h, --help show this help. -o, --output DIR output files to DIR. (default: same as input file) -q, --quiet no output to stderr. --suffix .SUF specify suffix of output files. (default: '.html') -v, --verbose verbose mode - output detail of operation. --version show BlueFeather version. Advanced Usage: * If specify files only '-', bluefeather read from stdin and write to stdout. Example: bluefeather *.bftext *.bfdoc bluefeather -v --sufix .xhtml -o ../ sample.markdown bluefeather - More info: see <http://ruby.morphball.net/bluefeather/> EOS
Constants included from FormatType
FormatType::DOCUMENT, FormatType::TEXT
Instance Attribute Summary collapse
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(stdout = $stdout, stderr = $stderr, stdin = $stdin) ⇒ CUI
constructor
A new instance of CUI.
- #run(argv) ⇒ Object
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
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
69 70 71 |
# File 'lib/bluefeather/cui.rb', line 69 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
69 70 71 |
# File 'lib/bluefeather/cui.rb', line 69 def stdin @stdin end |
#stdout ⇒ Object (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' = @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'){ = 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 .puts "ERROR: please text file paths, patterns, or '-' (stdin-mode).\nEx) bluefeather *.bfdoc" return false end .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 .puts "bluefeather: stdin -> stdout mode." .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 .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 .puts "#{src} skipped. (suffix = #{suffix})" if verbose elsif not force and dest.exist? and (dest.mtime > src.mtime) then .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) } .puts "#{src} => #{dest} (#{File.size(dest)} byte)" if verbose then .puts " Format: #{current_format}" .puts " Encoding: #{this_encoding}" .puts sprintf(' Parsing Time: %g sec', parsing_sec) .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 |