1if not modules then modules = { } end modules ['lang-tra'] = { 2 version = 1.001, 3 comment = "companion to lang-tra.mkiv", 4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", 5 copyright = "PRAGMA ADE / ConTeXt Development Team", 6 license = "see context related readme files" 7} 8 9local utfbyte, utfsplit = utf.byte, utf.split 10 11local nuts = nodes.nuts 12 13local nextchar = nuts.traversers.char 14 15local getattr = nuts.getattr 16local setchar = nuts.setchar 17 18local insertbefore = nuts.insertbefore 19local copy_node = nuts.copy 20 21local texsetattribute = tex.setattribute 22 23local transliteration = { } 24languages.transliteration = transliteration 25 26local a_transliteration = attributes.private("transliteration") 27local unsetvalue = attributes.unsetvalue 28 29local lastmapping = 0 30local loadedmappings = { } 31 32function transliteration.define(name,vector) 33 local m = loadedmappings[vector] 34 if m == nil then 35 lastmapping = lastmapping + 1 36 local data = require("lang-imp-" .. name) 37 if data then 38 local transliterations = data.transliterations 39 if transliterations then 40 for name, d in next, transliterations do 41 local vector = d.vector 42 if not vector then 43 local mapping = d.mapping 44 if mapping then 45 vector = { } 46 for k, v in next, mapping do 47 local vv = utfsplit(v) 48 for i=1,#vv do 49 vv[i] = utfbyte(vv[i]) 50 end 51 vector[utfbyte(k)] = vv 52 end 53 d.vector = vector 54 end 55 end 56 d.attribute = lastmapping 57 loadedmappings[name] = d 58 loadedmappings[lastmapping] = d 59 end 60 end 61 end 62 m = loadedmappings[vector] or false 63 end 64end 65 66local enabled = false 67 68function transliteration.set(vector) 69 if not enabled then 70 nodes.tasks.enableaction("processors", "languages.transliteration.handler") 71 enabled = true 72 end 73 local m = loadedmappings[vector] 74 texsetattribute(a_transliteration,m and m.attribute or unsetvalue) 75end 76 77function transliteration.handler(head) 78 local aprev = nil 79 local vector = nil 80 for current, char in nextchar, head do 81 local a = getattr(current,a_transliteration) 82 if a then 83 if a ~= aprev then 84 aprev = a 85 vector = loadedmappings[a] 86 if vector then 87 vector = vector.vector 88 end 89 end 90 if vector then 91 local t = vector[char] 92 if t then 93 local n = #t 94 setchar(current,t[n]) 95 local p = current 96 if n > 1 then 97 for i = n-1,1,-1 do 98 local g = copy_node(current) 99 setchar(g,t[i]) 100 head, p = insertbefore(head, p, g) 101 end 102 end 103 end 104 end 105 end 106 end 107 return head 108end 109 110interfaces.implement { 111 name = "settransliteration", 112 arguments = "string", 113 actions = transliteration.set, 114} 115 116interfaces.implement { 117 name = "definedtransliteration", 118 arguments = "2 strings", 119 actions = transliteration.define, 120} 121 122nodes.tasks.prependaction("processors", "normalizers", "languages.transliteration.handler", nil, "nut", "disabled" ) 123 124 |