Dd9c3fd0786a478b7cbf33eff5c841e4

I'd like to take a bunch of textile and turn it into an s5 file.
I was trying to use rubular to isolate blocks of text, but couldn't get just the seperate blocks isolated... Went to looping and ended up with this monstrosity.
There has to be a better way.

example regex trial over at http://www.rubular.com/regexes/4983
closer, but eliminate any more special characters and you get chaos
http://www.rubular.com/regexes/4986

Remember - the point is to output s5, not just redcloth html. each "h1." block needs to be surrounded by a <div class="slide">redcloth html</div> Then we can drop our text into an s5 html file and voila!! http://meyerweb.com/eric/tools/s5/primer.html

/* sample input
h1. Introduction
* s5 is way cool, but I think in textile
* textile is easy to write
* it would be great to take a bunch of textile and transform it instantly into an s5 presentation

h1. Regular Expressions 
* they are difficult for multiple lines
* http://www.rubular.com/ helps, but I can't narrow it down to the simple bits.
* let's go shopping or use loops

h1. Looping 
* redcloth does the hard work
* you just need to surround the redcloth output with <div class=slide> </div>
* this code smells so bad

h1. You
* please make sense of this

*/

def convert_from_textile_to_s5(fin)
	slide = ""
	File.open(fin) do | file_in|
		lines = file_in.readlines
		i=0
		while i < lines.length
			if lines[i][0..2] == "h1."
				puts '<div class="slide">'
				puts RedCloth.new(lines[i]).to_html
				while i+1 < lines.length && lines[i+1][0,1]=='*'
					slide += lines[i+1]
					i+=1
				end
				puts RedCloth.new(slide).to_html
				slide = ""
				puts '</div>'
			end
			i+=1
		end
	end
end

Refactorings

No refactoring yet !

D7a31f22c11776898826f7c1ed0ff80a

mischamolhoek.myopenid.com

January 13, 2009, January 13, 2009 09:41, permalink

1 rating. Login to rate!

i think this should do it...

def convert_from_textile_to_s5(fin)
    File.read('fin').scan(/h1\..[^\n]*\n(?:\*[^\n]*\n)*/m).map{|s| RedCloth.new(s).to_html}
end
Dd9c3fd0786a478b7cbf33eff5c841e4

MattK

February 13, 2009, February 13, 2009 14:42, permalink

No rating. Login to rate!

Looks like Pandoc is a solution for the problem, but I'd still like to know how to efficiently do this in Ruby... http://johnmacfarlane.net/pandoc/#downloads

Your refactoring





Format Copy from initial code

or Cancel