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 (($), (.), flip)
11 import Data.Maybe (Maybe(..), maybe)
12 import Data.TreeSeq.Strict (Tree(..), tree0)
13 import qualified Data.Sequence as Seq
14 import qualified Data.Text.Lazy as TL
17 import Language.RNC.Sym as RNC
18 import Language.DTC.Anchor (wordify)
19 import qualified Language.DTC.Document as DTC
20 import qualified Language.RNC.Write as RNC
22 foldlApp :: (Default a, Foldable t) => t (a -> a) -> a
23 foldlApp = foldl' (flip ($)) def
24 foldrApp :: (Default a, Foldable t) => t (a -> a) -> a
25 foldrApp = foldr ($) def
28 -- | Use a symantic (tagless final) class to encode
29 -- both the parsing and the schema of DTC,
30 -- when repr is respectively instanciated
31 -- on 'DTC.Parser' or 'RNC.RuleWriter'.
32 class RNC.Sym_RNC repr => Sym_DTC repr where
33 position :: repr DTC.Pos
34 document :: repr DTC.Document
37 about :: repr DTC.About
38 header :: repr DTC.Header
40 author :: repr DTC.Entity
41 editor :: repr DTC.Entity
43 entity :: repr DTC.Entity
45 serie :: repr DTC.Serie
46 alias :: repr DTC.Alias
49 include :: repr DTC.Include
51 block :: repr DTC.Block
52 blockToC :: repr DTC.Block
53 blockToF :: repr DTC.Block
54 blockIndex :: repr DTC.Block
55 blockFigure :: repr DTC.Block
56 blockReferences :: repr DTC.Block
57 reference :: repr DTC.Reference
60 paraItem :: repr DTC.ParaItem
61 paraItems :: repr DTC.Para
63 plain :: repr DTC.Plain
64 plainNode :: repr (Tree DTC.PlainNode)
66 commonAttrs :: repr DTC.CommonAttrs
68 title :: repr DTC.Title
79 <$?> (def, Just <$> id)
80 <|?> (def, rule "class" $ attribute "class" $ TL.words <$> text)
82 document = rule "document" $
88 <$> optional (rule "about" $ element "about" about)
94 [ element "section" $ Tree <$> section <*> body
95 , tree0 . DTC.BodyBlock <$> block
104 title = rule "title" $ DTC.Title <$> element "title" plain
105 name = rule "name" $ attribute "name" text
106 url = rule "url" $ URL <$> text
107 path = rule "path" $ Path <$> text
108 ident = rule "ident" $ Ident <$> text
109 to = rule "to" $ attribute "to" ident
110 id = rule "id" $ attribute "id" ident
115 <$?> (0, attribute "year" int)
116 <|?> (Nothing, Just <$> attribute "month" nat1)
117 <|?> (Nothing, Just <$> attribute "day" nat1)
123 <$?> (def, attribute "href" path)
124 block = rule "block" $
126 [ DTC.BlockPara <$> para
133 , anyElem $ \n@XmlName{..} ->
144 <*> optional (attribute "depth" nat)
169 (wordify <$>) . TL.lines <$> text)
176 <*> attribute "type" text
180 rule "blockReferences" $
181 element "references" $
187 para = rule "para" $ paraItems <|> DTC.ParaItem <$> paraItem
191 [ element "ol" $ DTC.ParaOL <$> many (element "li" $ DTC.ListItem <$> attribute "name" text <*> many para)
192 , element "ul" $ DTC.ParaUL <$> many (element "li" $ many para)
193 , element "artwork" $ DTC.ParaArtwork <$> attribute "type" text <*> text
194 , element "quote" $ DTC.ParaQuote <$> attribute "type" text <*> many para
195 , DTC.ParaPlain . Seq.fromList <$> some plainNode
196 , DTC.ParaComment <$> comment
205 plain = rule "plain" $ (Seq.fromList <$>) $ many plainNode
209 [ tree0 . DTC.PlainText <$> text
210 , element "br" $ tree0 DTC.PlainBR <$ none
211 , element "b" $ Tree DTC.PlainB <$> plain
212 , element "code" $ Tree DTC.PlainCode <$> plain
213 , element "del" $ Tree DTC.PlainDel <$> plain
214 , element "i" $ Tree DTC.PlainI <$> plain
215 , element "q" $ Tree DTC.PlainQ <$> plain
216 , element "sc" $ Tree DTC.PlainSC <$> plain
217 , element "sub" $ Tree DTC.PlainSub <$> plain
218 , element "sup" $ Tree DTC.PlainSup <$> plain
219 , element "u" $ Tree DTC.PlainU <$> plain
220 , element "note" $ tree0 . DTC.PlainNote Nothing <$> many para
221 , element "iref" $ Tree . DTC.PlainIref Nothing . wordify <$> attribute "to" text <*> plain
222 , element "eref" $ Tree . DTC.PlainEref <$> attribute "to" url <*> plain
223 , element "ref" $ Tree . DTC.PlainRef <$> to <*> plain
224 , element "rref" $ Tree . DTC.PlainRref Nothing <$> to <*> plain
226 tag = rule "tag" $ element "tag" text
228 (foldl' (flip ($)) def <$>) $
230 [ (\a acc -> acc{DTC.titles=a:DTC.titles acc}) <$> title
231 , (\a acc -> (acc::DTC.About){DTC.url=Just a}) <$> attribute "url" url
232 , (\a acc -> acc{DTC.authors=a:DTC.authors acc}) <$> author
233 , (\a acc -> acc{DTC.editor=Just a}) <$> editor
234 , (\a acc -> acc{DTC.date=Just a}) <$> date
235 , (\a acc -> acc{DTC.tags=a:DTC.tags acc}) <$> tag
236 , (\a acc -> acc{DTC.links=a:DTC.links acc}) <$> link
237 , (\a acc -> acc{DTC.series=a:DTC.series acc}) <$> serie
238 , (\a acc -> acc{DTC.headers=a:DTC.headers acc}) <$> header
243 DTC.Header (xmlNameLocal n)
245 author = rule "author" $ element "author" entity
246 editor = rule "editor" $ element "editor" entity
247 entity = rule "entity" $
251 <|?> (def, attribute "street" text)
252 <|?> (def, attribute "zipcode" text)
253 <|?> (def, attribute "city" text)
254 <|?> (def, attribute "region" text)
255 <|?> (def, attribute "country" text)
256 <|?> (def, attribute "email" text)
257 <|?> (def, attribute "tel" text)
258 <|?> (def, attribute "fax" text)
259 <|?> (def, Just <$> attribute "url" url)
260 <|?> (def, Just <$> element "org" entity)
261 serie = rule "serie" $
266 <|?> (def, attribute "id" text)
270 (\n h r t p -> DTC.Link n h r t (Seq.fromList p))
272 <|?> (def, attribute "href" url)
273 <|?> (def, attribute "rel" text)
274 <|?> (def, Just <$> attribute "type" text)
276 alias = rule "alias" $
281 reference = rule "reference" $
282 element "reference" $
287 instance Sym_DTC RNC.Writer where
288 position = RNC.writeText ""
289 instance Sym_DTC RNC.RuleWriter where
290 position = RNC.RuleWriter position
292 -- | RNC schema for DTC
293 schema :: [RNC.RuleWriter ()]
298 , void $ rule "about" $ element "about" about
317 , void $ blockReferences