1 {-# LANGUAGE NoMonomorphismRestriction #-}
2 {-# LANGUAGE DuplicateRecordFields #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 module Hdoc.DTC.Sym where
6 import Control.Applicative (Applicative(..), (<$>), (<$))
7 import Control.Arrow (second)
8 import Control.Monad (void)
9 import Data.Bool (Bool(..))
10 import Data.Default.Class (Default(..))
11 import Data.Foldable (concat)
12 import Data.Function (($), (.), flip)
13 import Data.Maybe (Maybe(..))
14 import Data.Semigroup (Semigroup(..))
15 import Data.TreeSeq.Strict (Tree(..), tree0)
16 import qualified Data.HashMap.Strict as HM
17 import qualified Data.Sequence as Seq
18 import qualified Data.Text.Lazy as TL
21 import Hdoc.RNC.Sym as RNC
22 import qualified Hdoc.DTC.Analyze.Index as Index
23 import qualified Hdoc.DTC.Document as DTC
24 import qualified Hdoc.RNC.Write as RNC
25 import qualified Hdoc.TCT.Cell as TCT
26 import qualified Hdoc.XML as XML
29 -- | Use a symantic (tagless final) class to encode
30 -- both the parsing and the schema of DTC,
31 -- when repr is respectively instanciated
32 -- on 'DTC.Parser' or 'RNC.RuleWriter'.
33 class RNC.Sym_RNC repr => Sym_DTC repr where
34 positionXML :: repr XML.Pos
35 locationTCT :: repr TCT.Location
36 document :: repr DTC.Document
39 about :: repr DTC.About
40 header :: repr DTC.Header
44 include :: repr DTC.Include
46 block :: repr DTC.Block
47 blockBreak :: repr DTC.Block
48 blockToC :: repr DTC.Block
49 blockToF :: repr DTC.Block
50 blockIndex :: repr DTC.Block
51 blockAside :: repr DTC.Block
52 blockFigure :: repr DTC.Block
53 blockReferences :: repr DTC.Block
54 blockJudges :: repr DTC.Block
55 blockGrades :: repr DTC.Block
56 reference :: repr DTC.Reference
59 paraItem :: repr DTC.ParaItem
60 paraItems :: repr DTC.Para
62 plain :: repr DTC.Plain
63 plainNode :: repr (Tree DTC.PlainNode)
65 commonAttrs :: repr DTC.CommonAttrs
67 title :: repr DTC.Title
70 path :: repr DTC.FilePath
73 class_ :: repr [TL.Text]
75 author :: repr DTC.Entity
76 editor :: repr DTC.Entity
78 entity :: repr DTC.Entity
80 serie :: repr DTC.Serie
81 alias :: repr DTC.Alias
82 judgment :: repr DTC.Judgment
83 choice_ :: repr DTC.Choice
84 opinion :: repr DTC.Opinion
85 judges :: repr DTC.Judges
86 judge :: repr DTC.Judge
87 grade :: repr DTC.Grade
89 document = rule "document" $
96 <$?> (def, rule "about" $ element "about" about)
103 [ element "section" $ Tree . DTC.BodySection <$> section <*> body
104 , tree0 . DTC.BodyBlock <$> block
114 title = rule "title" $ DTC.Title <$> element "title" plain
115 name = rule "name" $ DTC.Name <$> text
116 url = rule "url" $ URL <$> text
117 path = rule "path" $ TL.unpack <$> text
118 ident = rule "ident" $ Ident <$> text
119 to = rule "to" $ attribute "to" ident
124 <$?> (def, Just <$> id)
126 id = rule "id" $ attribute "id" ident
127 class_ = rule "class" $ attribute "class" $ TL.words <$> text
132 <$?> (0, attribute "year" int)
133 <|?> (Nothing, Just <$> attribute "month" nat1)
134 <|?> (Nothing, Just <$> attribute "day" nat1)
140 <$?> (def, attribute "href" path)
141 block = rule "block" $
143 [ DTC.BlockPara <$> para
154 , anyElem $ \n@XmlName{..} ->
159 blockBreak = rule "break" $
169 <*> optional (attribute "depth" nat)
194 (Index.wordify <$>) . TL.lines <$> text)
207 <*> attribute "type" text
212 rule "blockReferences" $
213 element "references" $
218 blockJudges = rule "blockJudges" $ DTC.BlockJudges <$> judges
231 <*> attribute "name" name
232 <*> attribute "color" text
233 <*> option False (True <$ attribute "default" text)
237 paraItems <|> -- within a <para>
238 DTC.ParaItem <$> paraItem -- without a <para>
242 [ element "ol" $ DTC.ParaOL <$> many (element "li" $ DTC.ListItem <$> attribute "name" name <*> many para)
243 , element "ul" $ DTC.ParaUL <$> many (element "li" $ many para)
244 , element "artwork" $ DTC.ParaArtwork <$> attribute "type" text <*> text
245 , element "quote" $ DTC.ParaQuote <$> attribute "type" text <*> many para
246 , DTC.ParaPlain . Seq.fromList <$> some plainNode
247 , DTC.ParaComment <$> comment
248 , DTC.ParaJudgment <$> judgment
257 plain = rule "plain" $ (Seq.fromList <$>) $ many plainNode
261 [ tree0 . DTC.PlainText <$> text
262 , element "br" $ tree0 DTC.PlainBreak <$ none
263 , element "b" $ Tree DTC.PlainB <$> plain
264 , element "code" $ Tree DTC.PlainCode <$> plain
265 , element "del" $ Tree DTC.PlainDel <$> plain
266 , element "i" $ Tree DTC.PlainI <$> plain
267 , element "q" $ Tree DTC.PlainQ <$> plain
268 , element "sc" $ Tree DTC.PlainSC <$> plain
269 , element "span" $ Tree . DTC.PlainSpan <$> commonAttrs <*> plain
270 , element "sub" $ Tree DTC.PlainSub <$> plain
271 , element "sup" $ Tree DTC.PlainSup <$> plain
272 , element "u" $ Tree DTC.PlainU <$> plain
273 , element "note" $ tree0 . DTC.PlainNote <$> many para
274 , element "iref" $ Tree . DTC.PlainIref . Index.wordify <$> attribute "to" text <*> plain
275 , element "eref" $ Tree . DTC.PlainEref <$> attribute "to" url <*> plain
276 , element "tag" $ tree0 <$> (DTC.PlainTag <$> locationTCT <*> positionXML <*> ident)
277 , element "rref" $ Tree <$> (DTC.PlainRref <$> locationTCT <*> positionXML <*> to) <*> plain
279 tag = rule "tag" $ element "tag" text
288 <|?> (def, Just <$> attribute "url" url)
290 <|?> (def, Just <$> editor)
291 <|?> (def, Just <$> date)
295 <|?> (def, Just <$> element "description" para)
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" $
337 reference = rule "reference" $
338 element "reference" $
353 DTC.Judgment def def def
356 <||> attribute "judges" ident
357 <||> attribute "grades" ident
358 <|?> (def, Just <$> attribute "importance" rationalPositive)
359 -- <|?> (def, Just <$> attribute "importance" (pure 0))
360 <|?> (def, Just <$> title)
372 (interleaved $ DTC.Opinion
375 <|?> (def, attribute "judge" name)
376 <|?> (def, attribute "grade" name)
377 <|?> (def, Just <$> attribute "importance" rationalPositive))
378 -- <|?> (def, Just <$> attribute "importance" (pure 0)))
390 HM.fromListWith (flip (<>)) .
391 ((\j@DTC.Judge{..} -> (judge_name, pure j)) <$>)
399 <*> attribute "name" name
404 HM.fromListWith (flip (<>)) .
406 <$> many defaultGrade
411 <$> attribute "grades" ident
412 <*> attribute "grade" (DTC.Name <$> text)
414 instance Sym_DTC RNC.Writer where
415 positionXML = RNC.writeText ""
416 locationTCT = RNC.writeText ""
417 instance Sym_DTC RNC.RuleWriter where
418 positionXML = RNC.RuleWriter positionXML
419 locationTCT = RNC.RuleWriter locationTCT
421 -- | RNC schema for DTC
422 schema :: [RNC.RuleWriter ()]
427 , void $ rule "about" $ element "about" about
452 , void $ blockReferences