tshm
1/31/2014 - 8:29 AM

io_sample.rb

#!/usr/bin/ruby

org = STDIN.readlines

arr=[]
open("|reformime -i", "r+") do |io|
  io << org
	io.close_write
	buf = io.read
	buf.gsub(/section: ([^\s]+).*?content-type: ([^\s]+)/m) {|x| arr<<[$1,$2]}
end

arr.each do |x,y|
  next if x=="1"
	case y
	when "text/plain"
	  open("|reformime -e -s #{x}", "r+") do |io|
		  io << org
			io.close_write
			print io.read
		end
	when "application/msword"
	  open
	end
end


#cat org.txt | reformime > secs.txt
#cat org.txt | reformime -i | sed -ne 's/content-type: //p' > type.txt
#
#cat org.txt | sed -ne '0,/^$/{p}'
#
#paste secs.txt type.txt |\
#while read i; do
#  sec=`echo $i | cut -d' ' -f1;`
#  typ=`echo $i | cut -d' ' -f2;`
#	case $typ in
#	("text/plain") 
#	  reformime -e -s $sec < org.txt
#		;;
#	("application/msword")
#	  reformime -e -s $sec < org.txt > tmp.doc
#		wvHtml tmp.doc tmp.html
#		w3m -dump tmp.html >> out.txt
#		;;
#	esac
#	echo "------- MIME splitter ----------"
#done
#rm tmp.* out.txt org.txt secs.txt type.txt