{-# 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           Data.Text              (Text)
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
import           Text.Parsec.Text       (Parser)

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

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

pRaw :: Parser Inline
pRaw :: ParsecT Text () Identity Inline
pRaw = ParsecT Text () Identity Inline -> ParsecT Text () Identity Inline
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text () Identity Inline
 -> ParsecT Text () Identity Inline)
-> ParsecT Text () Identity Inline
-> ParsecT Text () Identity Inline
forall a b. (a -> b) -> a -> b
$ do
  SourceName
_ <- SourceName -> ParsecT Text () Identity SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string SourceName
"{{"
  SourceName
format <- ParsecT Text () Identity Char
-> ParsecT Text () Identity SourceName
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Text () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
letter
  SourceName
_ <- SourceName -> ParsecT Text () Identity SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string SourceName
"}}"
  SourceName
contents <- ParsecT Text () Identity Char
-> ParsecT Text () Identity SourceName
-> ParsecT Text () Identity SourceName
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 Text () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar (ParsecT Text () Identity SourceName
-> ParsecT Text () Identity SourceName
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (SourceName -> ParsecT Text () Identity SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string (SourceName
"{{/" SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ SourceName
format SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ SourceName
"}}")))
  Inline -> ParsecT Text () Identity Inline
forall (m :: * -> *) a. Monad m => a -> m a
return (Inline -> ParsecT Text () Identity Inline)
-> Inline -> ParsecT Text () 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
$ SourceName -> Text
T.pack SourceName
format) (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ SourceName -> Text
T.pack SourceName
contents

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

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

output :: Formatting -> Text -> [Output]
output :: Formatting -> Text -> [Output]
output Formatting
fm Text
s = case Text -> Maybe (Char, Text)
T.uncons Text
s of
  Maybe (Char, Text)
Nothing        -> []
  Just (Char
' ', Text
xs) -> Output
OSpace Output -> [Output] -> [Output]
forall a. a -> [a] -> [a]
: Formatting -> Text -> [Output]
output Formatting
fm Text
xs
  Maybe (Char, Text)
_              -> [Text -> Formatting -> Output
OStr Text
s Formatting
fm]

appendOutput :: Formatting -> [Output] -> [Output]
appendOutput :: Formatting -> [Output] -> [Output]
appendOutput Formatting
fm [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 -> Text -> [Output] -> [Output]
outputList Formatting
fm Text
d = Formatting -> [Output] -> [Output]
appendOutput Formatting
fm ([Output] -> [Output])
-> ([Output] -> [Output]) -> [Output] -> [Output]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Output] -> [Output]
addDelim Text
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' Output
o
          | Output [Output]
xs Formatting
f <- Output
o = case [Output] -> [Output]
cleanOutput [Output]
xs of
                                 [] -> Maybe Output
forall a. Maybe a
Nothing
                                 [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 (Output
o:[Output]
os)
          | Output
ONull       <- Output
o     = [Output] -> [Output]
flatten [Output]
os
          | Output [Output]
xs 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 [Output]
xs 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 Output
o
    | Output [] Formatting
_ <- Output
o = Maybe Output
forall a. Maybe a
Nothing
    | OStr Text
""   Formatting
_ <- 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 Text
""     <- Output
o = Maybe Output
forall a. Maybe a
Nothing
    | Bool
otherwise        = Output -> Maybe Output
forall a. a -> Maybe a
Just Output
o

addDelim :: Text -> [Output] -> [Output]
addDelim :: Text -> [Output] -> [Output]
addDelim Text
"" = [Output] -> [Output]
forall a. a -> a
id
addDelim Text
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 Output
ONull [Output]
xs   = [Output]
xs
      check Output
x     []   = [Output
x]
      check Output
x (Output
z:[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]
: Text -> Output
ODel Text
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 Cite
c = CiteprocError -> Output
OErr (CiteprocError -> Output) -> CiteprocError -> Output
forall a b. (a -> b) -> a -> b
$ Text -> CiteprocError
ReferenceNotFound (Cite -> Text
citeId Cite
c)

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

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

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

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

addFormatting :: Formatting -> Formatted -> Formatted
addFormatting :: Formatting -> Formatted -> Formatted
addFormatting 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 Formatted
i = case Formatting -> Text
hyperlink Formatting
f of
                         Text
""  -> Formatted
i
                         Text
url -> [Inline] -> Formatted
Formatted [Attr -> [Inline] -> (Text, Text) -> Inline
Link Attr
nullAttr (Formatted -> [Inline]
unFormatted Formatted
i) (Text
url, Text
"")]
        pref :: Formatted -> Formatted
pref Formatted
i = case Formatting -> Text
prefix Formatting
f of
                      Text
"" -> Formatted
i
                      Text
x  -> Text -> Formatted
formatString Text
x Formatted -> Formatted -> Formatted
forall a. Semigroup a => a -> a -> a
<> Formatted
i
        addSuffix :: Formatted -> Formatted
addSuffix Formatted
i
          | Text -> Bool
T.null (Formatting -> Text
suffix Formatting
f)       = Formatted
i
          | Bool -> ((Char, Text) -> Bool) -> Maybe (Char, Text) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Char -> Bool
isPunct (Char -> Bool) -> ((Char, Text) -> Char) -> (Char, Text) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char, Text) -> Char
forall a b. (a, b) -> a
fst) (Text -> Maybe (Char, Text)
T.uncons (Formatting -> Text
suffix Formatting
f))
          , case [Inline] -> Maybe Char
lastInline (Formatted -> [Inline]
unFormatted Formatted
i) of {Just Char
c | Char -> Bool
isPunct Char
c -> Bool
True; Maybe Char
_ -> Bool
False}
                                  = Formatted
i Formatted -> Formatted -> Formatted
forall a. Semigroup a => a -> a -> a
<> Text -> Formatted
formatString (Text -> Text
T.tail (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Formatting -> Text
suffix Formatting
f)
          | Bool
otherwise             = Formatted
i Formatted -> Formatted -> Formatted
forall a. Semigroup a => a -> a -> a
<> Text -> Formatted
formatString (Formatting -> Text
suffix Formatting
f)

        strip_periods :: Formatted -> Formatted
strip_periods (Formatted [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 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
/=Char
'.') Text
xs)
        removePeriod Inline
x        = Inline
x

        quote :: Formatted -> Formatted
quote (Formatted [])  = [Inline] -> Formatted
Formatted []
        quote (Formatted [Inline]
ils) =
                    case Formatting -> Quote
quotes Formatting
f of
                         Quote
NoQuote     -> [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Inline]
valign [Inline]
ils
                         Quote
NativeQuote -> [Inline] -> Formatted
Formatted
                                  [Attr -> [Inline] -> Inline
Span (Text
"",[Text
"csl-inquote"],[]) [Inline]
ils]
                         Quote
_           -> [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 [Inline]
ils) =
                     case Formatting -> Text
display Formatting
f of
                          Text
"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])
                          Text
_          -> [Inline] -> Formatted
Formatted [Inline]
ils

        font :: Formatted -> Formatted
font (Formatted [Inline]
ils)
          | Formatting -> Bool
noDecor Formatting
f    = [Inline] -> Formatted
Formatted [Attr -> [Inline] -> Inline
Span (Text
"",[Text
"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 [Inline]
ils =
          case Formatting -> Text
fontVariant Formatting
f of
               Text
"small-caps" -> [[Inline] -> Inline
SmallCaps [Inline]
ils]
               Text
_            -> [Inline]
ils
        font_style :: [Inline] -> [Inline]
font_style [Inline]
ils =
          case Formatting -> Text
fontStyle Formatting
f of
               Text
"italic"  -> [[Inline] -> Inline
Emph [Inline]
ils]
               Text
"oblique" -> [[Inline] -> Inline
Emph [Inline]
ils]
               Text
_         -> [Inline]
ils
        font_weight :: [Inline] -> [Inline]
font_weight [Inline]
ils =
          case Formatting -> Text
fontWeight Formatting
f of
               Text
"bold" -> [[Inline] -> Inline
Strong [Inline]
ils]
               Text
_      -> [Inline]
ils

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

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

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