From: Aleksi Date: 2005-06-24T02:20:37+09:00 Subject: Re: MiniQuiz : Renesting Nodes (OWLScratch) Gavin Kistner wrote: > In OWLScratch, the following represents a nested list: > > * List item 1 > * List item 2 > * List item 2.1 > * List item 2.2 > * List item 3 > * List item 3.1 > * List item 3.1.1 > > Right now, that tokenizes into one node per line: > List item 1 > List item 2 > List item 2.1 > List item 2.2 > List item 3 > List item 3.1 > List item 3.1.1 > > The challenge is that I need to be able to spin through the list and > (should be possible in one pass) properly nest those as the HTML requires: How about something like: bullets = [[1, "List Item 1."], [1, "List Item 2."], [2, "List Item 2.1"], [2, "List Item 2.2"], [1, "List Item 3."], [2, "List Item 3.1"], [3, "List Item 3.1.1"] ] def indent(level, text) puts " "*level + text end current_level = 0 bullets.each do |level, text| indent((current_level+=1)-1, "") while level < current_level indent( current_level, "
  • #{text}
  • ") end indent(current_level-=1, "") while 0 < current_level Now, I'm sure you don't want the output in text, so perhaps you like to replace indent(level, "text") with insert(""), but the structure might not be that different. - Aleksi