{-# LANGUAGE CPP #-}
-- | This is a pretty-printer for turning the internal representation
--   of generic structured XML documents into the Doc type (which can
--   later be rendered using Text.PrettyPrint.HughesPJ.render).
--   Essentially there is one pp function for each type in
--   Text.Xml.HaXml.Types, so you can pretty-print as much or as little
--   of the document as you wish.

module Text.XML.HaXml.Pretty
  (
  -- * Pretty-print a whole document
    document
  -- ** Just one content
  ,   content
  -- ** Just one tagged element
  ,   element
  -- * Pretty-print just a DTD
  , doctypedecl
  -- ** The prolog
  ,   prolog
  -- ** A content particle description
  ,   cp
  ) where

#if MIN_VERSION_base(4,11,0)
import Prelude hiding (maybe,either,(<>))
#else
import Prelude hiding (maybe,either)
#endif

import Data.Maybe hiding (maybe)
import Data.List (intersperse)
--import Char (isSpace)
import Text.PrettyPrint.HughesPJ
import Text.XML.HaXml.Types
import Text.XML.HaXml.Namespaces

either :: (t -> t1) -> (t2 -> t1) -> Either t t2 -> t1
either :: (t -> t1) -> (t2 -> t1) -> Either t t2 -> t1
either f :: t -> t1
f _ (Left x :: t
x)  = t -> t1
f t
x
either _ g :: t2 -> t1
g (Right x :: t2
x) = t2 -> t1
g t2
x

maybe :: (t -> Doc) -> Maybe t -> Doc
maybe :: (t -> Doc) -> Maybe t -> Doc
maybe _ Nothing  = Doc
empty
maybe f :: t -> Doc
f (Just x :: t
x) = t -> Doc
f t
x

--peref p   = text "%" <> text p <> text ";"

----

document :: Document i -> Doc
prolog   :: Prolog -> Doc
xmldecl  :: XMLDecl -> Doc
misc     :: Misc -> Doc
sddecl   :: Bool -> Doc

doctypedecl :: DocTypeDecl -> Doc
markupdecl  :: MarkupDecl -> Doc
--extsubset   :: ExtSubset -> Doc
--extsubsetdecl :: ExtSubsetDecl -> Doc
cp          :: CP -> Doc

element   :: Element i -> Doc
attribute :: Attribute -> Doc                     --etc
content   :: Content i -> Doc

----

document :: Document i -> Doc
document (Document p :: Prolog
p _ e :: Element i
e m :: [Misc]
m)= Prolog -> Doc
prolog Prolog
p Doc -> Doc -> Doc
$$ Element i -> Doc
forall i. Element i -> Doc
element Element i
e Doc -> Doc -> Doc
$$ [Doc] -> Doc
vcat ((Misc -> Doc) -> [Misc] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Misc -> Doc
misc [Misc]
m)
prolog :: Prolog -> Doc
prolog (Prolog x :: Maybe XMLDecl
x m1 :: [Misc]
m1 dtd :: Maybe DocTypeDecl
dtd m2 :: [Misc]
m2)= (XMLDecl -> Doc) -> Maybe XMLDecl -> Doc
forall t. (t -> Doc) -> Maybe t -> Doc
maybe XMLDecl -> Doc
xmldecl Maybe XMLDecl
x Doc -> Doc -> Doc
$$
                             [Doc] -> Doc
vcat ((Misc -> Doc) -> [Misc] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Misc -> Doc
misc [Misc]
m1) Doc -> Doc -> Doc
$$
                             (DocTypeDecl -> Doc) -> Maybe DocTypeDecl -> Doc
forall t. (t -> Doc) -> Maybe t -> Doc
maybe DocTypeDecl -> Doc
doctypedecl Maybe DocTypeDecl
dtd Doc -> Doc -> Doc
$$
                             [Doc] -> Doc
vcat ((Misc -> Doc) -> [Misc] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Misc -> Doc
misc [Misc]
m2)
xmldecl :: XMLDecl -> Doc
xmldecl (XMLDecl v :: VersionInfo
v e :: Maybe EncodingDecl
e sd :: Maybe SDDecl
sd)   = VersionInfo -> Doc
text "<?xml version='" Doc -> Doc -> Doc
<> VersionInfo -> Doc
text VersionInfo
v Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "'" Doc -> Doc -> Doc
<+>
                             (EncodingDecl -> Doc) -> Maybe EncodingDecl -> Doc
forall t. (t -> Doc) -> Maybe t -> Doc
maybe EncodingDecl -> Doc
encodingdecl Maybe EncodingDecl
e Doc -> Doc -> Doc
<+>
                             (SDDecl -> Doc) -> Maybe SDDecl -> Doc
forall t. (t -> Doc) -> Maybe t -> Doc
maybe SDDecl -> Doc
sddecl Maybe SDDecl
sd Doc -> Doc -> Doc
<+>
                             VersionInfo -> Doc
text "?>"
misc :: Misc -> Doc
misc (Comment s :: VersionInfo
s)           = VersionInfo -> Doc
text "<!--" Doc -> Doc -> Doc
<> VersionInfo -> Doc
text VersionInfo
s Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "-->"
misc (PI (n :: VersionInfo
n,s :: VersionInfo
s))            = VersionInfo -> Doc
text "<?" Doc -> Doc -> Doc
<> VersionInfo -> Doc
text VersionInfo
n Doc -> Doc -> Doc
<+> VersionInfo -> Doc
text VersionInfo
s Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "?>"
sddecl :: SDDecl -> Doc
sddecl sd :: SDDecl
sd   | SDDecl
sd           = VersionInfo -> Doc
text "standalone='yes'"
            | SDDecl
otherwise    = VersionInfo -> Doc
text "standalone='no'"
doctypedecl :: DocTypeDecl -> Doc
doctypedecl (DTD n :: QName
n eid :: Maybe ExternalID
eid ds :: [MarkupDecl]
ds) = if [MarkupDecl] -> SDDecl
forall (t :: * -> *) a. Foldable t => t a -> SDDecl
null [MarkupDecl]
ds then
                                  Doc
hd Doc -> Doc -> Doc
<> VersionInfo -> Doc
text ">"
                             else Doc
hd Doc -> Doc -> Doc
<+> VersionInfo -> Doc
text " [" Doc -> Doc -> Doc
$$
                                  [Doc] -> Doc
vcat ((MarkupDecl -> Doc) -> [MarkupDecl] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map MarkupDecl -> Doc
markupdecl [MarkupDecl]
ds) Doc -> Doc -> Doc
$$ VersionInfo -> Doc
text "]>"
                           where hd :: Doc
hd = VersionInfo -> Doc
text "<!DOCTYPE" Doc -> Doc -> Doc
<+> QName -> Doc
qname QName
n Doc -> Doc -> Doc
<+>
                                      (ExternalID -> Doc) -> Maybe ExternalID -> Doc
forall t. (t -> Doc) -> Maybe t -> Doc
maybe ExternalID -> Doc
externalid Maybe ExternalID
eid
markupdecl :: MarkupDecl -> Doc
markupdecl (Element e :: ElementDecl
e)     = ElementDecl -> Doc
elementdecl ElementDecl
e
markupdecl (AttList a :: AttListDecl
a)     = AttListDecl -> Doc
attlistdecl AttListDecl
a
markupdecl (Entity e :: EntityDecl
e)      = EntityDecl -> Doc
entitydecl EntityDecl
e
markupdecl (Notation n :: NotationDecl
n)    = NotationDecl -> Doc
notationdecl NotationDecl
n
markupdecl (MarkupMisc m :: Misc
m)  = Misc -> Doc
misc Misc
m
--markupdecl (MarkupPE p m)  = peref p

--extsubset (ExtSubset t ds) = maybe textdecl t $$
--                             vcat (map extsubsetdecl ds)
--extmarkupdecl (ExtMarkupDecl m)      = markupdecl m
--extsubsetdecl (ExtConditionalSect c) = conditionalsect c
-- -- extsubsetdecl (ExtPEReference p e)   = peref p

element :: Element i -> Doc
element (Elem n :: QName
n as :: [Attribute]
as []) = VersionInfo -> Doc
text "<" Doc -> Doc -> Doc
<> QName -> Doc
qname QName
n Doc -> Doc -> Doc
<+>
                         [Doc] -> Doc
fsep ((Attribute -> Doc) -> [Attribute] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Attribute -> Doc
attribute [Attribute]
as) Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "/>"
element e :: Element i
e@(Elem n :: QName
n as :: [Attribute]
as cs :: [Content i]
cs)
    | (Content i -> SDDecl) -> [Content i] -> SDDecl
forall (t :: * -> *) a.
Foldable t =>
(a -> SDDecl) -> t a -> SDDecl
all Content i -> SDDecl
forall t. Content t -> SDDecl
isText [Content i]
cs    = VersionInfo -> Doc
text "<" Doc -> Doc -> Doc
<> QName -> Doc
qname QName
n Doc -> Doc -> Doc
<+> [Doc] -> Doc
fsep ((Attribute -> Doc) -> [Attribute] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Attribute -> Doc
attribute [Attribute]
as) Doc -> Doc -> Doc
<>
                         VersionInfo -> Doc
text ">" Doc -> Doc -> Doc
<> [Doc] -> Doc
hcat ((Content i -> Doc) -> [Content i] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Content i -> Doc
forall i. Content i -> Doc
content [Content i]
cs) Doc -> Doc -> Doc
<>
                         VersionInfo -> Doc
text "</" Doc -> Doc -> Doc
<> QName -> Doc
qname QName
n Doc -> Doc -> Doc
<> VersionInfo -> Doc
text ">"
    | SDDecl
otherwise        = let (d :: Doc
d,c :: Doc
c) = Element i -> Doc -> (Doc, Doc)
forall t. Element t -> Doc -> (Doc, Doc)
carryelem Element i
e Doc
empty
                         in Doc
d Doc -> Doc -> Doc
<> Doc
c

isText :: Content t -> Bool
isText :: Content t -> SDDecl
isText (CString _ _ _) = SDDecl
True
isText (CRef _ _)      = SDDecl
True
isText _               = SDDecl
False

carryelem    ::  Element t  -> Doc -> (Doc, Doc)
carrycontent ::  Content t  -> Doc -> (Doc, Doc)
spancontent  :: [Content a] -> Doc -> ([Doc],Doc)

carryelem :: Element t -> Doc -> (Doc, Doc)
carryelem (Elem n :: QName
n as :: [Attribute]
as []) c :: Doc
c = ( Doc
c Doc -> Doc -> Doc
<>
                               VersionInfo -> Doc
text "<" Doc -> Doc -> Doc
<> QName -> Doc
qname QName
n Doc -> Doc -> Doc
<+> [Doc] -> Doc
fsep ((Attribute -> Doc) -> [Attribute] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Attribute -> Doc
attribute [Attribute]
as)
                             , VersionInfo -> Doc
text "/>")
carryelem (Elem n :: QName
n as :: [Attribute]
as cs :: [Content t]
cs) c :: Doc
c =  let (cs0 :: [Doc]
cs0,d0 :: Doc
d0) = [Content t] -> Doc -> ([Doc], Doc)
forall a. [Content a] -> Doc -> ([Doc], Doc)
spancontent [Content t]
cs (VersionInfo -> Doc
text ">") in
                              ( Doc
c Doc -> Doc -> Doc
<>
                                VersionInfo -> Doc
text "<"Doc -> Doc -> Doc
<>QName -> Doc
qname QName
n Doc -> Doc -> Doc
<+> [Doc] -> Doc
fsep ((Attribute -> Doc) -> [Attribute] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Attribute -> Doc
attribute [Attribute]
as) Doc -> Doc -> Doc
$$
                                Int -> Doc -> Doc
nest 2 ([Doc] -> Doc
vcat [Doc]
cs0) Doc -> Doc -> Doc
<>
                                Doc
d0 Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "</" Doc -> Doc -> Doc
<> QName -> Doc
qname QName
n
                              , VersionInfo -> Doc
text ">")

carrycontent :: Content t -> Doc -> (Doc, Doc)
carrycontent (CElem e :: Element t
e _) c :: Doc
c         = Element t -> Doc -> (Doc, Doc)
forall t. Element t -> Doc -> (Doc, Doc)
carryelem Element t
e Doc
c
carrycontent (CString False s :: VersionInfo
s _) c :: Doc
c = (Doc
c Doc -> Doc -> Doc
<> VersionInfo -> Doc
chardata VersionInfo
s, Doc
empty)
carrycontent (CString True  s :: VersionInfo
s _) c :: Doc
c = (Doc
c Doc -> Doc -> Doc
<> VersionInfo -> Doc
cdsect VersionInfo
s, Doc
empty)
carrycontent (CRef r :: Reference
r _) c :: Doc
c          = (Doc
c Doc -> Doc -> Doc
<> Reference -> Doc
reference Reference
r, Doc
empty)
carrycontent (CMisc m :: Misc
m _) c :: Doc
c         = (Doc
c Doc -> Doc -> Doc
<> Misc -> Doc
misc Misc
m, Doc
empty)

spancontent :: [Content a] -> Doc -> ([Doc], Doc)
spancontent []     c :: Doc
c = ([],Doc
c)
spancontent (a :: Content a
a:as :: [Content a]
as) c :: Doc
c | Content a -> SDDecl
forall t. Content t -> SDDecl
isText Content a
a  = let (ts :: [Content a]
ts,rest :: [Content a]
rest) = (Content a -> SDDecl) -> [Content a] -> ([Content a], [Content a])
forall a. (a -> SDDecl) -> [a] -> ([a], [a])
span Content a -> SDDecl
forall t. Content t -> SDDecl
isText (Content a
aContent a -> [Content a] -> [Content a]
forall a. a -> [a] -> [a]
:[Content a]
as)
                                       formatted :: Doc
formatted = Doc
c Doc -> Doc -> Doc
<> [Doc] -> Doc
hcat ((Content a -> Doc) -> [Content a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Content a -> Doc
forall i. Content i -> Doc
content [Content a]
ts)
                                   in  [Content a] -> Doc -> ([Doc], Doc)
forall a. [Content a] -> Doc -> ([Doc], Doc)
spancontent [Content a]
rest Doc
formatted
                     | SDDecl
otherwise = let (b :: Doc
b, c0 :: Doc
c0) = Content a -> Doc -> (Doc, Doc)
forall t. Content t -> Doc -> (Doc, Doc)
carrycontent Content a
a Doc
c
                                       (bs :: [Doc]
bs,c1 :: Doc
c1) = [Content a] -> Doc -> ([Doc], Doc)
forall a. [Content a] -> Doc -> ([Doc], Doc)
spancontent [Content a]
as Doc
c0
                                   in  (Doc
bDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
bs, Doc
c1)

attribute :: Attribute -> Doc
attribute (n :: QName
n,v :: AttValue
v)             = QName -> Doc
qname QName
n Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "=" Doc -> Doc -> Doc
<> AttValue -> Doc
attvalue AttValue
v
content :: Content i -> Doc
content (CElem e :: Element i
e _)         = Element i -> Doc
forall i. Element i -> Doc
element Element i
e
content (CString False s :: VersionInfo
s _) = VersionInfo -> Doc
chardata VersionInfo
s
content (CString True s :: VersionInfo
s _)  = VersionInfo -> Doc
cdsect VersionInfo
s
content (CRef r :: Reference
r _)          = Reference -> Doc
reference Reference
r
content (CMisc m :: Misc
m _)         = Misc -> Doc
misc Misc
m

elementdecl :: ElementDecl -> Doc
elementdecl :: ElementDecl -> Doc
elementdecl (ElementDecl n :: QName
n cs :: ContentSpec
cs) = VersionInfo -> Doc
text "<!ELEMENT" Doc -> Doc -> Doc
<+> QName -> Doc
qname QName
n Doc -> Doc -> Doc
<+>
                                 ContentSpec -> Doc
contentspec ContentSpec
cs Doc -> Doc -> Doc
<> VersionInfo -> Doc
text ">"
contentspec :: ContentSpec -> Doc
contentspec :: ContentSpec -> Doc
contentspec EMPTY              = VersionInfo -> Doc
text "EMPTY"
contentspec ANY                = VersionInfo -> Doc
text "ANY"
contentspec (Mixed m :: Mixed
m)          = Mixed -> Doc
mixed Mixed
m
contentspec (ContentSpec c :: CP
c)    = CP -> Doc
cp CP
c
--contentspec (ContentPE p cs)   = peref p
cp :: CP -> Doc
cp (TagName n :: QName
n m :: Modifier
m)       = Doc -> Doc
parens (QName -> Doc
qname QName
n) Doc -> Doc -> Doc
<> Modifier -> Doc
modifier Modifier
m
cp (Choice cs :: [CP]
cs m :: Modifier
m)       = Doc -> Doc
parens ([Doc] -> Doc
hcat (Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
intersperse (VersionInfo -> Doc
text "|") ((CP -> Doc) -> [CP] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map CP -> Doc
cp [CP]
cs))) Doc -> Doc -> Doc
<>
                           Modifier -> Doc
modifier Modifier
m
cp (Seq cs :: [CP]
cs m :: Modifier
m)          = Doc -> Doc
parens ([Doc] -> Doc
hcat (Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
intersperse (VersionInfo -> Doc
text ",") ((CP -> Doc) -> [CP] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map CP -> Doc
cp [CP]
cs))) Doc -> Doc -> Doc
<>
                           Modifier -> Doc
modifier Modifier
m
--cp (CPPE p c)          = peref p
modifier :: Modifier -> Doc
modifier :: Modifier -> Doc
modifier None          = Doc
empty
modifier Query         = VersionInfo -> Doc
text "?"
modifier Star          = VersionInfo -> Doc
text "*"
modifier Plus          = VersionInfo -> Doc
text "+"
mixed :: Mixed -> Doc
mixed :: Mixed -> Doc
mixed  PCDATA          = VersionInfo -> Doc
text "(#PCDATA)"
mixed (PCDATAplus ns :: [QName]
ns)  = VersionInfo -> Doc
text "(#PCDATA |" Doc -> Doc -> Doc
<+>
                         [Doc] -> Doc
hcat (Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
intersperse (VersionInfo -> Doc
text "|") ((QName -> Doc) -> [QName] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map QName -> Doc
qname [QName]
ns)) Doc -> Doc -> Doc
<>
                         VersionInfo -> Doc
text ")*"

attlistdecl :: AttListDecl -> Doc
attlistdecl :: AttListDecl -> Doc
attlistdecl (AttListDecl n :: QName
n ds :: [AttDef]
ds) = VersionInfo -> Doc
text "<!ATTLIST" Doc -> Doc -> Doc
<+> QName -> Doc
qname QName
n Doc -> Doc -> Doc
<+>
                                 [Doc] -> Doc
fsep ((AttDef -> Doc) -> [AttDef] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map AttDef -> Doc
attdef [AttDef]
ds) Doc -> Doc -> Doc
<> VersionInfo -> Doc
text ">"
attdef :: AttDef -> Doc
attdef :: AttDef -> Doc
attdef (AttDef n :: QName
n t :: AttType
t d :: DefaultDecl
d)          = QName -> Doc
qname QName
n Doc -> Doc -> Doc
<+> AttType -> Doc
atttype AttType
t Doc -> Doc -> Doc
<+> DefaultDecl -> Doc
defaultdecl DefaultDecl
d
atttype :: AttType -> Doc
atttype :: AttType -> Doc
atttype  StringType            = VersionInfo -> Doc
text "CDATA"
atttype (TokenizedType t :: TokenizedType
t)      = TokenizedType -> Doc
tokenizedtype TokenizedType
t
atttype (EnumeratedType t :: EnumeratedType
t)     = EnumeratedType -> Doc
enumeratedtype EnumeratedType
t
tokenizedtype :: TokenizedType -> Doc
tokenizedtype :: TokenizedType -> Doc
tokenizedtype ID               = VersionInfo -> Doc
text "ID"
tokenizedtype IDREF            = VersionInfo -> Doc
text "IDREF"
tokenizedtype IDREFS           = VersionInfo -> Doc
text "IDREFS"
tokenizedtype ENTITY           = VersionInfo -> Doc
text "ENTITY"
tokenizedtype ENTITIES         = VersionInfo -> Doc
text "ENTITIES"
tokenizedtype NMTOKEN          = VersionInfo -> Doc
text "NMTOKEN"
tokenizedtype NMTOKENS         = VersionInfo -> Doc
text "NMTOKENS"
enumeratedtype :: EnumeratedType -> Doc
enumeratedtype :: EnumeratedType -> Doc
enumeratedtype (NotationType n :: NotationType
n)= NotationType -> Doc
notationtype NotationType
n
enumeratedtype (Enumeration e :: NotationType
e) = NotationType -> Doc
enumeration NotationType
e
notationtype :: [String] -> Doc
notationtype :: NotationType -> Doc
notationtype ns :: NotationType
ns                = VersionInfo -> Doc
text "NOTATION" Doc -> Doc -> Doc
<+>
                                 Doc -> Doc
parens ([Doc] -> Doc
hcat (Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
intersperse (VersionInfo -> Doc
text "|") ((VersionInfo -> Doc) -> NotationType -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map VersionInfo -> Doc
text NotationType
ns)))
enumeration :: [String] -> Doc
enumeration :: NotationType -> Doc
enumeration ns :: NotationType
ns                 = Doc -> Doc
parens ([Doc] -> Doc
hcat (Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
intersperse (VersionInfo -> Doc
text "|") ((VersionInfo -> Doc) -> NotationType -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map VersionInfo -> Doc
nmtoken NotationType
ns)))
defaultdecl :: DefaultDecl -> Doc
defaultdecl :: DefaultDecl -> Doc
defaultdecl  REQUIRED          = VersionInfo -> Doc
text "#REQUIRED"
defaultdecl  IMPLIED           = VersionInfo -> Doc
text "#IMPLIED"
defaultdecl (DefaultTo a :: AttValue
a f :: Maybe FIXED
f)    = (FIXED -> Doc) -> Maybe FIXED -> Doc
forall t. (t -> Doc) -> Maybe t -> Doc
maybe (Doc -> FIXED -> Doc
forall a b. a -> b -> a
const (VersionInfo -> Doc
text "#FIXED")) Maybe FIXED
f Doc -> Doc -> Doc
<+> AttValue -> Doc
attvalue AttValue
a
--conditionalsect (IncludeSect i)= text "<![INCLUDE [" <+>
--                                 vcat (map extsubsetdecl i) <+> text "]]>"
--conditionalsect (IgnoreSect i) = text "<![IGNORE [" <+>
--                                 fsep (map ignoresectcontents i) <+> text "]]>"
--ignore (Ignore)                = empty
--ignoresectcontents (IgnoreSectContents i is)
--                               = ignore i <+> vcat (map internal is)
--                          where internal (ics,i) = text "<![[" <+>
--                                                   ignoresectcontents ics <+>
--                                                   text "]]>" <+> ignore i
reference :: Reference -> Doc
reference :: Reference -> Doc
reference (RefEntity er :: VersionInfo
er)       = VersionInfo -> Doc
entityref VersionInfo
er
reference (RefChar cr :: Int
cr)         = Int -> Doc
forall a. Show a => a -> Doc
charref Int
cr
entityref :: String -> Doc
entityref :: VersionInfo -> Doc
entityref n :: VersionInfo
n                    = VersionInfo -> Doc
text "&" Doc -> Doc -> Doc
<> VersionInfo -> Doc
text VersionInfo
n Doc -> Doc -> Doc
<> VersionInfo -> Doc
text ";"
charref :: (Show a) => a -> Doc
charref :: a -> Doc
charref c :: a
c                      = VersionInfo -> Doc
text "&#" Doc -> Doc -> Doc
<> VersionInfo -> Doc
text (a -> VersionInfo
forall a. Show a => a -> VersionInfo
show a
c) Doc -> Doc -> Doc
<> VersionInfo -> Doc
text ";"
entitydecl :: EntityDecl -> Doc
entitydecl :: EntityDecl -> Doc
entitydecl (EntityGEDecl d :: GEDecl
d)    = GEDecl -> Doc
gedecl GEDecl
d
entitydecl (EntityPEDecl d :: PEDecl
d)    = PEDecl -> Doc
pedecl PEDecl
d
gedecl :: GEDecl -> Doc
gedecl :: GEDecl -> Doc
gedecl (GEDecl n :: VersionInfo
n ed :: EntityDef
ed)           = VersionInfo -> Doc
text "<!ENTITY" Doc -> Doc -> Doc
<+> VersionInfo -> Doc
text VersionInfo
n Doc -> Doc -> Doc
<+> EntityDef -> Doc
entitydef EntityDef
ed Doc -> Doc -> Doc
<>
                                 VersionInfo -> Doc
text ">"
pedecl :: PEDecl -> Doc
pedecl :: PEDecl -> Doc
pedecl (PEDecl n :: VersionInfo
n pd :: PEDef
pd)           = VersionInfo -> Doc
text "<!ENTITY %" Doc -> Doc -> Doc
<+> VersionInfo -> Doc
text VersionInfo
n Doc -> Doc -> Doc
<+> PEDef -> Doc
pedef PEDef
pd Doc -> Doc -> Doc
<>
                                 VersionInfo -> Doc
text ">"
entitydef :: EntityDef -> Doc
entitydef :: EntityDef -> Doc
entitydef (DefEntityValue ew :: EntityValue
ew)  = EntityValue -> Doc
entityvalue EntityValue
ew
entitydef (DefExternalID i :: ExternalID
i nd :: Maybe NDataDecl
nd) = ExternalID -> Doc
externalid ExternalID
i Doc -> Doc -> Doc
<+> (NDataDecl -> Doc) -> Maybe NDataDecl -> Doc
forall t. (t -> Doc) -> Maybe t -> Doc
maybe NDataDecl -> Doc
ndatadecl Maybe NDataDecl
nd
pedef :: PEDef -> Doc
pedef :: PEDef -> Doc
pedef (PEDefEntityValue ew :: EntityValue
ew)    = EntityValue -> Doc
entityvalue EntityValue
ew
pedef (PEDefExternalID eid :: ExternalID
eid)    = ExternalID -> Doc
externalid ExternalID
eid
externalid :: ExternalID -> Doc
externalid :: ExternalID -> Doc
externalid (SYSTEM sl :: SystemLiteral
sl)         = VersionInfo -> Doc
text "SYSTEM" Doc -> Doc -> Doc
<+> SystemLiteral -> Doc
systemliteral SystemLiteral
sl
externalid (PUBLIC i :: PubidLiteral
i sl :: SystemLiteral
sl)       = VersionInfo -> Doc
text "PUBLIC" Doc -> Doc -> Doc
<+> PubidLiteral -> Doc
pubidliteral PubidLiteral
i Doc -> Doc -> Doc
<+>
                                 SystemLiteral -> Doc
systemliteral SystemLiteral
sl
ndatadecl :: NDataDecl -> Doc
ndatadecl :: NDataDecl -> Doc
ndatadecl (NDATA n :: VersionInfo
n)            = VersionInfo -> Doc
text "NDATA" Doc -> Doc -> Doc
<+> VersionInfo -> Doc
text VersionInfo
n
--textdecl (TextDecl vi ed)      = text "<?xml" <+> maybe text vi <+>
--                                 encodingdecl ed <+> text "?>"
--extparsedent (ExtParsedEnt t c)= maybe textdecl t <+> content c
--extpe (ExtPE t esd)            = maybe textdecl t <+>
--                                 vcat (map extsubsetdecl esd)
notationdecl :: NotationDecl -> Doc
notationdecl :: NotationDecl -> Doc
notationdecl (NOTATION n :: VersionInfo
n e :: Either ExternalID PublicID
e)    = VersionInfo -> Doc
text "<!NOTATION" Doc -> Doc -> Doc
<+> VersionInfo -> Doc
text VersionInfo
n Doc -> Doc -> Doc
<+>
                                 (ExternalID -> Doc)
-> (PublicID -> Doc) -> Either ExternalID PublicID -> Doc
forall t t1 t2. (t -> t1) -> (t2 -> t1) -> Either t t2 -> t1
either ExternalID -> Doc
externalid PublicID -> Doc
publicid Either ExternalID PublicID
e Doc -> Doc -> Doc
<>
                                 VersionInfo -> Doc
text ">"
publicid :: PublicID -> Doc
publicid :: PublicID -> Doc
publicid (PUBLICID p :: PubidLiteral
p)          = VersionInfo -> Doc
text "PUBLIC" Doc -> Doc -> Doc
<+> PubidLiteral -> Doc
pubidliteral PubidLiteral
p
encodingdecl :: EncodingDecl -> Doc
encodingdecl :: EncodingDecl -> Doc
encodingdecl (EncodingDecl s :: VersionInfo
s)  = VersionInfo -> Doc
text "encoding='" Doc -> Doc -> Doc
<> VersionInfo -> Doc
text VersionInfo
s Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "'"
nmtoken :: String -> Doc
nmtoken :: VersionInfo -> Doc
nmtoken s :: VersionInfo
s                      = VersionInfo -> Doc
text VersionInfo
s
attvalue :: AttValue -> Doc
attvalue :: AttValue -> Doc
attvalue (AttValue esr :: [Either VersionInfo Reference]
esr)        = VersionInfo -> Doc
text "\"" Doc -> Doc -> Doc
<>
                                 [Doc] -> Doc
hcat ((Either VersionInfo Reference -> Doc)
-> [Either VersionInfo Reference] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ((VersionInfo -> Doc)
-> (Reference -> Doc) -> Either VersionInfo Reference -> Doc
forall t t1 t2. (t -> t1) -> (t2 -> t1) -> Either t t2 -> t1
either VersionInfo -> Doc
text Reference -> Doc
reference) [Either VersionInfo Reference]
esr) Doc -> Doc -> Doc
<>
                                 VersionInfo -> Doc
text "\""
entityvalue :: EntityValue -> Doc
entityvalue :: EntityValue -> Doc
entityvalue (EntityValue evs :: [EV]
evs)
  | [EV] -> SDDecl
containsDoubleQuote [EV]
evs    = VersionInfo -> Doc
text "'"  Doc -> Doc -> Doc
<> [Doc] -> Doc
hcat ((EV -> Doc) -> [EV] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map EV -> Doc
ev [EV]
evs) Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "'"
  | SDDecl
otherwise                  = VersionInfo -> Doc
text "\"" Doc -> Doc -> Doc
<> [Doc] -> Doc
hcat ((EV -> Doc) -> [EV] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map EV -> Doc
ev [EV]
evs) Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "\""
ev :: EV -> Doc
ev :: EV -> Doc
ev (EVString s :: VersionInfo
s)                = VersionInfo -> Doc
text VersionInfo
s
--ev (EVPERef p e)               = peref p
ev (EVRef r :: Reference
r)                   = Reference -> Doc
reference Reference
r
pubidliteral :: PubidLiteral -> Doc
pubidliteral :: PubidLiteral -> Doc
pubidliteral (PubidLiteral s :: VersionInfo
s)
    | '"' Char -> VersionInfo -> SDDecl
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> SDDecl
`elem` VersionInfo
s             = VersionInfo -> Doc
text "'" Doc -> Doc -> Doc
<> VersionInfo -> Doc
text VersionInfo
s Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "'"
    | SDDecl
otherwise                = VersionInfo -> Doc
text "\"" Doc -> Doc -> Doc
<> VersionInfo -> Doc
text VersionInfo
s Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "\""
systemliteral :: SystemLiteral -> Doc
systemliteral :: SystemLiteral -> Doc
systemliteral (SystemLiteral s :: VersionInfo
s)
    | '"' Char -> VersionInfo -> SDDecl
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> SDDecl
`elem` VersionInfo
s             = VersionInfo -> Doc
text "'" Doc -> Doc -> Doc
<> VersionInfo -> Doc
text VersionInfo
s Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "'"
    | SDDecl
otherwise                = VersionInfo -> Doc
text "\"" Doc -> Doc -> Doc
<> VersionInfo -> Doc
text VersionInfo
s Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "\""
chardata :: String -> Doc
chardata :: VersionInfo -> Doc
chardata s :: VersionInfo
s                     = {-if all isSpace s then empty else-} VersionInfo -> Doc
text VersionInfo
s
cdsect :: String -> Doc
cdsect :: VersionInfo -> Doc
cdsect c :: VersionInfo
c                       = VersionInfo -> Doc
text "<![CDATA[" Doc -> Doc -> Doc
<> VersionInfo -> Doc
chardata VersionInfo
c Doc -> Doc -> Doc
<> VersionInfo -> Doc
text "]]>"

qname :: QName -> Doc
qname n :: QName
n                        = VersionInfo -> Doc
text (QName -> VersionInfo
printableName QName
n)

----
containsDoubleQuote :: [EV] -> Bool
containsDoubleQuote :: [EV] -> SDDecl
containsDoubleQuote evs :: [EV]
evs = (EV -> SDDecl) -> [EV] -> SDDecl
forall (t :: * -> *) a.
Foldable t =>
(a -> SDDecl) -> t a -> SDDecl
any EV -> SDDecl
csq [EV]
evs
    where csq :: EV -> SDDecl
csq (EVString s :: VersionInfo
s) = '"' Char -> VersionInfo -> SDDecl
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> SDDecl
`elem` VersionInfo
s
          csq _            = SDDecl
False