1 {-# LANGUAGE OverloadedStrings #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 module Language.DTC.Write.XML where
5 import Control.Monad (forM_, mapM_)
7 import Data.Function (($), (.))
8 import Data.Functor ((<$>))
9 import Data.Maybe (Maybe(..))
10 import Data.Monoid (Monoid(..))
11 import Data.Semigroup (Semigroup(..))
12 import Data.Text (Text)
13 import Text.Blaze ((!))
14 import Text.Blaze.Utils
15 import Text.Blaze.XML (XML)
16 import Data.TreeSeq.Strict (Tree(..))
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 Language.DTC.Index (plainifyWords)
28 import qualified Language.DTC.Document as DTC
30 xmlText :: Text -> XML
33 xmlDocument :: Locales ls => LocaleIn ls -> DTC.Document -> XML
34 xmlDocument loc DTC.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"
44 xmlHead :: DTC.Head -> XML
45 xmlHead DTC.Head{..} =
46 XML.about $ xmlAbout about
48 xmlBody :: DTC.Body -> XML
49 xmlBody = mapM_ $ \case
50 TreeN k ts -> xmlBodyKey k $ xmlBody ts
51 Tree0 v -> xmlBodyValue v
53 xmlBodyKey :: DTC.BodyKey -> XML -> XML
54 xmlBodyKey k body = case k of
56 xmlCommonAttrs attrs $
59 forM_ aliases xmlAlias
62 xmlBodyValue :: DTC.BodyValue -> XML
65 xmlCommonAttrs attrs $
67 !?? mayAttr XA.depth depth
69 xmlCommonAttrs attrs $
75 xmlCommonAttrs attrs $
77 ! XA.type_ (attrValue type_) $ do
81 xmlCommonAttrs attrs $
84 forM_ terms $ \aliases ->
88 plainifyWords <$> aliases
89 DTC.Block v -> xmlBlock v
91 xmlAbout :: DTC.About -> XML
92 xmlAbout DTC.About{..} = do
93 forM_ titles $ xmlTitle
94 forM_ authors $ xmlAuthor
95 forM_ editor $ xmlEditor
97 whenMayText version xmlVersion
98 forM_ keywords $ xmlKeyword
100 forM_ includes $ xmlInclude
102 xmlInclude :: DTC.Include -> XML
103 xmlInclude DTC.Include{..} =
105 ! XA.href (attrValue href)
107 xmlKeyword :: Text -> XML
108 xmlKeyword = XML.keyword . xmlText
110 xmlVersion :: MayText -> XML
111 xmlVersion (MayText t) = XML.version $ xmlText t
113 xmlDate :: DTC.Date -> XML
114 xmlDate DTC.Date{..} =
116 ! XA.year (attrValue year)
117 !?? mayAttr XA.month month
118 !?? mayAttr XA.day day
120 xmlLink :: DTC.Link -> XML
121 xmlLink DTC.Link{..} =
123 !?? mayAttr XA.name name
124 !?? mayAttr XA.rel rel
125 !?? mayAttr XA.href href
128 xmlAddress :: DTC.Address -> XML
129 xmlAddress DTC.Address{..} =
131 !?? mayAttr XA.street street
132 !?? mayAttr XA.zipcode zipcode
133 !?? mayAttr XA.city city
134 !?? mayAttr XA.region region
135 !?? mayAttr XA.country country
136 !?? mayAttr XA.email email
137 !?? mayAttr XA.tel tel
138 !?? mayAttr XA.fax fax
140 xmlAuthor :: DTC.Entity -> XML
141 xmlAuthor DTC.Entity{..} =
143 !?? mayAttr XA.name name
146 xmlEditor :: DTC.Entity -> XML
147 xmlEditor DTC.Entity{..} =
149 !?? mayAttr XA.name name
152 xmlTitle :: DTC.Title -> XML
153 xmlTitle (DTC.Title t) = XML.title $ xmlPara t
155 xmlAlias :: DTC.Alias -> XML
156 xmlAlias DTC.Alias{..} = XML.alias !?? mayAttr XA.id id
158 xmlId :: DTC.Ident -> B.Attribute
159 xmlId (DTC.Ident i) = XA.id $ attrValue i
161 xmlBlocks :: DTC.Blocks -> XML
162 xmlBlocks = (`forM_` xmlBlock)
164 xmlCommonAttrs :: DTC.CommonAttrs -> XML -> XML
165 xmlCommonAttrs DTC.CommonAttrs{id=ident, ..} =
168 Just (DTC.Ident i) -> B.AddCustomAttribute "id" (B.Text i)) .
171 _ -> B.AddCustomAttribute "class" (B.Text $ Text.unwords classes)
173 xmlBlock :: DTC.Block -> XML
176 xmlCommonAttrs attrs $
177 XML.para $ xmlPara para
179 xmlCommonAttrs attrs $
180 XML.ol $ forM_ items $ XML.li . xmlBlocks
182 xmlCommonAttrs attrs $
183 XML.ul $ forM_ items $ XML.li . xmlBlocks
185 xmlCommonAttrs attrs $
186 XML.rl $ forM_ refs $ xmlReference
187 -- DTC.Index -> XML.index
191 xmlCommonAttrs attrs $
194 xmlPara :: DTC.Para -> XML
195 xmlPara = (`forM_` xmlLine)
197 xmlLine :: Tree DTC.LineKey DTC.LineValue -> XML
201 DTC.Plain p -> B.toMarkup p
205 DTC.B -> XML.b $ xmlPara ls
206 DTC.Code -> XML.code $ xmlPara ls
207 DTC.Del -> XML.del $ xmlPara ls
208 DTC.I -> XML.i $ xmlPara ls
209 DTC.Note -> XML.note $ xmlPara ls
210 DTC.Q -> XML.q $ xmlPara ls
211 DTC.SC -> XML.sc $ xmlPara ls
212 DTC.Sub -> XML.sub $ xmlPara ls
213 DTC.Sup -> XML.sup $ xmlPara ls
214 DTC.U -> XML.u $ xmlPara ls
215 DTC.Eref to -> XML.eref ! XA.to (attrValue to) $ xmlPara ls
216 DTC.Iref{..} -> XML.iref ! XA.term (attrValue $ plainifyWords term) $ xmlPara ls
217 DTC.Ref to -> XML.ref ! XA.to (attrValue to) $ xmlPara ls
218 DTC.Rref to -> XML.rref ! XA.to (attrValue to) $ xmlPara ls
220 xmlReference :: DTC.Reference -> XML
221 xmlReference DTC.Reference{..} =