从缩进的文本树创建 html 列表呈现

我正在尝试创建一个带有缩进文本的 html 页面。例如:文本文件:


1. hello

    - stack

    - overflow

        - how

    - are you

将出现:


<ol>

<il>hello</li>

<ul>

<li>stack</li> ...

因此它将呈现为缩进列表。我认为最好为Python 中的类似问题创建一个受此答案启发的节点树


这是我从 Go 中的链接克隆的结构,它不能按预期工作,由于某种原因它卡在递归中:


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

        }


    }

}


守着星空守着你
浏览 170回答 1
1回答

慕侠2389804

我找到了Markdown作为任务的最佳工具!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go