{- |

Twitter Card metadata, as described at
<https://developer.twitter.com/en/docs/twitter-for-websites/cards/guides/getting-started>.
This feature should be used alongside "Hakyll.Web.Meta.OpenGraph".
The following properties are supported:

+-------------------+----------------------------------------------------+
| @twitter:card@    | __Hardcoded__ card type = @"summary"@.             |
+-------------------+----------------------------------------------------+
| @twitter:creator@ | __Optional__ author's Twitter user name.           |
|                   | Taken from @twitter-creator@ context field, if set.|
+-------------------+----------------------------------------------------+
| @twitter:site@    | __Optional__ publication's Twitter user name.      |
|                   | Taken from @twitter-site@ context field, if set.   |
+-------------------+----------------------------------------------------+

To use, add 'openGraphField' and 'twitterCardField' to the template context:

@
let
  context = 'defaultContext' <> …
  postContext =
    context
    <> 'openGraphField' "opengraph" context
    <> 'twitterCardField' "twitter" context
@

and update the template:

@
\<head>
  \<title>$title$\</title>
  \<link rel="stylesheet" type="text\/css" href="\/css\/default.css" />
  $if(opengraph)$$opengraph$$endif$
  $if(twitter)$$twitter$$endif$
\</head>
@

-}
module Hakyll.Web.Meta.TwitterCard
  ( twitterCardField
  ) where

import Hakyll.Core.Compiler
import Hakyll.Core.Item
import Hakyll.Web.Template
import Hakyll.Web.Template.Context

twitterCardField :: String -> Context String -> Context String
twitterCardField :: String -> Context String -> Context String
twitterCardField String
k Context String
ctx = forall a.
String -> ([String] -> Item a -> Compiler String) -> Context a
functionField String
k forall a b. (a -> b) -> a -> b
$ \[String]
_args Item String
i -> do
  Template
template <- Compiler Template
twitterCardTemplate
  forall a. Item a -> a
itemBody forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Template -> Context a -> Item a -> Compiler (Item String)
applyTemplate Template
template Context String
ctx Item String
i

twitterCardTemplate :: Compiler Template
twitterCardTemplate :: Compiler Template
twitterCardTemplate = do
  forall a. a -> Compiler (Item a)
makeItem String
twitterCardTemplateString forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Item String -> Compiler Template
compileTemplateItem

twitterCardTemplateString :: String
twitterCardTemplateString :: String
twitterCardTemplateString =
  String
"<meta name=\"twitter:card\" content=\"summary\" />\
  \$if(twitter-creator)$<meta name=\"twitter:creator\" content=\"$twitter-creator$\" />$endif$\
  \$if(twitter-site)$<meta name=\"twitter:site\" content=\"$twitter-site$\" />$endif$"