{-# LANGUAGE NoImplicitPrelude #-}
module Tests.Writers.Muse (tests) where
import Prelude
import Data.Text (unpack)
import Test.Tasty
import Tests.Helpers
import Text.Pandoc
import Text.Pandoc.Arbitrary ()
import Text.Pandoc.Builder
muse :: (ToPandoc a) => a -> String
muse = museWithOpts def{ writerWrapText = WrapPreserve,
                         writerExtensions = extensionsFromList [Ext_amuse,
                                                                Ext_auto_identifiers] }
museWithOpts :: (ToPandoc a) => WriterOptions -> a -> String
museWithOpts opts = unpack . purely (writeMuse opts) . toPandoc
infix 4 =:
(=:) :: (ToString a, ToPandoc a)
     => String -> (a, String) -> TestTree
(=:) = test muse
tests :: [TestTree]
tests = [ testGroup "block elements"
          [ "plain" =: plain (text "Foo bar.") =?> "Foo bar."
          , testGroup "paragraphs"
            [ "single paragraph" =: para (text "Sample paragraph.")
                                 =?> "Sample paragraph."
            , "two paragraphs" =: para (text "First paragraph.") <>
                                  para (text "Second paragraph.")
                               =?> unlines [ "First paragraph."
                                           , ""
                                           , "Second paragraph."
                                           ]
            ]
          , "line block" =: lineBlock [text "Foo", text "bar", text "baz"]
                         =?> unlines [ "> Foo"
                                     , "> bar"
                                     , "> baz"
                                     ]
          , "code block" =: codeBlock "int main(void) {\n\treturn 0;\n}"
                         =?> unlines [ "
" , "Foo" , "" ] , testGroup "lists" [ testGroup "simple lists" [ "ordered list" =: orderedList [ plain $ text "first" , plain $ text "second" , plain $ text "third" ] =?> unlines [ " 1. first" , " 2. second" , " 3. third" ] , "ordered list with Roman numerals" =: orderedListWith (1, UpperRoman, DefaultDelim) [ plain $ text "first" , plain $ text "second" , plain $ text "third" ] =?> unlines [ " I. first" , " II. second" , " III. third" ] , "bullet list" =: bulletList [ plain $ text "first" , plain $ text "second" , plain $ text "third" ] =?> unlines [ " - first" , " - second" , " - third" ] , "definition list" =: definitionList [ (text "first definition", [plain $ text "first description"]) , (text "second definition", [plain $ text "second description"]) , (text "third definition", [plain $ text "third description"]) ] =?> unlines [ " first definition :: first description" , " second definition :: second description" , " third definition :: third description" ] , "definition list with multiple descriptions" =: definitionList [ (text "first definition", [plain $ text "first description" ,plain $ text "second description"]) , (text "second definition", [plain $ text "third description"]) ] =?> unlines [ " first definition :: first description" , " :: second description" , " second definition :: third description" ] , "definition list with empty term" =: definitionList [ (text "first definition", [plain $ text "first description"]) , (mempty, [plain $ text "second description"]) , (str "", [plain $ text "third description"]) ] =?> unlines [ " first definition :: first description" , "
" , " 1. first" , " 2. second" , " 3. third" , "" ] ] , testGroup "headings" [ "normal heading" =: header 1 (text "foo") =?> "* foo" , "heading levels" =: header 1 (text "First level") <> header 3 (text "Third level") =?> unlines [ "* First level" , "" , "*** Third level" ] , "heading with ID" =: headerWith ("bar", [], []) 2 (text "Foo") =?> unlines [ "#bar" , "** Foo" ] , "empty heading" =: header 4 (mempty) =?> "****
foo"
            , "escape tag" =: code "foo = bar baz" =?> "foo = bar</code> baz"
            , "normalization with attributes" =: codeWith ("",["haskell"],[]) "foo" <> code "bar" =?> "foobar"
            , "normalization" =: code " code "de>" =?> "</code>"
            , "normalization with empty string" =: code " str "" <> code "de>" =?> "</code>"
            ]
          , testGroup "spaces"
            [ "space" =: text "a" <> space <> text "b" =?> "a b"
            , "soft break" =: text "a" <> softbreak <> text "b" =?> "a\nb"
            , test (museWithOpts def{ writerWrapText = WrapNone })
                   "remove soft break" $ text "a" <> softbreak <> text "b"
                   =?> "a b"
            , "line break" =: text "a" <> linebreak <> text "b" =?> "a
\nb"
            , "no newline after line break in header" =: header 1 (text "a" <> linebreak <> text "b") =?> "* a
b"
            , "no softbreak in header" =: header 1 (text "a" <> softbreak <> text "b") =?> "* a b"
            ]
          , testGroup "math"
            [ "inline math" =: math "2^3" =?> "23"
            , "display math" =: displayMath "2^3" =?> "23"
            , "multiple letters in inline math" =: math "abc" =?> "abc"
            , "expand math before normalization" =: math "[" <> str "2]" =?> "[2] "
            , "multiple math expressions inside one inline list" =: math "5_4" <> text ", " <> displayMath "3^2" =?> "54, 32"
            ]
          , "raw inline"
            =: rawInline "html" "marked text"
            =?> "marked text "
          , testGroup "links"
            [ "link with description" =: link "https://example.com" "" (str "Link 1")
                                      =?> "[[https://example.com][Link 1]]"
            , "link without description" =: link "https://example.com" "" (str "https://example.com")
                                         =?> "[[https://example.com]]"
            -- Internal links in Muse include '#'
            , "link to anchor" =: link "#intro" "" (str "Introduction")
                               =?> "[[#intro][Introduction]]"
            -- According to Emacs Muse manual, links to images should be prefixed with "URL:"
            , "link to image with description" =: link "1.png" "" (str "Link to image")
                                               =?> "[[URL:1.png][Link to image]]"
            , "link to image without description" =: link "1.png" "" (str "1.png")
                                                  =?> "[[URL:1.png]]"
            , testGroup "escape brackets in links"
              [ "link with description"
                =: link "https://example.com/foo].txt" "" (str "Description")
                =?> "[[https://example.com/foo%5D.txt][Description]]"
              , "link without description"
                =: link "https://example.com/foo].txt" "" (str "https://example.com/foo].txt")
                =?> "[[https://example.com/foo%5D.txt][https://example.com/foo].txt ]]"
              , "image link with description"
                =: link "foo]bar.png" "" (str "Image link")
                =?> "[[URL:foo%5Dbar.png][Image link]]"
              , "image link without description"
                =: link "foo]bar.png" "" (str "foo]bar.png")
                =?> "[[URL:foo%5Dbar.png][foo]bar.png ]]"
              ]
            ]
          , "image" =: image "image.png" "Image 1" (str "") =?> "[[image.png][Image 1]]"
          , "image with width" =:
            imageWith ("", [], [("width", "60%")]) "image.png" "Image" (str "") =?>
            "[[image.png 60][Image]]"
          , "left-aligned image with width" =:
            imageWith ("", ["align-left"], [("width", "60%")]) "image.png" "Image" (str "") =?>
            "[[image.png 60 l][Image]]"
          , "right-aligned image with width" =:
            imageWith ("", ["align-right"], [("width", "60%")]) "image.png" "Image" (str "") =?>
            "[[image.png 60 r][Image]]"
          , "escape brackets in image title" =: image "image.png" "Foo]bar" (str "") =?> "[[image.png][Foo]bar ]]"
          , "note" =: note (plain (text "Foo"))
                   =?> unlines [ "[1]"
                               , ""
                               , "[1] Foo"
                               ]
          , "span with class" =: spanWith ("",["foobar"],[]) (text "Some text")
                              =?> "Some text "
          , "span without class" =: spanWith ("",[],[]) (text "Some text")
                                 =?> "Some text "
          , "span with anchor" =: spanWith ("anchor", [], []) (mempty) <> (text "Foo bar")
                               =?> "#anchor Foo bar"
          , "empty span with anchor" =: spanWith ("anchor", [], []) (mempty)
                                     =?> "#anchor"
          , "empty span without class and anchor" =: spanWith ("", [], []) (mempty)
                                                  =?> " "
          , "span with class and anchor" =: spanWith ("anchor", ["foo"], []) (text "bar")
                                         =?> "#anchor bar "
          , "adjacent spans" =: spanWith ("", ["syllable"], []) (str "wa") <>
                                spanWith ("", ["syllable"], []) (str "ter")
                             =?> "wa ter "
          , testGroup "combined"
            [ "emph word before" =:
                para (text "foo" <> emph (text "bar")) =?>
                    "foobar"
            , "emph word after" =:
                para (emph (text "foo") <> text "bar") =?>
                    "foobar"
            , "emph quoted" =:
                para (doubleQuoted (emph (text "foo"))) =?>
                    "“foo”"
            , "strong word before" =:
                para (text "foo" <> strong (text "bar")) =?>
                    "foobar"
            , "strong word after" =:
                para (strong (text "foo") <> text "bar") =?>
                    "foobar"
            , "strong quoted" =:
                para (singleQuoted (strong (text "foo"))) =?>
                    "‘foo’"
            ]
         ]
       ]