1 {-# LANGUAGE NoMonomorphismRestriction #-}
2 {-# LANGUAGE DuplicateRecordFields #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 module Language.DTC.Sym where
6 import Control.Applicative (Applicative(..), (<$>), (<$))
7 import Control.Monad (void)
8 import Data.Default.Class (Default(..))
9 import Data.Foldable (Foldable(..), concat)
10 import Data.Function (($), (.))
11 import Data.Maybe (Maybe(..), maybe)
12 import Data.TreeSeq.Strict (Tree(..), tree0)
13 import qualified Control.Applicative as Alt
14 import qualified Data.Sequence as Seq
15 import qualified Data.Text.Lazy as TL
18 import Language.RNC.Sym as RNC
19 import Language.DTC.Anchor (wordify)
20 import qualified Language.DTC.Document as DTC
21 import qualified Language.RNC.Write as RNC
24 -- | Use a symantic (tagless final) class to encode
25 -- both the parsing and the schema of DTC,
26 -- when repr is respectively instanciated
27 -- on 'DTC.Parser' or 'RNC.RuleWriter'.
28 class RNC.Sym_RNC repr => Sym_DTC repr where
29 position :: repr DTC.Pos
30 document :: repr DTC.Document
33 about :: repr DTC.About
34 header :: repr DTC.Header
36 author :: repr DTC.Entity
37 editor :: repr DTC.Entity
39 entity :: repr DTC.Entity
41 serie :: repr DTC.Serie
42 alias :: repr DTC.Alias
45 include :: repr DTC.Include
47 block :: repr DTC.Block
48 blockToC :: repr DTC.Block
49 blockToF :: repr DTC.Block
50 blockIndex :: repr DTC.Block
51 blockFigure :: repr DTC.Block
52 blockReferences :: repr DTC.Block
53 reference :: repr DTC.Reference
56 paraItem :: repr DTC.ParaItem
57 paraItems :: repr DTC.Para
59 plain :: repr DTC.Plain
60 plainNode :: repr (Tree DTC.PlainNode)
62 commonAttrs :: repr DTC.CommonAttrs
64 title :: repr DTC.Title
75 <$?> (def, Just <$> id)
76 <|?> (def, rule "class" $ attribute "class" $ TL.words <$> text)
78 document = rule "document" $
84 <$> optional (rule "about" $ element "about" about)
90 [ element "section" $ Tree <$> section <*> body
91 , tree0 . DTC.BodyBlock <$> block
100 title = rule "title" $ DTC.Title <$> element "title" plain
101 name = rule "name" $ attribute "name" text
102 url = rule "url" $ URL <$> text
103 path = rule "path" $ Path <$> text
104 ident = rule "ident" $ Ident <$> text
105 to = rule "to" $ attribute "to" ident
106 id = rule "id" $ attribute "id" ident
111 <$?> (0, attribute "year" int)
112 <|?> (Nothing, Just <$> attribute "month" nat1)
113 <|?> (Nothing, Just <$> attribute "day" nat1)
119 <$?> (def, attribute "href" path)
120 block = rule "block" $
122 [ DTC.BlockPara <$> para
129 , anyElem $ \n@XmlName{..} ->
140 <*> optional (attribute "depth" nat)
165 (wordify <$>) . TL.lines <$> text)
172 <*> attribute "type" text
176 rule "blockReferences" $
177 element "references" $
183 para = rule "para" $ paraItems <|> DTC.ParaItem <$> paraItem
187 [ element "ol" $ DTC.ParaOL <$> many (element "li" $ DTC.ListItem <$> attribute "name" text <*> many para)
188 , element "ul" $ DTC.ParaUL <$> many (element "li" $ many para)
189 , element "artwork" $ DTC.ParaArtwork <$> attribute "type" text <*> text
190 , element "quote" $ DTC.ParaQuote <$> attribute "type" text <*> many para
191 , DTC.ParaPlain . Seq.fromList <$> some plainNode
192 , DTC.ParaComment <$> comment
201 plain = rule "plain" $ (Seq.fromList <$>) $ many plainNode
205 [ tree0 . DTC.PlainText <$> text
206 , element "br" $ tree0 DTC.PlainBR <$ none
207 , element "b" $ Tree DTC.PlainB <$> plain
208 , element "code" $ Tree DTC.PlainCode <$> plain
209 , element "del" $ Tree DTC.PlainDel <$> plain
210 , element "i" $ Tree DTC.PlainI <$> plain
211 , element "q" $ Tree DTC.PlainQ <$> plain
212 , element "sc" $ Tree DTC.PlainSC <$> plain
213 , element "sub" $ Tree DTC.PlainSub <$> plain
214 , element "sup" $ Tree DTC.PlainSup <$> plain
215 , element "u" $ Tree DTC.PlainU <$> plain
216 , element "note" $ tree0 . DTC.PlainNote Nothing <$> many para
217 , element "iref" $ Tree . DTC.PlainIref Nothing . wordify <$> attribute "to" text <*> plain
218 , element "eref" $ Tree . DTC.PlainEref <$> attribute "to" url <*> plain
219 , element "ref" $ Tree . DTC.PlainRef <$> to <*> plain
220 , element "rref" $ Tree . DTC.PlainRref Nothing <$> to <*> plain
222 tag = rule "tag" $ element "tag" text
224 (foldr ($) def <$>) $
226 [ (\a acc -> acc{DTC.titles=a:DTC.titles acc}) <$> title
227 , (\a acc -> (acc::DTC.About){DTC.url=Just a}) <$> attribute "url" url
228 , (\a acc -> acc{DTC.authors=a:DTC.authors acc}) <$> author
229 , (\a acc -> acc{DTC.editor=DTC.editor acc Alt.<|> Just a}) <$> editor
230 , (\a acc -> acc{DTC.date=DTC.date acc Alt.<|> Just a}) <$> date
231 , (\a acc -> acc{DTC.tags=a:DTC.tags acc}) <$> tag
232 , (\a acc -> acc{DTC.links=a:DTC.links acc}) <$> link
233 , (\a acc -> acc{DTC.series=a:DTC.series acc}) <$> serie
234 , (\a acc -> acc{DTC.headers=a:DTC.headers acc}) <$> header
239 DTC.Header (xmlNameLocal n)
241 author = rule "author" $ element "author" entity
242 editor = rule "editor" $ element "editor" entity
243 entity = rule "entity" $
247 <|?> (def, attribute "street" text)
248 <|?> (def, attribute "zipcode" text)
249 <|?> (def, attribute "city" text)
250 <|?> (def, attribute "region" text)
251 <|?> (def, attribute "country" text)
252 <|?> (def, attribute "email" text)
253 <|?> (def, attribute "tel" text)
254 <|?> (def, attribute "fax" text)
255 <|?> (def, Just <$> attribute "url" url)
256 <|?> (def, Just <$> element "org" entity)
257 serie = rule "serie" $
262 <|?> (def, attribute "id" text)
266 (\n h r t p -> DTC.Link n h r t (Seq.fromList p))
268 <|?> (def, attribute "href" url)
269 <|?> (def, attribute "rel" text)
270 <|?> (def, Just <$> attribute "type" text)
272 alias = rule "alias" $
277 reference = rule "reference" $
278 element "reference" $
283 instance Sym_DTC RNC.Writer where
284 position = RNC.writeText ""
285 instance Sym_DTC RNC.RuleWriter where
286 position = RNC.RuleWriter position
288 -- | RNC schema for DTC
289 schema :: [RNC.RuleWriter ()]
294 , void $ rule "about" $ element "about" about
313 , void $ blockReferences