Search code examples
htmlgotextstackindentation

Creating an html list rendering from indented text tree


I'm trying to create an html page out indented text. For examle: text file:

1. hello
    - stack
    - overflow
        - how
    - are you

Will come out as:

<ol>
<il>hello</li>
<ul>
<li>stack</li> ...

so it will render as an indented list. I thought it would be best to create a node tree inspired by this answer for a similar problem in Python

Here's my cloned struct from the link in Go which doesn't work as intended, it gets stuck in the recursion for some reason:

func (n *node) addChildren(nodes []node) {
    childLevel := nodes[0].textStart
    for len(nodes) > 0 {
        // pop
        tempNode := nodes[0]
        nodes = nodes[1:]
        if tempNode.textStart == childLevel {
            n.children = append(n.children, tempNode)
        } else if tempNode.textStart > childLevel {
            nodes = append([]node{tempNode}, nodes...)
            n.children[len(n.children)-1].addChildren(nodes)
        } else if tempNode.textStart <= n.textStart {
            nodes = append([]node{tempNode}, nodes...)
            return
        }

    }
}

Solution

  • I have found Markdown

    As an optimal tool for the task!