]> git.phdru.name Git - dotfiles.git/blobdiff - bin/unicode_norm_nfd.py
bin/cleanup-filenames.sh: Remove accents
[dotfiles.git] / bin / unicode_norm_nfd.py
diff --git a/bin/unicode_norm_nfd.py b/bin/unicode_norm_nfd.py
new file mode 100755 (executable)
index 0000000..2cca0d9
--- /dev/null
@@ -0,0 +1,23 @@
+#! /usr/bin/env python3
+# https://stackoverflow.com/a/518232/7976758
+
+import sys
+import unicodedata
+
+
+def strip_accents(s):
+    return ''.join(c for c in unicodedata.normalize('NFD', s)
+                   if unicodedata.category(c) != 'Mn')
+
+
+def latin1_to_ascii(uinput):
+    if isinstance(uinput, bytes):
+        uinput = uinput.decode(sys.getfilesystemencoding())
+    return strip_accents(uinput)
+
+
+if __name__ == '__main__':
+    if len(sys.argv) == 1:
+        sys.exit('Usage: %s name\n' % sys.argv[0])
+    for name in sys.argv[1:]:
+        print(latin1_to_ascii(name))