{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Reader.ODT.Namespaces
   Copyright   : Copyright (C) 2015 Martin Linnemann
   License     : GNU GPL, version 2 or above

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

Namespaces used in odt files.
-}

module Text.Pandoc.Readers.ODT.Namespaces ( Namespace (..)
                                          ) where

import qualified Data.Map as M (empty, insert)
import Data.Maybe (fromMaybe, listToMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import Text.Pandoc.Readers.ODT.Generic.Namespaces


instance NameSpaceID Namespace where

  getInitialIRImap :: NameSpaceIRIs Namespace
getInitialIRImap     = NameSpaceIRIs Namespace
nsIDmap

  getNamespaceID :: Text
-> NameSpaceIRIs Namespace
-> Maybe (NameSpaceIRIs Namespace, Namespace)
getNamespaceID Text
""  NameSpaceIRIs Namespace
m = (NameSpaceIRIs Namespace, Namespace)
-> Maybe (NameSpaceIRIs Namespace, Namespace)
forall a. a -> Maybe a
Just(NameSpaceIRIs Namespace
m, Namespace
NsXML)
  getNamespaceID Text
iri NameSpaceIRIs Namespace
m = Namespace -> Maybe (NameSpaceIRIs Namespace, Namespace)
asPair (Namespace -> Maybe (NameSpaceIRIs Namespace, Namespace))
-> Namespace -> Maybe (NameSpaceIRIs Namespace, Namespace)
forall a b. (a -> b) -> a -> b
$ Namespace -> Maybe Namespace -> Namespace
forall a. a -> Maybe a -> a
fromMaybe (Text -> Namespace
NsOther Text
iri) (Text -> Maybe Namespace
findID Text
iri)
    where asPair :: Namespace -> Maybe (NameSpaceIRIs Namespace, Namespace)
asPair Namespace
nsID = (NameSpaceIRIs Namespace, Namespace)
-> Maybe (NameSpaceIRIs Namespace, Namespace)
forall a. a -> Maybe a
Just (Namespace
-> Text -> NameSpaceIRIs Namespace -> NameSpaceIRIs Namespace
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Namespace
nsID Text
iri NameSpaceIRIs Namespace
m, Namespace
nsID)


findID :: NameSpaceIRI -> Maybe Namespace
findID :: Text -> Maybe Namespace
findID Text
iri = [Namespace] -> Maybe Namespace
forall a. [a] -> Maybe a
listToMaybe [Namespace
nsID | (Text
iri',Namespace
nsID) <- [(Text, Namespace)]
nsIDs, Text
iri' Text -> Text -> Bool
`T.isPrefixOf` Text
iri]

nsIDmap :: NameSpaceIRIs Namespace
nsIDmap :: NameSpaceIRIs Namespace
nsIDmap = ((Text, Namespace)
 -> NameSpaceIRIs Namespace -> NameSpaceIRIs Namespace)
-> NameSpaceIRIs Namespace
-> [(Text, Namespace)]
-> NameSpaceIRIs Namespace
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((Text
 -> Namespace -> NameSpaceIRIs Namespace -> NameSpaceIRIs Namespace)
-> (Text, Namespace)
-> NameSpaceIRIs Namespace
-> NameSpaceIRIs Namespace
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((Text
  -> Namespace -> NameSpaceIRIs Namespace -> NameSpaceIRIs Namespace)
 -> (Text, Namespace)
 -> NameSpaceIRIs Namespace
 -> NameSpaceIRIs Namespace)
-> (Text
    -> Namespace -> NameSpaceIRIs Namespace -> NameSpaceIRIs Namespace)
-> (Text, Namespace)
-> NameSpaceIRIs Namespace
-> NameSpaceIRIs Namespace
forall a b. (a -> b) -> a -> b
$ (Namespace
 -> Text -> NameSpaceIRIs Namespace -> NameSpaceIRIs Namespace)
-> Text
-> Namespace
-> NameSpaceIRIs Namespace
-> NameSpaceIRIs Namespace
forall a b c. (a -> b -> c) -> b -> a -> c
flip Namespace
-> Text -> NameSpaceIRIs Namespace -> NameSpaceIRIs Namespace
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert) NameSpaceIRIs Namespace
forall k a. Map k a
M.empty [(Text, Namespace)]
nsIDs

data Namespace = -- Open Document core
                 NsOffice | NsStyle  | NsText   | NsTable  | NsForm
               | NsDraw   | Ns3D     | NsAnim   | NsChart  | NsConfig
               | NsDB     | NsMeta   | NsNumber | NsScript | NsManifest
               | NsPresentation
                 -- Metadata
               | NsODF
                 -- Compatible elements
               | NsXSL_FO  | NsSVG    | NsSmil
                 -- External standards
               | NsMathML | NsXForms | NsXLink  | NsXHtml  | NsGRDDL
               | NsDublinCore
                 -- Metadata manifest
               | NsPKG
                 -- Others
               | NsOpenFormula
                 -- Core XML (basically only for the 'id'-attribute)
               | NsXML
                 -- Fallback
               | NsOther Text
  deriving ( Namespace -> Namespace -> Bool
(Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Bool) -> Eq Namespace
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Namespace -> Namespace -> Bool
== :: Namespace -> Namespace -> Bool
$c/= :: Namespace -> Namespace -> Bool
/= :: Namespace -> Namespace -> Bool
Eq, Eq Namespace
Eq Namespace =>
(Namespace -> Namespace -> Ordering)
-> (Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Namespace)
-> (Namespace -> Namespace -> Namespace)
-> Ord Namespace
Namespace -> Namespace -> Bool
Namespace -> Namespace -> Ordering
Namespace -> Namespace -> Namespace
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
$ccompare :: Namespace -> Namespace -> Ordering
compare :: Namespace -> Namespace -> Ordering
$c< :: Namespace -> Namespace -> Bool
< :: Namespace -> Namespace -> Bool
$c<= :: Namespace -> Namespace -> Bool
<= :: Namespace -> Namespace -> Bool
$c> :: Namespace -> Namespace -> Bool
> :: Namespace -> Namespace -> Bool
$c>= :: Namespace -> Namespace -> Bool
>= :: Namespace -> Namespace -> Bool
$cmax :: Namespace -> Namespace -> Namespace
max :: Namespace -> Namespace -> Namespace
$cmin :: Namespace -> Namespace -> Namespace
min :: Namespace -> Namespace -> Namespace
Ord, Int -> Namespace -> ShowS
[Namespace] -> ShowS
Namespace -> String
(Int -> Namespace -> ShowS)
-> (Namespace -> String)
-> ([Namespace] -> ShowS)
-> Show Namespace
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Namespace -> ShowS
showsPrec :: Int -> Namespace -> ShowS
$cshow :: Namespace -> String
show :: Namespace -> String
$cshowList :: [Namespace] -> ShowS
showList :: [Namespace] -> ShowS
Show )

-- | Not the actual iri's, but large prefixes of them - this way there are
-- less versioning problems and the like.
nsIDs :: [(Text, Namespace)]
nsIDs :: [(Text, Namespace)]
nsIDs = [
  (Text
"urn:oasis:names:tc:opendocument:xmlns:animation"        , Namespace
NsAnim         ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:chart"            , Namespace
NsChart        ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:config"           , Namespace
NsConfig       ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:database"         , Namespace
NsDB           ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:dr3d"             , Namespace
Ns3D           ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:drawing"          , Namespace
NsDraw         ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:form"             , Namespace
NsForm         ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:manifest"         , Namespace
NsManifest     ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:meta"             , Namespace
NsMeta         ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:datastyle"        , Namespace
NsNumber       ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:of"               , Namespace
NsOpenFormula  ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:office:1.0"       , Namespace
NsOffice       ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:presentation"     , Namespace
NsPresentation ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:script"           , Namespace
NsScript       ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:style"            , Namespace
NsStyle        ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:table"            , Namespace
NsTable        ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:text"             , Namespace
NsText         ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible", Namespace
NsXSL_FO       ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:smil-compatible"  , Namespace
NsSmil         ),
  (Text
"urn:oasis:names:tc:opendocument:xmlns:svg-compatible"   , Namespace
NsSVG          ),
  (Text
"http://docs.oasis-open.org/ns/office/1.2/meta/odf"      , Namespace
NsODF          ),
  (Text
"http://docs.oasis-open.org/ns/office/1.2/meta/pkg"      , Namespace
NsPKG          ),
  (Text
"http://purl.org/dc/elements"                            , Namespace
NsDublinCore   ),
  (Text
"http://www.w3.org/2003/g/data-view"                     , Namespace
NsGRDDL        ),
  (Text
"http://www.w3.org/1998/Math/MathML"                     , Namespace
NsMathML       ),
  (Text
"http://www.w3.org/1999/xhtml"                           , Namespace
NsXHtml        ),
  (Text
"http://www.w3.org/2002/xforms"                          , Namespace
NsXForms       ),
  (Text
"http://www.w3.org/1999/xlink"                           , Namespace
NsXLink        )
  ]