1

入力を解析してHTML出力を取得するためにText.ParserCombinators.Parsecとを使用しています。Text.XHtml

私の入力が次の場合:

    *最初のアイテム、最初のレベル
    **最初のアイテム、2番目のレベル
    ** 2番目のアイテム、2番目のレベル
    * 2番目のアイテム、1番目のレベル

私の出力は次のようになります。

<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>

私はこれを書きましたが、明らかに再帰的には機能しません

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

何か案は?再帰は3つ以上のレベルにすることができます。
ありがとう!

4

1 に答える 1

2
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

parse (list 1) "foo" "* a\n** a 1\n** a 2\n* b\n** b 1\n** b 2\n"、あなたが求めたものを返します。

ただし、これが有効なxhtmlであるためには、ネストされたリスト自体がli内にある必要があることに注意してください。

于 2010-04-24T17:48:10.090 に答える