1 {-# LANGUAGE RecordWildCards #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module Language.DTC.Write.XML where
6 -- import Data.Foldable (Foldable(..))
7 import Control.Monad (forM_, mapM_)
9 import Data.Function (($), (.))
10 import Data.Maybe (Maybe(..))
11 import Data.Monoid (Monoid(..))
12 import Data.Semigroup (Semigroup(..))
13 import Data.Text (Text)
14 import Text.Blaze ((!))
15 import Text.Blaze.Utils
16 import Text.Blaze.XML (XML)
17 import qualified Data.Char as Char
18 import qualified Data.Map.Strict as Map
19 import qualified Data.Text as Text
20 import qualified Text.Blaze as B
21 import qualified Text.Blaze.DTC as XML
22 import qualified Text.Blaze.DTC.Attributes as XA
23 import qualified Text.Blaze.Internal as B
26 import Language.DTC.Document (MayText(..), whenMayText)
27 import qualified Language.DTC.Document as DTC
29 xmlText :: Text -> XML
32 xmlDocument :: Locales ls => LocaleIn ls -> DTC.Document -> XML
33 xmlDocument loc DTC.Document{..} = do
34 let lang = Text.takeWhile Char.isAlphaNum $ textLocales Map.! loc
35 XML.xmlModel "./schema/dtc.rnc"
36 XML.xmlStylesheet $ "./xsl/document.html5."<>lang<>".xsl"
37 XML.html5Stylesheet $ "./xsl/document.html5."<>lang<>".xsl"
38 XML.atomStylesheet $ "./xsl/document.atom."<>lang<>".xsl"
43 xmlHead :: DTC.Head -> XML
44 xmlHead DTC.Head{..} =
45 XML.about $ xmlAbout about
47 xmlBody :: [DTC.Body] -> XML
48 xmlBody = mapM_ $ \case
49 DTC.Verticals vs -> xmlVerticals vs
51 xmlCommonAttrs attrs $
54 forM_ aliases xmlAlias
57 xmlCommonAttrs attrs $
59 !?? mayAttr XA.depth depth
61 xmlCommonAttrs attrs $
63 !?? mayAttr XA.depth depth
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=ident, ..} =
142 Just (DTC.Ident i) -> B.AddCustomAttribute "id" (B.Text i)) .
145 _ -> B.AddCustomAttribute "class" (B.Text $ Text.unwords classes)
147 xmlVertical :: DTC.Vertical -> XML
150 xmlCommonAttrs attrs $
151 XML.para $ xmlHorizontals horis
153 xmlCommonAttrs attrs $
154 XML.ol $ forM_ items $ XML.li . xmlVerticals
156 xmlCommonAttrs attrs $
157 XML.ul $ forM_ items $ XML.li . xmlVerticals
159 xmlCommonAttrs attrs $
160 XML.rl $ forM_ refs $ xmlReference
161 -- DTC.Index -> XML.index
163 xmlCommonAttrs attrs $
165 ! XA.type_ (attrValue type_) $ do
171 xmlCommonAttrs attrs $
174 xmlHorizontals :: DTC.Horizontals -> XML
175 xmlHorizontals = (`forM_` xmlHorizontal)
177 xmlHorizontal :: DTC.Horizontal -> XML
178 xmlHorizontal = \case
179 DTC.Plain txt -> B.toMarkup txt
181 DTC.B hs -> XML.b $ xmlHorizontals hs
182 DTC.Code hs -> XML.code $ xmlHorizontals hs
183 DTC.Del hs -> XML.del $ xmlHorizontals hs
184 DTC.I hs -> XML.i $ xmlHorizontals hs
185 DTC.Note hs -> XML.note $ xmlHorizontals hs
186 DTC.Q hs -> XML.q $ xmlHorizontals hs
187 DTC.SC hs -> XML.sc $ xmlHorizontals hs
188 DTC.Sub hs -> XML.sub $ xmlHorizontals hs
189 DTC.Sup hs -> XML.sup $ xmlHorizontals hs
190 DTC.Eref to hs -> XML.eref ! XA.to (attrValue to) $ xmlHorizontals hs
191 DTC.Iref to hs -> XML.iref ! XA.to (attrValue to) $ xmlHorizontals hs
192 DTC.Ref to hs -> XML.ref ! XA.to (attrValue to) $ xmlHorizontals hs
193 DTC.Rref to hs -> XML.rref ! XA.to (attrValue to) $ xmlHorizontals hs
195 xmlReference :: DTC.Reference -> XML
196 xmlReference DTC.Reference{..} =