summaryrefslogtreecommitdiff
path: root/test/Tests/Readers/Org/Inline.hs
blob: da0d1db0ba194aef84df6a0195bfcc4cf8a2ad51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
{-# LANGUAGE OverloadedStrings #-}
module Tests.Readers.Org.Inline (tests) where

import Data.List (intersperse)
import Test.Tasty (TestTree, testGroup)
import Tests.Helpers ((=?>))
import Tests.Readers.Org.Shared ((=:), spcSep)
import Text.Pandoc
import Text.Pandoc.Builder
import Text.Pandoc.Shared (underlineSpan)
import qualified Data.Text as T
import qualified Tests.Readers.Org.Inline.Note as Note
import qualified Tests.Readers.Org.Inline.Smart as Smart

tests :: [TestTree]
tests =
  [ "Plain String" =:
      "Hello, World" =?>
      para (spcSep [ "Hello,", "World" ])

  , "Emphasis" =:
      "/Planet Punk/" =?>
      para (emph . spcSep $ ["Planet", "Punk"])

  , "Strong" =:
      "*Cider*" =?>
      para (strong "Cider")

  , "Strong Emphasis" =:
      "/*strength*/" =?>
      para (emph . strong $ "strength")

  , "Emphasized Strong preceded by space" =:
      " */super/*" =?>
      para (strong . emph $ "super")

  , "Underline" =:
      "_underline_" =?>
      para (underlineSpan "underline")

  , "Strikeout" =:
      "+Kill Bill+" =?>
      para (strikeout . spcSep $ [ "Kill", "Bill" ])

  , "Verbatim" =:
      "=Robot.rock()=" =?>
      para (code "Robot.rock()")

  , "Code" =:
      "~word for word~" =?>
      para (code "word for word")

  , "Math $..$" =:
      "$E=mc^2$" =?>
       para (math "E=mc^2")

  , "Math $$..$$" =:
      "$$E=mc^2$$" =?>
      para (displayMath "E=mc^2")

  , "Math \\[..\\]" =:
      "\\[E=ℎν\\]" =?>
      para (displayMath "E=ℎν")

  , "Math \\(..\\)" =:
      "\\(σ_x σ_p ≥ \\frac{ℏ}{2}\\)" =?>
      para (math "σ_x σ_p ≥ \\frac{ℏ}{2}")

  , "Symbol" =:
      "A * symbol" =?>
      para (str "A" <> space <> str "*" <> space <> "symbol")

  , "Superscript simple expression" =:
      "2^-λ" =?>
      para (str "2" <> superscript "-λ")

  , "Superscript multi char" =:
      "2^{n-1}" =?>
      para (str "2" <> superscript "n-1")

  , "Subscript simple expression" =:
      "a_n" =?>
      para (str "a" <> subscript "n")

  , "Subscript multi char" =:
      "a_{n+1}" =?>
      para (str "a" <> subscript "n+1")

  , "Linebreak" =:
      "line \\\\ \nbreak" =?>
      para ("line" <> linebreak <> "break")

  , "Inline note" =:
      "[fn::Schreib mir eine E-Mail]" =?>
      para (note $ para "Schreib mir eine E-Mail")

  , "Markup-chars not occuring on word break are symbols" =:
      T.unlines [ "this+that+ +so+on"
                , "seven*eight* nine*"
                , "+not+funny+"
                ] =?>
      para ("this+that+ +so+on" <> softbreak <>
            "seven*eight* nine*" <> softbreak <>
            strikeout "not+funny")

  , "No empty markup" =:
      "// ** __ <> == ~~ $$" =?>
      para (spcSep [ "//", "**", "__", "<>", "==", "~~", "$$" ])

  , "Adherence to Org's rules for markup borders" =:
      "/t/& a/ / ./r/ (*l*) /e/! /b/." =?>
      para (spcSep [ emph $ "t/&" <> space <> "a"
                   , "/"
                   , "./r/"
                   , "(" <> strong "l" <> ")"
                   , emph "e" <> "!"
                   , emph "b" <> "."
                   ])

  , "Quotes are allowed border chars" =:
      "/'yep/ *sure\"*" =?>
      para (emph "'yep" <> space <> strong "sure\"")

  , "Spaces are forbidden border chars" =:
      "/nada /" =?>
      para "/nada /"

  , "Markup should work properly after a blank line" =:
    T.unlines ["foo", "", "/bar/"] =?>
    para (text "foo") <>
    para (emph $ text "bar")

  , "Inline math must stay within three lines" =:
      T.unlines [ "$a", "b", "c$", "$d", "e", "f", "g$" ] =?>
      para (math "a\nb\nc" <> softbreak <>
            "$d" <> softbreak <> "e" <> softbreak <>
            "f" <> softbreak <> "g$")

  , "Single-character math" =:
      "$a$ $b$! $c$?" =?>
      para (spcSep [ math "a"
                   , "$b$!"
                   , math "c" <> "?"
                   ])

  , "Markup may not span more than two lines" =:
      "/this *is +totally\nnice+ not*\nemph/" =?>
      para ("/this" <> space <>
              strong ("is" <> space <>
                      strikeout ("totally" <>
                        softbreak <> "nice") <>
                      space <> "not") <>
              softbreak <> "emph/")

  , "Sub- and superscript expressions" =:
     T.unlines [ "a_(a(b)(c)d)"
               , "e^(f(g)h)"
               , "i_(jk)l)"
               , "m^()n"
               , "o_{p{q{}r}}"
               , "s^{t{u}v}"
               , "w_{xy}z}"
               , "1^{}2"
               , "3_{{}}"
               , "4^(a(*b(c*)d))"
               ] =?>
     para (mconcat $ intersperse softbreak
                  [ "a" <> subscript "(a(b)(c)d)"
                  , "e" <> superscript "(f(g)h)"
                  , "i" <> subscript "(jk)" <> "l)"
                  , "m" <> superscript "()" <> "n"
                  , "o" <> subscript "p{q{}r}"
                  , "s" <> superscript "t{u}v"
                  , "w" <> subscript "xy" <> "z}"
                  , "1" <> superscript "" <> "2"
                  , "3" <> subscript "{}"
                  , "4" <> superscript ("(a(" <> strong "b(c" <> ")d))")
                  ])
  , "Verbatim text can contain equal signes (=)" =:
      "=is_subst = True=" =?>
      para (code "is_subst = True")

  , testGroup "Images"
    [ "Image" =:
        "[[./sunset.jpg]]" =?>
    para (image "./sunset.jpg" "" "")

    , "Image with explicit file: prefix" =:
        "[[file:sunrise.jpg]]" =?>
    para (image "sunrise.jpg" "" "")

    , "Multiple images within a paragraph" =:
        T.unlines [ "[[file:sunrise.jpg]]"
                  , "[[file:sunset.jpg]]"
                  ] =?>
    para ((image "sunrise.jpg" "" "")
             <> softbreak
             <> (image "sunset.jpg" "" ""))

    , "Image with html attributes" =:
        T.unlines [ "#+ATTR_HTML: :width 50%"
                  , "[[file:guinea-pig.gif]]"
                  ] =?>
    para (imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "")
    ]

  , "Explicit link" =:
      "[[http://zeitlens.com/][pseudo-random /nonsense/]]" =?>
    para (link "http://zeitlens.com/" ""
                   ("pseudo-random" <> space <> emph "nonsense"))

  , "Self-link" =:
      "[[http://zeitlens.com/]]" =?>
    para (link "http://zeitlens.com/" "" "http://zeitlens.com/")

  , "Absolute file link" =:
      "[[/url][hi]]" =?>
    para (link "file:///url" "" "hi")

  , "Link to file in parent directory" =:
      "[[../file.txt][moin]]" =?>
    para (link "../file.txt" "" "moin")

  , "Empty link (for gitit interop)" =:
      "[[][New Link]]" =?>
    para (link "" "" "New Link")

  , "Image link" =:
      "[[sunset.png][file:dusk.svg]]" =?>
    para (link "sunset.png" "" (image "dusk.svg" "" ""))

  , "Image link with non-image target" =:
      "[[http://example.com][./logo.png]]" =?>
    para (link "http://example.com" "" (image "./logo.png" "" ""))

  , "Plain link" =:
      "Posts on http://zeitlens.com/ can be funny at times." =?>
    para (spcSep [ "Posts", "on"
                     , link "http://zeitlens.com/" "" "http://zeitlens.com/"
                     , "can", "be", "funny", "at", "times."
                     ])

  , "Angle link" =:
      "Look at <http://moltkeplatz.de> for fnords." =?>
    para (spcSep [ "Look", "at"
                     , link "http://moltkeplatz.de" "" "http://moltkeplatz.de"
                     , "for", "fnords."
                     ])

  , "Absolute file link" =:
      "[[file:///etc/passwd][passwd]]" =?>
    para (link "file:///etc/passwd" "" "passwd")

  , "File link" =:
      "[[file:target][title]]" =?>
    para (link "target" "" "title")

  , "Anchor" =:
      "<<anchor>> Link here later." =?>
    para (spanWith ("anchor", [], []) mempty <>
              "Link" <> space <> "here" <> space <> "later.")

  , "Inline code block" =:
      "src_emacs-lisp{(message \"Hello\")}" =?>
    para (codeWith ( ""
                       , [ "commonlisp" ]
                       , [ ("org-language", "emacs-lisp") ])
                       "(message \"Hello\")")

  , "Inline code block with arguments" =:
      "src_sh[:export both :results output]{echo 'Hello, World'}" =?>
    para (codeWith ( ""
                       , [ "bash" ]
                       , [ ("org-language", "sh")
                         , ("export", "both")
                         , ("results", "output")
                         ]
                       )
                       "echo 'Hello, World'")

  , "Inline code block with toggle" =:
      "src_sh[:toggle]{echo $HOME}" =?>
    para (codeWith ( ""
                       , [ "bash" ]
                       , [ ("org-language", "sh")
                         , ("toggle", "yes")
                         ]
                       )
                       "echo $HOME")

  , "Citation" =:
      "[@nonexistent]" =?>
      let citation = Citation
                     { citationId = "nonexistent"
                     , citationPrefix = []
                     , citationSuffix = []
                     , citationMode = NormalCitation
                     , citationNoteNum = 0
                     , citationHash = 0}
      in (para $ cite [citation] "[@nonexistent]")

  , "Citation containing text" =:
      "[see @item1 p. 34-35]" =?>
      let citation = Citation
                     { citationId = "item1"
                     , citationPrefix = [Str "see"]
                     , citationSuffix = [Space ,Str "p.",Space,Str "34-35"]
                     , citationMode = NormalCitation
                     , citationNoteNum = 0
                     , citationHash = 0}
      in (para $ cite [citation] "[see @item1 p. 34-35]")

  , "Org-ref simple citation" =:
    "cite:pandoc" =?>
    let citation = Citation
                   { citationId = "pandoc"
                   , citationPrefix = mempty
                   , citationSuffix = mempty
                   , citationMode = AuthorInText
                   , citationNoteNum = 0
                   , citationHash = 0
                   }
    in (para $ cite [citation] "cite:pandoc")

  , "Org-ref simple citation with underscores" =:
    "cite:pandoc_org_ref" =?>
    let citation = Citation
                   { citationId = "pandoc_org_ref"
                   , citationPrefix = mempty
                   , citationSuffix = mempty
                   , citationMode = AuthorInText
                   , citationNoteNum = 0
                   , citationHash = 0
                   }
    in (para $ cite [citation] "cite:pandoc_org_ref")

  , "Org-ref simple citation succeeded by comma" =:
    "cite:pandoc," =?>
    let citation = Citation
                   { citationId = "pandoc"
                   , citationPrefix = mempty
                   , citationSuffix = mempty
                   , citationMode = AuthorInText
                   , citationNoteNum = 0
                   , citationHash = 0
                   }
    in (para $ cite [citation] "cite:pandoc" <> str ",")

  , "Org-ref simple citation succeeded by dot" =:
    "cite:pandoc." =?>
    let citation = Citation
                   { citationId = "pandoc"
                   , citationPrefix = mempty
                   , citationSuffix = mempty
                   , citationMode = AuthorInText
                   , citationNoteNum = 0
                   , citationHash = 0
                   }
    in (para $ cite [citation] "cite:pandoc" <> str ".")

  , "Org-ref simple citation succeeded by colon" =:
    "cite:pandoc:" =?>
    let citation = Citation
                   { citationId = "pandoc"
                   , citationPrefix = mempty
                   , citationSuffix = mempty
                   , citationMode = AuthorInText
                   , citationNoteNum = 0
                   , citationHash = 0
                   }
    in (para $ cite [citation] "cite:pandoc" <> str ":")

  , "Org-ref simple citep citation" =:
    "citep:pandoc" =?>
    let citation = Citation
                   { citationId = "pandoc"
                   , citationPrefix = mempty
                   , citationSuffix = mempty
                   , citationMode = NormalCitation
                   , citationNoteNum = 0
                   , citationHash = 0
                   }
    in (para $ cite [citation] "citep:pandoc")

  , "Org-ref extended citation" =:
    "[[citep:Dominik201408][See page 20::, for example]]" =?>
    let citation = Citation
                   { citationId = "Dominik201408"
                   , citationPrefix = toList "See page 20"
                   , citationSuffix = toList ", for example"
                   , citationMode = NormalCitation
                   , citationNoteNum = 0
                   , citationHash = 0
                   }
    in (para $ cite [citation] "[[citep:Dominik201408][See page 20::, for example]]")

  , testGroup "Berkeley-style citations" $
    let pandocCite = Citation
          { citationId = "Pandoc"
          , citationPrefix = mempty
          , citationSuffix = mempty
          , citationMode = NormalCitation
          , citationNoteNum = 0
          , citationHash = 0
          }
        pandocInText = pandocCite { citationMode = AuthorInText }
        dominikCite = Citation
          { citationId = "Dominik201408"
          , citationPrefix = mempty
          , citationSuffix = mempty
          , citationMode = NormalCitation
          , citationNoteNum = 0
          , citationHash = 0
          }
        dominikInText = dominikCite { citationMode = AuthorInText }
    in [
        "Berkeley-style in-text citation" =:
          "See @Dominik201408." =?>
        para ("See "
                  <> cite [dominikInText] "@Dominik201408"
                  <> ".")

      , "Berkeley-style parenthetical citation list" =:
          "[(cite): see; @Dominik201408;also @Pandoc; and others]" =?>
          let pandocCite'  = pandocCite {
                               citationPrefix = toList "also"
                             , citationSuffix = toList "and others"
                             }
              dominikCite' = dominikCite {
                               citationPrefix = toList "see"
                             }
          in (para $ cite [dominikCite', pandocCite'] "")

      , "Berkeley-style plain citation list" =:
          "[cite: See; @Dominik201408; and @Pandoc; and others]" =?>
          let pandocCite' = pandocInText {
                              citationPrefix = toList "and"
                            }
          in (para $ "See "
                  <> cite [dominikInText] ""
                  <> "," <> space
                  <> cite [pandocCite'] ""
                  <> "," <> space <> "and others")
    ]

  , "Inline LaTeX symbol" =:
      "\\dots" =?>
      para "…"

  , "Inline LaTeX command" =:
      "\\textit{Emphasised}" =?>
      para (emph "Emphasised")

  , "Inline LaTeX command with spaces" =:
      "\\emph{Emphasis mine}" =?>
      para (emph "Emphasis mine")

  , "Inline LaTeX math symbol" =:
      "\\tau" =?>
      para (emph "τ")

  , "Unknown inline LaTeX command" =:
      "\\notacommand{foo}" =?>
      para (rawInline "latex" "\\notacommand{foo}")

  , "Export snippet" =:
      "@@html:<kbd>M-x org-agenda</kbd>@@" =?>
      para (rawInline "html" "<kbd>M-x org-agenda</kbd>")

  , "MathML symbol in LaTeX-style" =:
      "There is a hackerspace in Lübeck, Germany, called nbsp (unicode symbol: '\\nbsp')." =?>
      para "There is a hackerspace in Lübeck, Germany, called nbsp (unicode symbol: ' ')."

  , "MathML symbol in LaTeX-style, including braces" =:
      "\\Aacute{}stor" =?>
      para "Ástor"

  , "MathML copy sign" =:
      "\\copy" =?>
      para "©"

  , "MathML symbols, space separated" =:
      "\\ForAll \\Auml" =?>
      para "∀ Ä"

  , "LaTeX citation" =:
      "\\cite{Coffee}" =?>
      let citation = Citation
                     { citationId = "Coffee"
                     , citationPrefix = []
                     , citationSuffix = []
                     , citationMode = NormalCitation
                     , citationNoteNum = 0
                     , citationHash = 0}
      in (para . cite [citation] $ rawInline "latex" "\\cite{Coffee}")

  , "Macro" =:
      T.unlines [ "#+MACRO: HELLO /Hello, $1/"
                , "{{{HELLO(World)}}}"
                ] =?>
      para (emph "Hello, World")

  , "Macro repeting its argument" =:
      T.unlines [ "#+MACRO: HELLO $1$1"
                , "{{{HELLO(moin)}}}"
                ] =?>
      para "moinmoin"

  , "Macro called with too few arguments" =:
      T.unlines [ "#+MACRO: HELLO Foo $1 $2 Bar"
                , "{{{HELLO()}}}"
                ] =?>
      para "Foo Bar"

  , testGroup "Footnotes" Note.tests
  , testGroup "Smart punctuation" Smart.tests
  ]