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
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
140 !?? mayAttr XA.url url
146 forM_ tags $ XML.tag . xmlify
149 instance Xmlify Include where
152 ! XA.href (attrify href)
153 instance Xmlify Date where
156 ! XA.year (attrify year)
157 !?? mayAttr XA.month month
158 !?? mayAttr XA.day day
159 instance Xmlify Link where
162 !?? mayAttr XA.name name
163 !?? mayAttr XA.rel rel
164 !?? mayAttr XA.href href
166 instance Xmlify Entity where
169 !?? mayAttr XA.name name
170 !?? mayAttr XA.street street
171 !?? mayAttr XA.zipcode zipcode
172 !?? mayAttr XA.city city
173 !?? mayAttr XA.region region
174 !?? mayAttr XA.country country
175 !?? mayAttr XA.email email
176 !?? mayAttr XA.tel tel
177 !?? mayAttr XA.fax fax
178 instance Xmlify Title where
179 xmlify (Title t) = XML.title $ xmlify t
180 instance Xmlify Alias where
181 xmlify Alias{..} = XML.alias !?? mayAttr XA.id id
182 instance Xmlify Reference where
183 xmlify Reference{..} = XML.reference mempty -- TODO: to be coded
185 instance Xmlify a => Xmlify (Maybe a) where
186 xmlify = foldMap xmlify
187 instance Xmlify a => Xmlify [a] where
188 xmlify = foldMap xmlify
189 instance Xmlify a => Xmlify (Seq a) where
190 xmlify = foldMap xmlify
192 xmlId :: Ident -> B.Attribute
193 xmlId (Ident i) = XA.id $ attrify i
195 xmlCommonAttrs :: CommonAttrs -> XML -> XML
196 xmlCommonAttrs CommonAttrs{id=ident, ..} =
200 B.AddCustomAttribute "id" $
201 B.String $ TL.unpack i) .
205 B.AddCustomAttribute "class" $
206 B.String $ TL.unpack $ TL.unwords classes