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 blockBreak :: repr DTC.Block
49 blockToC :: repr DTC.Block
50 blockToF :: repr DTC.Block
51 blockIndex :: repr DTC.Block
52 blockFigure :: repr DTC.Block
53 blockReferences :: repr DTC.Block
54 reference :: repr DTC.Reference
57 paraItem :: repr DTC.ParaItem
58 paraItems :: repr DTC.Para
60 plain :: repr DTC.Plain
61 plainNode :: repr (Tree DTC.PlainNode)
63 commonAttrs :: repr DTC.CommonAttrs
65 title :: repr DTC.Title
76 <$?> (def, Just <$> id)
77 <|?> (def, rule "class" $ attribute "class" $ TL.words <$> text)
79 document = rule "document" $
85 <$> optional (rule "about" $ element "about" about)
91 [ element "section" $ Tree <$> section <*> body
92 , tree0 . DTC.BodyBlock <$> block
101 title = rule "title" $ DTC.Title <$> element "title" plain
102 name = rule "name" $ attribute "name" text
103 url = rule "url" $ URL <$> text
104 path = rule "path" $ Path <$> text
105 ident = rule "ident" $ Ident <$> text
106 to = rule "to" $ attribute "to" ident
107 id = rule "id" $ attribute "id" ident
112 <$?> (0, attribute "year" int)
113 <|?> (Nothing, Just <$> attribute "month" nat1)
114 <|?> (Nothing, Just <$> attribute "day" nat1)
120 <$?> (def, attribute "href" path)
121 block = rule "block" $
123 [ DTC.BlockPara <$> para
131 , anyElem $ \n@XmlName{..} ->
136 blockBreak = rule "break" $
146 <*> optional (attribute "depth" nat)
171 (wordify <$>) . TL.lines <$> text)
178 <*> attribute "type" text
182 rule "blockReferences" $
183 element "references" $
189 para = rule "para" $ paraItems <|> DTC.ParaItem <$> paraItem
193 [ element "ol" $ DTC.ParaOL <$> many (element "li" $ DTC.ListItem <$> attribute "name" text <*> many para)
194 , element "ul" $ DTC.ParaUL <$> many (element "li" $ many para)
195 , element "artwork" $ DTC.ParaArtwork <$> attribute "type" text <*> text
196 , element "quote" $ DTC.ParaQuote <$> attribute "type" text <*> many para
197 , DTC.ParaPlain . Seq.fromList <$> some plainNode
198 , DTC.ParaComment <$> comment
207 plain = rule "plain" $ (Seq.fromList <$>) $ many plainNode
211 [ tree0 . DTC.PlainText <$> text
212 , element "br" $ tree0 DTC.PlainBreak <$ none
213 , element "b" $ Tree DTC.PlainB <$> plain
214 , element "code" $ Tree DTC.PlainCode <$> plain
215 , element "del" $ Tree DTC.PlainDel <$> plain
216 , element "i" $ Tree DTC.PlainI <$> plain
217 , element "q" $ Tree DTC.PlainQ <$> plain
218 , element "sc" $ Tree DTC.PlainSC <$> plain
219 , element "sub" $ Tree DTC.PlainSub <$> plain
220 , element "sup" $ Tree DTC.PlainSup <$> plain
221 , element "u" $ Tree DTC.PlainU <$> plain
222 , element "note" $ tree0 . DTC.PlainNote Nothing <$> many para
223 , element "iref" $ Tree . DTC.PlainIref Nothing . wordify <$> attribute "to" text <*> plain
224 , element "eref" $ Tree . DTC.PlainEref <$> attribute "to" url <*> plain
225 , element "ref" $ Tree . DTC.PlainRef <$> to <*> plain
226 , element "rref" $ Tree . DTC.PlainRref Nothing <$> to <*> plain
228 tag = rule "tag" $ element "tag" text
230 (foldr ($) def <$>) $
232 [ (\a acc -> acc{DTC.titles=a:DTC.titles acc}) <$> title
233 , (\a acc -> (acc::DTC.About){DTC.url=Just a}) <$> attribute "url" url
234 , (\a acc -> acc{DTC.authors=a:DTC.authors acc}) <$> author
235 , (\a acc -> acc{DTC.editor=DTC.editor acc Alt.<|> Just a}) <$> editor
236 , (\a acc -> acc{DTC.date=DTC.date acc Alt.<|> Just a}) <$> date
237 , (\a acc -> acc{DTC.tags=a:DTC.tags acc}) <$> tag
238 , (\a acc -> acc{DTC.links=a:DTC.links acc}) <$> link
239 , (\a acc -> acc{DTC.series=a:DTC.series acc}) <$> serie
240 , (\a acc -> acc{DTC.headers=a:DTC.headers acc}) <$> header
245 DTC.Header (xmlNameLocal n)
247 author = rule "author" $ element "author" entity
248 editor = rule "editor" $ element "editor" entity
249 entity = rule "entity" $
253 <|?> (def, attribute "street" text)
254 <|?> (def, attribute "zipcode" text)
255 <|?> (def, attribute "city" text)
256 <|?> (def, attribute "region" text)
257 <|?> (def, attribute "country" text)
258 <|?> (def, attribute "email" text)
259 <|?> (def, attribute "tel" text)
260 <|?> (def, attribute "fax" text)
261 <|?> (def, Just <$> attribute "url" url)
262 <|?> (def, Just <$> element "org" entity)
263 serie = rule "serie" $
268 <|?> (def, attribute "id" text)
272 (\n h r t p -> DTC.Link n h r t (Seq.fromList p))
274 <|?> (def, attribute "href" url)
275 <|?> (def, attribute "rel" text)
276 <|?> (def, Just <$> attribute "type" text)
278 alias = rule "alias" $
283 reference = rule "reference" $
284 element "reference" $
289 instance Sym_DTC RNC.Writer where
290 position = RNC.writeText ""
291 instance Sym_DTC RNC.RuleWriter where
292 position = RNC.RuleWriter position
294 -- | RNC schema for DTC
295 schema :: [RNC.RuleWriter ()]
300 , void $ rule "about" $ element "about" about
319 , void $ blockReferences