9 import Control.Applicative
11 import Data.Text (Text)
14 import Rakka.Wiki.Parser
16 import Text.ParserCombinators.Parsec
18 cmdTypeOf ∷ Alternative f ⇒ Text → f CommandType
19 cmdTypeOf "br" = pure InlineCommandType
20 cmdTypeOf "i" = pure InlineCommandType
21 cmdTypeOf "b" = pure InlineCommandType
22 cmdTypeOf "span" = pure InlineCommandType
23 cmdTypeOf "div" = pure BlockCommandType
26 parseWiki ∷ String → Either String WikiPage
27 parseWiki src = case parse (wikiPage cmdTypeOf) "" src of
28 Left err → Left (show err)
29 Right page → Right page
32 testData = [ (parseWiki ""
40 , (parseWiki "=heading="
42 Right [ Heading 1 "heading" ])
44 , (parseWiki "== heading == \n"
46 Right [ Heading 2 "heading" ])
48 , (parseWiki "===== hello world =====\n"
50 Right [ Heading 5 "hello world" ])
52 , (parseWiki "a =not a heading="
54 Right [ Paragraph [ Text "a =not a heading=" ]
57 , (parseWiki "=h=\n\n=h="
62 , (parseWiki "foo\nbar"
64 Right [ Paragraph [ Text "foo"
69 , (parseWiki "foo\nbar\n\nbaz\n"
71 Right [ Paragraph [ Text "foo"
75 , Paragraph [ Text "baz"
79 , (parseWiki "foo\n\n\nbar"
81 Right [ Paragraph [ Text "foo" ]
82 , Paragraph [ Text "bar" ]
85 , (parseWiki "foo\n=h="
87 Right [ Paragraph [ Text "foo" ]
91 , (parseWiki "<!-- comment -->"
95 , (parseWiki "<!-- comment -->foo"
97 Right [ Paragraph [ Text "foo" ]
100 , (parseWiki "bar<!-- comment -->"
102 Right [ Paragraph [ Text "bar" ]
105 , (parseWiki "foo<!-- comment -->bar"
107 Right [ Paragraph [ Text "foo"
112 , (parseWiki "<!-- comment -->=h="
114 Right [ Heading 1 "h" ])
116 , (parseWiki "=h= <!---->"
118 Right [ Heading 1 "h" ])
120 , (parseWiki "<!-- <!-- nested --> comment -->"
124 , (parseWiki "[[[Page]]]"
126 Right [ Paragraph [ ObjectLink "Page" Nothing ] ])
128 , (parseWiki "[[[Page|foo]]]"
130 Right [ Paragraph [ ObjectLink "Page" (Just "foo") ] ])
132 , (parseWiki "[[Page]]"
134 Right [ Paragraph [ PageLink (Just "Page") Nothing Nothing ]
137 , (parseWiki "[[Page|Link to \"Page\"]]"
139 Right [ Paragraph [ PageLink (Just "Page") Nothing (Just "Link to \"Page\"") ]
142 , (parseWiki "[[Page#foo]]"
144 Right [ Paragraph [ PageLink (Just "Page") (Just "foo") Nothing ]
147 , (parseWiki "[[#foo]]"
149 Right [ Paragraph [ PageLink Nothing (Just "foo") Nothing ]
152 , (parseWiki "[[Page#foo|Link to \"Page#foo\"]]"
154 Right [ Paragraph [ PageLink (Just "Page") (Just "foo") (Just "Link to \"Page#foo\"") ]
157 , (parseWiki "foo [[Bar]] baz"
159 Right [ Paragraph [ Text "foo "
160 , PageLink (Just "Bar") Nothing Nothing
165 , (parseWiki "[[Foo]]\n[[Bar]]"
167 Right [ Paragraph [ PageLink (Just "Foo") Nothing Nothing
169 , PageLink (Just "Bar") Nothing Nothing
175 Right [ Preformatted [ Text "foo" ] ])
177 , (parseWiki " foo\n bar\n"
179 Right [ Preformatted [ Text "foo"
185 , (parseWiki "foo\n bar\nbaz"
187 Right [ Paragraph [ Text "foo" ]
188 , Preformatted [ Text "bar" ]
189 , Paragraph [ Text "baz" ]
194 Right [ HorizontalLine ])
196 , (parseWiki "\nfoo\nbar\n----\n"
198 Right [ Paragraph [ Text "foo"
205 , (parseWiki "a----b"
207 Right [ Paragraph [ Text "a----b" ] ])
211 Right [ List Bullet [[Inline (Text "a")]] ])
215 Right [ List Bullet [[Inline (Text "a*")]] ])
217 , (parseWiki "* a\n* b\n"
219 Right [ List Bullet [ [Inline (Text "a")]
220 , [Inline (Text "b")]
224 , (parseWiki "*a\n*#b\n*#c\n"
226 Right [ List Bullet [ [ Inline (Text "a")
227 , Block (List Numbered [ [Inline (Text "b")]
228 , [Inline (Text "c")]
234 , (parseWiki "*a\n#b"
236 Right [ List Bullet [ [Inline (Text "a")] ]
237 , List Numbered [ [Inline (Text "b")] ]
240 , (parseWiki "*a<!-- comment -->"
242 Right [ List Bullet [ [Inline (Text "a")] ] ])
244 , (parseWiki "*a<!-- comment -->\n*b"
246 Right [ List Bullet [ [Inline (Text "a")]
247 , [Inline (Text "b")]
251 , (parseWiki "foo:bar"
253 Right [ Paragraph [ Text "foo"
258 , (parseWiki "; foo: bar"
260 Right [ DefinitionList [Definition [Text "foo"] [Text "bar"]] ])
262 , (parseWiki "; foo: bar\n"
264 Right [ DefinitionList [Definition [Text "foo"] [Text "bar"]] ])
266 , (parseWiki "; foo\n: bar\n; bar\n: baz\n: baz"
268 Right [ DefinitionList [ Definition [Text "foo"] [ Text "bar" ]
269 , Definition [Text "bar"] [ Text "baz"
275 , (parseWiki "<!nowiki[foo [[bar]] baz]>"
277 Right [ Paragraph [ Text "foo [[bar]] baz" ] ])
279 , (parseWiki "<!verbatim[foo [[bar]] baz]>"
281 Right [ Preformatted [ Text "foo [[bar]] baz" ] ])
283 , (parseWiki "<!verbatim[\nfoo [[bar]] baz\n]>"
285 Right [ Preformatted [ Text "foo [[bar]] baz" ] ])
287 , (parseWiki "foo' bar"
289 Right [ Paragraph [ Text "foo"
294 , (parseWiki "''foo''"
296 Right [ Paragraph [ Italic [Text "foo"] ] ])
298 , (parseWiki "'''foo'''"
300 Right [ Paragraph [ Bold [Text "foo"] ] ])
302 , (parseWiki "foo''''"
304 Right [ Paragraph [ Text "foo"
309 , (parseWiki "'''''foo'''''"
311 Right [ Paragraph [ Italic [Bold [Text "foo"]] ] ])
313 , (parseWiki "<br />"
315 Right [ Paragraph [ InlineCmd (InlineCommand "br" [] []) ] ])
317 , (parseWiki "<br style=\"clear: both\"/>"
319 Right [ Paragraph [ InlineCmd (InlineCommand "br" [("style", "clear: both")] []) ] ])
321 , (parseWiki "<i><b>foo</b></i>"
323 Right [ Paragraph [ InlineCmd (InlineCommand "i" []
324 [ InlineCmd (InlineCommand "b" [] [ Text "foo" ]) ]) ] ])
326 , (parseWiki "<i>\nfoo\n<!-- comment -->\nbar</i>"
328 Right [ Paragraph [ InlineCmd (InlineCommand "i" []
336 , (parseWiki "<div>foo</div>"
338 Right [ BlockCmd (BlockCommand "div" []
339 [ Paragraph [Text "foo"] ]) ])
341 , (parseWiki "<div>\nbar\n</div>"
343 Right [ BlockCmd (BlockCommand "div" []
344 [ Paragraph [Text "bar"] ]) ])
346 , (parseWiki "<div><!-- comment --></div>"
348 Right [ BlockCmd (BlockCommand "div" [] []) ])
350 , (parseWiki "foo<div id=\"bar\"/>"
352 Right [ Paragraph [Text "foo"]
353 , BlockCmd (BlockCommand "div" [("id", "bar")] [])
356 , (parseWiki "[http://example.org/]"
358 Right [ Paragraph [ExternalLink (fromJust $ parseURI "http://example.org/") Nothing] ])
360 , (parseWiki "[http://example.org/ example.org]"
362 Right [ Paragraph [ExternalLink
363 (fromJust $ parseURI "http://example.org/")