aboutsummaryrefslogtreecommitdiff
path: root/test/Tests/Readers/Org/Meta.hs
blob: 041016f643ab154506db45945d031aa0331c8a0d (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
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Tests.Readers.Org.Meta
   Copyright   : © 2014-2020 Albert Krewinkel
   License     : GNU GPL, version 2 or above

   Maintainer  : Albert Krewinkel <albert@zeitkraut.de>
   Stability   : alpha
   Portability : portable

Tests parsing of org meta data (mostly lines starting with @#+@).
-}
module Tests.Readers.Org.Meta (tests) where

import Prelude
import Test.Tasty (TestTree, testGroup)
import Tests.Helpers ((=?>))
import Tests.Readers.Org.Shared ((=:), spcSep)
import Text.Pandoc
import Text.Pandoc.Builder
import qualified Data.Text as T

tests :: [TestTree]
tests =
  [ testGroup "Comments"
    [ "Comment" =:
      "# Nothing to see here" =?>
      (mempty::Blocks)

    , "Hash not followed by space is text" =:
      "#-tag" =?>
      para "#-tag"

    , "Comment surrounded by Text" =:
      T.unlines [ "Before"
                , "# Comment"
                , "After"
                ] =?>
      mconcat [ para "Before"
              , para "After"
              ]
    ]

  , testGroup "Export settings"
    [ "Title" =:
      "#+title: Hello, World" =?>
      let titleInline = toList $ "Hello," <> space <> "World"
          meta = setMeta "title" (MetaInlines titleInline) nullMeta
      in Pandoc meta mempty

    , testGroup "Author"
      [ "sets 'author' field" =:
        "#+author: John /Emacs-Fanboy/ Doe" =?>
        let author = toList . spcSep $ [ "John", emph "Emacs-Fanboy", "Doe" ]
            meta = setMeta "author" (MetaInlines author) nullMeta
        in Pandoc meta mempty

      , "Multiple author lines" =:
        T.unlines [ "#+author: James Dewey Watson,"
                  , "#+author: Francis Harry Compton Crick"
                  ] =?>
        let watson = toList "James Dewey Watson,"
            crick = toList "Francis Harry Compton Crick"
            meta = setMeta "author"
                           (MetaInlines (watson ++ SoftBreak : crick))
                           nullMeta
        in Pandoc meta mempty
      ]

    , "Date" =:
      "#+date: Feb. *28*, 2014" =?>
      let date = toList . spcSep $ [ "Feb.", strong "28" <> ",", "2014" ]
          meta = setMeta "date" (MetaInlines date) nullMeta
      in Pandoc meta mempty

    , testGroup "Description"
      [ "Single line" =:
        "#+description: Explanatory text" =?>
        let description = [Str "Explanatory", Space, Str "text"]
            meta = setMeta "description" (MetaInlines description) nullMeta
        in Pandoc meta mempty

      , "Multiline" =:
        T.unlines [ "#+description: /Short/ introduction"
                  , "#+description: to Org-mode"
                  ] =?>
        let description = [ Emph [Str "Short"], Space, Str "introduction"
                          , SoftBreak
                          , Str "to", Space, Str "Org-mode"
                          ]
            meta = setMeta "description" (MetaInlines description) nullMeta
        in Pandoc meta mempty
      ]

    , "Subtitle" =:
      T.unlines [ "#+subtitle: Your Life in"
                , "#+subtitle: /Plain/ Text"
                ] =?>
      let subtitle = "Your Life in" <> softbreak <> emph "Plain" <> " Text"
      in Pandoc (setMeta "subtitle" (toMetaValue subtitle) nullMeta) mempty

    , "Keywords" =:
      T.unlines [ "#+keywords: pandoc, testing,"
                , "#+keywords: Org"
                ] =?>
      let keywords = toList $ "pandoc, testing," <> softbreak <> "Org"
          meta = setMeta "keywords" (MetaInlines keywords) nullMeta
      in Pandoc meta mempty

    , "Institute" =:
      "#+institute: ACME Inc." =?>
      Pandoc (setMeta "institute" ("ACME Inc." :: Inlines) nullMeta) mempty

    , "Document language" =:
      "#+LANGUAGE: de-DE" =?>
      Pandoc (setMeta "lang" (MetaString "de-DE") nullMeta) mempty

    , testGroup "LaTeX"
      [ "LATEX_HEADER" =:
        "#+latex_header: \\usepackage{tikz}" =?>
        let latexInlines = rawInline "latex" "\\usepackage{tikz}"
            inclList = MetaList [MetaInlines (toList latexInlines)]
            meta = setMeta "header-includes" inclList nullMeta
        in Pandoc meta mempty

      , "LATEX_HEADER_EXTRA" =:
        "#+latex_header_extra: \\usepackage{calc}" =?>
        let latexInlines = rawInline "latex" "\\usepackage{calc}"
            inclList = toMetaValue [latexInlines]
        in Pandoc (setMeta "header-includes" inclList nullMeta) mempty

      , testGroup "LaTeX_CLASS"
        [ "stored as documentclass" =:
          "#+latex_class: article" =?>
          let meta = setMeta "documentclass" (MetaString "article") nullMeta
          in Pandoc meta mempty

        , "last definition takes precedence" =:
          T.unlines [ "#+latex_class: this will not be used"
                    , "#+latex_class: report"
                    ] =?>
          let meta = setMeta "documentclass" (MetaString "report") nullMeta
          in Pandoc meta mempty
        ]

      , "LATEX_CLASS_OPTIONS as classoption" =:
        "#+latex_class_options: [a4paper]" =?>
        let meta = setMeta "classoption" (MetaString "a4paper") nullMeta
        in Pandoc meta mempty
      ]

    , testGroup "HTML"
      [ "HTML_HEAD values are added to header-includes" =:
        "#+html_head: <meta/>" =?>
        let html = rawInline "html" "<meta/>"
            inclList = MetaList [MetaInlines (toList html)]
            meta = setMeta "header-includes" inclList nullMeta
        in Pandoc meta mempty

      , "HTML_HEAD_EXTRA behaves like HTML_HEAD" =:
        T.unlines [ "#+html_head: <meta name=\"generator\" content=\"pandoc\">"
                  , "#+html_head_extra: <meta charset=\"utf-8\">"
                  ] =?>
        let generator = rawInline "html"
                                  "<meta name=\"generator\" content=\"pandoc\">"
            charset = rawInline "html" "<meta charset=\"utf-8\">"
            inclList = toMetaValue [generator, charset]
        in Pandoc (setMeta "header-includes" inclList nullMeta) mempty
      ]
    ]

  , testGroup "Non-export keywords"
    [ testGroup "#+link"
      [ "Link abbreviation" =:
        T.unlines [ "#+link: wp https://en.wikipedia.org/wiki/%s"
                  , "[[wp:Org_mode][Wikipedia on Org-mode]]"
                  ] =?>
        para (link "https://en.wikipedia.org/wiki/Org_mode" ""
               ("Wikipedia" <> space <> "on" <> space <> "Org-mode"))

      , "Link abbreviation, defined after first use" =:
        T.unlines [ "[[zl:non-sense][Non-sense articles]]"
                  , "#+link: zl http://zeitlens.com/tags/%s.html"
                  ] =?>
        para (link "http://zeitlens.com/tags/non-sense.html" ""
               ("Non-sense" <> space <> "articles"))

      , "Link abbreviation, URL encoded arguments" =:
        T.unlines [ "#+link: expl http://example.com/%h/foo"
                  , "[[expl:Hello, World!][Moin!]]"
                  ] =?>
        para (link "http://example.com/Hello%2C%20World%21/foo" "" "Moin!")

      , "Link abbreviation, append arguments" =:
        T.unlines [ "#+link: expl http://example.com/"
                  , "[[expl:foo][bar]]"
                  ] =?>
        para (link "http://example.com/foo" "" "bar")
      ]

    , testGroup "emphasis config"
      [ "Changing pre and post chars for emphasis" =:
        T.unlines [ "#+pandoc-emphasis-pre: \"[)\""
                  , "#+pandoc-emphasis-post: \"]\\n\""
                  , "([/emph/])*foo*"
                  ] =?>
        para ("([" <> emph "emph" <> "])" <> strong "foo")

      , "setting an invalid value restores the default" =:
        T.unlines [ "#+pandoc-emphasis-pre: \"[\""
                  , "#+pandoc-emphasis-post: \"]\""
                  , "#+pandoc-emphasis-pre:"
                  , "#+pandoc-emphasis-post:"
                  , "[/noemph/]"
                  ] =?>
        para "[/noemph/]"
      ]

    , "Unknown keyword" =:
      T.unlines [ "#+unknown_keyword: Chumbawamba"
                , "#+another_unknown: Blur"
                ] =?>
      rawBlock "org" "#+unknown_keyword: Chumbawamba" <>
      rawBlock "org" "#+another_unknown: Blur"
    ]

  , "Properties drawer" =:
      T.unlines [ "  :PROPERTIES:"
                , "  :setting: foo"
                , "  :END:"
                ] =?>
      (mempty::Blocks)

  , "Logbook drawer" =:
      T.unlines [ "  :LogBook:"
                , "  - State \"DONE\"       from \"TODO\"       [2014-03-03 Mon 11:00]"
                , "  :END:"
                ] =?>
      (mempty::Blocks)

  , "Drawer surrounded by text" =:
      T.unlines [ "Before"
                , ":PROPERTIES:"
                , ":END:"
                , "After"
                ] =?>
      para "Before" <> para "After"

  , "Drawer markers must be the only text in the line" =:
      T.unlines [ "  :LOGBOOK: foo"
                , "  :END: bar"
                ] =?>
      para (":LOGBOOK: foo" <> softbreak <> ":END: bar")

  , "Drawers can be arbitrary" =:
      T.unlines [ ":FOO:"
                , "/bar/"
                , ":END:"
                ] =?>
      divWith (mempty, ["FOO", "drawer"], mempty) (para $ emph "bar")

  , "Anchor reference" =:
      T.unlines [ "<<link-here>> Target."
                , ""
                , "[[link-here][See here!]]"
                ] =?>
      (para (spanWith ("link-here", [], []) mempty <> "Target.") <>
       para (link "#link-here" "" ("See" <> space <> "here!")))

  , "Search links are read as emph" =:
      "[[Wally][Where's Wally?]]" =?>
      para (emph $ "Where's" <> space <> "Wally?")

  , "Link to nonexistent anchor" =:
      T.unlines [ "<<link-here>> Target."
                , ""
                , "[[link$here][See here!]]"
                ] =?>
      (para (spanWith ("link-here", [], []) mempty <> "Target.") <>
       para (emph ("See" <> space <> "here!")))
  ]