Aleksi
6/23/2005 5:19:00 PM
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:
> <bullet level="1">List item 1</bullet>
> <bullet level="1">List item 2</bullet>
> <bullet level="2">List item 2.1</bullet>
> <bullet level="2">List item 2.2</bullet>
> <bullet level="1">List item 3</bullet>
> <bullet level="2">List item 3.1</bullet>
> <bullet level="3">List item 3.1.1</bullet>
>
> 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, "<ul>" ) while level > current_level
indent( current_level-=1, "</ul>") while level < current_level
indent( current_level, "<li>#{text}</li>")
end
indent(current_level-=1, "</ul>") 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("<node>"), but the structure
might not be that different.
- Aleksi