1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module Language.DTC.Write.XML where
6 import Control.Monad (forM_)
8 import Data.Foldable (Foldable(..))
9 import Data.Function (($), (.))
10 import Data.Functor ((<$>))
11 import Data.Maybe (Maybe(..))
12 import Data.Monoid (Monoid(..))
13 import Data.Semigroup (Semigroup(..))
14 import Data.Sequence (Seq)
15 import Data.TreeSeq.Strict (Tree(..))
16 import Text.Blaze ((!))
17 import Text.Blaze.Utils
18 import Text.Blaze.XML (XML)
19 import qualified Data.Char as Char
20 import qualified Data.Function as Fun
21 import qualified Data.Map.Strict as Map
22 import qualified Data.Text as Text
23 import qualified Data.Text.Lazy as TL
24 import qualified Text.Blaze as B
25 import qualified Text.Blaze.DTC as XML
26 import qualified Text.Blaze.DTC.Attributes as XA
27 import qualified Text.Blaze.Internal as B
30 import Language.DTC.Anchor (plainifyWords)
31 import Language.DTC.Document as DTC hiding (XML)
33 document :: Locales ls => LocaleIn ls -> Document -> XML
34 document loc Document{..} = do
35 let lang = Text.takeWhile Char.isAlphaNum $ textLocales Map.! loc
36 XML.xmlModel "./schema/dtc.rnc"
37 XML.xmlStylesheet $ "./xsl/document.html5."<>lang<>".xsl"
38 XML.html5Stylesheet $ "./xsl/document.html5."<>lang<>".xsl"
39 XML.atomStylesheet $ "./xsl/document.atom."<>lang<>".xsl"
48 instance Xmlify TL.Text where
50 instance Xmlify Head where
52 XML.about $ xmlify about
53 instance Xmlify (Tree BodyNode) where
56 BodyBlock b -> xmlify b
58 xmlCommonAttrs attrs $
63 instance Xmlify Block where
65 BlockPara para -> xmlify para
67 xmlCommonAttrs attrs $
69 !?? mayAttr XA.depth depth
71 xmlCommonAttrs attrs $
77 xmlCommonAttrs attrs $
80 forM_ terms $ \aliases ->
84 plainifyWords <$> aliases
86 xmlCommonAttrs attrs $
88 ! XA.type_ (attrify type_) $ do
91 BlockReferences{..} ->
92 xmlCommonAttrs attrs $
93 XML.references $ xmlify refs
94 instance Xmlify Para where
96 ParaItem{..} -> xmlify item
97 ParaItems{..} -> xmlCommonAttrs attrs $ XML.para $ xmlify items
98 instance Xmlify ParaItem where
100 ParaPlain p -> XML.p $ xmlify p
102 XML.comment $ TL.toStrict c
105 ! XA.type_ (attrify type_) $ do
109 ! XA.type_ (attrify type_) $ do
111 ParaOL items -> XML.ol $ forM_ items xmlify
112 ParaUL items -> XML.ul $ forM_ items $ XML.li . xmlify
113 instance Xmlify ListItem where
114 xmlify ListItem{..} =
115 XML.li ! XA.name (attrify name) $ xmlify paras
116 instance Xmlify (Tree PlainNode) where
119 PlainText t -> xmlify t
121 PlainGroup -> xmlify ts
122 PlainB -> XML.b $ xmlify ts
123 PlainCode -> XML.code $ xmlify ts
124 PlainDel -> XML.del $ xmlify ts
125 PlainI -> XML.i $ xmlify ts
126 PlainNote{..} -> XML.note $ xmlify note
127 PlainQ -> XML.q $ xmlify ts
128 PlainSC -> XML.sc $ xmlify ts
129 PlainSub -> XML.sub $ xmlify ts
130 PlainSup -> XML.sup $ xmlify ts
131 PlainU -> XML.u $ xmlify ts
132 PlainEref to -> XML.eref ! XA.to (attrify to) $ xmlify ts
133 PlainIref{..} -> XML.iref ! XA.term (attrify $ plainifyWords term) $ xmlify ts
134 PlainRef to -> XML.ref ! XA.to (attrify to) $ xmlify ts
135 PlainRref{..} -> XML.rref ! XA.to (attrify to) $ xmlify ts
137 instance Xmlify About where
138 xmlify About{..} = do
143 forM_ version $ XML.version . xmlify
144 forM_ keywords $ XML.keyword . xmlify
147 instance Xmlify Include where
150 ! XA.href (attrify href)
151 instance Xmlify Date where
154 ! XA.year (attrify year)
155 !?? mayAttr XA.month month
156 !?? mayAttr XA.day day
157 instance Xmlify Link where
160 !?? mayAttr XA.name name
161 !?? mayAttr XA.rel rel
162 !?? mayAttr XA.href href
164 instance Xmlify Entity where
167 !?? mayAttr XA.name name
168 !?? mayAttr XA.street street
169 !?? mayAttr XA.zipcode zipcode
170 !?? mayAttr XA.city city
171 !?? mayAttr XA.region region
172 !?? mayAttr XA.country country
173 !?? mayAttr XA.email email
174 !?? mayAttr XA.tel tel
175 !?? mayAttr XA.fax fax
176 instance Xmlify Title where
177 xmlify (Title t) = XML.title $ xmlify t
178 instance Xmlify Alias where
179 xmlify Alias{..} = XML.alias !?? mayAttr XA.id id
180 instance Xmlify Reference where
181 xmlify Reference{..} = XML.reference mempty -- TODO: to be coded
183 instance Xmlify a => Xmlify (Maybe a) where
184 xmlify = foldMap xmlify
185 instance Xmlify a => Xmlify [a] where
186 xmlify = foldMap xmlify
187 instance Xmlify a => Xmlify (Seq a) where
188 xmlify = foldMap xmlify
190 xmlId :: Ident -> B.Attribute
191 xmlId (Ident i) = XA.id $ attrify i
193 xmlCommonAttrs :: CommonAttrs -> XML -> XML
194 xmlCommonAttrs CommonAttrs{id=ident, ..} =
198 B.AddCustomAttribute "id" $
199 B.String $ TL.unpack i) .
203 B.AddCustomAttribute "class" $
204 B.String $ TL.unpack $ TL.unwords classes