Parsec Haskell Lists
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!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.
精彩评论