2011-01-22 05:50:18 +01:00
{- # LANGUAGE OverloadedStrings # -}
2011-01-12 19:10:56 +01:00
module Tests.Readers.LaTeX ( tests ) where
2011-01-12 14:16:35 +01:00
2017-03-14 17:05:36 +01:00
import Test.Tasty
2011-01-13 19:59:44 +01:00
import Tests.Helpers
2011-01-19 08:34:34 +01:00
import Text.Pandoc
2017-03-04 13:03:41 +01:00
import Text.Pandoc.Arbitrary ( )
import Text.Pandoc.Builder
2017-06-10 23:39:49 +02:00
import Data.Text ( Text )
2017-06-10 18:26:44 +02:00
import qualified Data.Text as T
2011-01-19 08:34:34 +01:00
2017-06-10 18:26:44 +02:00
latex :: Text -> Pandoc
2017-01-15 20:42:00 +01:00
latex = purely $ readLaTeX def {
readerExtensions = getDefaultExtensions " latex " }
2011-01-22 05:50:18 +01:00
2012-02-05 22:23:06 +01:00
infix 4 =:
2011-01-22 05:50:18 +01:00
( =: ) :: ToString c
2017-06-10 18:26:44 +02:00
=> String -> ( Text , c ) -> TestTree
2011-01-22 05:50:18 +01:00
( =: ) = test latex
2011-01-12 14:16:35 +01:00
2015-03-08 15:17:09 +01:00
simpleTable' :: [ Alignment ] -> [ [ Blocks ] ] -> Blocks
simpleTable' aligns = table " " ( zip aligns ( repeat 0.0 ) )
( map ( const mempty ) aligns )
2017-03-14 17:05:36 +01:00
tests :: [ TestTree ]
2011-01-13 20:11:55 +01:00
tests = [ testGroup " basic "
2011-01-19 08:34:34 +01:00
[ " simple " =:
2011-01-23 01:01:14 +01:00
" word " =?> para " word "
2011-01-19 08:34:34 +01:00
, " space " =:
2013-12-20 02:43:25 +01:00
" some text " =?> para " some text "
2011-01-19 08:34:34 +01:00
, " emphasized " =:
2011-01-22 05:50:18 +01:00
" \ \ emph{emphasized} " =?> para ( emph " emphasized " )
2011-01-13 20:11:55 +01:00
]
, testGroup " headers "
2011-01-19 08:34:34 +01:00
[ " level 1 " =:
2013-09-01 18:13:31 +02:00
" \ \ section{header} " =?> headerWith ( " header " , [] , [] ) 1 " header "
2011-01-19 08:34:34 +01:00
, " level 2 " =:
2013-09-01 18:13:31 +02:00
" \ \ subsection{header} " =?> headerWith ( " header " , [] , [] ) 2 " header "
2011-01-19 08:34:34 +01:00
, " level 3 " =:
2013-09-01 18:13:31 +02:00
" \ \ subsubsection{header} " =?> headerWith ( " header " , [] , [] ) 3 " header "
2011-01-19 08:34:34 +01:00
, " emph " =:
2011-01-22 05:50:18 +01:00
" \ \ section{text \ \ emph{emph}} " =?>
2013-09-01 18:13:31 +02:00
headerWith ( " text-emph " , [] , [] ) 1 ( " text " <> space <> emph " emph " )
2011-01-19 08:34:34 +01:00
, " link " =:
2011-01-22 05:50:18 +01:00
" \ \ section{text \ \ href{/url}{link}} " =?>
2013-09-01 18:13:31 +02:00
headerWith ( " text-link " , [] , [] ) 1 ( " text " <> space <> link " /url " " " " link " )
2011-01-13 20:11:55 +01:00
]
2011-01-29 17:47:00 +01:00
2012-02-08 04:24:09 +01:00
, testGroup " math "
[ " escaped $ " =:
2012-02-08 04:24:09 +01:00
" $x= \ \ $4$ " =?> para ( math " x= \ \ $4 " )
2012-02-08 04:24:09 +01:00
]
2011-01-30 17:21:48 +01:00
, testGroup " space and comments "
[ " blank lines + space at beginning " =:
" \ n \ n hi " =?> para " hi "
, " blank lines + space + comments " =:
" % my comment \ n \ n \ n % another \ n \ n hi " =?> para " hi "
, " comment in paragraph " =:
Rewrote LaTeX reader with proper tokenization.
This rewrite is primarily motivated by the need to
get macros working properly. A side benefit is that the
reader is significantly faster (27s -> 19s in one
benchmark, and there is a lot of room for further
optimization).
We now tokenize the input text, then parse the token stream.
Macros modify the token stream, so they should now be effective
in any context, including math. Thus, we no longer need the clunky
macro processing capacities of texmath.
A custom state LaTeXState is used instead of ParserState.
This, plus the tokenization, will require some rewriting
of the exported functions rawLaTeXInline, inlineCommand,
rawLaTeXBlock.
* Added Text.Pandoc.Readers.LaTeX.Types (new exported module).
Exports Macro, Tok, TokType, Line, Column. [API change]
* Text.Pandoc.Parsing: adjusted type of `insertIncludedFile`
so it can be used with token parser.
* Removed old texmath macro stuff from Parsing.
Use Macro from Text.Pandoc.Readers.LaTeX.Types instead.
* Removed texmath macro material from Markdown reader.
* Changed types for Text.Pandoc.Readers.LaTeX's
rawLaTeXInline and rawLaTeXBlock. (Both now return a String,
and they are polymorphic in state.)
* Added orgMacros field to OrgState. [API change]
* Removed readerApplyMacros from ReaderOptions.
Now we just check the `latex_macros` reader extension.
* Allow `\newcommand\foo{blah}` without braces.
Fixes #1390.
Fixes #2118.
Fixes #3236.
Fixes #3779.
Fixes #934.
Fixes #982.
2017-07-01 19:31:43 +02:00
" hi % this is a comment \ n there \ n " =?>
para ( " hi " <> softbreak <> " there " )
2011-01-30 17:21:48 +01:00
]
2013-08-22 20:15:36 +02:00
, testGroup " code blocks "
[ " identifier " =:
" \ \ begin{lstlisting}[label=test] \ \ end{lstlisting} " =?> codeBlockWith ( " test " , [] , [ ( " label " , " test " ) ] ) " "
, " no identifier " =:
" \ \ begin{lstlisting} \ \ end{lstlisting} " =?> codeBlock " "
]
2015-03-08 15:17:09 +01:00
, testGroup " tables "
[ " Single cell table " =:
" \ \ begin{tabular}{|l|}Test \ \ \ \ \ \ end{tabular} " =?>
simpleTable' [ AlignLeft ] [ [ plain " Test " ] ]
, " Multi cell table " =:
" \ \ begin{tabular}{|rl|}One & Two \ \ \ \ \ \ end{tabular} " =?>
simpleTable' [ AlignRight , AlignLeft ] [ [ plain " One " , plain " Two " ] ]
, " Multi line table " =:
2017-06-10 18:26:44 +02:00
T . unlines [ " \ \ begin{tabular}{|c|} "
2015-03-08 15:17:09 +01:00
, " One \ \ \ \ "
, " Two \ \ \ \ "
, " Three \ \ \ \ "
, " \ \ end{tabular} " ] =?>
simpleTable' [ AlignCenter ]
[ [ plain " One " ] , [ plain " Two " ] , [ plain " Three " ] ]
, " Empty table " =:
" \ \ begin{tabular}{} \ \ end{tabular} " =?>
simpleTable' [] []
, " Table with fixed column width " =:
" \ \ begin{tabular}{|p{5cm}r|}One & Two \ \ \ \ \ \ end{tabular} " =?>
simpleTable' [ AlignLeft , AlignRight ] [ [ plain " One " , plain " Two " ] ]
, " Table with empty column separators " =:
" \ \ begin{tabular}{@{}r@{}l}One & Two \ \ \ \ \ \ end{tabular} " =?>
simpleTable' [ AlignRight , AlignLeft ] [ [ plain " One " , plain " Two " ] ]
2015-03-08 15:47:39 +01:00
, " Table with custom column separators " =:
2017-06-10 18:26:44 +02:00
T . unlines [ " \ \ begin{tabular}{@{($ \ \ to$)}r@{ \ \ hspace{2cm}}l} "
2015-03-08 15:47:39 +01:00
, " One&Two \ \ \ \ "
, " \ \ end{tabular} " ] =?>
simpleTable' [ AlignRight , AlignLeft ] [ [ plain " One " , plain " Two " ] ]
2015-03-08 15:30:05 +01:00
, " Table with vertical alignment argument " =:
" \ \ begin{tabular}[t]{r|r}One & Two \ \ \ \ \ \ end{tabular} " =?>
simpleTable' [ AlignRight , AlignRight ] [ [ plain " One " , plain " Two " ] ]
2015-03-08 15:17:09 +01:00
]
2011-01-29 17:47:00 +01:00
, testGroup " citations "
[ natbibCitations
, biblatexCitations
]
2015-04-13 02:22:39 +02:00
, let hex = [ '0' .. '9' ] ++ [ 'a' .. 'f' ] in
testGroup " Character Escapes "
[ " Two-character escapes " =:
2017-06-10 18:26:44 +02:00
mconcat [ " ^^ " <> T . pack [ i , j ] | i <- hex , j <- hex ] =?>
2015-04-13 02:22:39 +02:00
para ( str [ '\ 0 '.. '\ 255 ' ] )
, " One-character escapes " =:
2017-06-10 18:26:44 +02:00
mconcat [ " ^^ " <> T . pack [ i ] | i <- hex ] =?>
2015-04-13 02:22:39 +02:00
para ( str $ [ 'p' .. 'y' ] ++ [ '!' .. '&' ] )
]
2011-01-12 14:16:35 +01:00
]
2011-01-29 17:47:00 +01:00
baseCitation :: Citation
2012-10-22 08:16:23 +02:00
baseCitation = Citation { citationId = " item1 "
, citationPrefix = []
, citationSuffix = []
, citationMode = AuthorInText
, citationNoteNum = 0
2012-10-23 04:11:56 +02:00
, citationHash = 0
2012-10-28 17:36:15 +01:00
}
2011-01-29 17:47:00 +01:00
2012-02-06 21:41:34 +01:00
rt :: String -> Inlines
2013-03-17 16:48:29 +01:00
rt = rawInline " latex "
2012-02-06 21:41:34 +01:00
2017-03-14 17:05:36 +01:00
natbibCitations :: TestTree
2011-01-29 17:47:00 +01:00
natbibCitations = testGroup " natbib "
[ " citet " =: " \ \ citet{item1} "
2012-02-06 21:41:34 +01:00
=?> para ( cite [ baseCitation ] ( rt " \ \ citet{item1} " ) )
2011-01-29 17:47:00 +01:00
, " suffix " =: " \ \ citet[p.~30]{item1} "
=?> para
2013-07-21 20:44:49 +02:00
( cite [ baseCitation { citationSuffix = toList $ text " p. \ 160 \ & 30 " } ] ( rt " \ \ citet[p.~30]{item1} " ) )
2011-01-29 17:47:00 +01:00
, " suffix long " =: " \ \ citet[p.~30, with suffix]{item1} "
=?> para ( cite [ baseCitation { citationSuffix =
2013-07-21 20:44:49 +02:00
toList $ text " p. \ 160 \ & 30, with suffix " } ] ( rt " \ \ citet[p.~30, with suffix]{item1} " ) )
2011-01-29 17:47:00 +01:00
, " multiple " =: " \ \ citeauthor{item1} \ \ citetext{ \ \ citeyear{item1}; \ \ citeyear[p.~30]{item2}; \ \ citealp[see also][]{item3}} "
=?> para ( cite [ baseCitation { citationMode = AuthorInText }
, baseCitation { citationMode = SuppressAuthor
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 30 " ]
2011-01-29 17:47:00 +01:00
, citationId = " item2 " }
, baseCitation { citationId = " item3 "
, citationPrefix = [ Str " see " , Space , Str " also " ]
, citationMode = NormalCitation }
2012-02-06 21:41:34 +01:00
] ( rt " \ \ citetext{ \ \ citeyear{item1}; \ \ citeyear[p.~30]{item2}; \ \ citealp[see also][]{item3}} " ) )
2011-01-29 17:47:00 +01:00
, " group " =: " \ \ citetext{ \ \ citealp[see][p.~34--35]{item1}; \ \ citealp[also][chap. 3]{item3}} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
, citationPrefix = [ Str " see " ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 34 \ 8211 \ & 35 " ] }
2011-01-29 17:47:00 +01:00
, baseCitation { citationMode = NormalCitation
, citationId = " item3 "
, citationPrefix = [ Str " also " ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " chap. " , Space , Str " 3 " ] }
2012-02-06 21:41:34 +01:00
] ( rt " \ \ citetext{ \ \ citealp[see][p.~34--35]{item1}; \ \ citealp[also][chap. 3]{item3}} " ) )
2011-01-29 17:47:00 +01:00
, " suffix and locator " =: " \ \ citep[pp.~33, 35--37, and nowhere else]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " pp. \ 160 \ & 33, " , Space , Str " 35 \ 8211 \ & 37, " , Space , Str " and " , Space , Str " nowhere " , Space , Str " else " ] } ] ( rt " \ \ citep[pp.~33, 35--37, and nowhere else]{item1} " ) )
2011-01-29 17:47:00 +01:00
, " suffix only " =: " \ \ citep[and nowhere else]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
2013-07-21 20:44:49 +02:00
, citationSuffix = toList $ text " and nowhere else " } ] ( rt " \ \ citep[and nowhere else]{item1} " ) )
2011-01-29 17:47:00 +01:00
, " no author " =: " \ \ citeyearpar{item1}, and now Doe with a locator \ \ citeyearpar[p.~44]{item2} "
2012-02-06 21:41:34 +01:00
=?> para ( cite [ baseCitation { citationMode = SuppressAuthor } ] ( rt " \ \ citeyearpar{item1} " ) <>
2011-12-13 23:29:07 +01:00
text " , and now Doe with a locator " <>
2011-01-29 17:47:00 +01:00
cite [ baseCitation { citationMode = SuppressAuthor
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 44 " ]
2012-02-06 21:41:34 +01:00
, citationId = " item2 " } ] ( rt " \ \ citeyearpar[p.~44]{item2} " ) )
2011-01-29 17:47:00 +01:00
, " markup " =: " \ \ citep[ \ \ emph{see}][p. \ \ textbf{32}]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
, citationPrefix = [ Emph [ Str " see " ] ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. " , Space ,
2012-02-06 21:41:34 +01:00
Strong [ Str " 32 " ] ] } ] ( rt " \ \ citep[ \ \ emph{see}][p. \ \ textbf{32}]{item1} " ) )
2011-01-29 17:47:00 +01:00
]
2017-03-14 17:05:36 +01:00
biblatexCitations :: TestTree
2011-01-29 17:47:00 +01:00
biblatexCitations = testGroup " biblatex "
2011-01-29 18:01:30 +01:00
[ " textcite " =: " \ \ textcite{item1} "
2012-02-06 21:41:34 +01:00
=?> para ( cite [ baseCitation ] ( rt " \ \ textcite{item1} " ) )
2011-01-29 18:01:30 +01:00
, " suffix " =: " \ \ textcite[p.~30]{item1} "
=?> para
2013-07-21 20:44:49 +02:00
( cite [ baseCitation { citationSuffix = toList $ text " p. \ 160 \ & 30 " } ] ( rt " \ \ textcite[p.~30]{item1} " ) )
2011-01-29 18:01:30 +01:00
, " suffix long " =: " \ \ textcite[p.~30, with suffix]{item1} "
=?> para ( cite [ baseCitation { citationSuffix =
2013-07-21 20:44:49 +02:00
toList $ text " p. \ 160 \ & 30, with suffix " } ] ( rt " \ \ textcite[p.~30, with suffix]{item1} " ) )
2011-01-29 18:01:30 +01:00
, " multiple " =: " \ \ textcites{item1}[p.~30]{item2}[see also][]{item3} "
=?> para ( cite [ baseCitation { citationMode = AuthorInText }
, baseCitation { citationMode = NormalCitation
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 30 " ]
2011-01-29 18:01:30 +01:00
, citationId = " item2 " }
, baseCitation { citationId = " item3 "
, citationPrefix = [ Str " see " , Space , Str " also " ]
, citationMode = NormalCitation }
2012-02-06 21:41:34 +01:00
] ( rt " \ \ textcites{item1}[p.~30]{item2}[see also][]{item3} " ) )
2011-01-29 18:01:30 +01:00
, " group " =: " \ \ autocites[see][p.~34--35]{item1}[also][chap. 3]{item3} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
, citationPrefix = [ Str " see " ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 34 \ 8211 \ & 35 " ] }
2011-01-29 18:01:30 +01:00
, baseCitation { citationMode = NormalCitation
, citationId = " item3 "
, citationPrefix = [ Str " also " ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " chap. " , Space , Str " 3 " ] }
2012-02-06 21:41:34 +01:00
] ( rt " \ \ autocites[see][p.~34--35]{item1}[also][chap. 3]{item3} " ) )
2011-01-29 18:01:30 +01:00
, " suffix and locator " =: " \ \ autocite[pp.~33, 35--37, and nowhere else]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " pp. \ 160 \ & 33, " , Space , Str " 35 \ 8211 \ & 37, " , Space , Str " and " , Space , Str " nowhere " , Space , Str " else " ] } ] ( rt " \ \ autocite[pp.~33, 35--37, and nowhere else]{item1} " ) )
2011-01-29 18:01:30 +01:00
, " suffix only " =: " \ \ autocite[and nowhere else]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
2013-07-21 20:44:49 +02:00
, citationSuffix = toList $ text " and nowhere else " } ] ( rt " \ \ autocite[and nowhere else]{item1} " ) )
2011-01-29 18:01:30 +01:00
, " no author " =: " \ \ autocite*{item1}, and now Doe with a locator \ \ autocite*[p.~44]{item2} "
2012-02-06 21:41:34 +01:00
=?> para ( cite [ baseCitation { citationMode = SuppressAuthor } ] ( rt " \ \ autocite*{item1} " ) <>
2011-12-13 23:29:07 +01:00
text " , and now Doe with a locator " <>
2011-01-29 18:01:30 +01:00
cite [ baseCitation { citationMode = SuppressAuthor
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 44 " ]
2012-02-06 21:41:34 +01:00
, citationId = " item2 " } ] ( rt " \ \ autocite*[p.~44]{item2} " ) )
2011-01-29 18:01:30 +01:00
, " markup " =: " \ \ autocite[ \ \ emph{see}][p. \ \ textbf{32}]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
, citationPrefix = [ Emph [ Str " see " ] ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. " , Space ,
2012-02-06 21:41:34 +01:00
Strong [ Str " 32 " ] ] } ] ( rt " \ \ autocite[ \ \ emph{see}][p. \ \ textbf{32}]{item1} " ) )
2011-01-29 18:01:30 +01:00
, " parencite " =: " \ \ parencite{item1} "
2012-02-06 21:41:34 +01:00
=?> para ( cite [ baseCitation { citationMode = NormalCitation } ] ( rt " \ \ parencite{item1} " ) )
2011-01-29 17:47:00 +01:00
]