]> Git — Sourcephile - doclang.git/blob - Language/DTC/Document.hs
Fix ToF ordering.
[doclang.git] / Language / DTC / Document.hs
1 {-# LANGUAGE DisambiguateRecordFields #-}
2 {-# LANGUAGE DuplicateRecordFields #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
5 module Language.DTC.Document
6 ( module Language.DTC.Document
7 , module Language.XML
8 ) where
9
10 import Data.Default.Class (Default(..))
11 import Data.Default.Instances.Containers ()
12 import Data.Eq (Eq)
13 import Data.Function (on)
14 import Data.Int (Int)
15 import Data.Map.Strict (Map)
16 import Data.Maybe (Maybe(..))
17 import Data.Monoid (Monoid(..))
18 import Data.Ord (Ord(..))
19 import Data.Semigroup (Semigroup(..))
20 import Data.Sequence (Seq)
21 import Data.Text (Text)
22 import Data.TreeSeq.Strict (Tree, Trees)
23 import Text.Show (Show)
24
25 import Language.XML
26
27 -- * Type 'Document'
28 data Document
29 = Document
30 { head :: Head
31 , body :: Body
32 } deriving (Eq,Show)
33 instance Default Document where
34 def = Document
35 { head = def
36 , body = mempty
37 }
38
39 -- * Type 'Head'
40 data Head
41 = Head
42 { about :: About
43 } deriving (Eq,Show)
44 instance Default Head where
45 def = Head
46 { about = def
47 }
48
49 -- ** Type 'About'
50 data About
51 = About
52 { titles :: [Title]
53 , authors :: [Entity]
54 , editor :: Maybe Entity
55 , date :: Maybe Date
56 , version :: MayText
57 , keywords :: [Text]
58 , links :: [Link]
59 , series :: [Serie]
60 , includes :: [Include]
61 } deriving (Eq,Show)
62 instance Default About where
63 def = About
64 { includes = def
65 , titles = def
66 , date = def
67 , version = def
68 , editor = def
69 , authors = def
70 , keywords = def
71 , links = def
72 , series = def
73 }
74 instance Semigroup About where
75 x <> y = About
76 { titles = titles x <> titles y
77 , authors = authors x <> authors y
78 , editor = editor x <> editor y
79 , date = date x <> date y
80 , version = version x <> version y
81 , keywords = keywords x <> keywords y
82 , links = links x <> links y
83 , series = series x <> series y
84 , includes = includes x <> includes y
85 }
86
87 -- * Type 'Body'
88 type Body = Trees BodyKey BodyValue
89
90 -- ** Type 'BodyKey'
91 data BodyKey
92 = Section { pos :: Pos
93 , attrs :: CommonAttrs
94 , title :: Title
95 , aliases :: [Alias]
96 }
97 deriving (Eq,Show)
98
99 -- ** Type 'BodyValue'
100 data BodyValue
101 = ToC { pos :: Pos
102 , attrs :: CommonAttrs
103 , depth :: Maybe Nat
104 }
105 | ToF { pos :: Pos
106 , attrs :: CommonAttrs
107 , types :: [Text]
108 }
109 | Figure { pos :: Pos
110 , attrs :: CommonAttrs
111 , type_ :: Text
112 , title :: Title
113 , blocks :: Blocks
114 }
115 | Index { pos :: Pos
116 , attrs :: CommonAttrs
117 , terms :: Terms
118 }
119 | Block Block
120 deriving (Eq,Show)
121
122 -- ** Type 'Pos'
123 data Pos
124 = Pos
125 { posAncestors :: PosPath
126 , posAncestorsWithFigureNames :: PosPath
127 , posPrecedingsSiblings :: Map XmlName Rank
128 } deriving (Eq,Show)
129 instance Ord Pos where
130 compare = compare `on` posAncestors
131 instance Default Pos where
132 def = Pos mempty mempty mempty
133
134 -- *** Type 'PosPath'
135 type PosPath = Seq (XmlName,Rank)
136
137 -- ** Type 'Word'
138 type Word = Text
139
140 -- *** Type 'Words'
141 type Words = [WordOrSpace]
142
143 -- **** Type 'WordOrSpace'
144 data WordOrSpace
145 = Word Word
146 | Space
147 deriving (Eq,Ord,Show)
148
149 -- ** Type 'Aliases'
150 type Aliases = [Words]
151
152 -- ** Type 'Terms'
153 type Terms = [Aliases]
154
155 -- * Type 'Count'
156 type Count = Int
157
158 -- * Type 'Block'
159 data Block
160 = Para { pos :: Pos
161 , attrs :: CommonAttrs
162 , para :: Para
163 }
164 | OL { pos :: Pos
165 , attrs :: CommonAttrs
166 , items :: [Blocks]
167 }
168 | UL { pos :: Pos
169 , attrs :: CommonAttrs
170 , items :: [Blocks]
171 }
172 | RL { pos :: Pos
173 , attrs :: CommonAttrs
174 , refs :: [Reference]
175 }
176 | Artwork { pos :: Pos
177 , attrs :: CommonAttrs
178 , art :: Artwork
179 }
180 | Comment Text
181 deriving (Eq,Show)
182
183 -- * Type 'CommonAttrs'
184 data CommonAttrs
185 = CommonAttrs
186 { id :: Maybe Ident
187 , classes :: [Text]
188 } deriving (Eq,Show)
189
190 -- * Type 'Auto'
191 data Auto
192 = Auto
193 { auto_id :: Ident
194 } deriving (Eq,Show)
195
196 -- * Type 'Blocks'
197 type Blocks = [Block]
198
199 -- * Type 'Artwork'
200 data Artwork
201 = Raw Text
202 deriving (Eq,Show)
203
204 -- * Type 'Para'
205 type Para = Seq Lines
206
207 -- * Type 'Lines'
208 type Lines = Tree LineKey LineValue
209
210 -- ** Type 'LineKey'
211 data LineKey
212 = B
213 | Code
214 | Del
215 | I
216 | Note
217 | Q
218 | SC
219 | Sub
220 | Sup
221 | U
222 | Eref {href :: URL}
223 | Iref {count :: Int, term :: Words}
224 | Ref {to :: Ident}
225 | Rref {to :: Ident}
226 deriving (Eq,Show)
227
228 -- ** Type 'LineValue'
229 data LineValue
230 = BR
231 | Plain Text
232 deriving (Eq,Show)
233
234 -- * Type 'Title'
235 newtype Title = Title { unTitle :: Para }
236 deriving (Eq,Show,Default)
237
238 -- ** Type 'Address'
239 data Address
240 = Address
241 { street :: Text
242 , zipcode :: Text
243 , city :: Text
244 , region :: Text
245 , country :: Text
246 , email :: Text
247 , tel :: Text
248 , fax :: Text
249 } deriving (Eq,Show)
250 instance Default Address where
251 def = Address
252 { street = def
253 , zipcode = def
254 , city = def
255 , region = def
256 , country = def
257 , email = def
258 , tel = def
259 , fax = def
260 }
261
262 -- * Type 'Include'
263 data Include
264 = Include
265 { href :: Path
266 } deriving (Eq,Show)
267 instance Default Include where
268 def = Include
269 { href = def
270 }
271
272 -- * Type 'Reference'
273 data Reference
274 = Reference
275 { id :: Ident
276 , to :: Maybe URL
277 , about :: About
278 } deriving (Eq,Show)
279 reference :: Ident -> Reference
280 reference id =
281 Reference
282 { id
283 , to = def
284 , about = def
285 }
286 instance Default Reference where
287 def = reference def
288
289 -- * Type 'Entity'
290 data Entity
291 = Entity
292 { name :: Text
293 , address :: Address
294 } deriving (Eq,Show)
295 instance Default Entity where
296 def = Entity
297 { name = def
298 , address = def
299 }
300 instance Semigroup Entity where
301 _x <> y = y
302
303 -- * Type 'Date'
304 data Date
305 = Date
306 { year :: Int
307 , month :: Maybe Nat1
308 , day :: Maybe Nat1
309 } deriving (Eq,Show)
310 instance Default Date where
311 def = Date
312 { year = 1970
313 , month = Just (Nat1 01)
314 , day = Just (Nat1 01)
315 }
316 instance Semigroup Date where
317 _x <> y = y
318
319 -- * Type 'Link'
320 data Link
321 = Link
322 { name :: Text
323 , href :: URL
324 , rel :: Text
325 , para :: Para
326 } deriving (Eq,Show)
327 instance Default Link where
328 def = Link
329 { name = def
330 , href = def
331 , rel = def
332 , para = def
333 }
334
335 -- * Type 'Alias'
336 data Alias
337 = Alias
338 { id :: Ident
339 } deriving (Eq,Show)
340 instance Default Alias where
341 def = Alias
342 { id = def
343 }
344
345 -- * Type 'Serie'
346 data Serie
347 = Serie
348 { name :: Text
349 , key :: Text
350 } deriving (Eq,Show)
351 instance Default Serie where
352 def = Serie
353 { name = def
354 , key = def
355 }