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