#!/usr/bin/env ruby

require 'maruku'

	# If we are given filenames, convert each file 
	if not ARGV.empty?
		ARGV.each do |f|
			puts "Opening #{f}"
	
			# read file content
			input =  File.open(f,'r').read
		
			# create Maruku
			doc = Maruku.new(input, {:on_error=>:warning})
			# convert to a complete html document
			output = doc.to_md
	
			# write to file
			dir = File.dirname(f)
			filename = File.basename(f, File.extname(f)) + ".txt"

			output = File.join(dir, filename)	
			File.open(output,'w') do |f| f.puts html end
		end
	else
		# else, act as a filter
		data = $stdin.read
		puts Maruku.new(data, {:on_error=>:warning}).to_md
	end