Debian Codegraph @

importers/ding2tei/src/Language/TEI/ToXML/Header.hs

raw symbols SCIP
1 {-
2 - Language/TEI/ToXML/Header.hs - convert header to XML
3 -
4 - Copyright 2020,2022 Einhard Leichtfuß
5 -
6 - This file is part of ding2tei-haskell.
7 -
8 - ding2tei-haskell is free software: you can redistribute it and/or modify
9 - it under the terms of the GNU Affero General Public License as published
10 - by the Free Software Foundation, either version 3 of the License, or
11 - (at your option) any later version.
12 -
13 - ding2tei-haskell is distributed in the hope that it will be useful,
14 - but WITHOUT ANY WARRANTY; without even the implied warranty of
15 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 - GNU Affero General Public License for more details.
17 -
18 - You should have received a copy of the GNU Affero General Public License
19 - along with ding2tei-haskell. If not, see <https://www.gnu.org/licenses/>.
20 -}
21
22 {-|
23 - Convert the Ding header to TEI XML
24 -}
25 module Language.TEI.ToXML.Header (convHeader) where
26
27
28 import Data.List (intercalate)
29 import Text.XML.Light
30
31 import qualified Config as Cfg
32 import Data.NatLang.Language
33 import Language.Ding.Syntax (Header(..))
34 import Language.TEI.ToXML.Aux
35
36
37 -- Notes:
38 -- * Whenever plain text co-occurs (on the same level) with other Content
39 -- inside an element (e.g. "<x>text<y>z</<y>more text</x>"), one should
40 -- use `mergeContent' on that list to avoid a pretty-printing like
41 -- <x>
42 -- text
43 -- <y>z</y>
44 -- more text
45 -- </x>
46 -- and instead cause everything to pe put in a single line.
47 -- * For information on how the header should look like, see the FreeDict
48 -- Wiki [0].
49 -- * See also: note on `unode' and `note' in ToXML/Body.hs.
50
51
52 -- | Create a `ref' element with equal target and value.
53 plainRefNode :: String -> Element
54 plainRefNode tgt = unode "ref" (uattr "target" tgt, tgt)
55
56
57 -- | Convert the Ding (!) header straight to TEI XML.
58 -- A notable share of information is not extracted from the Ding header,
59 -- but rather hardcoded, or configured in the `Config' module. This is
60 -- because the Ding header requires a lot more information than the Ding
61 -- header provides; further much of that information does only depend on this
62 -- program.
63 convHeader :: Header -> Language -> Language -> Int -> Element
64 convHeader header srcLang tgtLang nHeadwords = teiHeader
65 where
66
67 ----------------------------------------------
68 -- Information extracted from the Ding header
69
70 -- Note:
71 -- * The headerLicense field is not used. Instead, the licensing
72 -- information is hardcoded below, since the interaction with AGPL is
73 -- non-trivial.
74
75 dingVersion = headerVersion header
76 dingDate = headerVersionDate header
77 dingAuthor = headerCopyrightHolder header
78 dingCopyrightPeriod = headerCopyrightPeriod header
79 dingURL = headerURL header
80
81
82 ---------------------------------------
83 -- (Partly) non-structural information
84
85 -- Contains information customary to this particular dictionary..
86
87 version = Cfg.makeVersion dingVersion Cfg.modVersion
88
89 title = show srcLang ++ " - " ++ show tgtLang
90 ++ " Ding/" ++ Cfg.projectName ++ " dictionary"
91
92 availability :: Element
93 availability = unode "availability" $ (,) [uattr "status" "free"]
94 [
95 -- Copyright holders (Ding author & this program's contributors)
96 unode "p"
97 $ (++) "Copyright (C) "
98 $ intercalate ", "
99 $ map (\ (p, y) -> p ++ " " ++ y)
100 $ (dingAuthor, dingCopyrightPeriod)
101 : map (\ (p, y) -> (Cfg.personName p, y)) Cfg.contributors
102 ,
103 -- License notice
104 unode "p" $ mergeContent
105 [ text $ unwords
106 [ "This dictionary is free software: you can redistribute it"
107 , "and/or modify it under the terms of both the "
108 ]
109 , Elem $ unode "ref"
110 ( uattr "target" Cfg.gpl3url
111 , "GNU General Public License, version 3 (GPLv3)"
112 )
113 , text " and the "
114 , Elem $ unode "ref"
115 ( uattr "target" Cfg.agpl3url
116 , "GNU Affero General Public License, version 3 (AGPLv3)"
117 )
118 , text $ unwords
119 [ ", where each of these licenses applies to different parts of"
120 , "this (combined) work."
121 ]
122 ]
123 , unode "p" $ unwords
124 [ "This dictionary is distributed in the hope that it will be useful,"
125 , "but WITHOUT ANY WARRANTY; without even the implied warranty of"
126 , "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
127 , "GNU General Public License"
128 , "and the"
129 , "GNU Affero General Public License"
130 , "for more details."
131 ]
132 , unode "p" $ mergeContent
133 [ text $ unwords
134 [ "You should have received a copy of both the"
135 , "GNU General Public License"
136 , "and the"
137 , "GNU Affero General Public License"
138 , "along with this dictionary. If not, see "
139 ]
140 , Elem $ plainRefNode "https://www.gnu.org/licenses/"
141 , text "."
142 ]
143 , unode "p" $ mergeContent
144 [ text $ unwords
145 [ "The \"source form\", as defined in both the GPLv3 and AGPLv3,"
146 , "of this dictionary is composed of the original"
147 , "Ding dictionary (licensed "
148 ]
149 , Elem $ unode "ref" (uattr "target" Cfg.gpl2url, "GPLv2")
150 , text $
151 "+) and the " ++ Cfg.programName ++ " program (licensed AGPLv3+)."
152 ]
153 ]
154
155 -- TODO: Add more notes (e.g., to provide an ontology -- for grammar keywords
156 -- et al.).
157 notesStmt :: Element
158 notesStmt = unode "notesStmt" $
159 [ unode "note" (uattr "type" "status", Cfg.status)
160 ]
161
162 sourceDesc :: Element
163 sourceDesc = unode "sourceDesc"
164 [ unode "p" $ mergeContent
165 [ text "Home: "
166 , Elem $ unode "ptr" $ uattr "target" dingURL
167 ]
168 , unode "p" $ mergeContent
169 [ text "This dictionary was generated from the "
170 , Elem $ unode "ref" (uattr "target" dingURL, "Ding")
171 , text $ " dictionary, version " ++ dingVersion
172 ++ " (" ++ dingDate ++ ") using the "
173 , Elem $ unode "ref" (uattr "target" Cfg.programURL, Cfg.programName)
174 , text " program."
175 ]
176 , unode "p" $ mergeContent
177 [ text "The Ding dictionary can be obtained from "
178 , Elem $ plainRefNode Cfg.dingDownloadURL
179 , text "."
180 ]
181 , unode "p" $ mergeContent
182 [ text $ "The " ++ Cfg.programName ++ " program can be obtained from "
183 , Elem $ plainRefNode Cfg.programDownloadURL
184 , text "."
185 ]
186 ]
187
188 projectDesc :: Element
189 projectDesc = unode "projectDesc" $ unode "p" $ mergeContent
190 [ text $ unwords
191 [ "This dictionary comes to you through nice people making it"
192 , "available for free and for good."
193 , "It is part of the", Cfg.projectName, "project, "
194 ]
195 , Elem $ plainRefNode Cfg.projectURL
196 , text $ unwords
197 [ "."
198 , "This project aims to make many translating dictionaries available"
199 , "for free."
200 , "Your contributions are welcome!"
201 ]
202 ]
203
204
205 ---------------------------------
206 -- Purely structural information
207
208 -- Code that only reflects some (TEI/FreeDict) rules & converntions.
209
210 teiHeader :: Element
211 teiHeader = unode "teiHeader" $ (,) [xmlLangAttr "en"] $
212 fileDesc
213 : encodingDesc
214 : maybe [] pure revisionDesc
215
216 fileDesc :: Element
217 fileDesc = unode "fileDesc"
218 [ titleStmt
219 , editionStmt
220 , unode "extent" $ show nHeadwords ++ " headwords"
221 , publicationStmt
222 , notesStmt
223 , sourceDesc
224 ]
225
226 -- TODO: Use @xml:id and @ref for <name>, as in eng-pol.
227 titleStmt :: Element
228 titleStmt = unode "titleStmt"
229 [ unode "title" title
230 , unode "author" dingAuthor
231 , unode "editor" $ Cfg.personName Cfg.editor
232 , unode "respStmt"
233 [ unode "resp" "Maintainer"
234 , unode "name" $ Cfg.personName Cfg.maintainer
235 ]
236 ]
237
238 editionStmt :: Element
239 editionStmt
240 = unode "editionStmt"
241 $ unode "edition"
242 $ version
243
244 publicationStmt :: Element
245 publicationStmt = unode "publicationStmt"
246 [ unode "publisher" Cfg.projectName
247 , availability
248 , unode "date" (uattr "when" Cfg.publicationDate, Cfg.publicationDate)
249 , unode "pubPlace" $ unode "ref" Cfg.projectURL
250 ]
251
252 encodingDesc :: Element
253 encodingDesc = unode "encodingDesc" projectDesc
254
255 -- For reference, see eng-pol.tei and swh-eng.tei.
256 revisionDesc :: Maybe Element
257 revisionDesc
258 | null Cfg.changes = Nothing
259 | otherwise = Just $ unode "revisionDesc"
260 $ map convChange Cfg.changes
261 where
262 convChange :: Cfg.Change -> Element
263 convChange ch = unode "change"
264 ( [ uattr "n" chVersion
265 , uattr "when" $ date
266 , uattr "who"
267 $ unwords
268 $ map (('#':) . Cfg.personId)
269 $ Cfg.chPersons ch
270 ]
271 , unode "list"
272 ( [uattr "type" "bulleted"]
273 , ( unode "head" $ mergeContent $
274 [ text $ "Version " ++ chVersion ++ ", "
275 , Elem $ unode "date" date
276 , text ". Changes by "
277 ] ++ nameList ++
278 [ text "."
279 ]
280 )
281 : map (unode "item") (Cfg.chItems ch)
282 )
283 )
284 where
285 chVersion = Cfg.makeVersion (Cfg.chDingVersion ch) (Cfg.chModVersion ch)
286 date = Cfg.chDate ch
287
288 nameList :: [Content]
289 nameList = intercalate [text " and "]
290 $ map (pure . Elem . unode "name" . Cfg.personName)
291 $ Cfg.chPersons ch
292
293
294 -- References
295 -- * [0] https://github.com/freedict/fd-dictionaries/wiki/FreeDict-HOWTO-%E2%80%93-Writing-Text-Encoding-Initiative-XML-Files
296
297 -- vi: ft=haskell ts=2 sw=2 et