Saturday, June 06, 2015

sitemap

Create sitemap html from a tree of nodes.
- Exclude certain nodes
- No redundant tags
example:
a {parent} /a
ul
  li
    a {child} /a
  /li
/ul
// include the node in sitemap
bool isinclude(node):

Skip node and its subtree too.
sitemap(n):
    buffer = new
    if !isinclude(n):
        return buffer
    buffer.append("a" + node.value + "/a")
    childrenbuffer = new
    foreach child in n.children:
        childbuffer = sitemap(child)
        if childbuffer.length > 0:
            childrenbuffer.append("li" + childbuffer + "/li")
    if childrenbuffer.length > 0:
        buffer.append("ul" + childrenbuffer + "/ul")
    return buffer

// wrap in div
sitemap-main(n):
    return "div" + sitemap(n) + "/div"

Skip node only not its subtree. But redundant nested tags are created.
sitemap(n):
    buffer = new
    if isinclude(n):
        buffer.append("a " + node.value + "/a")
    childrenbuffer = new
    foreach child in n.children:
        childbuffer = sitemap(child)
        if childbuffer.length > 0:
            childrenbuffer.append("li" + childbuffer + "/li")
    if childrenbuffer.length > 0:
        buffer.append("ul" + childrenbuffer + "/ul")
    return buffer

Skip node only not its subtree. There are still few redundant tags.
// creates redundant tags for this example:
//  x
//      x
//          n
//
// as
//
//  ul
//    li
//      a {n} a
//    /li
//  /ul

// wrap in ul if needed
sitemap-main(n):
    tuple = sitemap(n)
    buffer = new
    if tuple.isinclude:
        // tuple.buffer is a, lis
        buffer.append(tuple.buffer)
    else:
        // tuple.buffer is lis
        buffer.append("ul" + tuple.buffer + "/ul")
    return buffer

sitemap(n):
    buffer = new
    if isinclude(n):
        buffer.append("a" + node.value + "/a")
    childrenbuffer = new
    foreach child in n.children:
        tuple = sitemap(child)
        childbuffer = tuple.buffer
        isinclude = tuple.isinclude
        if childbuffer.length > 0:
            if isinclude:
                // childbuffer is a, lis
                childrenbuffer.append("li" + childbuffer + "/li")
            else:
                // childbuffer is lis
                childrenbuffer.append(childbuffer)
    if childrenbuffer.length > 0:
        if isinclude(n):
            buffer.append("ul" + childrenbuffer + "/ul")
        else:
            buffer.append(childrenbuffer)
    return tuple(buffer, isinclude(n))

Skip node only not its subtree. No redundant tags.
// works for this example:
//  x
//      x
//          n
//
// as
//
//  a {n} /a

// wrap in ul if needed
sitemap-main(n):
    //same

sitemap(n):
    buffer = new
    if isinclude(n):
        buffer.append("a" + node.value + "/a")
    childcount = 0
    childrenbuffer = new
    foreach child in n.children:
        tuple = sitemap(child)
        childbuffer = tuple.buffer
        isinclude = tuple.isinclude
        if childbuffer.length > 0:
            // capture child
            childcount++
            singlechild = tuple
            if isinclude:
                // childbuffer is a, lis
                childrenbuffer.append("li" + childbuffer + "/li")
            else:
                // childbuffer is lis
                childrenbuffer.append(childbuffer)
    // if node is not included and only one child, bubble up child
    if !isinclude(n) && childcount == 1:
        return singlechild
    if childrenbuffer.length > 0:
        if isinclude(n):
            buffer.append("ul" + childrenbuffer + "/ul")
        else:
            buffer.append(childrenbuffer)
    return tuple(buffer, isinclude(n))

No comments:

Post a Comment