9 import Rakka.Wiki.Parser
11 import Text.ParserCombinators.Parsec
14 cmdTypeOf :: String -> Maybe CommandType
15 cmdTypeOf "br" = Just InlineCommandType
16 cmdTypeOf "i" = Just InlineCommandType
17 cmdTypeOf "b" = Just InlineCommandType
18 cmdTypeOf "span" = Just InlineCommandType
19 cmdTypeOf "div" = Just BlockCommandType
23 parseWiki :: String -> Either String WikiPage
24 parseWiki src = case parse (wikiPage cmdTypeOf) "" src of
25 Left err -> Left (show err)
26 Right page -> Right page
30 testData = [ (parseWiki ""
38 , (parseWiki "=heading="
40 (Right [ Heading 1 "heading" ]))
42 , (parseWiki "== heading == \n"
44 (Right [ Heading 2 "heading" ]))
46 , (parseWiki "===== hello world =====\n"
48 (Right [ Heading 5 "hello world" ]))
50 , (parseWiki "a =not a heading="
52 (Right [ Paragraph [ Text "a =not a heading=" ]
55 , (parseWiki "=h=\n\n=h="
57 (Right [ Heading 1 "h"
60 , (parseWiki "foo\nbar"
62 (Right [ Paragraph [ Text "foo"
67 , (parseWiki "foo\nbar\n\nbaz\n"
69 (Right [ Paragraph [ Text "foo"
73 , Paragraph [ Text "baz"
77 , (parseWiki "foo\n\n\nbar"
79 (Right [ Paragraph [ Text "foo" ]
80 , Paragraph [ Text "bar" ]
83 , (parseWiki "foo\n=h="
85 (Right [ Paragraph [ Text "foo" ]
89 , (parseWiki "<!-- comment -->"
93 , (parseWiki "<!-- comment -->foo"
95 (Right [ Paragraph [ Text "foo" ]
98 , (parseWiki "bar<!-- comment -->"
100 (Right [ Paragraph [ Text "bar" ]
103 , (parseWiki "foo<!-- comment -->bar"
105 (Right [ Paragraph [ Text "foo"
110 , (parseWiki "<!-- comment -->=h="
112 (Right [ Heading 1 "h" ]))
114 , (parseWiki "=h= <!---->"
116 (Right [ Heading 1 "h" ]))
118 , (parseWiki "<!-- <!-- nested --> comment -->"
122 , (parseWiki "[[Page]]"
124 (Right [ Paragraph [ PageLink (Just "Page") Nothing Nothing ]
127 , (parseWiki "[[Page|Link to \"Page\"]]"
129 (Right [ Paragraph [ PageLink (Just "Page") Nothing (Just "Link to \"Page\"") ]
132 , (parseWiki "[[Page#foo]]"
134 (Right [ Paragraph [ PageLink (Just "Page") (Just "foo") Nothing ]
137 , (parseWiki "[[#foo]]"
139 (Right [ Paragraph [ PageLink Nothing (Just "foo") Nothing ]
142 , (parseWiki "[[Page#foo|Link to \"Page#foo\"]]"
144 (Right [ Paragraph [ PageLink (Just "Page") (Just "foo") (Just "Link to \"Page#foo\"") ]
147 , (parseWiki "foo [[Bar]] baz"
149 (Right [ Paragraph [ Text "foo "
150 , PageLink (Just "Bar") Nothing Nothing
155 , (parseWiki "[[Foo]]\n[[Bar]]"
157 (Right [ Paragraph [ PageLink (Just "Foo") Nothing Nothing
159 , PageLink (Just "Bar") Nothing Nothing
165 (Right [ Preformatted [ Text "foo" ] ]))
167 , (parseWiki " foo\n bar\n"
169 (Right [ Preformatted [ Text "foo"
175 , (parseWiki "foo\n bar\nbaz"
177 (Right [ Paragraph [ Text "foo" ]
178 , Preformatted [ Text "bar" ]
179 , Paragraph [ Text "baz" ]
184 (Right [ HorizontalLine ]))
186 , (parseWiki "\nfoo\nbar\n----\n"
188 (Right [ Paragraph [ Text "foo"
195 , (parseWiki "a----b"
197 (Right [ Paragraph [ Text "a----b" ] ]))
201 (Right [ List (ListElement Bullet [[Right (Text "a")]]) ]))
205 (Right [ List (ListElement Bullet [[Right (Text "a*")]]) ]))
207 , (parseWiki "* a\n* b\n"
209 (Right [ List (ListElement Bullet [ [Right (Text "a")]
214 , (parseWiki "*a\n*#b\n*#c\n"
216 (Right [ List (ListElement Bullet [ [ Right (Text "a")
217 , Left (ListElement Numbered [ [Right (Text "b")]
224 , (parseWiki "*a\n#b"
226 (Right [ List (ListElement Bullet [ [Right (Text "a")] ])
227 , List (ListElement Numbered [ [Right (Text "b")] ])
230 , (parseWiki "*a<!-- comment -->"
232 (Right [ List (ListElement Bullet [ [Right (Text "a")] ]) ]))
234 , (parseWiki "*a<!-- comment -->\n*b"
236 (Right [ List (ListElement Bullet [ [Right (Text "a")]
241 , (parseWiki "foo:bar"
243 (Right [ Paragraph [ Text "foo"
248 , (parseWiki "; foo: bar"
250 (Right [ DefinitionList [Definition [Text "foo"] [Text "bar"]] ]))
252 , (parseWiki "; foo: bar\n"
254 (Right [ DefinitionList [Definition [Text "foo"] [Text "bar"]] ]))
256 , (parseWiki "; foo\n: bar\n; bar\n: baz\n: baz"
258 (Right [ DefinitionList [ Definition [Text "foo"] [ Text "bar" ]
259 , Definition [Text "bar"] [ Text "baz"
265 , (parseWiki "<![CDATA[foo [[bar]] baz]]>"
267 (Right [ Paragraph [ Text "foo [[bar]] baz" ] ]))
269 , (parseWiki "<![PDATA[foo [[bar]] baz]]>"
271 (Right [ Preformatted [ Text "foo [[bar]] baz" ] ]))
273 , (parseWiki "<![PDATA[\nfoo [[bar]] baz\n]]>"
275 (Right [ Preformatted [ Text "foo [[bar]] baz" ] ]))
277 , (parseWiki "foo' bar"
279 (Right [ Paragraph [ Text "foo"
284 , (parseWiki "''foo''"
286 (Right [ Paragraph [ Italic [Text "foo"] ] ]))
288 , (parseWiki "'''foo'''"
290 (Right [ Paragraph [ Bold [Text "foo"] ] ]))
292 , (parseWiki "foo''''"
294 (Right [ Paragraph [ Text "foo"
299 , (parseWiki "'''''foo'''''"
301 (Right [ Paragraph [ Italic [Bold [Text "foo"]] ] ]))
303 , (parseWiki "<br />"
305 (Right [ Paragraph [ InlineCmd (InlineCommand "br" [] []) ] ]))
307 , (parseWiki "<br style=\"clear: both\"/>"
309 (Right [ Paragraph [ InlineCmd (InlineCommand "br" [("style", "clear: both")] []) ] ]))
311 , (parseWiki "<i><b>foo</b></i>"
313 (Right [ Paragraph [ InlineCmd (InlineCommand "i" []
314 [ InlineCmd (InlineCommand "b" [] [ Text "foo" ]) ]) ] ]))
316 , (parseWiki "<i>\nfoo\n<!-- comment -->\nbar</i>"
318 (Right [ Paragraph [ InlineCmd (InlineCommand "i" []
326 , (parseWiki "<div>foo</div>"
328 (Right [ BlockCmd (BlockCommand "div" []
329 [ Paragraph [Text "foo"] ]) ]))
331 , (parseWiki "<div>\nbar\n</div>"
333 (Right [ BlockCmd (BlockCommand "div" []
334 [ Paragraph [Text "bar"] ]) ]))
336 , (parseWiki "<div><!-- comment --></div>"
338 (Right [ BlockCmd (BlockCommand "div" [] []) ]))
340 , (parseWiki "foo<div id=\"bar\"/>"
342 (Right [ Paragraph [Text "foo"]
343 , BlockCmd (BlockCommand "div" [("id", "bar")] [])
346 , (parseWiki "[http://example.org/]"
348 (Right [ Paragraph [ExternalLink (fromJust $ parseURI "http://example.org/") Nothing] ]))
350 , (parseWiki "[http://example.org/ example.org]"
352 (Right [ Paragraph [ExternalLink
353 (fromJust $ parseURI "http://example.org/")