Last active
July 2, 2016 20:29
-
-
Save bvanrijn/33f9809474142e47cdf198a462a96a12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
''' | |
usage: python xh-system-convert.py FILE | |
Converts X or H system to Esperanto alphabet characters in FILE | |
Written by @grooveplex, released into the public domain under the terms of the | |
CC0 license. | |
''' | |
import sys | |
import re | |
if len(sys.argv) == 1: | |
print('Not enough arguments were given.\nHint: give a filename as the second argument') | |
exit(1) | |
else: | |
try: | |
open(sys.argv[1]) | |
except IOError: | |
print(('\'{0}\' does not exist.').format(sys.argv[1])) | |
exit(1) | |
if sys.argv[0] == sys.argv[1]: | |
_sure = raw_input('Are you sure you want to overwrite the script itself? [y/n] ') | |
if _sure.lower() != 'y': | |
exit(0) | |
else: | |
pass | |
with open(sys.argv[1], 'r+') as file: | |
text = file.read() | |
text = re.sub('ch', 'ĉ', text) | |
text = re.sub('gh', 'ĝ', text) | |
text = re.sub('hh', 'ĥ', text) | |
text = re.sub('jh', 'ĵ', text) | |
text = re.sub('sh', 'ŝ', text) | |
text = re.sub('uh', 'ŭ', text) | |
text = re.sub('cx', 'ĉ', text) | |
text = re.sub('gx', 'ĝ', text) | |
text = re.sub('hx', 'ĥ', text) | |
text = re.sub('jx', 'ĵ', text) | |
text = re.sub('sx', 'ŝ', text) | |
text = re.sub('ux', 'ŭ', text) | |
file.seek(0) | |
file.write(text) | |
file.truncate() | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment