1 {-# LANGUAGE DuplicateRecordFields #-}
2 {-# LANGUAGE NoMonomorphismRestriction #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 {-# LANGUAGE RankNTypes #-}
5 module Hdoc.DTC.Sym where
7 import Control.Applicative (Applicative(..), (<$>), (<$))
8 import Control.Arrow (second)
9 import Control.Monad (void)
10 import Data.Bool (Bool(..))
11 import Data.Default.Class (Default(..))
12 import Data.Foldable (concat)
13 import Data.Monoid (Monoid(..))
14 import Data.Function (($), (.), flip)
15 import Data.Maybe (Maybe(..))
16 import Data.Semigroup (Semigroup(..))
17 import Data.TreeSeq.Strict (Tree(..), tree0)
18 import Language.Symantic.RNC hiding (element, attribute)
19 import Text.Blaze.DTC (xmlns_dtc)
20 import qualified Data.HashMap.Strict as HM
21 import qualified Data.Text.Lazy as TL
22 import qualified Language.Symantic.RNC as RNC
23 import qualified Language.Symantic.RNC.Write as RNC
24 import qualified Language.Symantic.XML as XML
26 import Hdoc.RNC as RNC
28 import qualified Hdoc.DTC.Analyze.Index as Index
29 import qualified Hdoc.DTC.Document as DTC
30 import qualified Hdoc.TCT.Cell as TCT
31 import qualified Hdoc.XML as XML
33 element :: RNC.Sym_RNC repr => XML.NCName -> repr a -> repr a
34 element = RNC.element . XML.QName xmlns_dtc
35 attribute :: RNC.Sym_RNC repr => XML.NCName -> repr a -> repr a
36 attribute = RNC.attribute . XML.QName ""
39 -- | Use a symantic (tagless final) class to encode
40 -- both the parsing and the schema of DTC,
41 -- when repr is respectively instanciated
42 -- on 'DTC.Parser', 'RNC.NS' or 'RNC.Writer'.
43 class (Sym_RNC repr, Sym_RNC_Extra repr) => Sym_DTC repr where
44 positionXML :: repr XML.Pos
45 locationTCT :: repr TCT.Location
46 document :: repr DTC.Document
50 section :: repr DTC.Section
51 about :: repr DTC.About
52 include :: repr DTC.Include
54 block :: repr DTC.Block
55 blockBreak :: repr DTC.Block
56 blockToC :: repr DTC.Block
57 blockToF :: repr DTC.Block
58 blockIndex :: repr DTC.Block
59 blockAside :: repr DTC.Block
60 blockFigure :: repr DTC.Block
61 blockReferences :: repr DTC.Block
62 blockJudges :: repr DTC.Block
63 blockGrades :: repr DTC.Block
64 reference :: repr DTC.Reference
67 paraItem :: repr DTC.ParaItem
68 paraItems :: repr DTC.Para
70 plain :: repr DTC.Plain
71 plainNode :: repr (Tree DTC.PlainNode)
73 commonAttrs :: repr DTC.CommonAttrs
75 title :: repr DTC.Title
78 path :: repr DTC.FilePath
81 class_ :: repr [TL.Text]
85 author :: repr DTC.Entity
87 entity :: repr DTC.Entity
89 serie :: repr DTC.Serie
90 alias :: repr DTC.Alias
91 judgment :: repr DTC.Judgment
92 choice_ :: repr DTC.Choice
93 opinion :: repr DTC.Opinion
94 judges :: repr DTC.Judges
95 judge :: repr DTC.Judge
96 grade :: repr DTC.Grade
98 document = rule "document" $
110 [ element "section" $ Tree . DTC.BodySection <$> section <*> body
111 , tree0 . DTC.BodyBlock <$> block
113 section = rule "section" $
115 (\section_posXML section_locTCT section_attrs abouts ->
116 DTC.Section{section_about=mconcat abouts, ..})
121 about = rule "about" $
129 <|*> element "tag" text
132 <|*> element "description" para
134 title = rule "title" $ DTC.Title <$> element "title" plain
135 name = rule "name" $ DTC.Name <$> text
136 url = rule "url" $ URL <$> text
137 path = rule "path" $ TL.unpack <$> text
138 ident = rule "ident" $ Ident <$> text
139 to = rule "to" $ attribute "to" ident
144 <$?> (def, Just <$> id)
146 id = rule "id" $ attribute "id" ident
147 class_ = rule "class" $ attribute "class" $ TL.words <$> text
148 rel = rule "rel" $ attribute "rel" name
149 role = rule "role" $ attribute "role" name
157 <||> attribute "year" int
158 <|?> (def, Just <$> attribute "month" nat1)
159 <|?> (def, Just <$> attribute "day" nat1)
165 <$?> (def, attribute "href" path)
166 block = rule "block" $
168 [ DTC.BlockPara <$> para
179 , anyElem $ \n@XmlName{..} ->
184 blockBreak = rule "break" $
194 <*> optional (attribute "depth" nat)
223 (Index.wordify <$>) . TL.lines <$> text
240 <*> attribute "type" text
245 rule "blockReferences" $
246 element "references" $
251 blockJudges = rule "blockJudges" $ DTC.BlockJudges <$> judges
264 <*> attribute "name" name
265 <*> attribute "color" text
266 <*> option False (True <$ attribute "default" text)
270 paraItems <|> -- within a <para>
271 DTC.ParaItem <$> paraItem -- without a <para>
275 [ element "ol" $ DTC.ParaOL <$> many (element "li" $ DTC.ListItem <$> attribute "name" name <*> many para)
276 , element "ul" $ DTC.ParaUL <$> many (element "li" $ many para)
277 , element "artwork" $ DTC.ParaArtwork <$> attribute "type" text <*> text
278 , element "quote" $ DTC.ParaQuote <$> attribute "type" text <*> many para
279 , DTC.ParaPlain <$> someSeq plainNode
280 , DTC.ParaComment <$> comment
281 , DTC.ParaJudgment <$> judgment
290 plain = rule "plain" $ manySeq plainNode
294 [ tree0 . DTC.PlainText <$> text
295 , element "br" $ tree0 DTC.PlainBreak <$ empty
296 , element "b" $ Tree DTC.PlainB <$> plain
297 , element "code" $ Tree DTC.PlainCode <$> plain
298 , element "del" $ Tree DTC.PlainDel <$> plain
299 , element "i" $ Tree DTC.PlainI <$> plain
300 , element "q" $ Tree DTC.PlainQ <$> plain
301 , element "sc" $ Tree DTC.PlainSC <$> plain
302 , element "span" $ Tree . DTC.PlainSpan <$> commonAttrs <*> plain
303 , element "sub" $ Tree DTC.PlainSub <$> plain
304 , element "sup" $ Tree DTC.PlainSup <$> plain
305 , element "u" $ Tree DTC.PlainU <$> plain
306 , element "note" $ tree0 . DTC.PlainNote <$> many para
307 , element "iref" $ Tree . DTC.PlainIref . Index.wordify <$> attribute "to" text <*> plain
308 , element "eref" $ Tree . DTC.PlainEref <$> attribute "to" url <*> plain
309 , element "tag" $ tree0 <$> (DTC.PlainTag <$> locationTCT <*> positionXML <*> to <*> pure False)
310 , element "tag-back" $ tree0 <$> (DTC.PlainTag <$> locationTCT <*> positionXML <*> to <*> pure True )
311 , element "at" $ tree0 <$> (DTC.PlainAt <$> locationTCT <*> positionXML <*> to <*> pure False)
312 , element "at-back" $ tree0 <$> (DTC.PlainAt <$> locationTCT <*> positionXML <*> to <*> pure True )
313 , element "ref" $ Tree <$> (DTC.PlainRef <$> locationTCT <*> positionXML <*> to) <*> plain
319 DTC.Header (XML.nameLocal n)
322 author = rule "author" $ element "author" entity
323 entity = rule "entity" $
328 <|?> (def, attribute "name" text)
329 <|?> (def, attribute "street" text)
330 <|?> (def, attribute "zipcode" text)
331 <|?> (def, attribute "city" text)
332 <|?> (def, attribute "region" text)
333 <|?> (def, attribute "country" text)
334 <|?> (def, attribute "email" text)
335 <|?> (def, attribute "tel" text)
336 <|?> (def, attribute "fax" text)
337 <|?> (def, Just <$> attribute "url" url)
338 <|*> element "org" entity
339 serie = rule "serie" $
343 <$?> (def, attribute "name" name)
344 <|?> (def, attribute "id" text)
351 <|?> (def, attribute "url" url)
352 <|?> (def, someSeq plainNode)
353 alias = rule "alias" $
358 reference = rule "reference" $
359 element "reference" $
374 DTC.Judgment def -- def def
377 <||> attribute "judges" ident
378 <||> attribute "grades" ident
379 <|?> (def, Just <$> attribute "importance" rationalPositive)
380 <|?> (def, Just <$> attribute "hide" bool)
381 -- <|?> (def, Just <$> attribute "importance" (pure 0))
382 <|?> (def, Just <$> title)
394 (interleaved $ DTC.Opinion
397 <|?> (def, attribute "judge" name)
398 <|?> (def, attribute "grade" name)
399 <|?> (def, Just <$> attribute "default" name)
400 <|?> (def, Just <$> attribute "importance" rationalPositive))
401 -- <|?> (def, Just <$> attribute "importance" (pure 0)))
413 HM.fromListWith (flip (<>)) .
414 ((\j@DTC.Judge{..} -> (judge_name, pure j)) <$>)
422 <*> attribute "name" name
427 HM.fromListWith (flip (<>)) .
429 <$> many defaultGrade
434 <$> attribute "grades" ident
435 <*> attribute "grade" (DTC.Name <$> text)
437 instance Sym_DTC RNC.NS where
440 instance Sym_DTC RNC.Writer where
441 positionXML = RNC.writeText ""
442 locationTCT = RNC.writeText ""
444 -- | RNC schema for DTC
445 schema :: forall repr. Sym_DTC repr => [repr ()]
447 [ void $ RNC.namespace Nothing xmlns_dtc
474 , void $ blockReferences