if not modules then modules = { } end modules ['pret-xml'] = { version = 1.001, comment = "from pret-.*\.lua, http://wiki.contextgarden.net/Verbatim_text#Your_own_formatter and http://wiki.contextgarden.net/Custom_pretty_printer", author = "Renaud AUBIN", copyright = "to be defined", license = "to be defined" } local utf = unicode.utf8 local utfcharacters, utfvalues = string.utfcharacters, string.utfvalues local utfbyte, utffind = utf.byte, utf.find local byte, sub, find, gfind, match = string.byte, string.sub, string.find, string.gfind, string.match local texsprint, texwrite = tex.sprint, tex.write local ctxcatcodes = tex.ctxcatcodes local change_state, finish_state = buffers.change_state, buffers.finish_state buffers.visualizers.xml = buffers.visualizers.xml or { } buffers.visualizers.xml.identifiers = buffers.visualizers.xml.identifiers or { } incomment = false local colors = { "prettyone", "prettytwo", "prettythree", "prettyfour", "prettyfive", "prettysix", "prettyseven" } local states = { ['namespace'] = 1, ['element'] = 2, ['attrname'] = 3, ['attrvalue'] = 4, ['prolog'] = 5, ['comment'] = 6, ['pi'] = 7 } local function flush_text(str) for c in utfcharacters(str) do if c == " " then texsprint(ctxcatcodes,"\\obs") else texwrite(c) end end end local function process_attribute(str) local name, value, state = "", "", 0 buffers.currentcolors = colors local name,value = match(str,"^(.-)=(\".-\")$") state = change_state(states['attrname'], state) flush_text(name) state = finish_state(state) texwrite("=") state = change_state(states['attrvalue'], state) flush_text(value) state = finish_state(state) end local function process_namespace(str) local state = 0 buffers.currentcolors = colors state = change_state(states['namespace'], state) flush_text(str) state = finish_state(state) texwrite(":") end local function process_element(str) local state = 0 buffers.currentcolors = colors state = change_state(states['element'], state) flush_text(str) state = finish_state(state) end local function process_comment(str) local state = 0 buffers.currentcolors = colors state = change_state(states['comment'], state) flush_text(str) state = finish_state(state) end function process_tag(str) local name, value, state = "", "", 0 buffers.currentcolors = colors if match(str,"^<%?xml.-%?>$") then local prolog = match(str,"^<%?xml(.-)%?>$") state = change_state(states['pi'], state) texwrite("") state = finish_state(state) elseif match(str,"^$") then process_comment(str) elseif match(str,"^<[%a_]%w-%s*/?>$") or match(str,"^$") then local btag, name, etag = match(str,"^()$") texwrite(btag) process_element(name) texwrite(etag) elseif match(str,"^<%a%w-:[%a_]%w-%s*/?>$") or match(str,"^$") then local btag, ns, name, etag = match(str,"^()$") texwrite(btag) process_namespace(ns) process_element(name) texwrite(etag) elseif match(str,"^<%a%w-:[%a_][%w%-]-%s.*/?>$") or match(str,"^$") then local btag, ns, name, attributes, etag = match(str,"^()$") texwrite(btag) process_namespace(ns) process_element(name) repeat attributes=recursive_processing(attributes) until attributes==nil texwrite(etag) elseif match(str,"^<[%a_]%w-%s.*/?>$") or match(str,"^$") then local btag, name, attributes, etag = match(str,"^()$") texwrite(btag) process_element(name) repeat attributes=recursive_processing(attributes) until attributes==nil texwrite(etag) end end function process_uncomplete_starttag(str) local name, value, state = "", "", 0 buffers.currentcolors = colors if match(str,"^<%a%w-:[%a_]%w-%s.*$") then local btag, ns, name, attributes = match(str,"^(<)(%a%w-):([%a_]%w-%s)(.*)$") texwrite(btag) process_namespace(ns) process_element(name) repeat attributes=recursive_processing(attributes) until attributes==nil elseif match(str,"^<[%a_]%w-%s.*$") then local btag, name, attributes = match(str,"^(<)([%a_]%w-%s)(.*)$") texwrite(btag) process_element(name) repeat attributes=recursive_processing(attributes) until attributes==nil end end function recursive_processing(str) local first, second, third, attribute = "", "", "", "" if match(str, "^(.-)(%-%->)(.*)$") then -- stop multiline comment first, second, third = match(str, "^(.-)(%-%->)(.*)$") process_comment(first..second) incomment = false return third elseif incomment then process_comment(str) elseif match(str, "^(.-)(%b<>)(.*)$") then -- standard tag first, second, third = match(str, "^(.-)(%b<>)(.*)$") repeat first=recursive_processing(first) until first==nil process_tag(second) return third elseif match(str, "^(.-)(<[%a_]%w-:?%w-[%s%c])(.*)$") then -- multiline starttag first, second, third = match(str, "^(.-)(<[%a_]%w-:?%w-[%s%c])(.*)$") repeat first=recursive_processing(first) until first==nil process_uncomplete_starttag(second) return third -- elseif match(str, "^(.-)(<%?xml)(.*)$") then -- TODO manage multiline prolog -- first, second, third = match(str, "^(.-)(<%?xml)(.*)$") -- return third elseif match(str, "^(.-)(