1 {-# LANGUAGE RecordWildCards #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module Language.DTC.Write.XML where
6 import Control.Monad (forM_, mapM_)
8 -- import Data.Foldable (Foldable(..))
9 import Data.Function (($), (.))
10 import Data.Monoid (Monoid(..))
11 import Data.Semigroup (Semigroup(..))
12 import Data.Text (Text)
13 import Text.Blaze ((!))
14 import Text.Blaze.DTC (XML)
15 import Text.Blaze.Utils
16 import qualified Data.Text as Text
17 import qualified Text.Blaze as B
18 import qualified Text.Blaze.DTC as XML
19 import qualified Text.Blaze.DTC.Attributes as XA
20 import qualified Text.Blaze.Internal as B
22 import Language.DTC.Document (MayText(..), whenMayText)
23 import qualified Language.DTC.Document as DTC
25 instance AttrValue DTC.URL where
26 attrValue (DTC.URL a) = attrValue a
27 instance AttrValue DTC.Path where
28 attrValue (DTC.Path a) = attrValue a
29 instance AttrValue DTC.Ident where
30 attrValue (DTC.Ident a) = attrValue a
31 instance AttrValue DTC.Nat where
32 attrValue (DTC.Nat a) = attrValue a
33 instance AttrValue DTC.Nat1 where
34 attrValue (DTC.Nat1 a) = attrValue a
36 instance MayAttr DTC.URL where
37 mayAttr a (DTC.URL t) = mayAttr a t
38 instance MayAttr DTC.Path where
39 mayAttr a (DTC.Path t) = mayAttr a t
40 instance MayAttr DTC.Ident where
41 mayAttr a (DTC.Ident t) = mayAttr a t
42 instance MayAttr DTC.Nat where
43 mayAttr a (DTC.Nat t) = mayAttr a t
44 instance MayAttr DTC.Nat1 where
45 mayAttr a (DTC.Nat1 t) = mayAttr a t
47 xmlText :: Text -> XML
50 xmlDocument :: DTC.Document -> XML
51 xmlDocument DTC.Document{..} = do
53 XML.xmlModel "./schema/dtc.rnc"
54 XML.xmlStylesheet $ "./xsl/document.html5."<>lang<>".xsl"
55 XML.html5Stylesheet $ "./xsl/document.html5."<>lang<>".xsl"
56 XML.atomStylesheet $ "./xsl/document.atom."<>lang<>".xsl"
61 xmlHead :: DTC.Head -> XML
62 xmlHead DTC.Head{..} =
63 XML.about $ xmlAbout about
65 xmlBody :: [DTC.Body] -> XML
66 xmlBody = mapM_ $ \case
67 DTC.Verticals vs -> xmlVerticals vs
69 xmlCommonAttrs attrs $
72 forM_ aliases xmlAlias
75 xmlAbout :: DTC.About -> XML
76 xmlAbout DTC.About{..} = do
77 forM_ titles $ xmlTitle
78 forM_ authors $ xmlAuthor
79 forM_ editor $ xmlEditor
81 whenMayText version xmlVersion
82 forM_ keywords $ xmlKeyword
84 forM_ includes $ xmlInclude
86 xmlInclude :: DTC.Include -> XML
87 xmlInclude DTC.Include{..} =
89 ! XA.href (attrValue href)
91 xmlKeyword :: Text -> XML
92 xmlKeyword = XML.keyword . xmlText
94 xmlVersion :: MayText -> XML
95 xmlVersion (MayText t) = XML.version $ xmlText t
97 xmlDate :: DTC.Date -> XML
98 xmlDate DTC.Date{..} =
100 ! XA.year (attrValue year)
101 !?? mayAttr XA.month month
102 !?? mayAttr XA.day day
104 xmlLink :: DTC.Link -> XML
105 xmlLink DTC.Link{..} =
107 !?? mayAttr XA.name name
108 !?? mayAttr XA.rel rel
109 !?? mayAttr XA.href href
110 $ xmlHorizontals body
112 xmlAddress :: DTC.Address -> XML
113 xmlAddress DTC.Address{..} =
115 !?? mayAttr XA.street street
116 !?? mayAttr XA.zipcode zipcode
117 !?? mayAttr XA.city city
118 !?? mayAttr XA.region region
119 !?? mayAttr XA.country country
120 !?? mayAttr XA.email email
121 !?? mayAttr XA.tel tel
122 !?? mayAttr XA.fax fax
124 xmlAuthor :: DTC.Entity -> XML
125 xmlAuthor DTC.Entity{..} =
127 !?? mayAttr XA.name name
130 xmlEditor :: DTC.Entity -> XML
131 xmlEditor DTC.Entity{..} =
133 !?? mayAttr XA.name name
136 xmlTitle :: DTC.Title -> XML
137 xmlTitle (DTC.Title t) = XML.title $ xmlHorizontals t
139 xmlAlias :: DTC.Alias -> XML
140 xmlAlias DTC.Alias{..} = XML.alias !?? mayAttr XA.id id
142 xmlId :: DTC.Ident -> B.Attribute
143 xmlId (DTC.Ident i) = XA.id $ attrValue i
145 xmlVerticals :: DTC.Verticals -> XML
146 xmlVerticals = (`forM_` xmlVertical)
148 xmlCommonAttrs :: DTC.CommonAttrs -> XML -> XML
149 xmlCommonAttrs DTC.CommonAttrs{id=DTC.Ident ident, ..} =
152 i -> B.AddCustomAttribute "id" (B.Text i)) .
155 _ -> B.AddCustomAttribute "class" (B.Text $ Text.unwords class_)
157 xmlVertical :: DTC.Vertical -> XML
161 XML.para $ xmlHorizontals hs
164 XML.ol $ forM_ vs $ XML.li . xmlVerticals
167 XML.ul $ forM_ vs $ XML.li . xmlVerticals
171 !?? mayAttr XA.depth d
175 !?? mayAttr XA.depth d
178 XML.rl $ forM_ rs $ xmlReference
179 -- DTC.Index -> XML.index
181 xmlCommonAttrs attrs $
183 ! XA.type_ (attrValue type_) $ do
188 DTC.Artwork as _art ->
192 xmlHorizontals :: DTC.Horizontals -> XML
193 xmlHorizontals = (`forM_` xmlHorizontal)
195 xmlHorizontal :: DTC.Horizontal -> XML
196 xmlHorizontal = \case
197 DTC.Plain txt -> B.toMarkup txt
199 DTC.B hs -> XML.b $ xmlHorizontals hs
200 DTC.Code hs -> XML.code $ xmlHorizontals hs
201 DTC.Del hs -> XML.del $ xmlHorizontals hs
202 DTC.I hs -> XML.i $ xmlHorizontals hs
203 DTC.Note hs -> XML.note $ xmlHorizontals hs
204 DTC.Q hs -> XML.q $ xmlHorizontals hs
205 DTC.SC hs -> XML.sc $ xmlHorizontals hs
206 DTC.Sub hs -> XML.sub $ xmlHorizontals hs
207 DTC.Sup hs -> XML.sup $ xmlHorizontals hs
208 DTC.Eref to hs -> XML.eref ! XA.to (attrValue to) $ xmlHorizontals hs
209 DTC.Iref to hs -> XML.iref ! XA.to (attrValue to) $ xmlHorizontals hs
210 DTC.Ref to hs -> XML.ref ! XA.to (attrValue to) $ xmlHorizontals hs
211 DTC.Rref to hs -> XML.rref ! XA.to (attrValue to) $ xmlHorizontals hs
213 xmlReference :: DTC.Reference -> XML
214 xmlReference DTC.Reference{..} =