{-# LANGUAGE CPP             #-}
{-# LANGUAGE Arrows          #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections   #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Readers.Odt.StyleReader
   Copyright   : Copyright (C) 2015 Martin Linnemann
   License     : GNU GPL, version 2 or above

   Maintainer  : Martin Linnemann <theCodingMarlin@googlemail.com>
   Stability   : alpha
   Portability : portable

Reader for the style information in an odt document.
-}

module Text.Pandoc.Readers.Odt.StyleReader
( Style                (..)
, StyleName
, StyleFamily          (..)
, Styles               (..)
, StyleProperties      (..)
, TextProperties       (..)
, ParaProperties       (..)
, VerticalTextPosition (..)
, ListItemNumberFormat (..)
, ListLevel
, ListStyle            (..)
, ListLevelStyle       (..)
, ListLevelType        (..)
, LengthOrPercent      (..)
, lookupStyle
, getListLevelStyle
, getStyleFamily
, lookupDefaultStyle'
, lookupListStyleByName
, extendedStylePropertyChain
, readStylesAt
) where

import Control.Applicative hiding (liftA, liftA2, liftA3)
import Control.Arrow

import Data.Default
import qualified Data.Foldable as F
import Data.List (unfoldr, foldl')
import qualified Data.Map as M
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Set as S

import qualified Text.Pandoc.XML.Light as XML

import Text.Pandoc.Shared (safeRead, tshow)

import Text.Pandoc.Readers.Odt.Arrows.Utils

import Text.Pandoc.Readers.Odt.Generic.Fallible
import qualified Text.Pandoc.Readers.Odt.Generic.SetMap as SM
import Text.Pandoc.Readers.Odt.Generic.Utils
import Text.Pandoc.Readers.Odt.Generic.XMLConverter

import Text.Pandoc.Readers.Odt.Base
import Text.Pandoc.Readers.Odt.Namespaces

readStylesAt :: XML.Element -> Fallible Styles
readStylesAt :: Element -> Fallible Styles
readStylesAt Element
e = FallibleXMLConverter Namespace FontPitches () Styles
-> FontPitches -> Element -> Fallible Styles
forall nsID extraState success.
NameSpaceID nsID =>
FallibleXMLConverter nsID extraState () success
-> extraState -> Element -> Fallible success
runConverter' FallibleXMLConverter Namespace FontPitches () Styles
forall _x. StyleReader _x Styles
readAllStyles FontPitches
forall a. Monoid a => a
mempty Element
e

--------------------------------------------------------------------------------
-- Reader for font declarations and font pitches
--------------------------------------------------------------------------------

-- Pandoc has no support for different font pitches. Yet knowing them can be
-- very helpful in cases where Pandoc has more semantics than OpenDocument.
-- In these cases, the pitch can help deciding as what to define a block of
-- text. So let's start with a type for font pitches:

data FontPitch    = PitchVariable | PitchFixed
  deriving ( FontPitch -> FontPitch -> Bool
(FontPitch -> FontPitch -> Bool)
-> (FontPitch -> FontPitch -> Bool) -> Eq FontPitch
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FontPitch -> FontPitch -> Bool
$c/= :: FontPitch -> FontPitch -> Bool
== :: FontPitch -> FontPitch -> Bool
$c== :: FontPitch -> FontPitch -> Bool
Eq, Int -> FontPitch -> ShowS
[FontPitch] -> ShowS
FontPitch -> String
(Int -> FontPitch -> ShowS)
-> (FontPitch -> String)
-> ([FontPitch] -> ShowS)
-> Show FontPitch
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FontPitch] -> ShowS
$cshowList :: [FontPitch] -> ShowS
show :: FontPitch -> String
$cshow :: FontPitch -> String
showsPrec :: Int -> FontPitch -> ShowS
$cshowsPrec :: Int -> FontPitch -> ShowS
Show )

instance Lookupable FontPitch where
  lookupTable :: [(Text, FontPitch)]
lookupTable = [ (Text
"variable" , FontPitch
PitchVariable)
                , (Text
"fixed"    , FontPitch
PitchFixed   )
                ]

instance Default FontPitch where
  def :: FontPitch
def = FontPitch
PitchVariable

-- The font pitch can be specified in a style directly. Normally, however,
-- it is defined in the font. That is also the specs' recommendation.
--
-- Thus, we want

type FontFaceName = Text

type FontPitches = M.Map FontFaceName FontPitch

-- To get there, the fonts have to be read and the pitches extracted.
-- But the resulting map are only needed at one later place, so it should not be
-- transported on the value level, especially as we already use a state arrow.
-- So instead, the resulting map is lifted into the state of the reader.
-- (An alternative might be ImplicitParams, but again, we already have a state.)
--
-- So the main style readers will have the types
type StyleReader     a b  = XMLReader     FontPitches a b
-- and
type StyleReaderSafe a b  = XMLReaderSafe FontPitches a b
-- respectively.
--
-- But before we can work with these, we need to define the reader that reads
-- the fonts:

-- | A reader for font pitches
fontPitchReader :: XMLReader _s _x FontPitches
fontPitchReader :: XMLReader _s _x FontPitches
fontPitchReader = Namespace
-> Text
-> XMLReader _s _x FontPitches
-> XMLReader _s _x FontPitches
forall nsID extraState f s.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState f s
-> FallibleXMLConverter nsID extraState f s
executeInSub Namespace
NsOffice Text
"font-face-decls" (
                          Namespace
-> Text
-> FallibleXMLConverter Namespace _s _x (Maybe Text, FontPitch)
-> FallibleXMLConverter Namespace _s _x [(Maybe Text, FontPitch)]
forall nsID extraState a b.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState a b
-> FallibleXMLConverter nsID extraState a [b]
withEveryL Namespace
NsStyle Text
"font-face" (ArrowState
  (XMLConverterState Namespace _s) _x (Maybe Text, FontPitch)
-> FallibleXMLConverter Namespace _s _x (Maybe Text, FontPitch)
forall (a :: * -> * -> *) x success failure.
ArrowChoice a =>
a x success -> FallibleArrow a x failure success
liftAsSuccess (
                              Namespace -> Text -> XMLConverter Namespace _s _x (Maybe Text)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr' Namespace
NsStyle Text
"name"
                              XMLConverter Namespace _s _x (Maybe Text)
-> ArrowState (XMLConverterState Namespace _s) _x FontPitch
-> ArrowState
     (XMLConverterState Namespace _s) _x (Maybe Text, FontPitch)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&&
                              Namespace
-> Text -> ArrowState (XMLConverterState Namespace _s) _x FontPitch
forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a, Default a) =>
nsID -> Text -> XMLConverter nsID extraState x a
lookupDefaultingAttr Namespace
NsStyle Text
"font-pitch"
                            ))
                    FallibleXMLConverter Namespace _s _x [(Maybe Text, FontPitch)]
-> ([(Maybe Text, FontPitch)] -> FontPitches)
-> XMLReader _s _x FontPitches
forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> (success -> success') -> FallibleArrow a x failure success'
>>?^ ( [(Text, FontPitch)] -> FontPitches
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(Text, FontPitch)] -> FontPitches)
-> ([(Maybe Text, FontPitch)] -> [(Text, FontPitch)])
-> [(Maybe Text, FontPitch)]
-> FontPitches
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([(Text, FontPitch)]
 -> (Maybe Text, FontPitch) -> [(Text, FontPitch)])
-> [(Text, FontPitch)]
-> [(Maybe Text, FontPitch)]
-> [(Text, FontPitch)]
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' [(Text, FontPitch)]
-> (Maybe Text, FontPitch) -> [(Text, FontPitch)]
forall a b. [(a, b)] -> (Maybe a, b) -> [(a, b)]
accumLegalPitches [] )
                  ) XMLReader _s _x FontPitches
-> XMLReader _s _x FontPitches -> XMLReader _s _x FontPitches
forall (a :: * -> * -> *) x f y.
ArrowChoice a =>
FallibleArrow a x f y
-> FallibleArrow a x f y -> FallibleArrow a x f y
`ifFailedDo` Either () FontPitches -> XMLReader _s _x FontPitches
forall (a :: * -> * -> *) c x. Arrow a => c -> a x c
returnV (FontPitches -> Either () FontPitches
forall a b. b -> Either a b
Right FontPitches
forall k a. Map k a
M.empty)
  where accumLegalPitches :: [(a, b)] -> (Maybe a, b) -> [(a, b)]
accumLegalPitches [(a, b)]
ls (Maybe a
Nothing,b
_) = [(a, b)]
ls
        accumLegalPitches [(a, b)]
ls (Just a
n,b
p)  = (a
n,b
p)(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:[(a, b)]
ls


-- | A wrapper around the font pitch reader that lifts the result into the
-- state.
readFontPitches :: StyleReader x x
readFontPitches :: StyleReader x x
readFontPitches = ()
-> ()
-> FallibleXMLConverter Namespace () () FontPitches
-> StyleReader x x
forall extraState' a nsID extraState x.
extraState'
-> a
-> FallibleXMLConverter nsID extraState' a extraState
-> FallibleXMLConverter nsID extraState x x
producingExtraState () () FallibleXMLConverter Namespace () () FontPitches
forall _s _x. XMLReader _s _x FontPitches
fontPitchReader


-- | Looking up a pitch in the state of the arrow.
--
-- The function does the following:
-- * Look for the font pitch in an attribute.
-- * If that fails, look for the font name, look up the font in the state
--   and use the pitch from there.
-- * Return the result in a Maybe
--
findPitch :: XMLReaderSafe FontPitches _x (Maybe FontPitch)
findPitch :: XMLReaderSafe FontPitches _x (Maybe FontPitch)
findPitch =     ( Namespace
-> Text -> FallibleXMLConverter Namespace FontPitches _x FontPitch
forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> Text -> FallibleXMLConverter nsID extraState x a
lookupAttr Namespace
NsStyle Text
"font-pitch"
                  FallibleXMLConverter Namespace FontPitches _x FontPitch
-> FallibleXMLConverter Namespace FontPitches _x FontPitch
-> FallibleXMLConverter Namespace FontPitches _x FontPitch
forall (a :: * -> * -> *) x f y.
ArrowChoice a =>
FallibleArrow a x f y
-> FallibleArrow a x f y -> FallibleArrow a x f y
`ifFailedDo`     Namespace
-> Text -> FallibleXMLConverter Namespace FontPitches _x Text
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> FallibleXMLConverter nsID extraState x Text
findAttr Namespace
NsStyle Text
"font-name"
                               FallibleXMLConverter Namespace FontPitches _x Text
-> FallibleArrow
     (ArrowState (XMLConverterState Namespace FontPitches))
     Text
     ()
     FontPitch
-> FallibleXMLConverter Namespace FontPitches _x FontPitch
forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> FallibleArrow a success failure success'
-> FallibleArrow a x failure success'
>>? (     ArrowState
  (XMLConverterState Namespace FontPitches) Text FontPitches
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Text, FontPitches)
forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue ArrowState
  (XMLConverterState Namespace FontPitches) Text FontPitches
forall nsID extraState x. XMLConverter nsID extraState x extraState
getExtraState
                                     ArrowState
  (XMLConverterState Namespace FontPitches) Text (Text, FontPitches)
-> (Text -> FontPitches -> Maybe FontPitch)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Maybe FontPitch)
forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% Text -> FontPitches -> Maybe FontPitch
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup
                                     ArrowState
  (XMLConverterState Namespace FontPitches) Text (Maybe FontPitch)
-> (Maybe FontPitch -> Either () FontPitch)
-> FallibleArrow
     (ArrowState (XMLConverterState Namespace FontPitches))
     Text
     ()
     FontPitch
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ Maybe FontPitch -> Either () FontPitch
forall (a :: * -> * -> *) b.
ArrowChoice a =>
a (Maybe b) (Fallible b)
maybeToChoice
                                   )
                )
            FallibleXMLConverter Namespace FontPitches _x FontPitch
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     (Either () FontPitch)
     (Maybe FontPitch)
-> XMLReaderSafe FontPitches _x (Maybe FontPitch)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ArrowState
  (XMLConverterState Namespace FontPitches)
  (Either () FontPitch)
  (Maybe FontPitch)
forall (a :: * -> * -> *) l r.
ArrowChoice a =>
a (Either l r) (Maybe r)
choiceToMaybe

--------------------------------------------------------------------------------
-- Definitions of main data
--------------------------------------------------------------------------------

type StyleName        = Text

-- | There are two types of styles: named styles with a style family and an
-- optional style parent, and default styles for each style family,
-- defining default style properties
data Styles           = Styles
                          { Styles -> Map Text Style
stylesByName     :: M.Map StyleName   Style
                          , Styles -> Map Text ListStyle
listStylesByName :: M.Map StyleName   ListStyle
                          , Styles -> Map StyleFamily StyleProperties
defaultStyleMap  :: M.Map StyleFamily StyleProperties
                          }
  deriving ( Int -> Styles -> ShowS
[Styles] -> ShowS
Styles -> String
(Int -> Styles -> ShowS)
-> (Styles -> String) -> ([Styles] -> ShowS) -> Show Styles
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Styles] -> ShowS
$cshowList :: [Styles] -> ShowS
show :: Styles -> String
$cshow :: Styles -> String
showsPrec :: Int -> Styles -> ShowS
$cshowsPrec :: Int -> Styles -> ShowS
Show )

-- Styles from a monoid under union
instance Semigroup Styles where
  (Styles Map Text Style
sBn1 Map Text ListStyle
dSm1 Map StyleFamily StyleProperties
lsBn1) <> :: Styles -> Styles -> Styles
<> (Styles Map Text Style
sBn2 Map Text ListStyle
dSm2 Map StyleFamily StyleProperties
lsBn2)
          = Map Text Style
-> Map Text ListStyle -> Map StyleFamily StyleProperties -> Styles
Styles (Map Text Style -> Map Text Style -> Map Text Style
forall k a. Ord k => Map k a -> Map k a -> Map k a
M.union Map Text Style
sBn1  Map Text Style
sBn2)
                   (Map Text ListStyle -> Map Text ListStyle -> Map Text ListStyle
forall k a. Ord k => Map k a -> Map k a -> Map k a
M.union Map Text ListStyle
dSm1  Map Text ListStyle
dSm2)
                   (Map StyleFamily StyleProperties
-> Map StyleFamily StyleProperties
-> Map StyleFamily StyleProperties
forall k a. Ord k => Map k a -> Map k a -> Map k a
M.union Map StyleFamily StyleProperties
lsBn1 Map StyleFamily StyleProperties
lsBn2)
instance Monoid Styles where
  mempty :: Styles
mempty  = Map Text Style
-> Map Text ListStyle -> Map StyleFamily StyleProperties -> Styles
Styles Map Text Style
forall k a. Map k a
M.empty Map Text ListStyle
forall k a. Map k a
M.empty Map StyleFamily StyleProperties
forall k a. Map k a
M.empty
  mappend :: Styles -> Styles -> Styles
mappend = Styles -> Styles -> Styles
forall a. Semigroup a => a -> a -> a
(<>)

-- Not all families from the specifications are implemented, only those we need.
-- But there are none that are not mentioned here.
data StyleFamily      = FaText    | FaParagraph
--                    | FaTable   | FaTableCell | FaTableColumn | FaTableRow
--                    | FaGraphic | FaDrawing   | FaChart
--                    | FaPresentation
--                    | FaRuby
  deriving ( StyleFamily -> StyleFamily -> Bool
(StyleFamily -> StyleFamily -> Bool)
-> (StyleFamily -> StyleFamily -> Bool) -> Eq StyleFamily
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StyleFamily -> StyleFamily -> Bool
$c/= :: StyleFamily -> StyleFamily -> Bool
== :: StyleFamily -> StyleFamily -> Bool
$c== :: StyleFamily -> StyleFamily -> Bool
Eq, Eq StyleFamily
Eq StyleFamily
-> (StyleFamily -> StyleFamily -> Ordering)
-> (StyleFamily -> StyleFamily -> Bool)
-> (StyleFamily -> StyleFamily -> Bool)
-> (StyleFamily -> StyleFamily -> Bool)
-> (StyleFamily -> StyleFamily -> Bool)
-> (StyleFamily -> StyleFamily -> StyleFamily)
-> (StyleFamily -> StyleFamily -> StyleFamily)
-> Ord StyleFamily
StyleFamily -> StyleFamily -> Bool
StyleFamily -> StyleFamily -> Ordering
StyleFamily -> StyleFamily -> StyleFamily
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: StyleFamily -> StyleFamily -> StyleFamily
$cmin :: StyleFamily -> StyleFamily -> StyleFamily
max :: StyleFamily -> StyleFamily -> StyleFamily
$cmax :: StyleFamily -> StyleFamily -> StyleFamily
>= :: StyleFamily -> StyleFamily -> Bool
$c>= :: StyleFamily -> StyleFamily -> Bool
> :: StyleFamily -> StyleFamily -> Bool
$c> :: StyleFamily -> StyleFamily -> Bool
<= :: StyleFamily -> StyleFamily -> Bool
$c<= :: StyleFamily -> StyleFamily -> Bool
< :: StyleFamily -> StyleFamily -> Bool
$c< :: StyleFamily -> StyleFamily -> Bool
compare :: StyleFamily -> StyleFamily -> Ordering
$ccompare :: StyleFamily -> StyleFamily -> Ordering
$cp1Ord :: Eq StyleFamily
Ord, Int -> StyleFamily -> ShowS
[StyleFamily] -> ShowS
StyleFamily -> String
(Int -> StyleFamily -> ShowS)
-> (StyleFamily -> String)
-> ([StyleFamily] -> ShowS)
-> Show StyleFamily
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StyleFamily] -> ShowS
$cshowList :: [StyleFamily] -> ShowS
show :: StyleFamily -> String
$cshow :: StyleFamily -> String
showsPrec :: Int -> StyleFamily -> ShowS
$cshowsPrec :: Int -> StyleFamily -> ShowS
Show )

instance Lookupable StyleFamily where
  lookupTable :: [(Text, StyleFamily)]
lookupTable = [ ( Text
"text"         , StyleFamily
FaText         )
                , ( Text
"paragraph"    , StyleFamily
FaParagraph    )
--              , ( "table"        , FaTable        )
--              , ( "table-cell"   , FaTableCell    )
--              , ( "table-column" , FaTableColumn  )
--              , ( "table-row"    , FaTableRow     )
--              , ( "graphic"      , FaGraphic      )
--              , ( "drawing-page" , FaDrawing      )
--              , ( "chart"        , FaChart        )
--              , ( "presentation" , FaPresentation )
--              , ( "ruby"         , FaRuby         )
                ]

-- | A named style
data Style            = Style  { Style -> Maybe StyleFamily
styleFamily     :: Maybe StyleFamily
                               , Style -> Maybe Text
styleParentName :: Maybe StyleName
                               , Style -> Maybe Text
listStyle       :: Maybe StyleName
                               , Style -> StyleProperties
styleProperties :: StyleProperties
                               }
  deriving ( Style -> Style -> Bool
(Style -> Style -> Bool) -> (Style -> Style -> Bool) -> Eq Style
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Style -> Style -> Bool
$c/= :: Style -> Style -> Bool
== :: Style -> Style -> Bool
$c== :: Style -> Style -> Bool
Eq, Int -> Style -> ShowS
[Style] -> ShowS
Style -> String
(Int -> Style -> ShowS)
-> (Style -> String) -> ([Style] -> ShowS) -> Show Style
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Style] -> ShowS
$cshowList :: [Style] -> ShowS
show :: Style -> String
$cshow :: Style -> String
showsPrec :: Int -> Style -> ShowS
$cshowsPrec :: Int -> Style -> ShowS
Show )

data StyleProperties  = SProps { StyleProperties -> Maybe TextProperties
textProperties :: Maybe TextProperties
                               , StyleProperties -> Maybe ParaProperties
paraProperties :: Maybe ParaProperties
--                             , tableColProperties  :: Maybe TColProperties
--                             , tableRowProperties  :: Maybe TRowProperties
--                             , tableCellProperties :: Maybe TCellProperties
--                             , tableProperties     :: Maybe TableProperties
--                             , graphicProperties   :: Maybe GraphProperties
                               }
  deriving ( StyleProperties -> StyleProperties -> Bool
(StyleProperties -> StyleProperties -> Bool)
-> (StyleProperties -> StyleProperties -> Bool)
-> Eq StyleProperties
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StyleProperties -> StyleProperties -> Bool
$c/= :: StyleProperties -> StyleProperties -> Bool
== :: StyleProperties -> StyleProperties -> Bool
$c== :: StyleProperties -> StyleProperties -> Bool
Eq, Int -> StyleProperties -> ShowS
[StyleProperties] -> ShowS
StyleProperties -> String
(Int -> StyleProperties -> ShowS)
-> (StyleProperties -> String)
-> ([StyleProperties] -> ShowS)
-> Show StyleProperties
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StyleProperties] -> ShowS
$cshowList :: [StyleProperties] -> ShowS
show :: StyleProperties -> String
$cshow :: StyleProperties -> String
showsPrec :: Int -> StyleProperties -> ShowS
$cshowsPrec :: Int -> StyleProperties -> ShowS
Show )

instance  Default StyleProperties where
  def :: StyleProperties
def =                SProps :: Maybe TextProperties -> Maybe ParaProperties -> StyleProperties
SProps { textProperties :: Maybe TextProperties
textProperties       = TextProperties -> Maybe TextProperties
forall a. a -> Maybe a
Just TextProperties
forall a. Default a => a
def
                               , paraProperties :: Maybe ParaProperties
paraProperties      = ParaProperties -> Maybe ParaProperties
forall a. a -> Maybe a
Just ParaProperties
forall a. Default a => a
def
                               }

data TextProperties   = PropT  { TextProperties -> Bool
isEmphasised     :: Bool
                               , TextProperties -> Bool
isStrong         :: Bool
                               , TextProperties -> Maybe FontPitch
pitch            :: Maybe FontPitch
                               , TextProperties -> VerticalTextPosition
verticalPosition :: VerticalTextPosition
                               , TextProperties -> Maybe UnderlineMode
underline        :: Maybe UnderlineMode
                               , TextProperties -> Maybe UnderlineMode
strikethrough    :: Maybe UnderlineMode
                               }
  deriving ( TextProperties -> TextProperties -> Bool
(TextProperties -> TextProperties -> Bool)
-> (TextProperties -> TextProperties -> Bool) -> Eq TextProperties
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TextProperties -> TextProperties -> Bool
$c/= :: TextProperties -> TextProperties -> Bool
== :: TextProperties -> TextProperties -> Bool
$c== :: TextProperties -> TextProperties -> Bool
Eq, Int -> TextProperties -> ShowS
[TextProperties] -> ShowS
TextProperties -> String
(Int -> TextProperties -> ShowS)
-> (TextProperties -> String)
-> ([TextProperties] -> ShowS)
-> Show TextProperties
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TextProperties] -> ShowS
$cshowList :: [TextProperties] -> ShowS
show :: TextProperties -> String
$cshow :: TextProperties -> String
showsPrec :: Int -> TextProperties -> ShowS
$cshowsPrec :: Int -> TextProperties -> ShowS
Show )

instance Default TextProperties where
  def :: TextProperties
def =                 PropT :: Bool
-> Bool
-> Maybe FontPitch
-> VerticalTextPosition
-> Maybe UnderlineMode
-> Maybe UnderlineMode
-> TextProperties
PropT  { isEmphasised :: Bool
isEmphasised     = Bool
False
                               , isStrong :: Bool
isStrong         = Bool
False
                               , pitch :: Maybe FontPitch
pitch            = FontPitch -> Maybe FontPitch
forall a. a -> Maybe a
Just FontPitch
forall a. Default a => a
def
                               , verticalPosition :: VerticalTextPosition
verticalPosition = VerticalTextPosition
forall a. Default a => a
def
                               , underline :: Maybe UnderlineMode
underline        = Maybe UnderlineMode
forall a. Maybe a
Nothing
                               , strikethrough :: Maybe UnderlineMode
strikethrough    = Maybe UnderlineMode
forall a. Maybe a
Nothing
                               }

data ParaProperties   = PropP { ParaProperties -> ParaNumbering
paraNumbering :: ParaNumbering
                              , ParaProperties -> LengthOrPercent
indentation   :: LengthOrPercent
                              , ParaProperties -> LengthOrPercent
margin_left   :: LengthOrPercent
                              }
  deriving ( ParaProperties -> ParaProperties -> Bool
(ParaProperties -> ParaProperties -> Bool)
-> (ParaProperties -> ParaProperties -> Bool) -> Eq ParaProperties
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParaProperties -> ParaProperties -> Bool
$c/= :: ParaProperties -> ParaProperties -> Bool
== :: ParaProperties -> ParaProperties -> Bool
$c== :: ParaProperties -> ParaProperties -> Bool
Eq, Int -> ParaProperties -> ShowS
[ParaProperties] -> ShowS
ParaProperties -> String
(Int -> ParaProperties -> ShowS)
-> (ParaProperties -> String)
-> ([ParaProperties] -> ShowS)
-> Show ParaProperties
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParaProperties] -> ShowS
$cshowList :: [ParaProperties] -> ShowS
show :: ParaProperties -> String
$cshow :: ParaProperties -> String
showsPrec :: Int -> ParaProperties -> ShowS
$cshowsPrec :: Int -> ParaProperties -> ShowS
Show )

instance Default ParaProperties where
  def :: ParaProperties
def =                 PropP :: ParaNumbering
-> LengthOrPercent -> LengthOrPercent -> ParaProperties
PropP { paraNumbering :: ParaNumbering
paraNumbering = ParaNumbering
NumberingNone
                              , indentation :: LengthOrPercent
indentation   = LengthOrPercent
forall a. Default a => a
def
                              , margin_left :: LengthOrPercent
margin_left   = LengthOrPercent
forall a. Default a => a
def
                              }

----
-- All the little data types that make up the properties
----

data VerticalTextPosition = VPosNormal | VPosSuper | VPosSub
  deriving ( VerticalTextPosition -> VerticalTextPosition -> Bool
(VerticalTextPosition -> VerticalTextPosition -> Bool)
-> (VerticalTextPosition -> VerticalTextPosition -> Bool)
-> Eq VerticalTextPosition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VerticalTextPosition -> VerticalTextPosition -> Bool
$c/= :: VerticalTextPosition -> VerticalTextPosition -> Bool
== :: VerticalTextPosition -> VerticalTextPosition -> Bool
$c== :: VerticalTextPosition -> VerticalTextPosition -> Bool
Eq, Int -> VerticalTextPosition -> ShowS
[VerticalTextPosition] -> ShowS
VerticalTextPosition -> String
(Int -> VerticalTextPosition -> ShowS)
-> (VerticalTextPosition -> String)
-> ([VerticalTextPosition] -> ShowS)
-> Show VerticalTextPosition
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VerticalTextPosition] -> ShowS
$cshowList :: [VerticalTextPosition] -> ShowS
show :: VerticalTextPosition -> String
$cshow :: VerticalTextPosition -> String
showsPrec :: Int -> VerticalTextPosition -> ShowS
$cshowsPrec :: Int -> VerticalTextPosition -> ShowS
Show )

instance Default VerticalTextPosition where
  def :: VerticalTextPosition
def = VerticalTextPosition
VPosNormal

instance Read VerticalTextPosition where
  readsPrec :: Int -> ReadS VerticalTextPosition
readsPrec Int
_ String
s =    [ (VerticalTextPosition
VPosSub        , String
s') | (String
"sub"   , String
s') <- [(String, String)]
lexS          ]
                  [(VerticalTextPosition, String)]
-> [(VerticalTextPosition, String)]
-> [(VerticalTextPosition, String)]
forall a. [a] -> [a] -> [a]
++ [ (VerticalTextPosition
VPosSuper      , String
s') | (String
"super" , String
s') <- [(String, String)]
lexS          ]
                  [(VerticalTextPosition, String)]
-> [(VerticalTextPosition, String)]
-> [(VerticalTextPosition, String)]
forall a. [a] -> [a] -> [a]
++ [ (Int -> VerticalTextPosition
forall a. (Ord a, Num a) => a -> VerticalTextPosition
signumToVPos Int
n , String
s') | (  Int
n     , String
s') <- ReadS Int
readPercent String
s ]
    where
      lexS :: [(String, String)]
lexS = ReadS String
lex String
s
      signumToVPos :: a -> VerticalTextPosition
signumToVPos a
n | a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0     = VerticalTextPosition
VPosSub
                     | a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0     = VerticalTextPosition
VPosSuper
                     | Bool
otherwise = VerticalTextPosition
VPosNormal

data UnderlineMode = UnderlineModeNormal | UnderlineModeSkipWhitespace
  deriving ( UnderlineMode -> UnderlineMode -> Bool
(UnderlineMode -> UnderlineMode -> Bool)
-> (UnderlineMode -> UnderlineMode -> Bool) -> Eq UnderlineMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UnderlineMode -> UnderlineMode -> Bool
$c/= :: UnderlineMode -> UnderlineMode -> Bool
== :: UnderlineMode -> UnderlineMode -> Bool
$c== :: UnderlineMode -> UnderlineMode -> Bool
Eq, Int -> UnderlineMode -> ShowS
[UnderlineMode] -> ShowS
UnderlineMode -> String
(Int -> UnderlineMode -> ShowS)
-> (UnderlineMode -> String)
-> ([UnderlineMode] -> ShowS)
-> Show UnderlineMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UnderlineMode] -> ShowS
$cshowList :: [UnderlineMode] -> ShowS
show :: UnderlineMode -> String
$cshow :: UnderlineMode -> String
showsPrec :: Int -> UnderlineMode -> ShowS
$cshowsPrec :: Int -> UnderlineMode -> ShowS
Show )

instance Lookupable UnderlineMode where
  lookupTable :: [(Text, UnderlineMode)]
lookupTable = [ ( Text
"continuous"       , UnderlineMode
UnderlineModeNormal         )
                , ( Text
"skip-white-space" , UnderlineMode
UnderlineModeSkipWhitespace )
                ]


data ParaNumbering = NumberingNone | NumberingKeep | NumberingRestart Int
  deriving ( ParaNumbering -> ParaNumbering -> Bool
(ParaNumbering -> ParaNumbering -> Bool)
-> (ParaNumbering -> ParaNumbering -> Bool) -> Eq ParaNumbering
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParaNumbering -> ParaNumbering -> Bool
$c/= :: ParaNumbering -> ParaNumbering -> Bool
== :: ParaNumbering -> ParaNumbering -> Bool
$c== :: ParaNumbering -> ParaNumbering -> Bool
Eq, Int -> ParaNumbering -> ShowS
[ParaNumbering] -> ShowS
ParaNumbering -> String
(Int -> ParaNumbering -> ShowS)
-> (ParaNumbering -> String)
-> ([ParaNumbering] -> ShowS)
-> Show ParaNumbering
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParaNumbering] -> ShowS
$cshowList :: [ParaNumbering] -> ShowS
show :: ParaNumbering -> String
$cshow :: ParaNumbering -> String
showsPrec :: Int -> ParaNumbering -> ShowS
$cshowsPrec :: Int -> ParaNumbering -> ShowS
Show )

data LengthOrPercent = LengthValueMM Int | PercentValue Int
  deriving ( LengthOrPercent -> LengthOrPercent -> Bool
(LengthOrPercent -> LengthOrPercent -> Bool)
-> (LengthOrPercent -> LengthOrPercent -> Bool)
-> Eq LengthOrPercent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LengthOrPercent -> LengthOrPercent -> Bool
$c/= :: LengthOrPercent -> LengthOrPercent -> Bool
== :: LengthOrPercent -> LengthOrPercent -> Bool
$c== :: LengthOrPercent -> LengthOrPercent -> Bool
Eq, Int -> LengthOrPercent -> ShowS
[LengthOrPercent] -> ShowS
LengthOrPercent -> String
(Int -> LengthOrPercent -> ShowS)
-> (LengthOrPercent -> String)
-> ([LengthOrPercent] -> ShowS)
-> Show LengthOrPercent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LengthOrPercent] -> ShowS
$cshowList :: [LengthOrPercent] -> ShowS
show :: LengthOrPercent -> String
$cshow :: LengthOrPercent -> String
showsPrec :: Int -> LengthOrPercent -> ShowS
$cshowsPrec :: Int -> LengthOrPercent -> ShowS
Show )

instance Default LengthOrPercent where
  def :: LengthOrPercent
def = Int -> LengthOrPercent
LengthValueMM Int
0

instance Read LengthOrPercent where
  readsPrec :: Int -> ReadS LengthOrPercent
readsPrec Int
_ String
s =
      [ (Int -> LengthOrPercent
PercentValue  Int
percent  , String
s' ) | (Int
percent , String
s' ) <- ReadS Int
readPercent String
s]
   [(LengthOrPercent, String)]
-> [(LengthOrPercent, String)] -> [(LengthOrPercent, String)]
forall a. [a] -> [a] -> [a]
++ [ (Int -> LengthOrPercent
LengthValueMM Int
lengthMM , String
s'') | (Int
length' , String
s' ) <- ReadS Int
forall a. Read a => ReadS a
reads String
s
                                       , (XslUnit
unit    , String
s'') <- ReadS XslUnit
forall a. Read a => ReadS a
reads String
s'
                                       , let lengthMM :: Int
lengthMM = Int -> XslUnit -> Int
estimateInMillimeter
                                                                   Int
length' XslUnit
unit
                                       ]

data XslUnit = XslUnitMM | XslUnitCM
             | XslUnitInch
             | XslUnitPoints | XslUnitPica
             | XslUnitPixel
             | XslUnitEM

instance Show XslUnit where
  show :: XslUnit -> String
show XslUnit
XslUnitMM     = String
"mm"
  show XslUnit
XslUnitCM     = String
"cm"
  show XslUnit
XslUnitInch   = String
"in"
  show XslUnit
XslUnitPoints = String
"pt"
  show XslUnit
XslUnitPica   = String
"pc"
  show XslUnit
XslUnitPixel  = String
"px"
  show XslUnit
XslUnitEM     = String
"em"

instance Read XslUnit where
  readsPrec :: Int -> ReadS XslUnit
readsPrec Int
_ String
"mm" = [(XslUnit
XslUnitMM     , String
"")]
  readsPrec Int
_ String
"cm" = [(XslUnit
XslUnitCM     , String
"")]
  readsPrec Int
_ String
"in" = [(XslUnit
XslUnitInch   , String
"")]
  readsPrec Int
_ String
"pt" = [(XslUnit
XslUnitPoints , String
"")]
  readsPrec Int
_ String
"pc" = [(XslUnit
XslUnitPica   , String
"")]
  readsPrec Int
_ String
"px" = [(XslUnit
XslUnitPixel  , String
"")]
  readsPrec Int
_ String
"em" = [(XslUnit
XslUnitEM     , String
"")]
  readsPrec Int
_  String
_   = []

-- | Rough conversion of measures into millimetres.
-- Pixels and em's are actually implementation dependent/relative measures,
-- so I could not really easily calculate anything exact here even if I wanted.
-- But I do not care about exactness right now, as I only use measures
-- to determine if a paragraph is "indented" or not.
estimateInMillimeter :: Int -> XslUnit -> Int
estimateInMillimeter :: Int -> XslUnit -> Int
estimateInMillimeter Int
n XslUnit
XslUnitMM     = Int
n
estimateInMillimeter Int
n XslUnit
XslUnitCM     = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
10
estimateInMillimeter Int
n XslUnit
XslUnitInch   = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
25    -- \*             25.4
estimateInMillimeter Int
n XslUnit
XslUnitPoints = Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
3 -- \*      1/72 * 25.4
estimateInMillimeter Int
n XslUnit
XslUnitPica   = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
4     -- \* 12 * 1/72 * 25.4
estimateInMillimeter Int
n XslUnit
XslUnitPixel  = Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div`Int
3  -- \*      1/72 * 25.4
estimateInMillimeter Int
n XslUnit
XslUnitEM     = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
7     -- \* 16 * 1/72 * 25.4


----
-- List styles
----

type ListLevel = Int

newtype ListStyle = ListStyle { ListStyle -> Map Int ListLevelStyle
levelStyles :: M.Map ListLevel ListLevelStyle
                              }
  deriving ( ListStyle -> ListStyle -> Bool
(ListStyle -> ListStyle -> Bool)
-> (ListStyle -> ListStyle -> Bool) -> Eq ListStyle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ListStyle -> ListStyle -> Bool
$c/= :: ListStyle -> ListStyle -> Bool
== :: ListStyle -> ListStyle -> Bool
$c== :: ListStyle -> ListStyle -> Bool
Eq, Int -> ListStyle -> ShowS
[ListStyle] -> ShowS
ListStyle -> String
(Int -> ListStyle -> ShowS)
-> (ListStyle -> String)
-> ([ListStyle] -> ShowS)
-> Show ListStyle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ListStyle] -> ShowS
$cshowList :: [ListStyle] -> ShowS
show :: ListStyle -> String
$cshow :: ListStyle -> String
showsPrec :: Int -> ListStyle -> ShowS
$cshowsPrec :: Int -> ListStyle -> ShowS
Show )

--
getListLevelStyle :: ListLevel -> ListStyle -> Maybe ListLevelStyle
getListLevelStyle :: Int -> ListStyle -> Maybe ListLevelStyle
getListLevelStyle Int
level ListStyle{Map Int ListLevelStyle
levelStyles :: Map Int ListLevelStyle
levelStyles :: ListStyle -> Map Int ListLevelStyle
..} =
  let (Map Int ListLevelStyle
lower , Maybe ListLevelStyle
exactHit , Map Int ListLevelStyle
_) = Int
-> Map Int ListLevelStyle
-> (Map Int ListLevelStyle, Maybe ListLevelStyle,
    Map Int ListLevelStyle)
forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
M.splitLookup Int
level Map Int ListLevelStyle
levelStyles
  in  Maybe ListLevelStyle
exactHit Maybe ListLevelStyle
-> Maybe ListLevelStyle -> Maybe ListLevelStyle
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ((ListLevelStyle, Map Int ListLevelStyle) -> ListLevelStyle)
-> Maybe (ListLevelStyle, Map Int ListLevelStyle)
-> Maybe ListLevelStyle
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ListLevelStyle, Map Int ListLevelStyle) -> ListLevelStyle
forall a b. (a, b) -> a
fst (Map Int ListLevelStyle
-> Maybe (ListLevelStyle, Map Int ListLevelStyle)
forall k a. Map k a -> Maybe (a, Map k a)
M.maxView Map Int ListLevelStyle
lower)
  -- findBy (`M.lookup` levelStyles) [level, (level-1) .. 1]
  -- \^ simpler, but in general less efficient

data ListLevelStyle = ListLevelStyle { ListLevelStyle -> ListLevelType
listLevelType  :: ListLevelType
                                     , ListLevelStyle -> Maybe Text
listItemPrefix :: Maybe Text
                                     , ListLevelStyle -> Maybe Text
listItemSuffix :: Maybe Text
                                     , ListLevelStyle -> ListItemNumberFormat
listItemFormat :: ListItemNumberFormat
                                     , ListLevelStyle -> Int
listItemStart  :: Int
                                     }
  deriving ( ListLevelStyle -> ListLevelStyle -> Bool
(ListLevelStyle -> ListLevelStyle -> Bool)
-> (ListLevelStyle -> ListLevelStyle -> Bool) -> Eq ListLevelStyle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ListLevelStyle -> ListLevelStyle -> Bool
$c/= :: ListLevelStyle -> ListLevelStyle -> Bool
== :: ListLevelStyle -> ListLevelStyle -> Bool
$c== :: ListLevelStyle -> ListLevelStyle -> Bool
Eq, Eq ListLevelStyle
Eq ListLevelStyle
-> (ListLevelStyle -> ListLevelStyle -> Ordering)
-> (ListLevelStyle -> ListLevelStyle -> Bool)
-> (ListLevelStyle -> ListLevelStyle -> Bool)
-> (ListLevelStyle -> ListLevelStyle -> Bool)
-> (ListLevelStyle -> ListLevelStyle -> Bool)
-> (ListLevelStyle -> ListLevelStyle -> ListLevelStyle)
-> (ListLevelStyle -> ListLevelStyle -> ListLevelStyle)
-> Ord ListLevelStyle
ListLevelStyle -> ListLevelStyle -> Bool
ListLevelStyle -> ListLevelStyle -> Ordering
ListLevelStyle -> ListLevelStyle -> ListLevelStyle
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ListLevelStyle -> ListLevelStyle -> ListLevelStyle
$cmin :: ListLevelStyle -> ListLevelStyle -> ListLevelStyle
max :: ListLevelStyle -> ListLevelStyle -> ListLevelStyle
$cmax :: ListLevelStyle -> ListLevelStyle -> ListLevelStyle
>= :: ListLevelStyle -> ListLevelStyle -> Bool
$c>= :: ListLevelStyle -> ListLevelStyle -> Bool
> :: ListLevelStyle -> ListLevelStyle -> Bool
$c> :: ListLevelStyle -> ListLevelStyle -> Bool
<= :: ListLevelStyle -> ListLevelStyle -> Bool
$c<= :: ListLevelStyle -> ListLevelStyle -> Bool
< :: ListLevelStyle -> ListLevelStyle -> Bool
$c< :: ListLevelStyle -> ListLevelStyle -> Bool
compare :: ListLevelStyle -> ListLevelStyle -> Ordering
$ccompare :: ListLevelStyle -> ListLevelStyle -> Ordering
$cp1Ord :: Eq ListLevelStyle
Ord )

instance Show ListLevelStyle where
  show :: ListLevelStyle -> String
show ListLevelStyle{Int
Maybe Text
ListItemNumberFormat
ListLevelType
listItemStart :: Int
listItemFormat :: ListItemNumberFormat
listItemSuffix :: Maybe Text
listItemPrefix :: Maybe Text
listLevelType :: ListLevelType
listItemStart :: ListLevelStyle -> Int
listItemFormat :: ListLevelStyle -> ListItemNumberFormat
listItemSuffix :: ListLevelStyle -> Maybe Text
listItemPrefix :: ListLevelStyle -> Maybe Text
listLevelType :: ListLevelStyle -> ListLevelType
..} =    String
"<LLS|"
                            String -> ShowS
forall a. [a] -> [a] -> [a]
++ ListLevelType -> String
forall a. Show a => a -> String
show ListLevelType
listLevelType
                            String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"|"
                            String -> ShowS
forall a. [a] -> [a] -> [a]
++ Maybe String -> String
maybeToString (Text -> String
T.unpack (Text -> String) -> Maybe Text -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
listItemPrefix)
                            String -> ShowS
forall a. [a] -> [a] -> [a]
++ ListItemNumberFormat -> String
forall a. Show a => a -> String
show ListItemNumberFormat
listItemFormat
                            String -> ShowS
forall a. [a] -> [a] -> [a]
++ Maybe String -> String
maybeToString (Text -> String
T.unpack (Text -> String) -> Maybe Text -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
listItemSuffix)
                            String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
">"
    where maybeToString :: Maybe String -> String
maybeToString = String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
""

data ListLevelType = LltBullet | LltImage | LltNumbered
  deriving ( ListLevelType -> ListLevelType -> Bool
(ListLevelType -> ListLevelType -> Bool)
-> (ListLevelType -> ListLevelType -> Bool) -> Eq ListLevelType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ListLevelType -> ListLevelType -> Bool
$c/= :: ListLevelType -> ListLevelType -> Bool
== :: ListLevelType -> ListLevelType -> Bool
$c== :: ListLevelType -> ListLevelType -> Bool
Eq, Eq ListLevelType
Eq ListLevelType
-> (ListLevelType -> ListLevelType -> Ordering)
-> (ListLevelType -> ListLevelType -> Bool)
-> (ListLevelType -> ListLevelType -> Bool)
-> (ListLevelType -> ListLevelType -> Bool)
-> (ListLevelType -> ListLevelType -> Bool)
-> (ListLevelType -> ListLevelType -> ListLevelType)
-> (ListLevelType -> ListLevelType -> ListLevelType)
-> Ord ListLevelType
ListLevelType -> ListLevelType -> Bool
ListLevelType -> ListLevelType -> Ordering
ListLevelType -> ListLevelType -> ListLevelType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ListLevelType -> ListLevelType -> ListLevelType
$cmin :: ListLevelType -> ListLevelType -> ListLevelType
max :: ListLevelType -> ListLevelType -> ListLevelType
$cmax :: ListLevelType -> ListLevelType -> ListLevelType
>= :: ListLevelType -> ListLevelType -> Bool
$c>= :: ListLevelType -> ListLevelType -> Bool
> :: ListLevelType -> ListLevelType -> Bool
$c> :: ListLevelType -> ListLevelType -> Bool
<= :: ListLevelType -> ListLevelType -> Bool
$c<= :: ListLevelType -> ListLevelType -> Bool
< :: ListLevelType -> ListLevelType -> Bool
$c< :: ListLevelType -> ListLevelType -> Bool
compare :: ListLevelType -> ListLevelType -> Ordering
$ccompare :: ListLevelType -> ListLevelType -> Ordering
$cp1Ord :: Eq ListLevelType
Ord, Int -> ListLevelType -> ShowS
[ListLevelType] -> ShowS
ListLevelType -> String
(Int -> ListLevelType -> ShowS)
-> (ListLevelType -> String)
-> ([ListLevelType] -> ShowS)
-> Show ListLevelType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ListLevelType] -> ShowS
$cshowList :: [ListLevelType] -> ShowS
show :: ListLevelType -> String
$cshow :: ListLevelType -> String
showsPrec :: Int -> ListLevelType -> ShowS
$cshowsPrec :: Int -> ListLevelType -> ShowS
Show )

data ListItemNumberFormat = LinfNone
                          | LinfNumber
                          | LinfRomanLC | LinfRomanUC
                          | LinfAlphaLC | LinfAlphaUC
                          | LinfString String
  deriving ( ListItemNumberFormat -> ListItemNumberFormat -> Bool
(ListItemNumberFormat -> ListItemNumberFormat -> Bool)
-> (ListItemNumberFormat -> ListItemNumberFormat -> Bool)
-> Eq ListItemNumberFormat
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
$c/= :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
== :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
$c== :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
Eq, Eq ListItemNumberFormat
Eq ListItemNumberFormat
-> (ListItemNumberFormat -> ListItemNumberFormat -> Ordering)
-> (ListItemNumberFormat -> ListItemNumberFormat -> Bool)
-> (ListItemNumberFormat -> ListItemNumberFormat -> Bool)
-> (ListItemNumberFormat -> ListItemNumberFormat -> Bool)
-> (ListItemNumberFormat -> ListItemNumberFormat -> Bool)
-> (ListItemNumberFormat
    -> ListItemNumberFormat -> ListItemNumberFormat)
-> (ListItemNumberFormat
    -> ListItemNumberFormat -> ListItemNumberFormat)
-> Ord ListItemNumberFormat
ListItemNumberFormat -> ListItemNumberFormat -> Bool
ListItemNumberFormat -> ListItemNumberFormat -> Ordering
ListItemNumberFormat
-> ListItemNumberFormat -> ListItemNumberFormat
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ListItemNumberFormat
-> ListItemNumberFormat -> ListItemNumberFormat
$cmin :: ListItemNumberFormat
-> ListItemNumberFormat -> ListItemNumberFormat
max :: ListItemNumberFormat
-> ListItemNumberFormat -> ListItemNumberFormat
$cmax :: ListItemNumberFormat
-> ListItemNumberFormat -> ListItemNumberFormat
>= :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
$c>= :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
> :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
$c> :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
<= :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
$c<= :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
< :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
$c< :: ListItemNumberFormat -> ListItemNumberFormat -> Bool
compare :: ListItemNumberFormat -> ListItemNumberFormat -> Ordering
$ccompare :: ListItemNumberFormat -> ListItemNumberFormat -> Ordering
$cp1Ord :: Eq ListItemNumberFormat
Ord )

instance Show ListItemNumberFormat where
  show :: ListItemNumberFormat -> String
show  ListItemNumberFormat
LinfNone      = String
""
  show  ListItemNumberFormat
LinfNumber    = String
"1"
  show  ListItemNumberFormat
LinfRomanLC   = String
"i"
  show  ListItemNumberFormat
LinfRomanUC   = String
"I"
  show  ListItemNumberFormat
LinfAlphaLC   = String
"a"
  show  ListItemNumberFormat
LinfAlphaUC   = String
"A"
  show (LinfString String
s) =  String
s

instance Default ListItemNumberFormat where
  def :: ListItemNumberFormat
def = ListItemNumberFormat
LinfNone

instance Read ListItemNumberFormat where
  readsPrec :: Int -> ReadS ListItemNumberFormat
readsPrec Int
_ String
""  = [(ListItemNumberFormat
LinfNone     , String
"")]
  readsPrec Int
_ String
"1" = [(ListItemNumberFormat
LinfNumber   , String
"")]
  readsPrec Int
_ String
"i" = [(ListItemNumberFormat
LinfRomanLC  , String
"")]
  readsPrec Int
_ String
"I" = [(ListItemNumberFormat
LinfRomanUC  , String
"")]
  readsPrec Int
_ String
"a" = [(ListItemNumberFormat
LinfAlphaLC  , String
"")]
  readsPrec Int
_ String
"A" = [(ListItemNumberFormat
LinfAlphaUC  , String
"")]
  readsPrec Int
_  String
s  = [(String -> ListItemNumberFormat
LinfString String
s , String
"")]

--------------------------------------------------------------------------------
-- Readers
--
-- ...it seems like a whole lot of this should be automatically derivable
--    or at least moveable into a class. Most of this is data concealed in
--    code.
--------------------------------------------------------------------------------

--
readAllStyles :: StyleReader _x Styles
readAllStyles :: StyleReader _x Styles
readAllStyles = (      StyleReader _x _x
forall x. StyleReader x x
readFontPitches
                  StyleReader _x _x
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     _x
     (Fallible Styles, Fallible Styles)
-> FallibleArrow
     (ArrowState (XMLConverterState Namespace FontPitches))
     _x
     ()
     (Fallible Styles, Fallible Styles)
forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> a success success' -> FallibleArrow a x failure success'
>>?! (     StyleReader _x Styles
forall _x. StyleReader _x Styles
readAutomaticStyles
                         StyleReader _x Styles
-> StyleReader _x Styles
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     _x
     (Fallible Styles, Fallible Styles)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& StyleReader _x Styles
forall _x. StyleReader _x Styles
readStyles ))
                  FallibleArrow
  (ArrowState (XMLConverterState Namespace FontPitches))
  _x
  ()
  (Fallible Styles, Fallible Styles)
-> (Fallible Styles -> Fallible Styles -> Fallible Styles)
-> StyleReader _x Styles
forall (a :: * -> * -> *) x f b b' c.
ArrowChoice a =>
FallibleArrow a x f (b, b')
-> (b -> b' -> Either f c) -> FallibleArrow a x f c
>>?%? Fallible Styles -> Fallible Styles -> Fallible Styles
forall a b.
(Monoid a, Monoid b) =>
Either a b -> Either a b -> Either a b
chooseMax
 -- all top elements are always on the same hierarchy level

--
readStyles :: StyleReader _x Styles
readStyles :: StyleReader _x Styles
readStyles = Namespace -> Text -> StyleReader _x Styles -> StyleReader _x Styles
forall nsID extraState f s.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState f s
-> FallibleXMLConverter nsID extraState f s
executeInSub Namespace
NsOffice Text
"styles" (StyleReader _x Styles -> StyleReader _x Styles)
-> StyleReader _x Styles -> StyleReader _x Styles
forall a b. (a -> b) -> a -> b
$ ArrowState (XMLConverterState Namespace FontPitches) _x Styles
-> StyleReader _x Styles
forall (a :: * -> * -> *) x success failure.
ArrowChoice a =>
a x success -> FallibleArrow a x failure success
liftAsSuccess
  (ArrowState (XMLConverterState Namespace FontPitches) _x Styles
 -> StyleReader _x Styles)
-> ArrowState (XMLConverterState Namespace FontPitches) _x Styles
-> StyleReader _x Styles
forall a b. (a -> b) -> a -> b
$ (Map Text Style
 -> Map Text ListStyle -> Map StyleFamily StyleProperties -> Styles)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Map Text Style)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Map Text ListStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     _x
     (Map StyleFamily StyleProperties)
-> ArrowState (XMLConverterState Namespace FontPitches) _x Styles
forall (a :: * -> * -> *) z y x r b.
Arrow a =>
(z -> y -> x -> r) -> a b z -> a b y -> a b x -> a b r
liftA3 Map Text Style
-> Map Text ListStyle -> Map StyleFamily StyleProperties -> Styles
Styles
    ( Namespace
-> Text
-> FallibleXMLConverter Namespace FontPitches _x (Text, Style)
-> XMLConverter Namespace FontPitches _x [(Text, Style)]
forall nsID extraState b a.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState b a
-> XMLConverter nsID extraState b [a]
tryAll Namespace
NsStyle Text
"style"         FallibleXMLConverter Namespace FontPitches _x (Text, Style)
forall _x. StyleReader _x (Text, Style)
readStyle        XMLConverter Namespace FontPitches _x [(Text, Style)]
-> ([(Text, Style)] -> Map Text Style)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Map Text Style)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ [(Text, Style)] -> Map Text Style
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList )
    ( Namespace
-> Text
-> FallibleXMLConverter Namespace FontPitches _x (Text, ListStyle)
-> XMLConverter Namespace FontPitches _x [(Text, ListStyle)]
forall nsID extraState b a.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState b a
-> XMLConverter nsID extraState b [a]
tryAll Namespace
NsText  Text
"list-style"    FallibleXMLConverter Namespace FontPitches _x (Text, ListStyle)
forall _x. StyleReader _x (Text, ListStyle)
readListStyle    XMLConverter Namespace FontPitches _x [(Text, ListStyle)]
-> ([(Text, ListStyle)] -> Map Text ListStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Map Text ListStyle)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ [(Text, ListStyle)] -> Map Text ListStyle
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList )
    ( Namespace
-> Text
-> FallibleXMLConverter
     Namespace FontPitches _x (StyleFamily, StyleProperties)
-> XMLConverter
     Namespace FontPitches _x [(StyleFamily, StyleProperties)]
forall nsID extraState b a.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState b a
-> XMLConverter nsID extraState b [a]
tryAll Namespace
NsStyle Text
"default-style" FallibleXMLConverter
  Namespace FontPitches _x (StyleFamily, StyleProperties)
forall _x. StyleReader _x (StyleFamily, StyleProperties)
readDefaultStyle XMLConverter
  Namespace FontPitches _x [(StyleFamily, StyleProperties)]
-> ([(StyleFamily, StyleProperties)]
    -> Map StyleFamily StyleProperties)
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     _x
     (Map StyleFamily StyleProperties)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ [(StyleFamily, StyleProperties)] -> Map StyleFamily StyleProperties
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList )

--
readAutomaticStyles :: StyleReader _x Styles
readAutomaticStyles :: StyleReader _x Styles
readAutomaticStyles = Namespace -> Text -> StyleReader _x Styles -> StyleReader _x Styles
forall nsID extraState f s.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState f s
-> FallibleXMLConverter nsID extraState f s
executeInSub Namespace
NsOffice Text
"automatic-styles" (StyleReader _x Styles -> StyleReader _x Styles)
-> StyleReader _x Styles -> StyleReader _x Styles
forall a b. (a -> b) -> a -> b
$ ArrowState (XMLConverterState Namespace FontPitches) _x Styles
-> StyleReader _x Styles
forall (a :: * -> * -> *) x success failure.
ArrowChoice a =>
a x success -> FallibleArrow a x failure success
liftAsSuccess
  (ArrowState (XMLConverterState Namespace FontPitches) _x Styles
 -> StyleReader _x Styles)
-> ArrowState (XMLConverterState Namespace FontPitches) _x Styles
-> StyleReader _x Styles
forall a b. (a -> b) -> a -> b
$ (Map Text Style
 -> Map Text ListStyle -> Map StyleFamily StyleProperties -> Styles)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Map Text Style)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Map Text ListStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     _x
     (Map StyleFamily StyleProperties)
-> ArrowState (XMLConverterState Namespace FontPitches) _x Styles
forall (a :: * -> * -> *) z y x r b.
Arrow a =>
(z -> y -> x -> r) -> a b z -> a b y -> a b x -> a b r
liftA3 Map Text Style
-> Map Text ListStyle -> Map StyleFamily StyleProperties -> Styles
Styles
    ( Namespace
-> Text
-> FallibleXMLConverter Namespace FontPitches _x (Text, Style)
-> XMLConverter Namespace FontPitches _x [(Text, Style)]
forall nsID extraState b a.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState b a
-> XMLConverter nsID extraState b [a]
tryAll Namespace
NsStyle Text
"style"         FallibleXMLConverter Namespace FontPitches _x (Text, Style)
forall _x. StyleReader _x (Text, Style)
readStyle        XMLConverter Namespace FontPitches _x [(Text, Style)]
-> ([(Text, Style)] -> Map Text Style)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Map Text Style)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ [(Text, Style)] -> Map Text Style
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList )
    ( Namespace
-> Text
-> FallibleXMLConverter Namespace FontPitches _x (Text, ListStyle)
-> XMLConverter Namespace FontPitches _x [(Text, ListStyle)]
forall nsID extraState b a.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState b a
-> XMLConverter nsID extraState b [a]
tryAll Namespace
NsText  Text
"list-style"    FallibleXMLConverter Namespace FontPitches _x (Text, ListStyle)
forall _x. StyleReader _x (Text, ListStyle)
readListStyle    XMLConverter Namespace FontPitches _x [(Text, ListStyle)]
-> ([(Text, ListStyle)] -> Map Text ListStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Map Text ListStyle)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ [(Text, ListStyle)] -> Map Text ListStyle
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList )
    ( Map StyleFamily StyleProperties
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     _x
     (Map StyleFamily StyleProperties)
forall (a :: * -> * -> *) c x. Arrow a => c -> a x c
returnV Map StyleFamily StyleProperties
forall k a. Map k a
M.empty                                                )

--
readDefaultStyle :: StyleReader _x (StyleFamily, StyleProperties)
readDefaultStyle :: StyleReader _x (StyleFamily, StyleProperties)
readDefaultStyle =      Namespace
-> Text
-> FallibleXMLConverter Namespace FontPitches _x StyleFamily
forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> Text -> FallibleXMLConverter nsID extraState x a
lookupAttr Namespace
NsStyle Text
"family"
                   FallibleXMLConverter Namespace FontPitches _x StyleFamily
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     StyleFamily
     (StyleFamily, StyleProperties)
-> StyleReader _x (StyleFamily, StyleProperties)
forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> a success success' -> FallibleArrow a x failure success'
>>?! ArrowState
  (XMLConverterState Namespace FontPitches)
  StyleFamily
  StyleProperties
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     StyleFamily
     (StyleFamily, StyleProperties)
forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue ArrowState
  (XMLConverterState Namespace FontPitches)
  StyleFamily
  StyleProperties
forall _x. StyleReaderSafe _x StyleProperties
readStyleProperties

--
readStyle :: StyleReader _x (StyleName,Style)
readStyle :: StyleReader _x (Text, Style)
readStyle =      Namespace
-> Text -> FallibleXMLConverter Namespace FontPitches _x Text
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> FallibleXMLConverter nsID extraState x Text
findAttr Namespace
NsStyle Text
"name"
            FallibleXMLConverter Namespace FontPitches _x Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Text, Style)
-> StyleReader _x (Text, Style)
forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> a success success' -> FallibleArrow a x failure success'
>>?! ArrowState (XMLConverterState Namespace FontPitches) Text Style
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Text, Style)
forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue
                   ( (Maybe StyleFamily
 -> Maybe Text -> Maybe Text -> StyleProperties -> Style)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Maybe StyleFamily)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Maybe Text)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Maybe Text)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text StyleProperties
-> ArrowState (XMLConverterState Namespace FontPitches) Text Style
forall (a :: * -> * -> *) z y x w r b.
Arrow a =>
(z -> y -> x -> w -> r)
-> a b z -> a b y -> a b x -> a b w -> a b r
liftA4 Maybe StyleFamily
-> Maybe Text -> Maybe Text -> StyleProperties -> Style
Style
                       ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Maybe StyleFamily)
forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe a)
lookupAttr' Namespace
NsStyle Text
"family"            )
                       ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Maybe Text)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr'   Namespace
NsStyle Text
"parent-style-name" )
                       ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Maybe Text)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr'   Namespace
NsStyle Text
"list-style-name"   )
                       ArrowState
  (XMLConverterState Namespace FontPitches) Text StyleProperties
forall _x. StyleReaderSafe _x StyleProperties
readStyleProperties
                   )

--
readStyleProperties :: StyleReaderSafe _x StyleProperties
readStyleProperties :: StyleReaderSafe _x StyleProperties
readStyleProperties = (Maybe TextProperties -> Maybe ParaProperties -> StyleProperties)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe TextProperties)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe ParaProperties)
-> StyleReaderSafe _x StyleProperties
forall (a :: * -> * -> *) x y z b.
Arrow a =>
(x -> y -> z) -> a b x -> a b y -> a b z
liftA2 Maybe TextProperties -> Maybe ParaProperties -> StyleProperties
SProps
                       ( StyleReader _x TextProperties
forall _x. StyleReader _x TextProperties
readTextProperties StyleReader _x TextProperties
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     (Fallible TextProperties)
     (Maybe TextProperties)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe TextProperties)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ArrowState
  (XMLConverterState Namespace FontPitches)
  (Fallible TextProperties)
  (Maybe TextProperties)
forall (a :: * -> * -> *) l r.
ArrowChoice a =>
a (Either l r) (Maybe r)
choiceToMaybe )
                       ( StyleReader _x ParaProperties
forall _x. StyleReader _x ParaProperties
readParaProperties StyleReader _x ParaProperties
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     (Fallible ParaProperties)
     (Maybe ParaProperties)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe ParaProperties)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ArrowState
  (XMLConverterState Namespace FontPitches)
  (Fallible ParaProperties)
  (Maybe ParaProperties)
forall (a :: * -> * -> *) l r.
ArrowChoice a =>
a (Either l r) (Maybe r)
choiceToMaybe )

--
readTextProperties :: StyleReader _x TextProperties
readTextProperties :: StyleReader _x TextProperties
readTextProperties =
  Namespace
-> Text
-> StyleReader _x TextProperties
-> StyleReader _x TextProperties
forall nsID extraState f s.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState f s
-> FallibleXMLConverter nsID extraState f s
executeInSub Namespace
NsStyle Text
"text-properties" (StyleReader _x TextProperties -> StyleReader _x TextProperties)
-> StyleReader _x TextProperties -> StyleReader _x TextProperties
forall a b. (a -> b) -> a -> b
$ ArrowState
  (XMLConverterState Namespace FontPitches) _x TextProperties
-> StyleReader _x TextProperties
forall (a :: * -> * -> *) x success failure.
ArrowChoice a =>
a x success -> FallibleArrow a x failure success
liftAsSuccess
    ( (Bool
 -> Bool
 -> Maybe FontPitch
 -> VerticalTextPosition
 -> Maybe UnderlineMode
 -> Maybe UnderlineMode
 -> TextProperties)
-> ArrowState (XMLConverterState Namespace FontPitches) _x Bool
-> ArrowState (XMLConverterState Namespace FontPitches) _x Bool
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe FontPitch)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x VerticalTextPosition
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe UnderlineMode)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe UnderlineMode)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x TextProperties
forall (a :: * -> * -> *) z y x w v u r b.
Arrow a =>
(z -> y -> x -> w -> v -> u -> r)
-> a b z -> a b y -> a b x -> a b w -> a b v -> a b u -> a b r
liftA6 Bool
-> Bool
-> Maybe FontPitch
-> VerticalTextPosition
-> Maybe UnderlineMode
-> Maybe UnderlineMode
-> TextProperties
PropT
       ( Namespace
-> Text
-> Bool
-> [(Text, Bool)]
-> ArrowState (XMLConverterState Namespace FontPitches) _x Bool
forall nsID a extraState x.
NameSpaceID nsID =>
nsID
-> Text -> a -> [(Text, a)] -> XMLConverter nsID extraState x a
searchAttr   Namespace
NsXSL_FO Text
"font-style"  Bool
False [(Text, Bool)]
isFontEmphasised )
       ( Namespace
-> Text
-> Bool
-> [(Text, Bool)]
-> ArrowState (XMLConverterState Namespace FontPitches) _x Bool
forall nsID a extraState x.
NameSpaceID nsID =>
nsID
-> Text -> a -> [(Text, a)] -> XMLConverter nsID extraState x a
searchAttr   Namespace
NsXSL_FO Text
"font-weight" Bool
False [(Text, Bool)]
isFontBold       )
       ArrowState
  (XMLConverterState Namespace FontPitches) _x (Maybe FontPitch)
forall _x. XMLReaderSafe FontPitches _x (Maybe FontPitch)
findPitch
       ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x VerticalTextPosition
forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue, Default attrValue) =>
nsID -> Text -> XMLConverter nsID extraState x attrValue
getAttr      Namespace
NsStyle  Text
"text-position"                      )
       ArrowState
  (XMLConverterState Namespace FontPitches) _x (Maybe UnderlineMode)
forall _x. StyleReaderSafe _x (Maybe UnderlineMode)
readUnderlineMode
       ArrowState
  (XMLConverterState Namespace FontPitches) _x (Maybe UnderlineMode)
forall _x. StyleReaderSafe _x (Maybe UnderlineMode)
readStrikeThroughMode
     )
  where isFontEmphasised :: [(Text, Bool)]
isFontEmphasised = [(Text
"normal",Bool
False),(Text
"italic",Bool
True),(Text
"oblique",Bool
True)]
        isFontBold :: [(Text, Bool)]
isFontBold = (Text
"normal",Bool
False)(Text, Bool) -> [(Text, Bool)] -> [(Text, Bool)]
forall a. a -> [a] -> [a]
:(Text
"bold",Bool
True)
                    (Text, Bool) -> [(Text, Bool)] -> [(Text, Bool)]
forall a. a -> [a] -> [a]
:(Int -> (Text, Bool)) -> [Int] -> [(Text, Bool)]
forall a b. (a -> b) -> [a] -> [b]
map ((,Bool
True) (Text -> (Text, Bool)) -> (Int -> Text) -> Int -> (Text, Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text
forall a. Show a => a -> Text
tshow) ([Int
100,Int
200..Int
900]::[Int])

readUnderlineMode     :: StyleReaderSafe _x (Maybe UnderlineMode)
readUnderlineMode :: StyleReaderSafe _x (Maybe UnderlineMode)
readUnderlineMode     = Text -> Text -> StyleReaderSafe _x (Maybe UnderlineMode)
forall _x. Text -> Text -> StyleReaderSafe _x (Maybe UnderlineMode)
readLineMode Text
"text-underline-mode"
                                     Text
"text-underline-style"

readStrikeThroughMode :: StyleReaderSafe _x (Maybe UnderlineMode)
readStrikeThroughMode :: StyleReaderSafe _x (Maybe UnderlineMode)
readStrikeThroughMode = Text -> Text -> StyleReaderSafe _x (Maybe UnderlineMode)
forall _x. Text -> Text -> StyleReaderSafe _x (Maybe UnderlineMode)
readLineMode Text
"text-line-through-mode"
                                     Text
"text-line-through-style"

readLineMode :: Text -> Text -> StyleReaderSafe _x (Maybe UnderlineMode)
readLineMode :: Text -> Text -> StyleReaderSafe _x (Maybe UnderlineMode)
readLineMode Text
modeAttr Text
styleAttr = proc _x
x -> do
  Bool
isUL <- Namespace
-> Text
-> Bool
-> [(Text, Bool)]
-> XMLConverter Namespace FontPitches _x Bool
forall nsID a extraState x.
NameSpaceID nsID =>
nsID
-> Text -> a -> [(Text, a)] -> XMLConverter nsID extraState x a
searchAttr  Namespace
NsStyle Text
styleAttr Bool
False [(Text, Bool)]
isLinePresent -< _x
x
  Maybe UnderlineMode
mode <- Namespace -> Text -> StyleReaderSafe _x (Maybe UnderlineMode)
forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe a)
lookupAttr' Namespace
NsStyle  Text
modeAttr                     -< _x
x
  if Bool
isUL
    then case Maybe UnderlineMode
mode of
           Just UnderlineMode
m  -> ArrowState
  (XMLConverterState Namespace FontPitches)
  (Maybe UnderlineMode)
  (Maybe UnderlineMode)
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< UnderlineMode -> Maybe UnderlineMode
forall a. a -> Maybe a
Just UnderlineMode
m
           Maybe UnderlineMode
Nothing -> ArrowState
  (XMLConverterState Namespace FontPitches)
  (Maybe UnderlineMode)
  (Maybe UnderlineMode)
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< UnderlineMode -> Maybe UnderlineMode
forall a. a -> Maybe a
Just UnderlineMode
UnderlineModeNormal
    else              ArrowState
  (XMLConverterState Namespace FontPitches)
  (Maybe UnderlineMode)
  (Maybe UnderlineMode)
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< Maybe UnderlineMode
forall a. Maybe a
Nothing
  where
    isLinePresent :: [(Text, Bool)]
isLinePresent = (Text
"none",Bool
False) (Text, Bool) -> [(Text, Bool)] -> [(Text, Bool)]
forall a. a -> [a] -> [a]
: (Text -> (Text, Bool)) -> [Text] -> [(Text, Bool)]
forall a b. (a -> b) -> [a] -> [b]
map (,Bool
True)
                    [ Text
"dash"      , Text
"dot-dash" , Text
"dot-dot-dash" , Text
"dotted"
                    , Text
"long-dash" , Text
"solid"    , Text
"wave"
                    ]

--
readParaProperties :: StyleReader _x ParaProperties
readParaProperties :: StyleReader _x ParaProperties
readParaProperties =
   Namespace
-> Text
-> StyleReader _x ParaProperties
-> StyleReader _x ParaProperties
forall nsID extraState f s.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState f s
-> FallibleXMLConverter nsID extraState f s
executeInSub Namespace
NsStyle Text
"paragraph-properties" (StyleReader _x ParaProperties -> StyleReader _x ParaProperties)
-> StyleReader _x ParaProperties -> StyleReader _x ParaProperties
forall a b. (a -> b) -> a -> b
$ ArrowState
  (XMLConverterState Namespace FontPitches) _x ParaProperties
-> StyleReader _x ParaProperties
forall (a :: * -> * -> *) x success failure.
ArrowChoice a =>
a x success -> FallibleArrow a x failure success
liftAsSuccess
     ( (ParaNumbering
 -> LengthOrPercent -> LengthOrPercent -> ParaProperties)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x ParaNumbering
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x LengthOrPercent
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x LengthOrPercent
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x ParaProperties
forall (a :: * -> * -> *) z y x r b.
Arrow a =>
(z -> y -> x -> r) -> a b z -> a b y -> a b x -> a b r
liftA3 ParaNumbering
-> LengthOrPercent -> LengthOrPercent -> ParaProperties
PropP
       ( (Maybe Bool -> Maybe Int -> ParaNumbering)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe Bool)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe Int)
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x ParaNumbering
forall (a :: * -> * -> *) x y z b.
Arrow a =>
(x -> y -> z) -> a b x -> a b y -> a b z
liftA2 Maybe Bool -> Maybe Int -> ParaNumbering
readNumbering
         ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe Bool)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Bool)
isSet'           Namespace
NsText   Text
"number-lines"           )
         ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x (Maybe Int)
forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue) =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe attrValue)
readAttr'        Namespace
NsText   Text
"line-number"            )
       )
       ( (Bool -> LengthOrPercent -> LengthOrPercent)
-> ArrowState (XMLConverterState Namespace FontPitches) _x Bool
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x LengthOrPercent
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x LengthOrPercent
forall (a :: * -> * -> *) x y z b.
Arrow a =>
(x -> y -> z) -> a b x -> a b y -> a b z
liftA2 Bool -> LengthOrPercent -> LengthOrPercent
forall p. Default p => Bool -> p -> p
readIndentation
         ( Namespace
-> Text
-> Bool
-> ArrowState (XMLConverterState Namespace FontPitches) _x Bool
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> Bool -> XMLConverter nsID extraState x Bool
isSetWithDefault Namespace
NsStyle  Text
"auto-text-indent" Bool
False )
         ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x LengthOrPercent
forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue, Default attrValue) =>
nsID -> Text -> XMLConverter nsID extraState x attrValue
getAttr          Namespace
NsXSL_FO Text
"text-indent"            )
       )
       (   Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) _x LengthOrPercent
forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue, Default attrValue) =>
nsID -> Text -> XMLConverter nsID extraState x attrValue
getAttr          Namespace
NsXSL_FO Text
"margin-left"            )
     )
  where readNumbering :: Maybe Bool -> Maybe Int -> ParaNumbering
readNumbering (Just Bool
True) (Just Int
n) = Int -> ParaNumbering
NumberingRestart Int
n
        readNumbering (Just Bool
True)  Maybe Int
_       = ParaNumbering
NumberingKeep
        readNumbering      Maybe Bool
_       Maybe Int
_       = ParaNumbering
NumberingNone

        readIndentation :: Bool -> p -> p
readIndentation Bool
False p
indent = p
indent
        readIndentation Bool
True  p
_      = p
forall a. Default a => a
def

----
-- List styles
----

--
readListStyle :: StyleReader _x (StyleName, ListStyle)
readListStyle :: StyleReader _x (Text, ListStyle)
readListStyle =
       Namespace
-> Text -> FallibleXMLConverter Namespace FontPitches _x Text
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> FallibleXMLConverter nsID extraState x Text
findAttr Namespace
NsStyle Text
"name"
  FallibleXMLConverter Namespace FontPitches _x Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Text, ListStyle)
-> StyleReader _x (Text, ListStyle)
forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> a success success' -> FallibleArrow a x failure success'
>>?! ArrowState (XMLConverterState Namespace FontPitches) Text ListStyle
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text (Text, ListStyle)
forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue
       ( (Map Int ListLevelStyle -> ListStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     Text
     (Map Int ListLevelStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text ListStyle
forall (a :: * -> * -> *) y z b.
Arrow a =>
(y -> z) -> a b y -> a b z
liftA Map Int ListLevelStyle -> ListStyle
ListStyle
         (ArrowState
   (XMLConverterState Namespace FontPitches)
   Text
   (Map Int ListLevelStyle)
 -> ArrowState
      (XMLConverterState Namespace FontPitches) Text ListStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     Text
     (Map Int ListLevelStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Text ListStyle
forall a b. (a -> b) -> a -> b
$ (SetMap Int ListLevelStyle
 -> SetMap Int ListLevelStyle
 -> SetMap Int ListLevelStyle
 -> SetMap Int ListLevelStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     Text
     (SetMap Int ListLevelStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     Text
     (SetMap Int ListLevelStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     Text
     (SetMap Int ListLevelStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     Text
     (SetMap Int ListLevelStyle)
forall (a :: * -> * -> *) z y x r b.
Arrow a =>
(z -> y -> x -> r) -> a b z -> a b y -> a b x -> a b r
liftA3 SetMap Int ListLevelStyle
-> SetMap Int ListLevelStyle
-> SetMap Int ListLevelStyle
-> SetMap Int ListLevelStyle
forall k v.
Ord k =>
SetMap k v -> SetMap k v -> SetMap k v -> SetMap k v
SM.union3
             ( Namespace
-> Text
-> ListLevelType
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     Text
     (SetMap Int ListLevelStyle)
forall _x.
Namespace
-> Text
-> ListLevelType
-> StyleReaderSafe _x (SetMap Int ListLevelStyle)
readListLevelStyles Namespace
NsText Text
"list-level-style-number" ListLevelType
LltNumbered )
             ( Namespace
-> Text
-> ListLevelType
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     Text
     (SetMap Int ListLevelStyle)
forall _x.
Namespace
-> Text
-> ListLevelType
-> StyleReaderSafe _x (SetMap Int ListLevelStyle)
readListLevelStyles Namespace
NsText Text
"list-level-style-bullet" ListLevelType
LltBullet   )
             ( Namespace
-> Text
-> ListLevelType
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     Text
     (SetMap Int ListLevelStyle)
forall _x.
Namespace
-> Text
-> ListLevelType
-> StyleReaderSafe _x (SetMap Int ListLevelStyle)
readListLevelStyles Namespace
NsText Text
"list-level-style-image"  ListLevelType
LltImage    ) ArrowState
  (XMLConverterState Namespace FontPitches)
  Text
  (SetMap Int ListLevelStyle)
-> (SetMap Int ListLevelStyle -> Map Int ListLevelStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches)
     Text
     (Map Int ListLevelStyle)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (Set ListLevelStyle -> Maybe ListLevelStyle)
-> SetMap Int ListLevelStyle -> Map Int ListLevelStyle
forall a b k. (a -> Maybe b) -> Map k a -> Map k b
M.mapMaybe Set ListLevelStyle -> Maybe ListLevelStyle
chooseMostSpecificListLevelStyle
       )
--
readListLevelStyles :: Namespace -> ElementName
                    -> ListLevelType
                    -> StyleReaderSafe _x (SM.SetMap Int ListLevelStyle)
readListLevelStyles :: Namespace
-> Text
-> ListLevelType
-> StyleReaderSafe _x (SetMap Int ListLevelStyle)
readListLevelStyles Namespace
namespace Text
elementName ListLevelType
levelType =
  Namespace
-> Text
-> FallibleXMLConverter
     Namespace FontPitches _x (Int, ListLevelStyle)
-> XMLConverter Namespace FontPitches _x [(Int, ListLevelStyle)]
forall nsID extraState b a.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState b a
-> XMLConverter nsID extraState b [a]
tryAll Namespace
namespace Text
elementName (ListLevelType
-> FallibleXMLConverter
     Namespace FontPitches _x (Int, ListLevelStyle)
forall _x. ListLevelType -> StyleReader _x (Int, ListLevelStyle)
readListLevelStyle ListLevelType
levelType)
    XMLConverter Namespace FontPitches _x [(Int, ListLevelStyle)]
-> ([(Int, ListLevelStyle)] -> SetMap Int ListLevelStyle)
-> StyleReaderSafe _x (SetMap Int ListLevelStyle)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ [(Int, ListLevelStyle)] -> SetMap Int ListLevelStyle
forall k v. (Ord k, Ord v) => [(k, v)] -> SetMap k v
SM.fromList

--
readListLevelStyle :: ListLevelType -> StyleReader _x (Int, ListLevelStyle)
readListLevelStyle :: ListLevelType -> StyleReader _x (Int, ListLevelStyle)
readListLevelStyle ListLevelType
levelType =      Namespace
-> Text -> FallibleXMLConverter Namespace FontPitches _x Int
forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue) =>
nsID -> Text -> FallibleXMLConverter nsID extraState x attrValue
readAttr Namespace
NsText Text
"level"
                               FallibleXMLConverter Namespace FontPitches _x Int
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int (Int, ListLevelStyle)
-> StyleReader _x (Int, ListLevelStyle)
forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> a success success' -> FallibleArrow a x failure success'
>>?! ArrowState
  (XMLConverterState Namespace FontPitches) Int ListLevelStyle
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int (Int, ListLevelStyle)
forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue
                                    ( (ListLevelType
 -> Maybe Text
 -> Maybe Text
 -> ListItemNumberFormat
 -> Maybe Text
 -> ListLevelStyle)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int ListLevelType
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int (Maybe Text)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int (Maybe Text)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int ListItemNumberFormat
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int (Maybe Text)
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int ListLevelStyle
forall (a :: * -> * -> *) z y x w v r b.
Arrow a =>
(z -> y -> x -> w -> v -> r)
-> a b z -> a b y -> a b x -> a b w -> a b v -> a b r
liftA5 ListLevelType
-> Maybe Text
-> Maybe Text
-> ListItemNumberFormat
-> Maybe Text
-> ListLevelStyle
toListLevelStyle
                                      ( ListLevelType
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int ListLevelType
forall (a :: * -> * -> *) c x. Arrow a => c -> a x c
returnV       ListLevelType
levelType             )
                                      ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int (Maybe Text)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr'     Namespace
NsStyle Text
"num-prefix"  )
                                      ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int (Maybe Text)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr'     Namespace
NsStyle Text
"num-suffix"  )
                                      ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int ListItemNumberFormat
forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue, Default attrValue) =>
nsID -> Text -> XMLConverter nsID extraState x attrValue
getAttr       Namespace
NsStyle Text
"num-format"  )
                                      ( Namespace
-> Text
-> ArrowState
     (XMLConverterState Namespace FontPitches) Int (Maybe Text)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttrText' Namespace
NsText  Text
"start-value" )
                                    )
  where
  toListLevelStyle :: ListLevelType
-> Maybe Text
-> Maybe Text
-> ListItemNumberFormat
-> Maybe Text
-> ListLevelStyle
toListLevelStyle ListLevelType
_ Maybe Text
p Maybe Text
s ListItemNumberFormat
LinfNone Maybe Text
b         = ListLevelType
-> Maybe Text
-> Maybe Text
-> ListItemNumberFormat
-> Int
-> ListLevelStyle
ListLevelStyle ListLevelType
LltBullet Maybe Text
p Maybe Text
s ListItemNumberFormat
LinfNone (Maybe Text -> Int
forall a. (Num a, Read a) => Maybe Text -> a
startValue Maybe Text
b)
  toListLevelStyle ListLevelType
_ Maybe Text
p Maybe Text
s f :: ListItemNumberFormat
f@(LinfString String
_) Maybe Text
b = ListLevelType
-> Maybe Text
-> Maybe Text
-> ListItemNumberFormat
-> Int
-> ListLevelStyle
ListLevelStyle ListLevelType
LltBullet Maybe Text
p Maybe Text
s ListItemNumberFormat
f (Maybe Text -> Int
forall a. (Num a, Read a) => Maybe Text -> a
startValue Maybe Text
b)
  toListLevelStyle ListLevelType
t Maybe Text
p Maybe Text
s ListItemNumberFormat
f Maybe Text
b                = ListLevelType
-> Maybe Text
-> Maybe Text
-> ListItemNumberFormat
-> Int
-> ListLevelStyle
ListLevelStyle ListLevelType
t      Maybe Text
p Maybe Text
s ListItemNumberFormat
f (Maybe Text -> Int
forall a. (Num a, Read a) => Maybe Text -> a
startValue Maybe Text
b)
  startValue :: Maybe Text -> a
startValue Maybe Text
mbx = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
1 (Maybe Text
mbx Maybe Text -> (Text -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Maybe a
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead)

--
chooseMostSpecificListLevelStyle :: S.Set ListLevelStyle -> Maybe ListLevelStyle
chooseMostSpecificListLevelStyle :: Set ListLevelStyle -> Maybe ListLevelStyle
chooseMostSpecificListLevelStyle Set ListLevelStyle
ls = (ListLevelStyle -> Maybe ListLevelStyle -> Maybe ListLevelStyle)
-> Maybe ListLevelStyle
-> Set ListLevelStyle
-> Maybe ListLevelStyle
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr ListLevelStyle -> Maybe ListLevelStyle -> Maybe ListLevelStyle
select Maybe ListLevelStyle
forall a. Maybe a
Nothing Set ListLevelStyle
ls
  where
   select :: ListLevelStyle -> Maybe ListLevelStyle -> Maybe ListLevelStyle
select ListLevelStyle
l Maybe ListLevelStyle
Nothing = ListLevelStyle -> Maybe ListLevelStyle
forall a. a -> Maybe a
Just ListLevelStyle
l
   select ( ListLevelStyle ListLevelType
t1 Maybe Text
p1 Maybe Text
s1 ListItemNumberFormat
f1 Int
b1 )
          ( Just ( ListLevelStyle ListLevelType
t2 Maybe Text
p2 Maybe Text
s2 ListItemNumberFormat
f2 Int
_ ))
        =   ListLevelStyle -> Maybe ListLevelStyle
forall a. a -> Maybe a
Just (ListLevelStyle -> Maybe ListLevelStyle)
-> ListLevelStyle -> Maybe ListLevelStyle
forall a b. (a -> b) -> a -> b
$ ListLevelType
-> Maybe Text
-> Maybe Text
-> ListItemNumberFormat
-> Int
-> ListLevelStyle
ListLevelStyle (ListLevelType -> ListLevelType -> ListLevelType
select' ListLevelType
t1 ListLevelType
t2) (Maybe Text
p1 Maybe Text -> Maybe Text -> Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Text
p2) (Maybe Text
s1 Maybe Text -> Maybe Text -> Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Text
s2)
                                  (ListItemNumberFormat
-> ListItemNumberFormat -> ListItemNumberFormat
selectLinf ListItemNumberFormat
f1 ListItemNumberFormat
f2) Int
b1
   select' :: ListLevelType -> ListLevelType -> ListLevelType
select' ListLevelType
LltNumbered ListLevelType
_           = ListLevelType
LltNumbered
   select' ListLevelType
_           ListLevelType
LltNumbered = ListLevelType
LltNumbered
   select' ListLevelType
_           ListLevelType
_           = ListLevelType
LltBullet
   selectLinf :: ListItemNumberFormat
-> ListItemNumberFormat -> ListItemNumberFormat
selectLinf ListItemNumberFormat
LinfNone       ListItemNumberFormat
f2             = ListItemNumberFormat
f2
   selectLinf ListItemNumberFormat
f1             ListItemNumberFormat
LinfNone       = ListItemNumberFormat
f1
   selectLinf (LinfString String
_) ListItemNumberFormat
f2             = ListItemNumberFormat
f2
   selectLinf ListItemNumberFormat
f1             (LinfString String
_) = ListItemNumberFormat
f1
   selectLinf ListItemNumberFormat
f1             ListItemNumberFormat
_              = ListItemNumberFormat
f1


--------------------------------------------------------------------------------
-- Tools to access style data
--------------------------------------------------------------------------------

--
lookupStyle           :: StyleName   -> Styles -> Maybe Style
lookupStyle :: Text -> Styles -> Maybe Style
lookupStyle Text
name Styles{Map Text ListStyle
Map Text Style
Map StyleFamily StyleProperties
defaultStyleMap :: Map StyleFamily StyleProperties
listStylesByName :: Map Text ListStyle
stylesByName :: Map Text Style
defaultStyleMap :: Styles -> Map StyleFamily StyleProperties
listStylesByName :: Styles -> Map Text ListStyle
stylesByName :: Styles -> Map Text Style
..} = Text -> Map Text Style -> Maybe Style
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
name Map Text Style
stylesByName

--
lookupDefaultStyle'   :: Styles -> StyleFamily -> StyleProperties
lookupDefaultStyle' :: Styles -> StyleFamily -> StyleProperties
lookupDefaultStyle' Styles{Map Text ListStyle
Map Text Style
Map StyleFamily StyleProperties
defaultStyleMap :: Map StyleFamily StyleProperties
listStylesByName :: Map Text ListStyle
stylesByName :: Map Text Style
defaultStyleMap :: Styles -> Map StyleFamily StyleProperties
listStylesByName :: Styles -> Map Text ListStyle
stylesByName :: Styles -> Map Text Style
..} StyleFamily
family = StyleProperties -> Maybe StyleProperties -> StyleProperties
forall a. a -> Maybe a -> a
fromMaybe StyleProperties
forall a. Default a => a
def
                                        (StyleFamily
-> Map StyleFamily StyleProperties -> Maybe StyleProperties
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup StyleFamily
family Map StyleFamily StyleProperties
defaultStyleMap)

--
lookupListStyleByName :: StyleName   -> Styles -> Maybe ListStyle
lookupListStyleByName :: Text -> Styles -> Maybe ListStyle
lookupListStyleByName Text
name Styles{Map Text ListStyle
Map Text Style
Map StyleFamily StyleProperties
defaultStyleMap :: Map StyleFamily StyleProperties
listStylesByName :: Map Text ListStyle
stylesByName :: Map Text Style
defaultStyleMap :: Styles -> Map StyleFamily StyleProperties
listStylesByName :: Styles -> Map Text ListStyle
stylesByName :: Styles -> Map Text Style
..} = Text -> Map Text ListStyle -> Maybe ListStyle
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
name Map Text ListStyle
listStylesByName


-- | Returns a chain of parent of the current style. The direct parent will
-- be the first element of the list, followed by its parent and so on.
-- The current style is not in the list.
parents               :: Style       -> Styles ->      [Style]
parents :: Style -> Styles -> [Style]
parents Style
style Styles
styles = (Style -> Maybe (Style, Style)) -> Style -> [Style]
forall b a. (b -> Maybe (a, b)) -> b -> [a]
unfoldr Style -> Maybe (Style, Style)
findNextParent Style
style -- Ha!
  where findNextParent :: Style -> Maybe (Style, Style)
findNextParent Style{Maybe Text
Maybe StyleFamily
StyleProperties
styleProperties :: StyleProperties
listStyle :: Maybe Text
styleParentName :: Maybe Text
styleFamily :: Maybe StyleFamily
styleProperties :: Style -> StyleProperties
listStyle :: Style -> Maybe Text
styleParentName :: Style -> Maybe Text
styleFamily :: Style -> Maybe StyleFamily
..}
          = (Style -> (Style, Style)) -> Maybe Style -> Maybe (Style, Style)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Style -> (Style, Style)
forall (a :: * -> * -> *) b. Arrow a => a b (b, b)
duplicate (Maybe Style -> Maybe (Style, Style))
-> Maybe Style -> Maybe (Style, Style)
forall a b. (a -> b) -> a -> b
$ (Text -> Styles -> Maybe Style
`lookupStyle` Styles
styles) (Text -> Maybe Style) -> Maybe Text -> Maybe Style
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe Text
styleParentName

-- | Looks up the style family of the current style. Normally, every style
-- should have one. But if not, all parents are searched.
getStyleFamily        :: Style       -> Styles -> Maybe StyleFamily
getStyleFamily :: Style -> Styles -> Maybe StyleFamily
getStyleFamily style :: Style
style@Style{Maybe Text
Maybe StyleFamily
StyleProperties
styleProperties :: StyleProperties
listStyle :: Maybe Text
styleParentName :: Maybe Text
styleFamily :: Maybe StyleFamily
styleProperties :: Style -> StyleProperties
listStyle :: Style -> Maybe Text
styleParentName :: Style -> Maybe Text
styleFamily :: Style -> Maybe StyleFamily
..} Styles
styles
  =     Maybe StyleFamily
styleFamily
    Maybe StyleFamily -> Maybe StyleFamily -> Maybe StyleFamily
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Maybe StyleFamily] -> Maybe StyleFamily
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
F.asum ((Style -> Maybe StyleFamily) -> [Style] -> [Maybe StyleFamily]
forall a b. (a -> b) -> [a] -> [b]
map (Style -> Styles -> Maybe StyleFamily
`getStyleFamily` Styles
styles) ([Style] -> [Maybe StyleFamily]) -> [Style] -> [Maybe StyleFamily]
forall a b. (a -> b) -> a -> b
$ Style -> Styles -> [Style]
parents Style
style Styles
styles)

-- | Each 'Style' has certain 'StyleProperties'. But sometimes not all property
-- values are specified. Instead, a value might be inherited from a
-- parent style. This function makes this chain of inheritance
-- concrete and easily accessible by encapsulating the necessary lookups.
-- The resulting list contains the direct properties of the style as the first
-- element, the ones of the direct parent element as the next one, and so on.
--
-- Note: There should also be default properties for each style family. These
--       are @not@ contained in this list because properties inherited from
--       parent elements take precedence over default styles.
--
-- This function is primarily meant to be used through convenience wrappers.
--
stylePropertyChain    :: Style       -> Styles -> [StyleProperties]
stylePropertyChain :: Style -> Styles -> [StyleProperties]
stylePropertyChain Style
style Styles
styles
  = (Style -> StyleProperties) -> [Style] -> [StyleProperties]
forall a b. (a -> b) -> [a] -> [b]
map Style -> StyleProperties
styleProperties (Style
style Style -> [Style] -> [Style]
forall a. a -> [a] -> [a]
: Style -> Styles -> [Style]
parents Style
style Styles
styles)

--
extendedStylePropertyChain :: [Style] -> Styles -> [StyleProperties]
extendedStylePropertyChain :: [Style] -> Styles -> [StyleProperties]
extendedStylePropertyChain [] Styles
_ = []
extendedStylePropertyChain [Style
style]       Styles
styles =    Style -> Styles -> [StyleProperties]
stylePropertyChain Style
style Styles
styles
                                                  [StyleProperties] -> [StyleProperties] -> [StyleProperties]
forall a. [a] -> [a] -> [a]
++ Maybe StyleProperties -> [StyleProperties]
forall a. Maybe a -> [a]
maybeToList ((StyleFamily -> StyleProperties)
-> Maybe StyleFamily -> Maybe StyleProperties
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Styles -> StyleFamily -> StyleProperties
lookupDefaultStyle' Styles
styles) (Style -> Styles -> Maybe StyleFamily
getStyleFamily Style
style Styles
styles))
extendedStylePropertyChain (Style
style:[Style]
trace) Styles
styles =    Style -> Styles -> [StyleProperties]
stylePropertyChain Style
style Styles
styles
                                                  [StyleProperties] -> [StyleProperties] -> [StyleProperties]
forall a. [a] -> [a] -> [a]
++ [Style] -> Styles -> [StyleProperties]
extendedStylePropertyChain [Style]
trace Styles
styles