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 [ ObjectLink "Page" Nothing ] ]))
126 , (parseWiki "[[[Page|foo]]]"
128 (Right [ Paragraph [ ObjectLink "Page" (Just "foo") ] ]))
130 , (parseWiki "[[Page]]"
132 (Right [ Paragraph [ PageLink (Just "Page") Nothing Nothing ]
135 , (parseWiki "[[Page|Link to \"Page\"]]"
137 (Right [ Paragraph [ PageLink (Just "Page") Nothing (Just "Link to \"Page\"") ]
140 , (parseWiki "[[Page#foo]]"
142 (Right [ Paragraph [ PageLink (Just "Page") (Just "foo") Nothing ]
145 , (parseWiki "[[#foo]]"
147 (Right [ Paragraph [ PageLink Nothing (Just "foo") Nothing ]
150 , (parseWiki "[[Page#foo|Link to \"Page#foo\"]]"
152 (Right [ Paragraph [ PageLink (Just "Page") (Just "foo") (Just "Link to \"Page#foo\"") ]
155 , (parseWiki "foo [[Bar]] baz"
157 (Right [ Paragraph [ Text "foo "
158 , PageLink (Just "Bar") Nothing Nothing
163 , (parseWiki "[[Foo]]\n[[Bar]]"
165 (Right [ Paragraph [ PageLink (Just "Foo") Nothing Nothing
167 , PageLink (Just "Bar") Nothing Nothing
173 (Right [ Preformatted [ Text "foo" ] ]))
175 , (parseWiki " foo\n bar\n"
177 (Right [ Preformatted [ Text "foo"
183 , (parseWiki "foo\n bar\nbaz"
185 (Right [ Paragraph [ Text "foo" ]
186 , Preformatted [ Text "bar" ]
187 , Paragraph [ Text "baz" ]
192 (Right [ HorizontalLine ]))
194 , (parseWiki "\nfoo\nbar\n----\n"
196 (Right [ Paragraph [ Text "foo"
203 , (parseWiki "a----b"
205 (Right [ Paragraph [ Text "a----b" ] ]))
209 (Right [ List Bullet [[Inline (Text "a")]] ]))
213 (Right [ List Bullet [[Inline (Text "a*")]] ]))
215 , (parseWiki "* a\n* b\n"
217 (Right [ List Bullet [ [Inline (Text "a")]
218 , [Inline (Text "b")]
222 , (parseWiki "*a\n*#b\n*#c\n"
224 (Right [ List Bullet [ [ Inline (Text "a")
225 , Block (List Numbered [ [Inline (Text "b")]
226 , [Inline (Text "c")]
232 , (parseWiki "*a\n#b"
234 (Right [ List Bullet [ [Inline (Text "a")] ]
235 , List Numbered [ [Inline (Text "b")] ]
238 , (parseWiki "*a<!-- comment -->"
240 (Right [ List Bullet [ [Inline (Text "a")] ] ]))
242 , (parseWiki "*a<!-- comment -->\n*b"
244 (Right [ List Bullet [ [Inline (Text "a")]
245 , [Inline (Text "b")]
249 , (parseWiki "foo:bar"
251 (Right [ Paragraph [ Text "foo"
256 , (parseWiki "; foo: bar"
258 (Right [ DefinitionList [Definition [Text "foo"] [Text "bar"]] ]))
260 , (parseWiki "; foo: bar\n"
262 (Right [ DefinitionList [Definition [Text "foo"] [Text "bar"]] ]))
264 , (parseWiki "; foo\n: bar\n; bar\n: baz\n: baz"
266 (Right [ DefinitionList [ Definition [Text "foo"] [ Text "bar" ]
267 , Definition [Text "bar"] [ Text "baz"
273 , (parseWiki "<!nowiki[foo [[bar]] baz]>"
275 (Right [ Paragraph [ Text "foo [[bar]] baz" ] ]))
277 , (parseWiki "<!verbatim[foo [[bar]] baz]>"
279 (Right [ Preformatted [ Text "foo [[bar]] baz" ] ]))
281 , (parseWiki "<!verbatim[\nfoo [[bar]] baz\n]>"
283 (Right [ Preformatted [ Text "foo [[bar]] baz" ] ]))
285 , (parseWiki "foo' bar"
287 (Right [ Paragraph [ Text "foo"
292 , (parseWiki "''foo''"
294 (Right [ Paragraph [ Italic [Text "foo"] ] ]))
296 , (parseWiki "'''foo'''"
298 (Right [ Paragraph [ Bold [Text "foo"] ] ]))
300 , (parseWiki "foo''''"
302 (Right [ Paragraph [ Text "foo"
307 , (parseWiki "'''''foo'''''"
309 (Right [ Paragraph [ Italic [Bold [Text "foo"]] ] ]))
311 , (parseWiki "<br />"
313 (Right [ Paragraph [ InlineCmd (InlineCommand "br" [] []) ] ]))
315 , (parseWiki "<br style=\"clear: both\"/>"
317 (Right [ Paragraph [ InlineCmd (InlineCommand "br" [("style", "clear: both")] []) ] ]))
319 , (parseWiki "<i><b>foo</b></i>"
321 (Right [ Paragraph [ InlineCmd (InlineCommand "i" []
322 [ InlineCmd (InlineCommand "b" [] [ Text "foo" ]) ]) ] ]))
324 , (parseWiki "<i>\nfoo\n<!-- comment -->\nbar</i>"
326 (Right [ Paragraph [ InlineCmd (InlineCommand "i" []
334 , (parseWiki "<div>foo</div>"
336 (Right [ BlockCmd (BlockCommand "div" []
337 [ Paragraph [Text "foo"] ]) ]))
339 , (parseWiki "<div>\nbar\n</div>"
341 (Right [ BlockCmd (BlockCommand "div" []
342 [ Paragraph [Text "bar"] ]) ]))
344 , (parseWiki "<div><!-- comment --></div>"
346 (Right [ BlockCmd (BlockCommand "div" [] []) ]))
348 , (parseWiki "foo<div id=\"bar\"/>"
350 (Right [ Paragraph [Text "foo"]
351 , BlockCmd (BlockCommand "div" [("id", "bar")] [])
354 , (parseWiki "[http://example.org/]"
356 (Right [ Paragraph [ExternalLink (fromJust $ parseURI "http://example.org/") Nothing] ]))
358 , (parseWiki "[http://example.org/ example.org]"
360 (Right [ Paragraph [ExternalLink
361 (fromJust $ parseURI "http://example.org/")