2013-01-23 17:47:43 +01:00
{- # LANGUAGE OverloadedStrings # -}
2011-01-30 01:26:00 +01:00
module Tests.Writers.HTML ( tests ) where
2017-06-10 23:39:49 +02:00
import Data.Text ( unpack )
2017-03-14 17:05:36 +01:00
import Test.Tasty
2011-01-30 01:26:00 +01:00
import Tests.Helpers
2017-03-04 13:03:41 +01:00
import Text.Pandoc
import Text.Pandoc.Arbitrary ( )
import Text.Pandoc.Builder
2011-01-30 01:26:00 +01:00
2021-09-10 18:30:05 +02:00
htmlWithOpts :: ( ToPandoc a ) => WriterOptions -> a -> String
htmlWithOpts opts = unpack . purely ( writeHtml4String opts { writerWrapText = WrapNone } ) . toPandoc
2016-07-14 17:54:06 +02:00
html :: ( ToPandoc a ) => a -> String
2021-09-10 18:30:05 +02:00
html = htmlWithOpts def
2011-01-30 01:26:00 +01:00
2019-10-25 07:27:49 +02:00
htmlQTags :: ( ToPandoc a ) => a -> String
htmlQTags = unpack
. purely ( writeHtml4String def { writerWrapText = WrapNone , writerHtmlQTags = True } )
. toPandoc
2011-01-30 01:26:00 +01:00
{-
" my test " =: X =?> Y
is shorthand for
test html " my test " $ X =?> Y
which is in turn shorthand for
test html " my test " ( X , Y )
- }
2012-02-05 22:23:06 +01:00
infix 4 =:
2011-01-30 01:26:00 +01:00
( =: ) :: ( ToString a , ToPandoc a )
2017-03-14 17:05:36 +01:00
=> String -> ( a , String ) -> TestTree
2011-01-30 01:26:00 +01:00
( =: ) = test html
2021-09-10 18:30:05 +02:00
noteTestDoc :: Blocks
noteTestDoc =
header 1 " Page title " <>
header 2 " First section " <>
para ( " This is a footnote. " <>
note ( para " Down here. " ) <>
" And this is a " <>
link " https://www.google.com " " " " link " <>
" . " ) <>
blockQuote ( para ( " A note inside a block quote. " <>
note ( para " The second note. " ) ) <>
para " A second paragraph. " ) <>
header 2 " Second section " <>
para " Some more text. "
2017-03-14 17:05:36 +01:00
tests :: [ TestTree ]
2021-05-17 15:37:25 +02:00
tests =
[ testGroup " inline code "
[ " basic " =: code " @& " =?> " <code>@&</code> "
, " haskell " =: codeWith ( " " , [ " haskell " ] , [] ) " >>= "
=?> " <code class= \ " sourceCode haskell \ " ><span class= \ " op \ " >>>=</span></code> "
, " nolanguage " =: codeWith ( " " , [ " nolanguage " ] , [] ) " >>= "
=?> " <code class= \ " nolanguage \ " >>>=</code> "
]
, testGroup " images "
[ " alt with formatting " =:
image " /url " " title " ( " my " <> emph " image " )
=?> " <img src= \ " /url \ " title= \ " title \ " alt= \ " my image \ " /> "
]
, testGroup " blocks "
[ " definition list with empty <dt> " =:
definitionList [ ( mempty , [ para $ text " foo bar " ] ) ]
=?> " <dl><dt></dt><dd><p>foo bar</p></dd></dl> "
, " heading with disallowed attributes " =:
headerWith ( " " , [] , [ ( " invalid " , " 1 " ) , ( " lang " , " en " ) ] ) 1 " test "
=?>
" <h1 lang= \ " en \ " >test</h1> "
]
, testGroup " quotes "
[ " quote with cite attribute (without q-tags) " =:
doubleQuoted ( spanWith ( " " , [] , [ ( " cite " , " http://example.org " ) ] ) ( str " examples " ) )
=?> " “<span cite= \ " http://example.org \ " >examples</span>” "
, tQ " quote with cite attribute (with q-tags) " $
doubleQuoted ( spanWith ( " " , [] , [ ( " cite " , " http://example.org " ) ] ) ( str " examples " ) )
=?> " <q cite= \ " http://example.org \ " >examples</q> "
]
, testGroup " sample "
[ " sample should be rendered correctly " =:
plain ( codeWith ( " " , [ " sample " ] , [] ) " Answer is 42 " ) =?>
" <samp>Answer is 42</samp> "
]
, testGroup " variable "
[ " variable should be rendered correctly " =:
plain ( codeWith ( " " , [ " variable " ] , [] ) " result " ) =?>
" <var>result</var> "
]
, testGroup " sample with style "
[ " samp should wrap highlighted code " =:
codeWith ( " " , [ " sample " , " haskell " ] , [] ) " >>= "
=?> ( " <samp><code class= \ " sourceCode haskell \ " > " ++
" <span class= \ " op \ " >>>=</span></code></samp> " )
]
, testGroup " variable with style "
[ " var should wrap highlighted code " =:
codeWith ( " " , [ " haskell " , " variable " ] , [] ) " >>= "
=?> ( " <var><code class= \ " sourceCode haskell \ " > " ++
" <span class= \ " op \ " >>>=</span></code></var> " )
]
2021-09-10 18:30:05 +02:00
, testGroup " footnotes "
[ test ( htmlWithOpts def { writerReferenceLocation = EndOfDocument } )
" at the end of a document " $
noteTestDoc =?>
concat
[ " <h1>Page title</h1> "
, " <h2>First section</h2> "
, " <p>This is a footnote.<a href= \ " #fn1 \ " class= \ " footnote-ref \ " id= \ " fnref1 \ " ><sup>1</sup></a> And this is a <a href= \ " https://www.google.com \ " >link</a>.</p> "
, " <blockquote><p>A note inside a block quote.<a href= \ " #fn2 \ " class= \ " footnote-ref \ " id= \ " fnref2 \ " ><sup>2</sup></a></p><p>A second paragraph.</p></blockquote> "
, " <h2>Second section</h2> "
, " <p>Some more text.</p> "
, " <div class= \ " footnotes footnotes-end-of-document \ " ><hr /><ol><li id= \ " fn1 \ " ><p>Down here.<a href= \ " #fnref1 \ " class= \ " footnote-back \ " >↩︎</a></p></li><li id= \ " fn2 \ " ><p>The second note.<a href= \ " #fnref2 \ " class= \ " footnote-back \ " >↩︎</a></p></li></ol></div> "
]
, test ( htmlWithOpts def { writerReferenceLocation = EndOfBlock } )
" at the end of a block " $
noteTestDoc =?>
concat
[ " <h1>Page title</h1> "
, " <h2>First section</h2> "
, " <p>This is a footnote.<a href= \ " #fn1 \ " class= \ " footnote-ref \ " id= \ " fnref1 \ " ><sup>1</sup></a> And this is a <a href= \ " https://www.google.com \ " >link</a>.</p> "
, " <div class= \ " footnotes footnotes-end-of-block \ " ><ol><li id= \ " fn1 \ " ><p>Down here.<a href= \ " #fnref1 \ " class= \ " footnote-back \ " >↩︎</a></p></li></ol></div> "
, " <blockquote><p>A note inside a block quote.<a href= \ " #fn2 \ " class= \ " footnote-ref \ " id= \ " fnref2 \ " ><sup>2</sup></a></p><p>A second paragraph.</p></blockquote> "
, " <div class= \ " footnotes footnotes-end-of-block \ " ><ol start= \ " 2 \ " ><li id= \ " fn2 \ " ><p>The second note.<a href= \ " #fnref2 \ " class= \ " footnote-back \ " >↩︎</a></p></li></ol></div> "
, " <h2>Second section</h2> "
, " <p>Some more text.</p> "
]
, test ( htmlWithOpts def { writerReferenceLocation = EndOfSection } )
" at the end of a section " $
noteTestDoc =?>
concat
[ " <h1>Page title</h1> "
, " <h2>First section</h2> "
, " <p>This is a footnote.<a href= \ " #fn1 \ " class= \ " footnote-ref \ " id= \ " fnref1 \ " ><sup>1</sup></a> And this is a <a href= \ " https://www.google.com \ " >link</a>.</p> "
, " <blockquote><p>A note inside a block quote.<a href= \ " #fn2 \ " class= \ " footnote-ref \ " id= \ " fnref2 \ " ><sup>2</sup></a></p><p>A second paragraph.</p></blockquote> "
, " <div class= \ " footnotes footnotes-end-of-section \ " ><hr /><ol><li id= \ " fn1 \ " ><p>Down here.<a href= \ " #fnref1 \ " class= \ " footnote-back \ " >↩︎</a></p></li><li id= \ " fn2 \ " ><p>The second note.<a href= \ " #fnref2 \ " class= \ " footnote-back \ " >↩︎</a></p></li></ol></div> "
, " <h2>Second section</h2> "
, " <p>Some more text.</p> "
]
, test ( htmlWithOpts def { writerReferenceLocation = EndOfSection , writerSectionDivs = True } )
" at the end of a section, with section divs " $
noteTestDoc =?>
-- Footnotes are rendered _after_ their section (in this case after the level2 section
-- that contains it).
concat
[ " <div class= \ " section level1 \ " > "
, " <h1>Page title</h1> "
, " <div class= \ " section level2 \ " > "
, " <h2>First section</h2> "
, " <p>This is a footnote.<a href= \ " #fn1 \ " class= \ " footnote-ref \ " id= \ " fnref1 \ " ><sup>1</sup></a> And this is a <a href= \ " https://www.google.com \ " >link</a>.</p><blockquote><p>A note inside a block quote.<a href= \ " #fn2 \ " class= \ " footnote-ref \ " id= \ " fnref2 \ " ><sup>2</sup></a></p><p>A second paragraph.</p></blockquote> "
, " </div> "
, " <div class= \ " footnotes footnotes-end-of-section \ " ><hr /><ol><li id= \ " fn1 \ " ><p>Down here.<a href= \ " #fnref1 \ " class= \ " footnote-back \ " >↩︎</a></p></li><li id= \ " fn2 \ " ><p>The second note.<a href= \ " #fnref2 \ " class= \ " footnote-back \ " >↩︎</a></p></li></ol></div> "
, " <div class= \ " section level2 \ " ><h2>Second section</h2><p>Some more text.</p></div> "
, " </div> "
]
]
2021-05-17 15:37:25 +02:00
]
where
tQ :: ( ToString a , ToPandoc a )
=> String -> ( a , String ) -> TestTree
tQ = test htmlQTags