1 {-# LANGUAGE RecordWildCards #-}
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.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 xmlAbout :: DTC.About -> XML
66 xmlAbout DTC.About{..} = do
67 forM_ titles $ xmlTitle
68 forM_ authors $ xmlAuthor
69 forM_ editor $ xmlEditor
71 whenMayText version xmlVersion
72 forM_ keywords $ xmlKeyword
74 forM_ includes $ xmlInclude
76 xmlInclude :: DTC.Include -> XML
77 xmlInclude DTC.Include{..} =
79 ! XA.href (attrValue href)
81 xmlKeyword :: Text -> XML
82 xmlKeyword = XML.keyword . xmlText
84 xmlVersion :: MayText -> XML
85 xmlVersion (MayText t) = XML.version $ xmlText t
87 xmlDate :: DTC.Date -> XML
88 xmlDate DTC.Date{..} =
90 ! XA.year (attrValue year)
91 !?? mayAttr XA.month month
92 !?? mayAttr XA.day day
94 xmlLink :: DTC.Link -> XML
95 xmlLink DTC.Link{..} =
97 !?? mayAttr XA.name name
98 !?? mayAttr XA.rel rel
99 !?? mayAttr XA.href href
100 $ xmlHorizontals body
102 xmlAddress :: DTC.Address -> XML
103 xmlAddress DTC.Address{..} =
105 !?? mayAttr XA.street street
106 !?? mayAttr XA.zipcode zipcode
107 !?? mayAttr XA.city city
108 !?? mayAttr XA.region region
109 !?? mayAttr XA.country country
110 !?? mayAttr XA.email email
111 !?? mayAttr XA.tel tel
112 !?? mayAttr XA.fax fax
114 xmlAuthor :: DTC.Entity -> XML
115 xmlAuthor DTC.Entity{..} =
117 !?? mayAttr XA.name name
120 xmlEditor :: DTC.Entity -> XML
121 xmlEditor DTC.Entity{..} =
123 !?? mayAttr XA.name name
126 xmlTitle :: DTC.Title -> XML
127 xmlTitle (DTC.Title t) = XML.title $ xmlHorizontals t
129 xmlAlias :: DTC.Alias -> XML
130 xmlAlias DTC.Alias{..} = XML.alias !?? mayAttr XA.id id
132 xmlId :: DTC.Ident -> B.Attribute
133 xmlId (DTC.Ident i) = XA.id $ attrValue i
135 xmlVerticals :: DTC.Verticals -> XML
136 xmlVerticals = (`forM_` xmlVertical)
138 xmlCommonAttrs :: DTC.CommonAttrs -> XML -> XML
139 xmlCommonAttrs DTC.CommonAttrs{id=DTC.Ident ident, ..} =
142 i -> B.AddCustomAttribute "id" (B.Text i)) .
145 _ -> B.AddCustomAttribute "class" (B.Text $ Text.unwords class_)
147 xmlVertical :: DTC.Vertical -> XML
150 xmlCommonAttrs attrs $
153 forM_ aliases xmlAlias
157 XML.para $ xmlHorizontals hs
160 XML.ol $ forM_ vs $ XML.li . xmlVerticals
163 XML.ul $ forM_ vs $ XML.li . xmlVerticals
167 !?? mayAttr XA.depth d
171 !?? mayAttr XA.depth d
174 XML.rl $ forM_ rs $ xmlReference
175 -- DTC.Index -> XML.index
177 xmlCommonAttrs attrs $
179 ! XA.type_ (attrValue type_) $ do
184 DTC.Artwork as _art ->
188 xmlHorizontals :: DTC.Horizontals -> XML
189 xmlHorizontals = (`forM_` xmlHorizontal)
191 xmlHorizontal :: DTC.Horizontal -> XML
192 xmlHorizontal = \case
193 DTC.Plain txt -> B.toMarkup txt
195 DTC.B hs -> XML.b $ xmlHorizontals hs
196 DTC.Code hs -> XML.code $ xmlHorizontals hs
197 DTC.Del hs -> XML.del $ xmlHorizontals hs
198 DTC.I hs -> XML.i $ xmlHorizontals hs
199 DTC.Note hs -> XML.note $ xmlHorizontals hs
200 DTC.Q hs -> XML.q $ xmlHorizontals hs
201 DTC.SC hs -> XML.sc $ xmlHorizontals hs
202 DTC.Sub hs -> XML.sub $ xmlHorizontals hs
203 DTC.Sup hs -> XML.sup $ xmlHorizontals hs
204 DTC.Eref to hs -> XML.eref ! XA.to (attrValue to) $ xmlHorizontals hs
205 DTC.Iref to hs -> XML.iref ! XA.to (attrValue to) $ xmlHorizontals hs
206 DTC.Ref to hs -> XML.ref ! XA.to (attrValue to) $ xmlHorizontals hs
207 DTC.Rref to hs -> XML.rref ! XA.to (attrValue to) $ xmlHorizontals hs
209 xmlReference :: DTC.Reference -> XML
210 xmlReference DTC.Reference{..} =