1 {-# LANGUAGE NoMonomorphismRestriction #-}
2 {-# LANGUAGE DuplicateRecordFields #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 module Hdoc.DTC.Sym where
6 import Data.Bool (Bool(..))
7 import Control.Applicative (Applicative(..), (<$>), (<$))
8 import Control.Monad (void)
9 import Data.Default.Class (Default(..))
10 import Data.Foldable (Foldable(..), concat)
11 import Data.Function (($), (.))
12 import Data.Maybe (Maybe(..))
13 import Data.TreeSeq.Strict (Tree(..), tree0)
14 import qualified Control.Applicative as Alt
15 import qualified Data.Sequence as Seq
16 import qualified Data.Text.Lazy as TL
19 import Hdoc.RNC.Sym as RNC
20 import Hdoc.DTC.Index (wordify)
21 import qualified Hdoc.DTC.Document as DTC
22 import qualified Hdoc.RNC.Write as RNC
23 import qualified Hdoc.TCT.Cell as TCT
24 import qualified Hdoc.XML as XML
27 -- | Use a symantic (tagless final) class to encode
28 -- both the parsing and the schema of DTC,
29 -- when repr is respectively instanciated
30 -- on 'DTC.Parser' or 'RNC.RuleWriter'.
31 class RNC.Sym_RNC repr => Sym_DTC repr where
32 posXML :: repr XML.Pos
33 locationTCT :: repr TCT.Location
34 document :: repr DTC.Document
37 about :: repr DTC.About
38 header :: repr DTC.Header
42 include :: repr DTC.Include
44 block :: repr DTC.Block
45 blockBreak :: repr DTC.Block
46 blockToC :: repr DTC.Block
47 blockToF :: repr DTC.Block
48 blockIndex :: repr DTC.Block
49 blockAside :: repr DTC.Block
50 blockFigure :: repr DTC.Block
51 blockReferences :: repr DTC.Block
52 blockJudges :: repr DTC.Block
53 blockGrades :: 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
68 path :: repr DTC.FilePath
71 class_ :: repr [TL.Text]
73 author :: repr DTC.Entity
74 editor :: repr DTC.Entity
76 entity :: repr DTC.Entity
78 serie :: repr DTC.Serie
79 alias :: repr DTC.Alias
80 judgment :: repr DTC.Judgment
81 choice_ :: repr DTC.Choice
82 opinion :: repr DTC.Opinion
83 judge :: repr DTC.Judge
84 grade :: repr DTC.Grade
86 document = rule "document" $
93 <$?> (def, rule "about" $ element "about" about)
100 [ element "section" $ Tree . DTC.BodySection <$> section <*> body
101 , tree0 . DTC.BodyBlock <$> block
111 title = rule "title" $ DTC.Title <$> element "title" plain
112 name = rule "name" $ DTC.Name <$> text
113 url = rule "url" $ URL <$> text
114 path = rule "path" $ TL.unpack <$> text
115 ident = rule "ident" $ Ident <$> text
116 to = rule "to" $ attribute "to" ident
121 <$?> (def, Just <$> id)
123 id = rule "id" $ attribute "id" ident
124 class_ = rule "class" $ attribute "class" $ TL.words <$> text
129 <$?> (0, attribute "year" int)
130 <|?> (Nothing, Just <$> attribute "month" nat1)
131 <|?> (Nothing, Just <$> attribute "day" nat1)
137 <$?> (def, attribute "href" path)
138 block = rule "block" $
140 [ DTC.BlockPara <$> para
151 , anyElem $ \n@XmlName{..} ->
156 blockBreak = rule "break" $
166 <*> optional (attribute "depth" nat)
191 (wordify <$>) . TL.lines <$> text)
204 <*> attribute "type" text
209 rule "blockReferences" $
210 element "references" $
234 <*> attribute "name" name
235 <*> attribute "color" text
236 <*> option False (True <$ attribute "default" text)
240 paraItems <|> -- within a <para>
241 DTC.ParaItem <$> paraItem -- without a <para>
245 [ element "ol" $ DTC.ParaOL <$> many (element "li" $ DTC.ListItem <$> attribute "name" name <*> many para)
246 , element "ul" $ DTC.ParaUL <$> many (element "li" $ many para)
247 , element "artwork" $ DTC.ParaArtwork <$> attribute "type" text <*> text
248 , element "quote" $ DTC.ParaQuote <$> attribute "type" text <*> many para
249 , DTC.ParaPlain . Seq.fromList <$> some plainNode
250 , DTC.ParaComment <$> comment
251 , DTC.ParaJudgment <$> judgment
260 plain = rule "plain" $ (Seq.fromList <$>) $ many plainNode
264 [ tree0 . DTC.PlainText <$> text
265 , element "br" $ tree0 DTC.PlainBreak <$ none
266 , element "b" $ Tree DTC.PlainB <$> plain
267 , element "code" $ Tree DTC.PlainCode <$> plain
268 , element "del" $ Tree DTC.PlainDel <$> plain
269 , element "i" $ Tree DTC.PlainI <$> plain
270 , element "q" $ Tree DTC.PlainQ <$> plain
271 , element "sc" $ Tree DTC.PlainSC <$> plain
272 , element "span" $ Tree . DTC.PlainSpan <$> commonAttrs <*> plain
273 , element "sub" $ Tree DTC.PlainSub <$> plain
274 , element "sup" $ Tree DTC.PlainSup <$> plain
275 , element "u" $ Tree DTC.PlainU <$> plain
276 , element "note" $ tree0 . DTC.PlainNote Nothing <$> many para
277 , element "iref" $ Tree . DTC.PlainIref Nothing . wordify <$> attribute "to" text <*> plain
278 , element "eref" $ Tree . DTC.PlainEref <$> attribute "to" url <*> plain
279 , element "tag" $ Tree . DTC.PlainTag def <$> locationTCT <*> plain
280 , element "rref" $ Tree <$> (DTC.PlainRref Nothing Nothing <$> locationTCT <*> to) <*> plain
282 tag = rule "tag" $ element "tag" text
284 (foldr ($) def <$>) $
286 [ (\a acc -> acc{DTC.titles=a:DTC.titles acc}) <$> title
287 , (\a acc -> (acc::DTC.About){DTC.url=Just a}) <$> attribute "url" url
288 , (\a acc -> acc{DTC.authors=a:DTC.authors acc}) <$> author
289 , (\a acc -> acc{DTC.editor=DTC.editor acc Alt.<|> Just a}) <$> editor
290 , (\a acc -> acc{DTC.date=DTC.date acc Alt.<|> Just a}) <$> date
291 , (\a acc -> acc{DTC.tags=a:DTC.tags acc}) <$> tag
292 , (\a acc -> acc{DTC.links=a:DTC.links acc}) <$> link
293 , (\a acc -> acc{DTC.series=a:DTC.series acc}) <$> serie
294 , (\a acc -> acc{DTC.headers=a:DTC.headers acc}) <$> header
299 DTC.Header (XML.nameLocal n)
301 author = rule "author" $ element "author" entity
302 editor = rule "editor" $ element "editor" entity
303 entity = rule "entity" $
306 <$?> (def, attribute "name" text)
307 <|?> (def, attribute "street" text)
308 <|?> (def, attribute "zipcode" text)
309 <|?> (def, attribute "city" text)
310 <|?> (def, attribute "region" text)
311 <|?> (def, attribute "country" text)
312 <|?> (def, attribute "email" text)
313 <|?> (def, attribute "tel" text)
314 <|?> (def, attribute "fax" text)
315 <|?> (def, Just <$> attribute "url" url)
316 <|?> (def, Just <$> element "org" entity)
317 serie = rule "serie" $
321 <$?> (def, attribute "name" name)
322 <|?> (def, attribute "id" text)
326 (\n h r t p -> DTC.Link n h r t (Seq.fromList p))
327 <$?> (def, attribute "name" name)
328 <|?> (def, attribute "href" url)
329 <|?> (def, attribute "rel" text)
330 <|?> (def, Just <$> attribute "type" text)
332 alias = rule "alias" $
336 reference = rule "reference" $
337 element "reference" $
338 DTC.Reference Nothing
353 <$$> attribute "judges" ident
354 <||> attribute "grades" ident
355 <|?> (def, Just <$> attribute "importance" rationalPositive)
356 <|?> (def, Just <$> title)
366 (interleaved $ DTC.Opinion
367 <$?> (def, attribute "judge" name)
368 <|?> (def, attribute "grade" name)
369 <|?> (def, Just <$> attribute "importance" rationalPositive))
375 <$> attribute "name" name
377 <*> many defaultGrade
383 <$> attribute "grades" ident
384 <*> attribute "grade" (DTC.Name <$> text)
386 instance Sym_DTC RNC.Writer where
387 posXML = RNC.writeText ""
388 locationTCT = RNC.writeText ""
389 instance Sym_DTC RNC.RuleWriter where
390 posXML = RNC.RuleWriter posXML
391 locationTCT = RNC.RuleWriter locationTCT
393 -- | RNC schema for DTC
394 schema :: [RNC.RuleWriter ()]
399 , void $ rule "about" $ element "about" about
423 , void $ blockReferences