| 1 |
{- |
| 2 |
- Language/TEI/ToXML/Body.hs - convert TEI body to XML (AST) |
| 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 |
|
| 24 |
|
| 25 |
module Language.TEI.ToXML.Body (convBody) where |
| 26 |
|
| 27 |
import Data.Foldable (toList) |
| 28 |
import Data.List.NonEmpty (NonEmpty((:|))) |
| 29 |
import Data.Maybe (mapMaybe) |
| 30 |
import Text.XML.Light |
| 31 |
|
| 32 |
import Data.NatLang.Dictionary (Body(Body)) |
| 33 |
import Data.NatLang.Example (Example(..)) |
| 34 |
import Data.NatLang.InflectedForms (InflectedForms(..), InflectedForm(..)) |
| 35 |
import Data.NatLang.Language |
| 36 |
import Data.NatLang.Usage (Usage(..)) |
| 37 |
import Language.TEI.Syntax |
| 38 |
import Language.TEI.Syntax.Reference |
| 39 |
import Language.TEI.ToXML.Aux |
| 40 |
import Language.TEI.ToXML.Grammar |
| 41 |
import Language.TEI.ToXML.Ident |
| 42 |
|
| 43 |
|
| 44 |
-- Notes: |
| 45 |
-- * The auxiliary functions are set up in the `where' block of the main |
| 46 |
-- function so that they all have shared access to `srcLang' and `tgtLang'. |
| 47 |
-- * Otherwise, these variables would need to be passed around a lot, even |
| 48 |
-- to functions that do not use them directly but only pass them further |
| 49 |
-- down. |
| 50 |
-- * Some functions could be taken off the where block, but they are not, |
| 51 |
-- for consistency. |
| 52 |
-- * The functions `node _' and `unode _' are heavily overloaded. They may |
| 53 |
-- operate on e.g.: |
| 54 |
-- ([Attr], String), (Attr, Element), ([Attr], [Element]), Attr. |
| 55 |
-- * If a pair is provided, the left element is always the attribute (list) |
| 56 |
-- and the right element the content. |
| 57 |
-- * The @xml:lang attribute is placed |
| 58 |
-- - on the top-level <text> element (source language), and |
| 59 |
-- - on cit@type="trans" elements (target language). |
| 60 |
-- - This includes translations of examples. |
| 61 |
-- * Reasoning: All but translations are in the source language, this is |
| 62 |
-- the minimal approach. |
| 63 |
-- * Note that there are some fixed abbreviations (e.g., for grammatical |
| 64 |
-- gender, or register), which violate this (usually English; TODO?). |
| 65 |
-- * lg1-lg2.tei suggests otherwise. |
| 66 |
-- * TEI Lex-0 suggests instead to add the attribute to <entry> and <cit> |
| 67 |
-- elements. |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
convBody :: Body Entry -> Language -> Language -> Element |
| 72 |
convBody body srcLang tgtLang = convBody' body |
| 73 |
where |
| 74 |
|
| 75 |
------------------------- |
| 76 |
-- Some useful constants |
| 77 |
|
| 78 |
xmlLangSrc = xmlLangAttr $ showCode srcLang |
| 79 |
xmlLangTgt = xmlLangAttr $ showCode tgtLang |
| 80 |
|
| 81 |
|
| 82 |
--------------------------- |
| 83 |
-- The converter functions |
| 84 |
|
| 85 |
convBody' :: Body Entry -> Element |
| 86 |
convBody' (Body entries) |
| 87 |
= unode "text" $ (,) xmlLangSrc |
| 88 |
$ unode "body" |
| 89 |
$ map convEntry $ entries |
| 90 |
|
| 91 |
|
| 92 |
convEntry :: Entry -> Element |
| 93 |
convEntry e = unode "entry" |
| 94 |
( [xmlIdAttr $ identToXMLId $ entryIdent e] |
| 95 |
, (convForm $ entryForm e) |
| 96 |
: maybe id (:) |
| 97 |
(convGrammar $ entryGrammar e) |
| 98 |
(mapMaybe convSense $ entrySenses e) |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
-- @type=lemma is a TEI Lex-0 recommendation. |
| 103 |
convForm :: Form -> Element |
| 104 |
convForm form = unode "form" $ |
| 105 |
unode "orth" (formOrth form) |
| 106 |
: map (convAbbrev "form") (formAbbrevs form) |
| 107 |
++ maybe [] convInflectedForms (formInflected form) |
| 108 |
|
| 109 |
-- Encoded as suggested by Sebastian Humenda (on <[email protected]>, |
| 110 |
-- 2020-08-29). |
| 111 |
-- To be nested inside the main <form> element. |
| 112 |
-- Note: The tagName is "form" inside <form> and "cit" inside <cit>. |
| 113 |
convAbbrev :: String -> String -> Element |
| 114 |
convAbbrev tagName = unode tagName . (,) (uattr "type" "abbrev") |
| 115 |
. unode "orth" |
| 116 |
|
| 117 |
|
| 118 |
-- tns[@value="past"]: no source - guessed / newly defined. |
| 119 |
-- tns[@value="pstp"] was found as an example in the TEI P5 doc (9.5.3.1). |
| 120 |
-- - TEI Lex-0 sug.: <gram type="participle">pap</gram> |
| 121 |
convInflectedForms :: InflectedForms -> [Element] |
| 122 |
convInflectedForms (InflectedForms (sp :| sps) (pp :| pps)) = |
| 123 |
map (convInflectedForm "past" (Just "indicative")) (sp : sps) |
| 124 |
++ map (convInflectedForm "pstp" Nothing) (pp : pps) |
| 125 |
|
| 126 |
-- Syntax: |
| 127 |
-- a) E-Mail from Sebastian Humenda (on <[email protected]>, |
| 128 |
-- 2020-05-03) |
| 129 |
-- * Nest inside the main form. |
| 130 |
-- b) TEI Lex-0 (3.3): suggests value as content, e.g. <tns>pres</tns>. |
| 131 |
-- c) TEI P5 |
| 132 |
-- * 9.3.1: grammar tags |
| 133 |
-- * <tns> doc: tns, mood example: <tns value="..."/> |
| 134 |
-- d) FreeDict TEI (@shumenda): Do not use @value, instead content. |
| 135 |
convInflectedForm :: String -> Maybe String -> InflectedForm -> Element |
| 136 |
convInflectedForm tense mMood (InflectedForm orth usages) = unode "form" |
| 137 |
( [uattr "type" "infl"] |
| 138 |
, unode "gramGrp" |
| 139 |
( |
| 140 |
(unode "tns" $ tense) |
| 141 |
: maybe [] (pure . unode "mood") mMood |
| 142 |
) |
| 143 |
: unode "orth" orth |
| 144 |
: map convUsage usages |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
-- When there is no content, there needs not be no sense element. Hence |
| 149 |
-- `Maybe Element'. |
| 150 |
convSense :: Sense -> Maybe Element |
| 151 |
convSense sense = |
| 152 |
let content = maybe id (:) |
| 153 |
(convGrammar $ senseGrammar sense) |
| 154 |
$ (map convUsage $ senseUsages sense) |
| 155 |
++ (map convTranslation $ senseTranslations sense) |
| 156 |
++ (map convExample $ senseExamples sense) |
| 157 |
++ (map convRefGroup $ senseReferences sense) |
| 158 |
++ (map (unode "note") $ senseNotes sense) |
| 159 |
in |
| 160 |
if null content |
| 161 |
then Nothing |
| 162 |
else Just $ unode "sense" content |
| 163 |
|
| 164 |
|
| 165 |
-- Note: inflected forms are dropped for now - unsure how to encode. (TODO) |
| 166 |
convTranslation :: Translation -> Element |
| 167 |
convTranslation trans = unode "cit" |
| 168 |
( [uattr "type" "trans", xmlLangTgt] |
| 169 |
, unode "quote" (translationOrth trans) |
| 170 |
: (maybe id (:) |
| 171 |
(convGrammar $ translationGrammar trans) |
| 172 |
$ (map convUsage $ translationUsages trans) |
| 173 |
++ (map (convAbbrev "cit") $ translationAbbrevs trans) |
| 174 |
++ (map (unode "note") $ translationNotes trans) |
| 175 |
) |
| 176 |
) |
| 177 |
|
| 178 |
|
| 179 |
convUsage :: Usage -> Element |
| 180 |
convUsage (Usage uType uStr) = unode "usg" |
| 181 |
( uattr "type" (show uType) |
| 182 |
, uStr |
| 183 |
) |
| 184 |
|
| 185 |
|
| 186 |
convExample :: Example -> Element |
| 187 |
convExample (Example srcEx tgtExs) = unode "cit" |
| 188 |
( [uattr "type" "example"] |
| 189 |
, unode "quote" srcEx : map convExTrans tgtExs |
| 190 |
) |
| 191 |
where |
| 192 |
convExTrans :: String -> Element |
| 193 |
convExTrans = unode "cit" . (,) [uattr "type" "trans", xmlLangTgt] |
| 194 |
. unode "quote" |
| 195 |
|
| 196 |
|
| 197 |
convRefGroup :: ReferenceGroup -> Element |
| 198 |
convRefGroup (ReferenceGroup refType refs) = unode "xr" |
| 199 |
( [uattr "type" (show refType)] |
| 200 |
, map convReference $ toList refs |
| 201 |
) |
| 202 |
where |
| 203 |
-- Notes: |
| 204 |
-- * Some references do not contain a link to another entry. |
| 205 |
-- * Such links need to be annotated with '#'. |
| 206 |
convReference :: Reference -> Element |
| 207 |
convReference (Reference mRef tgt) = unode "ref" |
| 208 |
( maybe [] (pure . uattr "target" . ('#':) . identToXMLId) mRef |
| 209 |
, tgt |
| 210 |
) |
| 211 |
|
| 212 |
|
| 213 |
-- vi: ft=haskell ts=2 sw=2 et |