Debian Codegraph @

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

raw symbols SCIP
1 {-
2 - Language/TEI/ToXML.hs - convert TEI to XML (AST)
3 -
4 - Copyright 2020 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 {-|
24 - Convert the TEI AST to textual TEI XML.
25 -}
26 module Language.TEI.ToXML (prettyTEI) where
27
28 import Text.XML.Light
29
30 import Data.NatLang.Dictionary (Dictionary(..), Body(..))
31 import Language.TEI.Syntax (TEI)
32 import Language.TEI.ToXML.Aux
33 import Language.TEI.ToXML.Body
34 import Language.TEI.ToXML.Header
35
36
37 -- Notes:
38 -- * haskell-xml does not catch invalid input !
39 -- * `Language.TEI.ToXML.ValidateChar.validateString' should be applied
40 -- to the Ding input before attempting to convert to XML here.
41 -- * Currently done in Main.
42
43
44 -- | Pretty-print the TEI AST, as XML.
45 prettyTEI :: TEI -> String
46
47 -- Do not use `ppcTopElement' here, it adds a boring default <?xml ?> header.
48 prettyTEI tei =
49 xmlDeclDoctype ++ (ppcElement prettyConfigPP $ teiToElement tei) ++ "\n"
50
51
52 -- | String form of the XML declaration and doctype (haskell-xml does not
53 -- support such properly).
54 xmlDeclDoctype :: String
55
56 -- TODO: Put the referenced files into place (e.g., by symlinking).
57 xmlDeclDoctype = unlines
58 [ "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
59 , "<?xml-stylesheet type=\"text/xsl\" href=\"freedict-dictionary.xsl\"?>"
60 , "<?xml-stylesheet type=\"text/css\" href=\"freedict-dictionary.css\"?>"
61 , "<?oxygen SCHSchema=\"freedict-P5.rng\"?>"
62 , "<?oxygen RNGSchema=\"freedict-P5.rng\" type=\"xml\"?>"
63 , "<!DOCTYPE TEI SYSTEM \"freedict-P5.dtd\">"
64 ]
65
66
67 -- | Translate a TEI AST to the corresponding XML AST.
68 teiToElement :: TEI -> Element
69 teiToElement tei = unode "TEI"
70 ( [ uattr "xmlns" "http://www.tei-c.org/ns/1.0", uattr "version" "5.0" ]
71 , [ convHeader (dictHeader tei) srcLang tgtLang nHeadwords
72 , convBody (dictBody tei) srcLang tgtLang
73 ]
74 )
75 where
76 srcLang = dictSrcLang tei
77 tgtLang = dictTgtLang tei
78
79 -- Note: It seems more reasonable to calculate the number of headwords, which
80 -- is to become part of the TEI header, in the ding2tei translation
81 -- function. However, as of now, the TEI AST is unable to store that
82 -- number.
83 nHeadwords = length entries
84 where
85 (Body entries) = dictBody tei
86
87
88 -- vi: ft=haskell ts=2 sw=2 et