<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rails Jan</title>
	<atom:link href="http://rails.yo2.cn/feed" rel="self" type="application/rss+xml" />
	<link>http://rails.yo2.cn</link>
	<description></description>
	<lastBuildDate>Thu, 06 Dec 2007 07:14:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Add a format command to e-texteditor</title>
		<link>http://rails.yo2.cn/articles/add-a-format-command-to-e-texteditor.html</link>
		<comments>http://rails.yo2.cn/articles/add-a-format-command-to-e-texteditor.html#comments</comments>
		<pubDate>Thu, 06 Dec 2007 07:08:19 +0000</pubDate>
		<dc:creator></dc:creator>
		
		<guid isPermaLink="false">http://rails.yo2.cn/articles/add-a-format-command-to-e-texteditor.html</guid>
		<description><![CDATA[1. Add a new command "format" under ruby bundle 2. Set the input to "Selected text" or "Document" 3. fill scope selector with source.ruby 4. fill this script into the center text area: #!/usr/bin/env ruby # Ruby beautifier, version 2.1, &#8230; <a href="http://rails.yo2.cn/articles/add-a-format-command-to-e-texteditor.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>1. Add a new command "format" under ruby bundle<br>
2. Set the input to "Selected text" or "Document"<br>
3. fill scope selector with source.ruby<br>
4. fill this script into the center text area:</p>
<blockquote>
<p>#!/usr/bin/env ruby</p>
<p># Ruby beautifier, version 2.1, 09/11/2006<br>
# Copyright (c) 2006, P. Lutus<br>
# Released under the GPL</p>
<p># Modificated to work as a Textmate command</p>
<p>$tabSize = 2<br>
$tabStr = " "</p>
<p># indent regexp tests</p>
<p>$indentExp = [<br>
/^module\b/,<br>
/^if\b/,<br>
/(=\s*|^)until\b/,<br>
/(=\s*|^)for\b/,<br>
/^unless\b/,<br>
/(=\s*|^)while\b/,<br>
/(=\s*|^)begin\b/,<br>
/(^| )case\b/,<br>
/\bthen\b/,<br>
/^class\b/,<br>
/^rescue\b/,<br>
/^def\b/,<br>
/\bdo\b/,<br>
/^else\b/,<br>
/^elsif\b/,<br>
/^ensure\b/,<br>
/\bwhen\b/,<br>
/\{[^\}]*$/,<br>
/\[[^\]]*$/<br>
]</p>
<p># outdent regexp tests</p>
<p>$outdentExp = [<br>
/^rescue\b/,<br>
/^ensure\b/,<br>
/^elsif\b/,<br>
/^end\b/,<br>
/^else\b/,<br>
/\bwhen\b/,<br>
/^[^\{]*\}/,<br>
/^[^\[]*\]/<br>
]</p>
<p>def makeTab(tab)<br>
return (tab &lt; 0) ? "" : $tabStr * $tabSize * tab end def addLine(line,tab) line.strip! line = makeTab(tab)+line if line.length &gt; 0<br>
return line + "\n"<br>
end</p>
<p>def beautifyRuby<br>
commentBlock = false<br>
programEnd = false<br>
multiLineArray = Array.new<br>
multiLineStr = ""<br>
tab = 0<br>
source = STDIN.read<br>
dest = ""<br>
source.split("\n").each do |line|<br>
if(!programEnd)<br>
# detect program end mark<br>
if(line =~ /^__END__$/)<br>
programEnd = true<br>
else<br>
# combine continuing lines<br>
if(!(line =~ /^\s*#/) &amp;&amp; line =~ /[^\\]\\\s*$/)<br>
multiLineArray.push line<br>
multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")<br>
next<br>
end</p>
<p># add final line<br>
if(multiLineStr.length &gt; 0)<br>
multiLineArray.push line<br>
multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")<br>
end</p>
<p>tline = ((multiLineStr.length &gt; 0)?multiLineStr:line).strip<br>
if(tline =~ /^=begin/)<br>
commentBlock = true<br>
end<br>
end<br>
end<br>
if(commentBlock || programEnd)<br>
# add the line unchanged<br>
dest += line + "\n"<br>
else<br>
commentLine = (tline =~ /^#/)<br>
if(!commentLine)<br>
# throw out sequences that will<br>
# only sow confusion<br>
while tline.gsub!(/'.*?'/,"")<br>
end<br>
while tline.gsub!(/".*?"/,"")<br>
end<br>
while tline.gsub!(/\`.*?\`/,"")<br>
end<br>
while tline.gsub!(/\{[^\{]*?\}/,"")<br>
end<br>
while tline.gsub!(/\([^\(]*?\)/,"")<br>
end<br>
while tline.gsub!(/\/.*?\//,"")<br>
end<br>
while tline.gsub!(/%r(.).*?\1/,"")<br>
end<br>
tline.gsub!(/\\\"/,"'")<br>
$outdentExp.each do |re|<br>
if(tline =~ re)<br>
tab -= 1<br>
break<br>
end<br>
end<br>
end<br>
if (multiLineArray.length &gt; 0)<br>
multiLineArray.each do |ml|<br>
dest += addLine(ml,tab)<br>
end<br>
multiLineArray.clear<br>
multiLineStr = ""<br>
else<br>
dest += addLine(line,tab)<br>
end<br>
if(!commentLine)<br>
$indentExp.each do |re|<br>
if(tline =~ re &amp;&amp; !(tline =~ /\s+end\s*$/))<br>
tab += 1<br>
break<br>
end<br>
end<br>
end<br>
end<br>
if(tline =~ /^=end/)<br>
commentBlock = false<br>
end<br>
end<br>
STDOUT.write(dest)<br>
# uncomment this to complain about mismatched blocks<br>
# if(tab != 0)<br>
# STDERR.puts "#{path}: Indentation error: #{tab}"<br>
# end<br>
end</p>
<p>beautifyRuby</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://rails.yo2.cn/articles/add-a-format-command-to-e-texteditor.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

