[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

MiniQuiz : Renesting Nodes (OWLScratch

Gavin Kistner

6/23/2005 2:53:00 AM

return unless bored? #MiniQuiz = do my 'work' for me.

I'm writing a library (OWLScratch) for converting wiki markup into
HTML. (The OWL part of the name is because it's derived from the
markup used by OpenWiki, hence "OpenWiki Language"; the Scratch is
because it's my own half-implemented flavor.)

It's not nearly ready yet, but the core concepts are working. So far,
it tokenizes the document into a series of hierarchically-nested
nodes. If I wanted XML, I could make up my own schema and be done.
But I want HTML.

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:

<ul> <-- new node!
<li>List item 1</li>
<li>List item 2
<ul> <-- new node, child of list item 2!
<li>List item 2.1</li>
<li>List item 2.2</li>
</ul> <-- closed because the next item is a lower level
<li>List item 3
<ul> <-- holy crap, it happened again!
<li>List item 3.1
<ul> <-- when will the madness end?
<li>List item 3.2</li>
</ul>
</li>
</ul>
</li>
</ul>

I know this should be not-hard, but I just played a full game of
Ultimate Frisbee, and I thought this might appeal to someone else.
The full library (so far, v0.0.1 or so) is attached, but I can (and
probably will have to) extend the Tag class to support DOM-like
properties such as previous_sibling and next_sibling. And make
reparenting a node properly remove it from the previous parent's
@child_nodes collection.

I'll speak more about OWLScratch in a few days, when I hope to have
it ready for a really preliminary release.

Oh, for extra credit - think about nested list types, as seen here:
http://openwiki.com/ow.asp?HelpOnFor...
(My lists do not (currently) allow content of a single item to be
manually wrapped onto multiple lines.)


1 Answer

Aleksi

6/23/2005 5:19:00 PM

0

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