1 {-# LANGUAGE NoMonomorphismRestriction #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 module Language.DTC.Sym where
5 import Control.Applicative (Applicative(..), (<$>), (<$))
6 import Control.Monad (void)
7 import Data.Default.Class (Default(..))
8 import Data.Foldable (Foldable(..), concat)
9 import Data.Function (($), (.), flip)
10 import Data.Maybe (Maybe(..), maybe)
11 import Data.TreeSeq.Strict (Tree(..), tree0)
12 import qualified Data.Sequence as Seq
13 import qualified Data.Text.Lazy as TL
16 import Language.RNC.Sym as RNC
17 import Language.DTC.Anchor (wordify)
18 import qualified Language.DTC.Document as DTC
19 import qualified Language.RNC.Write as RNC
21 foldlApp :: (Default a, Foldable t) => t (a -> a) -> a
22 foldlApp = foldl' (flip ($)) def
23 foldrApp :: (Default a, Foldable t) => t (a -> a) -> a
24 foldrApp = foldr ($) def
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 position :: repr DTC.Pos
33 document :: repr DTC.Document
36 about :: repr DTC.About
37 keyword :: repr TL.Text
38 version :: repr MayText
39 author :: repr DTC.Entity
40 editor :: repr DTC.Entity
42 entity :: repr DTC.Entity
44 serie :: repr DTC.Serie
45 alias :: repr DTC.Alias
48 bodyValue :: repr DTC.BodyNode
49 toc :: repr DTC.BodyNode
50 tof :: repr DTC.BodyNode
51 index :: repr DTC.BodyNode
52 figure :: repr DTC.BodyNode
53 references :: repr DTC.BodyNode
54 reference :: repr DTC.Reference
55 include :: repr DTC.Include
57 block :: repr DTC.Block
59 lines :: repr DTC.Lines
61 commonAttrs :: repr DTC.CommonAttrs
63 title :: repr DTC.Title
74 <$?> (def, Just <$> id)
75 <|?> (def, rule "class" $ attribute "class" $ TL.words <$> text)
77 document = rule "document" $
83 <$> optional (rule "about" $ element "about" about)
89 [ element "section" $ Tree <$> section <*> body
106 , DTC.Block <$> block
108 title = rule "title" $ DTC.Title <$> element "title" para
109 name = rule "name" $ attribute "name" text
110 url = rule "url" $ URL <$> text
111 path = rule "path" $ Path <$> text
112 ident = rule "ident" $ Ident <$> text
113 to = rule "to" $ attribute "to" ident
114 id = rule "id" $ attribute "id" ident
119 <$?> (0, attribute "year" int)
120 <|?> (Nothing, Just <$> attribute "month" nat1)
121 <|?> (Nothing, Just <$> attribute "day" nat1)
122 include = rule "include" $
126 <$?> (def, attribute "href" path)
127 block = rule "block" $
129 [ DTC.Comment <$> comment
139 <*> many (element "li" $ many block)
144 <*> many (element "li" $ many block)
145 , element "artwork" $
149 <*> attribute "type" text
155 <*> attribute "type" text
158 , anyElem $ \n@XmlName{..} ->
169 <*> optional (attribute "depth" nat)
194 (wordify <$>) . TL.lines <$> text)
201 <*> attribute "type" text
205 element "references" $
210 para = rule "para" $ (Seq.fromList <$>) $ many lines
214 [ element "b" $ Tree DTC.B <$> para
215 , element "code" $ Tree DTC.Code <$> para
216 , element "del" $ Tree DTC.Del <$> para
217 , element "i" $ Tree DTC.I <$> para
218 , element "note" $ Tree (DTC.Note Nothing) <$> para
219 , element "q" $ Tree DTC.Q <$> para
220 , element "sc" $ Tree DTC.SC <$> para
221 , element "sub" $ Tree DTC.Sub <$> para
222 , element "sup" $ Tree DTC.Sup <$> para
223 , element "u" $ Tree DTC.U <$> para
224 , element "eref" $ Tree . DTC.Eref <$> attribute "to" url <*> para
225 , element "iref" $ Tree . DTC.Iref Nothing . wordify <$> attribute "to" text <*> para
226 , element "ref" $ Tree . DTC.Ref <$> to <*> para
227 , element "rref" $ Tree . DTC.Rref Nothing <$> to <*> para
228 , element "br" $ tree0 DTC.BR <$ none
229 , tree0 . DTC.Plain <$> text
231 keyword = rule "keyword" $
232 element "keyword" text
233 version = rule "version" $
235 element "version" text
240 <|?> (def, Just <$> attribute "url" url)
242 <|?> (Nothing, Just <$> editor)
243 <|?> (Nothing, Just <$> date)
249 author = rule "author" $ element "author" entity
250 editor = rule "editor" $ element "editor" entity
251 entity = rule "entity" $
255 <|?> (def, attribute "street" text)
256 <|?> (def, attribute "zipcode" text)
257 <|?> (def, attribute "city" text)
258 <|?> (def, attribute "region" text)
259 <|?> (def, attribute "country" text)
260 <|?> (def, attribute "email" text)
261 <|?> (def, attribute "tel" text)
262 <|?> (def, attribute "fax" text)
263 <|?> (def, Just <$> attribute "url" url)
264 <|?> (def, Just <$> attribute "org" entity)
265 serie = rule "serie" $
270 <|?> (def, attribute "key" text)
274 (\n h r ls -> DTC.Link n h r (Seq.fromList ls))
276 <|?> (def, attribute "href" url)
277 <|?> (def, attribute "rel" text)
279 alias = rule "alias" $
284 reference = rule "reference" $
285 element "reference" $
290 instance Sym_DTC RNC.Writer where
291 position = RNC.writeText ""
292 instance Sym_DTC RNC.RuleWriter where
293 position = RNC.RuleWriter position
295 -- | RNC schema for DTC
296 schema :: [RNC.RuleWriter ()]
301 , void $ rule "about" $ element "about" about