秒差距 Haskell 列表

发布于 2024-08-30 07:06:05 字数 917 浏览 1 评论 0原文

我正在使用 Text.ParserCombinators .Parsec< code>Text.XHtml 解析输入并获取 HTML 输出。

如果我的输入是:

    * First item, First level
    ** First item, Second level
    ** Second item, Second level
    * Second item, First level

我的输出应该是:

  • 第一项,第一级
    • 第一项,第二级
    • 第二项,第二级
  • 第二项,第一级

我写了这个,但显然不能递归地工作

list = do{ s <- many1 item;return (olist << s) }
item = do{ 
    (count 1 (char '*'))
    ;s <- manyTill anyChar newline
    ;return ( li <<  s)
  }

有什么想法吗? 递归可以超过两级。
谢谢!

I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input and get a HTML output.

If my input is:

    * First item, First level
    ** First item, Second level
    ** Second item, Second level
    * Second item, First level

My output should be:

<ul><li>First item, First level <ul><li>First item, Second level </li><li>Second item, Second level </li></ul></li><li>Second item, First level</li></ul>

I wrote this, but obviously does not work recursively

list = do{ s <- many1 item;return (olist << s) }
item = do{ 
    (count 1 (char '*'))
    ;s <- manyTill anyChar newline
    ;return ( li <<  s)
  }

Any ideas?
The recursion can be more than two levels.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥醉 2024-09-06 07:06:05
list n = do first <- item n
            rest <- many $ try $ try (list (n+1)) <|> item n
            return $ ulist << (first : rest)

item n = do count n (char '*')
            s <- manyTill anyChar newline
            return $ li << s

现在解析 (list 1) "foo" "* a\n** a 1\n** a 2\n* b\n** b 1\n** b 2\n"将返回您所要求的内容。

但请注意,嵌套列表本身应该位于 li 内,这样才是有效的 xhtml。

list n = do first <- item n
            rest <- many $ try $ try (list (n+1)) <|> item n
            return $ ulist << (first : rest)

item n = do count n (char '*')
            s <- manyTill anyChar newline
            return $ li << s

Now parse (list 1) "foo" "* a\n** a 1\n** a 2\n* b\n** b 1\n** b 2\n" will return what you asked for.

Note though, that the nested lists should themselves be inside a li, for this to be valid xhtml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文