{-
- Language/TEI/ToXML/Body.hs - convert TEI body to XML (AST)
-
- Copyright 2020,2022 Einhard Leichtfuß
-
- This file is part of ding2tei-haskell.
-
- ding2tei-haskell is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as published
- by the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- ding2tei-haskell is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with ding2tei-haskell. If not, see .
-}
{-|
- Convert the TEI body to XML.
-}
module Language.TEI.ToXML.Body (convBody) where
import Data.Foldable (toList)
import Data.List.NonEmpty (NonEmpty((:|)))
import Data.Maybe (mapMaybe)
import Text.XML.Light
import Data.NatLang.Dictionary (Body(Body))
import Data.NatLang.Example (Example(..))
import Data.NatLang.InflectedForms (InflectedForms(..), InflectedForm(..))
import Data.NatLang.Language
import Data.NatLang.Usage (Usage(..))
import Language.TEI.Syntax
import Language.TEI.Syntax.Reference
import Language.TEI.ToXML.Aux
import Language.TEI.ToXML.Grammar
import Language.TEI.ToXML.Ident
-- Notes:
-- * The auxiliary functions are set up in the `where' block of the main
-- function so that they all have shared access to `srcLang' and `tgtLang'.
-- * Otherwise, these variables would need to be passed around a lot, even
-- to functions that do not use them directly but only pass them further
-- down.
-- * Some functions could be taken off the where block, but they are not,
-- for consistency.
-- * The functions `node _' and `unode _' are heavily overloaded. They may
-- operate on e.g.:
-- ([Attr], String), (Attr, Element), ([Attr], [Element]), Attr.
-- * If a pair is provided, the left element is always the attribute (list)
-- and the right element the content.
-- * The @xml:lang attribute is placed
-- - on the top-level element (source language), and
-- - on cit@type="trans" elements (target language).
-- - This includes translations of examples.
-- * Reasoning: All but translations are in the source language, this is
-- the minimal approach.
-- * Note that there are some fixed abbreviations (e.g., for grammatical
-- gender, or register), which violate this (usually English; TODO?).
-- * lg1-lg2.tei suggests otherwise.
-- * TEI Lex-0 suggests instead to add the attribute to and
-- elements.
-- | Convert the TEI Body to the corresponding TEI XML.
convBody :: Body Entry -> Language -> Language -> Element
convBody body srcLang tgtLang = convBody' body
where
-------------------------
-- Some useful constants
xmlLangSrc = xmlLangAttr $ showCode srcLang
xmlLangTgt = xmlLangAttr $ showCode tgtLang
---------------------------
-- The converter functions
convBody' :: Body Entry -> Element
convBody' (Body entries)
= unode "text" $ (,) xmlLangSrc
$ unode "body"
$ map convEntry $ entries
convEntry :: Entry -> Element
convEntry e = unode "entry"
( [xmlIdAttr $ identToXMLId $ entryIdent e]
, (convForm $ entryForm e)
: maybe id (:)
(convGrammar $ entryGrammar e)
(mapMaybe convSense $ entrySenses e)
)
-- @type=lemma is a TEI Lex-0 recommendation.
convForm :: Form -> Element
convForm form = unode "form" $
unode "orth" (formOrth form)
: map (convAbbrev "form") (formAbbrevs form)
++ maybe [] convInflectedForms (formInflected form)
-- Encoded as suggested by Sebastian Humenda (on ,
-- 2020-08-29).
-- To be nested inside the main