7 import Rakka.Wiki.Parser
9 import Text.ParserCombinators.Parsec
12 cmdTypeOf :: String -> Maybe CommandType
13 cmdTypeOf "br" = Just InlineCommandType
14 cmdTypeOf "i" = Just InlineCommandType
15 cmdTypeOf "b" = Just InlineCommandType
16 cmdTypeOf "span" = Just InlineCommandType
17 cmdTypeOf "div" = Just BlockCommandType
21 parseWiki :: String -> Either String WikiPage
22 parseWiki src = case parse (wikiPage cmdTypeOf) "" src of
23 Left err -> Left (show err)
24 Right page -> Right page
28 testData = [ (parseWiki ""
36 , (parseWiki "=heading="
38 (Right [ Heading 1 "heading" ]))
40 , (parseWiki "== heading == \n"
42 (Right [ Heading 2 "heading" ]))
44 , (parseWiki "===== hello world =====\n"
46 (Right [ Heading 5 "hello world" ]))
48 , (parseWiki "a =not a heading="
50 (Right [ Paragraph [ Text "a =not a heading=" ]
53 , (parseWiki "=h=\n\n=h="
55 (Right [ Heading 1 "h"
58 , (parseWiki "foo\nbar"
60 (Right [ Paragraph [ Text "foo"
65 , (parseWiki "foo\nbar\n\nbaz\n"
67 (Right [ Paragraph [ Text "foo"
71 , Paragraph [ Text "baz"
75 , (parseWiki "foo\n\n\nbar"
77 (Right [ Paragraph [ Text "foo" ]
78 , Paragraph [ Text "bar" ]
81 , (parseWiki "foo\n=h="
83 (Right [ Paragraph [ Text "foo" ]
87 , (parseWiki "<!-- comment -->"
91 , (parseWiki "<!-- comment -->foo"
93 (Right [ Paragraph [ Text "foo" ]
96 , (parseWiki "bar<!-- comment -->"
98 (Right [ Paragraph [ Text "bar" ]
101 , (parseWiki "foo<!-- comment -->bar"
103 (Right [ Paragraph [ Text "foo"
108 , (parseWiki "<!-- comment -->=h="
110 (Right [ Heading 1 "h" ]))
112 , (parseWiki "=h= <!---->"
114 (Right [ Heading 1 "h" ]))
116 , (parseWiki "<!-- <!-- nested --> comment -->"
120 , (parseWiki "[[Page]]"
122 (Right [ Paragraph [ PageLink (Just "Page") Nothing Nothing ]
125 , (parseWiki "[[Page|Link to \"Page\"]]"
127 (Right [ Paragraph [ PageLink (Just "Page") Nothing (Just "Link to \"Page\"") ]
130 , (parseWiki "[[Page#foo]]"
132 (Right [ Paragraph [ PageLink (Just "Page") (Just "foo") Nothing ]
135 , (parseWiki "[[#foo]]"
137 (Right [ Paragraph [ PageLink Nothing (Just "foo") Nothing ]
140 , (parseWiki "[[Page#foo|Link to \"Page#foo\"]]"
142 (Right [ Paragraph [ PageLink (Just "Page") (Just "foo") (Just "Link to \"Page#foo\"") ]
145 , (parseWiki "foo [[Bar]] baz"
147 (Right [ Paragraph [ Text "foo "
148 , PageLink (Just "Bar") Nothing Nothing
153 , (parseWiki "[[Foo]]\n[[Bar]]"
155 (Right [ Paragraph [ PageLink (Just "Foo") Nothing Nothing
157 , PageLink (Just "Bar") Nothing Nothing
163 (Right [ Preformatted [ Text "foo" ] ]))
165 , (parseWiki " foo\n bar\n"
167 (Right [ Preformatted [ Text "foo"
173 , (parseWiki "foo\n bar\nbaz"
175 (Right [ Paragraph [ Text "foo" ]
176 , Preformatted [ Text "bar" ]
177 , Paragraph [ Text "baz" ]
182 (Right [ HorizontalLine ]))
184 , (parseWiki "\nfoo\nbar\n----\n"
186 (Right [ Paragraph [ Text "foo"
193 , (parseWiki "a----b"
195 (Right [ Paragraph [ Text "a----b" ] ]))
199 (Right [ List (ListElement Bullet [[Right (Text "a")]]) ]))
203 (Right [ List (ListElement Bullet [[Right (Text "a*")]]) ]))
205 , (parseWiki "* a\n* b\n"
207 (Right [ List (ListElement Bullet [ [Right (Text "a")]
212 , (parseWiki "*a\n*#b\n*#c\n"
214 (Right [ List (ListElement Bullet [ [ Right (Text "a")
215 , Left (ListElement Numbered [ [Right (Text "b")]
222 , (parseWiki "*a\n#b"
224 (Right [ List (ListElement Bullet [ [Right (Text "a")] ])
225 , List (ListElement Numbered [ [Right (Text "b")] ])
228 , (parseWiki "*a<!-- comment -->"
230 (Right [ List (ListElement Bullet [ [Right (Text "a")] ]) ]))
232 , (parseWiki "*a<!-- comment -->\n*b"
234 (Right [ List (ListElement Bullet [ [Right (Text "a")]
239 , (parseWiki "foo:bar"
241 (Right [ Paragraph [ Text "foo"
246 , (parseWiki "; foo: bar"
248 (Right [ DefinitionList [Definition [Text "foo"] [Text "bar"]] ]))
250 , (parseWiki "; foo: bar\n"
252 (Right [ DefinitionList [Definition [Text "foo"] [Text "bar"]] ]))
254 , (parseWiki "; foo\n: bar\n; bar\n: baz\n: baz"
256 (Right [ DefinitionList [ Definition [Text "foo"] [ Text "bar" ]
257 , Definition [Text "bar"] [ Text "baz"
263 , (parseWiki "<![CDATA[foo [[bar]] baz]]>"
265 (Right [ Paragraph [ Text "foo [[bar]] baz" ] ]))
267 , (parseWiki "<![PDATA[foo [[bar]] baz]]>"
269 (Right [ Preformatted [ Text "foo [[bar]] baz" ] ]))
271 , (parseWiki "<![PDATA[\nfoo [[bar]] baz\n]]>"
273 (Right [ Preformatted [ Text "foo [[bar]] baz" ] ]))
275 , (parseWiki "foo' bar"
277 (Right [ Paragraph [ Text "foo"
282 , (parseWiki "''foo''"
284 (Right [ Paragraph [ Italic [Text "foo"] ] ]))
286 , (parseWiki "'''foo'''"
288 (Right [ Paragraph [ Bold [Text "foo"] ] ]))
290 , (parseWiki "foo''''"
292 (Right [ Paragraph [ Text "foo"
297 , (parseWiki "'''''foo'''''"
299 (Right [ Paragraph [ Italic [Bold [Text "foo"]] ] ]))
301 , (parseWiki "<br />"
303 (Right [ Paragraph [ InlineCmd (InlineCommand "br" [] []) ] ]))
305 , (parseWiki "<br style=\"clear: both\"/>"
307 (Right [ Paragraph [ InlineCmd (InlineCommand "br" [("style", "clear: both")] []) ] ]))
309 , (parseWiki "<i><b>foo</b></i>"
311 (Right [ Paragraph [ InlineCmd (InlineCommand "i" []
312 [ InlineCmd (InlineCommand "b" [] [ Text "foo" ]) ]) ] ]))
314 , (parseWiki "<i>\nfoo\n<!-- comment -->\nbar</i>"
316 (Right [ Paragraph [ InlineCmd (InlineCommand "i" []
324 , (parseWiki "<div>foo</div>"
326 (Right [ BlockCmd (BlockCommand "div" []
327 [ Paragraph [Text "foo"] ]) ]))
329 , (parseWiki "<div><!-- comment --></div>"
331 (Right [ BlockCmd (BlockCommand "div" [] []) ]))
333 , (parseWiki "foo<div id=\"bar\"/>"
335 (Right [ Paragraph [Text "foo"]
336 , BlockCmd (BlockCommand "div" [("id", "bar")] [])