老话题,但......
看起来我找到了一种方法来使Glenn Jackman代码html有效(避免
- 与孩子
- )。
我正在使用带缩进缩进的字符串。
require 'haml'
class String
def text2htmllist
tabs = -1
topUL=true
addme=''
haml = self.gsub(/^([t]*)/) do |match|
line_tabs = match.length
if ( line_tabs > tabs )
if topUL
repl = "#{match}#{addme}%uln"
topUL=false
else
repl = "#{match}#{addme}%lin"
addme += "t"
repl += "#{match}#{addme}%uln"
end
else
repl = ''
addme = addme.gsub(/^[t]/,'') if ( line_tabs < tabs ) #remove one t
end
tabs = line_tabs
repl << "t#{match}#{addme}%li "
end
puts haml
Haml::Engine.new(haml).render
end
end #String class
str = <
I am the first top-level list item
I am his son
Me too
Second one here
His son
His daughter
I am the son of the one above
Me too because of the indentation
Another one
FIM
puts str.text2htmllist
生产:
%ul
%li I am the first top-level list item
%li
%ul
%li I am his son
%li Me too
%li Second one here
%li
%ul
%li His son
%li His daughter
%li
%ul
%li I am the son of the one above
%li Me too because of the indentation
%li Another one
I am the first top-level list item- I am his son
- Me too
- His son
- His daughter
-
- I am the son of the one above
- Me too because of the indentation
- Another one