Based on the encodings you have specified, here is a simple plugin that will automatically open any XML file in the encoding
it specifies.
To use this plugin, go to Tools -> Developer -> New Plugin
, remove whatever boilerplate is present in that and paste the below code there. Save that (in the User
directory) with any file name you want.
Now, whenever you open any XML file, if it has a encoding specified, it would open it based on the _xml_to_st_encoding_map
variable.
import sublime
import sublime_plugin
from xml.dom.minidom import parseString
_xml_to_st_encoding_map = {
"Windows-1254": "Turkish (Windows 1254)",
"ISO-8859-9": "Turkish (ISO 8859-9)",
"UTF-8": "utf-8"
}
class SetXmlEncodingListener(sublime_plugin.ViewEventListener):
@classmethod
def is_applicable(cls, settings):
syntax = settings.get("syntax")
return syntax == "Packages/XML/XML.sublime-syntax"
def on_load_async(self):
opened_file = self.view.file_name()
try:
with open(opened_file, "r") as xml_file:
xml_data = parseString(xml_file.read())
except Exception as e:
sublime.status_message("No XML data")
return
# If there is no encoding specified, return.
if not xml_data.encoding:
sublime.status_message("XML doesn't have any encoding specified.")
return
# If the encoding specified is not defined in the map, return.
if not _xml_to_st_encoding_map.get(xml_data.encoding):
sublime.status_message("Encoding specified is not present in the mapping.")
return
self.view.run_command("reopen", {
"encoding": _xml_to_st_encoding_map.get(xml_data.encoding)
})