{-# LANGUAGE Arrows #-}

-- | Parsing for the spine section of the OPF Package XML Document
module Codec.Epub.Parse.Spine
   ( spineP
   )
   where

import Control.Arrow.ListArrows ( (>>>), listA, returnA )
import Data.Tree.NTree.TypeDefs ( NTree )
import Text.XML.HXT.Arrow.XmlArrow ( ArrowXml, getAttrValue )
import Text.XML.HXT.DOM.TypeDefs ( XNode )

import Codec.Epub.Data.Spine
import Codec.Epub.Parse.Util


spineItemrefP :: (ArrowXml a) => a (NTree XNode) SpineItemref
spineItemrefP :: forall (a :: * -> * -> *).
ArrowXml a =>
a (NTree XNode) SpineItemref
spineItemrefP = QName -> a (NTree XNode) (NTree XNode)
forall (a :: * -> * -> *).
ArrowXml a =>
QName -> a (NTree XNode) (NTree XNode)
atQTag (String -> QName
opfName String
"itemref") a (NTree XNode) (NTree XNode)
-> a (NTree XNode) SpineItemref -> a (NTree XNode) SpineItemref
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
   proc NTree XNode
x -> do
      String
i <- String -> a (NTree XNode) String
forall (a :: * -> * -> *).
ArrowXml a =>
String -> a (NTree XNode) String
getAttrValue String
"idref" -< NTree XNode
x
      Maybe String
ml <- String -> a (NTree XNode) (Maybe String)
forall (a :: * -> * -> *).
ArrowXml a =>
String -> a (NTree XNode) (Maybe String)
mbGetAttrValue String
"linear" -< NTree XNode
x
      let l :: Maybe Bool
l = Maybe Bool -> (String -> Maybe Bool) -> Maybe String -> Maybe Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Maybe Bool
forall a. Maybe a
Nothing (\String
v -> if String
v String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"no" then Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False else Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True) Maybe String
ml 
      a SpineItemref SpineItemref
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< String -> Maybe Bool -> SpineItemref
SpineItemref String
i Maybe Bool
l


spineP :: (ArrowXml a) => a (NTree XNode) Spine
spineP :: forall (a :: * -> * -> *). ArrowXml a => a (NTree XNode) Spine
spineP = QName -> a (NTree XNode) (NTree XNode)
forall (a :: * -> * -> *).
ArrowXml a =>
QName -> a (NTree XNode) (NTree XNode)
atQTag (String -> QName
opfName String
"spine") a (NTree XNode) (NTree XNode)
-> a (NTree XNode) Spine -> a (NTree XNode) Spine
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
   proc NTree XNode
x -> do
      String
i <- String -> a (NTree XNode) String
forall (a :: * -> * -> *).
ArrowXml a =>
String -> a (NTree XNode) String
getAttrValue String
"toc" -< NTree XNode
x
      [SpineItemref]
l <- a (NTree XNode) SpineItemref -> a (NTree XNode) [SpineItemref]
forall b c. a b c -> a b [c]
forall (a :: * -> * -> *) b c. ArrowList a => a b c -> a b [c]
listA a (NTree XNode) SpineItemref
forall (a :: * -> * -> *).
ArrowXml a =>
a (NTree XNode) SpineItemref
spineItemrefP -< NTree XNode
x
      a Spine Spine
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< (String -> [SpineItemref] -> Spine
Spine String
i [SpineItemref]
l)