{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternGuards #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Text.CSL.Eval.Output
-- Copyright   :  (c) Andrea Rossato
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Andrea Rossato <andrea.rossato@unitn.it>
-- Stability   :  unstable
-- Portability :  unportable
--
-- The CSL implementation
--
-----------------------------------------------------------------------------

module Text.CSL.Eval.Output where

import Prelude
import           Data.Maybe             (mapMaybe)
import           Data.String            (fromString)
import qualified Data.Text              as T
import           Text.CSL.Output.Pandoc (lastInline)
import           Text.CSL.Style
import           Text.CSL.Util          (capitalize, isPunct, titlecase,
                                         unTitlecase)
import           Text.Pandoc.Definition
import           Text.Pandoc.Walk       (walk)
import           Text.Parsec

-- Parse affix or delimiter into Formatted, splitting out
-- raw components in @{{format}}...{{/format}}@.
formatString :: String -> Formatted
formatString :: String -> Formatted
formatString s :: String
s =
  case Parsec String () [Inline]
-> String -> String -> Either ParseError [Inline]
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () [Inline]
pAffix String
s String
s of
       Left _    -> String -> Formatted
forall a. IsString a => String -> a
fromString String
s
       Right ils :: [Inline]
ils -> [Inline] -> Formatted
Formatted [Inline]
ils

pAffix :: Parsec String () [Inline]
pAffix :: Parsec String () [Inline]
pAffix = ParsecT String () Identity Inline -> Parsec String () [Inline]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (ParsecT String () Identity Inline
pRaw ParsecT String () Identity Inline
-> ParsecT String () Identity Inline
-> ParsecT String () Identity Inline
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT String () Identity Inline
pString ParsecT String () Identity Inline
-> ParsecT String () Identity Inline
-> ParsecT String () Identity Inline
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT String () Identity Inline
pSpace)

pRaw :: Parsec String () Inline
pRaw :: ParsecT String () Identity Inline
pRaw = ParsecT String () Identity Inline
-> ParsecT String () Identity Inline
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT String () Identity Inline
 -> ParsecT String () Identity Inline)
-> ParsecT String () Identity Inline
-> ParsecT String () Identity Inline
forall a b. (a -> b) -> a -> b
$ do
  String
_ <- String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string "{{"
  String
format <- ParsecT String () Identity Char
-> ParsecT String () Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
letter
  String
_ <- String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string "}}"
  String
contents <- ParsecT String () Identity Char
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar (ParsecT String () Identity String
-> ParsecT String () Identity String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string ("{{/" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
format String -> String -> String
forall a. [a] -> [a] -> [a]
++ "}}")))
  Inline -> ParsecT String () Identity Inline
forall (m :: * -> *) a. Monad m => a -> m a
return (Inline -> ParsecT String () Identity Inline)
-> Inline -> ParsecT String () Identity Inline
forall a b. (a -> b) -> a -> b
$ Format -> Text -> Inline
RawInline (Text -> Format
Format (Text -> Format) -> Text -> Format
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
format) (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
contents

pString :: Parsec String () Inline
pString :: ParsecT String () Identity Inline
pString = Text -> Inline
Str (Text -> Inline) -> (String -> Text) -> String -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Inline)
-> ParsecT String () Identity String
-> ParsecT String () Identity Inline
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ParsecT String () Identity Char
-> ParsecT String () Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf " \t\n\r{}") ParsecT String () Identity String
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Int
-> ParsecT String () Identity Char
-> ParsecT String () Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
Int -> ParsecT s u m a -> ParsecT s u m [a]
count 1 (String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
oneOf "{}"))

pSpace :: Parsec String () Inline
pSpace :: ParsecT String () Identity Inline
pSpace = Inline
Space Inline
-> ParsecT String () Identity String
-> ParsecT String () Identity Inline
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT String () Identity Char
-> ParsecT String () Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
oneOf " \t\n\r")

output :: Formatting -> String -> [Output]
output :: Formatting -> String -> [Output]
output fm :: Formatting
fm s :: String
s
    | ' ':xs :: String
xs <- String
s = Output
OSpace Output -> [Output] -> [Output]
forall a. a -> [a] -> [a]
: Formatting -> String -> [Output]
output Formatting
fm String
xs
    | []     <- String
s = []
    | Bool
otherwise   = [String -> Formatting -> Output
OStr String
s Formatting
fm]

appendOutput :: Formatting -> [Output] -> [Output]
appendOutput :: Formatting -> [Output] -> [Output]
appendOutput fm :: Formatting
fm xs :: [Output]
xs = [[Output] -> Formatting -> Output
Output [Output]
xs Formatting
fm | [Output]
xs [Output] -> [Output] -> Bool
forall a. Eq a => a -> a -> Bool
/= []]

outputList :: Formatting -> Delimiter -> [Output] -> [Output]
outputList :: Formatting -> String -> [Output] -> [Output]
outputList fm :: Formatting
fm d :: String
d = Formatting -> [Output] -> [Output]
appendOutput Formatting
fm ([Output] -> [Output])
-> ([Output] -> [Output]) -> [Output] -> [Output]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [Output] -> [Output]
addDelim String
d ([Output] -> [Output])
-> ([Output] -> [Output]) -> [Output] -> [Output]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Output -> Maybe Output) -> [Output] -> [Output]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Output -> Maybe Output
cleanOutput'
    where
      cleanOutput' :: Output -> Maybe Output
cleanOutput' o :: Output
o
          | Output xs :: [Output]
xs f :: Formatting
f <- Output
o = case [Output] -> [Output]
cleanOutput [Output]
xs of
                                 [] -> Maybe Output
forall a. Maybe a
Nothing
                                 ys :: [Output]
ys -> Output -> Maybe Output
forall a. a -> Maybe a
Just ([Output] -> Formatting -> Output
Output [Output]
ys Formatting
f)
          | Bool
otherwise        = Output -> Maybe Output
rmEmptyOutput Output
o

cleanOutput :: [Output] -> [Output]
cleanOutput :: [Output] -> [Output]
cleanOutput = [Output] -> [Output]
flatten
    where
      flatten :: [Output] -> [Output]
flatten [] = []
      flatten (o :: Output
o:os :: [Output]
os)
          | Output
ONull       <- Output
o     = [Output] -> [Output]
flatten [Output]
os
          | Output xs :: [Output]
xs f :: Formatting
f <- Output
o
          , Formatting
f Formatting -> Formatting -> Bool
forall a. Eq a => a -> a -> Bool
== Formatting
emptyFormatting = [Output] -> [Output]
flatten ((Output -> Maybe Output) -> [Output] -> [Output]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Output -> Maybe Output
rmEmptyOutput [Output]
xs) [Output] -> [Output] -> [Output]
forall a. [a] -> [a] -> [a]
++ [Output] -> [Output]
flatten [Output]
os
          | Output xs :: [Output]
xs f :: Formatting
f <- Output
o     = [Output] -> Formatting -> Output
Output ([Output] -> [Output]
flatten ([Output] -> [Output]) -> [Output] -> [Output]
forall a b. (a -> b) -> a -> b
$ (Output -> Maybe Output) -> [Output] -> [Output]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Output -> Maybe Output
rmEmptyOutput [Output]
xs) Formatting
f Output -> [Output] -> [Output]
forall a. a -> [a] -> [a]
: [Output] -> [Output]
flatten [Output]
os
          | Bool
otherwise            = ([Output] -> [Output])
-> (Output -> [Output] -> [Output])
-> Maybe Output
-> [Output]
-> [Output]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Output] -> [Output]
forall a. a -> a
id (:) (Output -> Maybe Output
rmEmptyOutput Output
o) ([Output] -> [Output]) -> [Output] -> [Output]
forall a b. (a -> b) -> a -> b
$ [Output] -> [Output]
flatten [Output]
os

rmEmptyOutput :: Output -> Maybe Output
rmEmptyOutput :: Output -> Maybe Output
rmEmptyOutput o :: Output
o
    | Output [] _ <- Output
o = Maybe Output
forall a. Maybe a
Nothing
    | OStr []   _ <- Output
o = Maybe Output
forall a. Maybe a
Nothing
    | OPan []     <- Output
o = Maybe Output
forall a. Maybe a
Nothing
    | OStatus []  <- Output
o = Maybe Output
forall a. Maybe a
Nothing
    | ODel []     <- Output
o = Maybe Output
forall a. Maybe a
Nothing
    | Bool
otherwise        = Output -> Maybe Output
forall a. a -> Maybe a
Just Output
o

addDelim :: String -> [Output] -> [Output]
addDelim :: String -> [Output] -> [Output]
addDelim "" = [Output] -> [Output]
forall a. a -> a
id
addDelim d :: String
d  = (Output -> [Output] -> [Output])
-> [Output] -> [Output] -> [Output]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Output -> [Output] -> [Output]
check []
    where
      check :: Output -> [Output] -> [Output]
check ONull xs :: [Output]
xs   = [Output]
xs
      check x :: Output
x     []   = [Output
x]
      check x :: Output
x (z :: Output
z:zs :: [Output]
zs)   = if Output -> Formatted
formatOutput Output
x Formatted -> Formatted -> Bool
forall a. Eq a => a -> a -> Bool
== Formatted
forall a. Monoid a => a
mempty Bool -> Bool -> Bool
|| Output -> Formatted
formatOutput Output
z Formatted -> Formatted -> Bool
forall a. Eq a => a -> a -> Bool
== Formatted
forall a. Monoid a => a
mempty
                            then Output
x Output -> [Output] -> [Output]
forall a. a -> [a] -> [a]
: Output
z Output -> [Output] -> [Output]
forall a. a -> [a] -> [a]
: [Output]
zs
                            else Output
x Output -> [Output] -> [Output]
forall a. a -> [a] -> [a]
: String -> Output
ODel String
d Output -> [Output] -> [Output]
forall a. a -> [a] -> [a]
: Output
z Output -> [Output] -> [Output]
forall a. a -> [a] -> [a]
: [Output]
zs

noOutputError :: Output
noOutputError :: Output
noOutputError = CiteprocError -> Output
OErr CiteprocError
NoOutput

noBibDataError :: Cite -> Output
noBibDataError :: Cite -> Output
noBibDataError c :: Cite
c = CiteprocError -> Output
OErr (CiteprocError -> Output) -> CiteprocError -> Output
forall a b. (a -> b) -> a -> b
$ String -> CiteprocError
ReferenceNotFound (Cite -> String
citeId Cite
c)

oStr :: String -> [Output]
oStr :: String -> [Output]
oStr s :: String
s = String -> Formatting -> [Output]
oStr' String
s Formatting
emptyFormatting

oStr' :: String -> Formatting -> [Output]
oStr' :: String -> Formatting -> [Output]
oStr' [] _ = []
oStr' s :: String
s  f :: Formatting
f = [String -> Formatting -> Output
OStr String
s Formatting
f]

oPan :: [Inline] -> [Output]
oPan :: [Inline] -> [Output]
oPan []  = []
oPan ils :: [Inline]
ils = [[Inline] -> Output
OPan [Inline]
ils]

oPan' :: [Inline] -> Formatting -> [Output]
oPan' :: [Inline] -> Formatting -> [Output]
oPan' [] _  = []
oPan' ils :: [Inline]
ils f :: Formatting
f = [[Output] -> Formatting -> Output
Output [[Inline] -> Output
OPan [Inline]
ils] Formatting
f]

formatOutputList :: [Output] -> Formatted
formatOutputList :: [Output] -> Formatted
formatOutputList = [Formatted] -> Formatted
forall a. Monoid a => [a] -> a
mconcat ([Formatted] -> Formatted)
-> ([Output] -> [Formatted]) -> [Output] -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Output -> Formatted) -> [Output] -> [Formatted]
forall a b. (a -> b) -> [a] -> [b]
map Output -> Formatted
formatOutput

-- | Convert evaluated 'Output' into 'Formatted', ready for the
-- output filters.
formatOutput :: Output -> Formatted
formatOutput :: Output -> Formatted
formatOutput o :: Output
o =
  case Output
o of
      OSpace              -> [Inline] -> Formatted
Formatted [Inline
Space]
      OPan     i :: [Inline]
i          -> [Inline] -> Formatted
Formatted [Inline]
i
      OStatus  i :: [Inline]
i          -> [Inline] -> Formatted
Formatted [Inline]
i
      ODel     []         -> [Inline] -> Formatted
Formatted []
      ODel     " "        -> [Inline] -> Formatted
Formatted [Inline
Space]
      ODel     "\n"       -> [Inline] -> Formatted
Formatted [Inline
SoftBreak]
      ODel     s :: String
s          -> String -> Formatted
formatString String
s
      OStr     []      _  -> [Inline] -> Formatted
Formatted []
      OStr     s :: String
s       f :: Formatting
f  -> Formatting -> Formatted -> Formatted
addFormatting Formatting
f (Formatted -> Formatted) -> Formatted -> Formatted
forall a b. (a -> b) -> a -> b
$ String -> Formatted
formatString String
s
      OErr NoOutput       -> [Inline] -> Formatted
Formatted [Attr -> [Inline] -> Inline
Span ("",["citeproc-no-output"],[])
                                     [[Inline] -> Inline
Strong [Text -> Inline
Str "???"]]]
      OErr (ReferenceNotFound r :: String
r)
                          -> [Inline] -> Formatted
Formatted [Attr -> [Inline] -> Inline
Span ("",["citeproc-not-found"],
                                            [("data-reference-id",String -> Text
T.pack String
r)])
                                     [[Inline] -> Inline
Strong [Text -> Inline
Str "???"]]]
      OLabel   []      _  -> [Inline] -> Formatted
Formatted []
      OLabel   s :: String
s       f :: Formatting
f  -> Formatting -> Formatted -> Formatted
addFormatting Formatting
f (Formatted -> Formatted) -> Formatted -> Formatted
forall a b. (a -> b) -> a -> b
$ String -> Formatted
formatString String
s
      ODate    os :: [Output]
os         -> [Output] -> Formatted
formatOutputList [Output]
os
      OYear    s :: String
s _     f :: Formatting
f  -> Formatting -> Formatted -> Formatted
addFormatting Formatting
f (Formatted -> Formatted) -> Formatted -> Formatted
forall a b. (a -> b) -> a -> b
$ String -> Formatted
formatString String
s
      OYearSuf s :: String
s _ _   f :: Formatting
f  -> Formatting -> Formatted -> Formatted
addFormatting Formatting
f (Formatted -> Formatted) -> Formatted -> Formatted
forall a b. (a -> b) -> a -> b
$ String -> Formatted
formatString String
s
      ONum     i :: Int
i       f :: Formatting
f  -> Output -> Formatted
formatOutput (String -> Formatting -> Output
OStr (Int -> String
forall a. Show a => a -> String
show Int
i) Formatting
f)
      OCitNum  i :: Int
i       f :: Formatting
f  -> if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0
                                then [Inline] -> Formatted
Formatted [[Inline] -> Inline
Strong [Text -> Inline
Str "???"]]
                                else Output -> Formatted
formatOutput (String -> Formatting -> Output
OStr (Int -> String
forall a. Show a => a -> String
show Int
i) Formatting
f)
      OCitLabel s :: String
s      f :: Formatting
f  -> if String
s String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== ""
                                then [Inline] -> Formatted
Formatted [[Inline] -> Inline
Strong [Text -> Inline
Str "???"]]
                                else Output -> Formatted
formatOutput (String -> Formatting -> Output
OStr String
s Formatting
f)
      OName  _ os :: [Output]
os _    f :: Formatting
f  -> Output -> Formatted
formatOutput ([Output] -> Formatting -> Output
Output [Output]
os Formatting
f)
      OContrib _ _ os :: [Output]
os _ _ -> [Output] -> Formatted
formatOutputList [Output]
os
      OLoc     os :: [Output]
os      f :: Formatting
f  -> Output -> Formatted
formatOutput ([Output] -> Formatting -> Output
Output [Output]
os Formatting
f)
      Output   []      _  -> [Inline] -> Formatted
Formatted []
      Output   os :: [Output]
os      f :: Formatting
f  -> Formatting -> Formatted -> Formatted
addFormatting Formatting
f (Formatted -> Formatted) -> Formatted -> Formatted
forall a b. (a -> b) -> a -> b
$ [Output] -> Formatted
formatOutputList [Output]
os
      _                   -> [Inline] -> Formatted
Formatted []

addFormatting :: Formatting -> Formatted -> Formatted
addFormatting :: Formatting -> Formatted -> Formatted
addFormatting f :: Formatting
f =
  Formatted -> Formatted
addDisplay (Formatted -> Formatted)
-> (Formatted -> Formatted) -> Formatted -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Formatted -> Formatted
addLink (Formatted -> Formatted)
-> (Formatted -> Formatted) -> Formatted -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Formatted -> Formatted
addSuffix (Formatted -> Formatted)
-> (Formatted -> Formatted) -> Formatted -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Formatted -> Formatted
pref (Formatted -> Formatted)
-> (Formatted -> Formatted) -> Formatted -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Formatted -> Formatted
quote (Formatted -> Formatted)
-> (Formatted -> Formatted) -> Formatted -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Formatted -> Formatted
font (Formatted -> Formatted)
-> (Formatted -> Formatted) -> Formatted -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Formatted -> Formatted
text_case (Formatted -> Formatted)
-> (Formatted -> Formatted) -> Formatted -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Formatted -> Formatted
strip_periods
  where addLink :: Formatted -> Formatted
addLink i :: Formatted
i = case Formatting -> String
hyperlink Formatting
f of
                         ""  -> Formatted
i
                         url :: String
url -> [Inline] -> Formatted
Formatted [Attr -> [Inline] -> Target -> Inline
Link Attr
nullAttr (Formatted -> [Inline]
unFormatted Formatted
i) (String -> Text
T.pack String
url, "")]
        pref :: Formatted -> Formatted
pref i :: Formatted
i = case Formatting -> String
prefix Formatting
f of
                      "" -> Formatted
i
                      x :: String
x  -> String -> Formatted
formatString String
x Formatted -> Formatted -> Formatted
forall a. Semigroup a => a -> a -> a
<> Formatted
i
        addSuffix :: Formatted -> Formatted
addSuffix i :: Formatted
i
          | String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Formatting -> String
suffix Formatting
f)       = Formatted
i
          | case Formatting -> String
suffix Formatting
f of {(c :: Char
c:_) | Char -> Bool
isPunct Char
c -> Bool
True; _ -> Bool
False}
          , case [Inline] -> String
lastInline (Formatted -> [Inline]
unFormatted Formatted
i) of {(c :: Char
c:_) | Char -> Bool
isPunct Char
c -> Bool
True; _ -> Bool
False}
                                  = Formatted
i Formatted -> Formatted -> Formatted
forall a. Semigroup a => a -> a -> a
<> String -> Formatted
formatString (String -> String
forall a. [a] -> [a]
tail (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Formatting -> String
suffix Formatting
f)
          | Bool
otherwise             = Formatted
i Formatted -> Formatted -> Formatted
forall a. Semigroup a => a -> a -> a
<> String -> Formatted
formatString (Formatting -> String
suffix Formatting
f)

        strip_periods :: Formatted -> Formatted
strip_periods (Formatted ils :: [Inline]
ils) = [Inline] -> Formatted
Formatted ((Inline -> Inline) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
removePeriod [Inline]
ils)
        removePeriod :: Inline -> Inline
removePeriod (Str xs :: Text
xs) | Formatting -> Bool
stripPeriods Formatting
f = Text -> Inline
Str ((Char -> Bool) -> Text -> Text
T.filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/='.') Text
xs)
        removePeriod x :: Inline
x        = Inline
x

        quote :: Formatted -> Formatted
quote (Formatted [])  = [Inline] -> Formatted
Formatted []
        quote (Formatted ils :: [Inline]
ils) =
                    case Formatting -> Quote
quotes Formatting
f of
                         NoQuote     -> [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Inline]
valign [Inline]
ils
                         NativeQuote -> [Inline] -> Formatted
Formatted
                                  [Attr -> [Inline] -> Inline
Span ("",["csl-inquote"],[]) [Inline]
ils]
                         _           -> [Inline] -> Formatted
Formatted [QuoteType -> [Inline] -> Inline
Quoted QuoteType
DoubleQuote ([Inline] -> Inline) -> [Inline] -> Inline
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Inline]
valign [Inline]
ils]

        addDisplay :: Formatted -> Formatted
addDisplay (Formatted []) = [Inline] -> Formatted
Formatted []
        addDisplay (Formatted ils :: [Inline]
ils) =
                     case Formatting -> String
display Formatting
f of
                          "block"    -> [Inline] -> Formatted
Formatted (Inline
LineBreak Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
ils [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++
                                                       [Inline
LineBreak])
                          _          -> [Inline] -> Formatted
Formatted [Inline]
ils

        font :: Formatted -> Formatted
font (Formatted ils :: [Inline]
ils)
          | Formatting -> Bool
noDecor Formatting
f    = [Inline] -> Formatted
Formatted [Attr -> [Inline] -> Inline
Span ("",["nodecor"],[]) [Inline]
ils]
          | Bool
otherwise    = [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Inline]
font_variant ([Inline] -> [Inline])
-> ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> [Inline]
font_style ([Inline] -> [Inline])
-> ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  [Inline] -> [Inline]
font_weight ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$ [Inline]
ils
        font_variant :: [Inline] -> [Inline]
font_variant ils :: [Inline]
ils =
          case Formatting -> String
fontVariant Formatting
f of
               "small-caps" -> [[Inline] -> Inline
SmallCaps [Inline]
ils]
               _            -> [Inline]
ils
        font_style :: [Inline] -> [Inline]
font_style ils :: [Inline]
ils =
          case Formatting -> String
fontStyle Formatting
f of
               "italic"  -> [[Inline] -> Inline
Emph [Inline]
ils]
               "oblique" -> [[Inline] -> Inline
Emph [Inline]
ils]
               _         -> [Inline]
ils
        font_weight :: [Inline] -> [Inline]
font_weight ils :: [Inline]
ils =
          case Formatting -> String
fontWeight Formatting
f of
               "bold" -> [[Inline] -> Inline
Strong [Inline]
ils]
               _      -> [Inline]
ils

        text_case :: Formatted -> Formatted
text_case (Formatted []) = [Inline] -> Formatted
Formatted []
        text_case (Formatted ils :: [Inline]
ils@(i :: Inline
i:is' :: [Inline]
is'))
          | Formatting -> Bool
noCase Formatting
f  = [Inline] -> Formatted
Formatted [Attr -> [Inline] -> Inline
Span ("",["nocase"],[]) [Inline]
ils]
          | Bool
otherwise = [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$
              case Formatting -> String
textCase Formatting
f of
                   "lowercase"        -> (Inline -> Inline) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
lowercaseStr [Inline]
ils
                   "uppercase"        -> (Inline -> Inline) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
uppercaseStr [Inline]
ils
                   "capitalize-all"   -> (Inline -> Inline) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
capitalizeStr [Inline]
ils
                   "title"            -> [Inline] -> [Inline]
titlecase [Inline]
ils
                   "capitalize-first"
                     -> case Inline
i of
                             Str cs :: Text
cs -> Text -> Inline
Str (String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ String -> String
capitalize (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
cs) Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
is'
                             _ -> [Inline] -> [Inline]
unTitlecase [Inline
i] [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Inline]
is'
                   "sentence"         -> [Inline] -> [Inline]
unTitlecase [Inline]
ils
                   _                  -> [Inline]
ils

        lowercaseStr :: Inline -> Inline
lowercaseStr (Str xs :: Text
xs) = Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Text -> Text
T.toLower Text
xs
        lowercaseStr x :: Inline
x        = Inline
x
        uppercaseStr :: Inline -> Inline
uppercaseStr (Str xs :: Text
xs) = Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Text -> Text
T.toUpper Text
xs
        uppercaseStr x :: Inline
x        = Inline
x
        capitalizeStr :: Inline -> Inline
capitalizeStr (Str xs :: Text
xs) = Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ String -> String
capitalize (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
xs
        capitalizeStr x :: Inline
x        = Inline
x

        valign :: [Inline] -> [Inline]
valign [] = []
        valign ils :: [Inline]
ils
          | String
"sup"      <- Formatting -> String
verticalAlign Formatting
f = [[Inline] -> Inline
Superscript [Inline]
ils]
          | String
"sub"      <- Formatting -> String
verticalAlign Formatting
f = [[Inline] -> Inline
Subscript   [Inline]
ils]
          | String
"baseline" <- Formatting -> String
verticalAlign Formatting
f =
                              [Attr -> [Inline] -> Inline
Span ("",["csl-baseline"],[]) [Inline]
ils]
          | Bool
otherwise                     = [Inline]
ils