Debian Codegraph @

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

raw symbols SCIP
1 {-
2 - Language/TEI/ToXML/Ident.hs - encode xml:id's
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 identifiers to valid `@xml:id' values.
24 -}
25 module Language.TEI.ToXML.Ident
26 ( identToXMLId
27 ) where
28
29 import Data.Char (ord)
30
31 import Language.TEI.Syntax.Reference (Ident(Ident))
32
33
34 -- The syntax of XML id's is described in by the W3C's XML specification:
35 -- * https://www.w3.org/TR/xmlschema-2/#ID | #NCName
36 -- / https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#ID | #NCName
37 -- * https://www.w3.org/TR/xml-names/#NT-NCName
38 -- / https://www.w3.org/TR/2009/REC-xml-names-20091208/#NT-NCName
39 -- > NCName ::= Name - (Char* ':' Char*) /* An XML Name, minus the ":" */
40 -- * https://www.w3.org/TR/xml/#NT-Name
41 -- / https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name
42 -- > Name ::= NameStartChar (NameChar)*
43 -- * https://www.w3.org/TR/xml/#NT-NameStartChar | #NT-NameChar
44 -- / http://www.w3.org/TR/2008/REC-xml-20081126/
45 -- * defines NameStartChar and NameChar
46 -- * Deduce: NCNameStartChar and NCNameChar (- ':')
47
48
49 -- | Translate an `Ident' to a legal @xml:id (NCName).
50 -- The identifier's number is appended dot-separated to avoid ambiguity.
51 identToXMLId :: Ident -> String
52 identToXMLId (Ident s n) = encodeNCName s ++ '.' : show n
53
54
55 -- Use '_' as escape char, since it is legal everywhere.
56 encodeNCName :: String -> String
57 encodeNCName "" = "_empty_"
58 encodeNCName (c:cs) = encodeNCNameStartChar c ++ concatMap encodeNCNameChar cs
59
60 encodeNCNameStartChar :: Char -> String
61 encodeNCNameStartChar ' ' = "__"
62 encodeNCNameStartChar c
63 | isNCNameStartChar c && c /= '_' = pure c
64 | otherwise = escapeChar c
65
66 encodeNCNameChar :: Char -> String
67 encodeNCNameChar ' ' = "__"
68 encodeNCNameChar c
69 | isNCNameChar c && c /= '_' = pure c
70 | otherwise = escapeChar c
71
72 escapeChar :: Char -> String
73 escapeChar c = '_' : show (ord c) ++ "_"
74
75
76 -- Treat `€' special to work around a libxml2 bug.
77 -- - <https://gitlab.gnome.org/GNOME/libxml2/-/issues/364>
78 -- - Otherwise, `make validation' would fail.
79 -- - TODO: Revert once bug fixed.
80 isNCNameStartChar :: Char -> Bool
81 isNCNameStartChar '€' = False
82 isNCNameStartChar c =
83 'A' <= c && c <= 'Z'
84 || c == '_'
85 || 'a'<= c && c <= 'z'
86 || '\xC0' <= c && c <= '\xD6'
87 || '\xD8' <= c && c <= '\xF6'
88 || '\xF8' <= c && c <= '\x2FF'
89 || '\x370' <= c && c <= '\x37D'
90 || '\x37F' <= c && c <= '\x1FFF'
91 || '\x200C' <= c && c <= '\x200D'
92 || '\x2070' <= c && c <= '\x218F'
93 || '\x2C00' <= c && c <= '\x2FEF'
94 || '\x3001' <= c && c <= '\xD7FF'
95 || '\xF900' <= c && c <= '\xFDCF'
96 || '\xFDF0' <= c && c <= '\xFFFD'
97 || '\x10000' <= c && c <= '\xEFFFF'
98
99 isNCNameChar :: Char -> Bool
100 isNCNameChar c =
101 isNCNameStartChar c
102 || c == '-'
103 || c == '.'
104 || '0' <= c && c <= '9'
105 || c == '\xB7'
106 || '\x0300' <= c && c <= '\x036F'
107 || '\x203F' <= c && c <= '\x2040'
108
109
110 -- vi: ft=haskell ts=2 sw=2 et