diff --git a/.gitignore b/.gitignore index 2f04a14..09bb46a 100644 --- a/.gitignore +++ b/.gitignore @@ -132,4 +132,5 @@ dmypy.json .history # Test +test.py test/ diff --git a/README.md b/README.md index ee87a3b..a4dccb6 100644 --- a/README.md +++ b/README.md @@ -53,4 +53,30 @@ print(f"{str6} -> {punycode.convert(str6)}") # xn--n8jub8754b.xn--rhqv96g -> 美しい.世界 # xn--28j2af.xn--q9jyb4c -> こっち.みんな # xn--wgv71a119e.jp -> 日本語.jp +``` + +## ascii_only option (v0.2.0 or later) + +If you don't want to convert a Punycode domain to Unicode when you use `convert()` function, you can use `ascii_only` option. + +With this option `True`, `convert()` will convert only Unicode domains. + +```python +# ascii_only option (v0.2.0 or later) +ascii_only = True + +# Only Unicode domains will be converted if ascii_only=True +print(f"{str4} -> {punycode.convert(str4, ascii_only)}") +print(f"{str5} -> {punycode.convert(str5, ascii_only)}") +print(f"{str6} -> {punycode.convert(str6, ascii_only)}") + +# xn--n8jub8754b.xn--rhqv96g -> xn--n8jub8754b.xn--rhqv96g +# xn--28j2af.xn--q9jyb4c -> xn--28j2af.xn--q9jyb4c +# xn--wgv71a119e.jp -> xn--wgv71a119e.jp +``` + +This option is available on 0.2.0 or later version, so if you use v0.1.0, please update `punycode`. + +```powershell +pip install -U punycode ``` \ No newline at end of file diff --git a/punycode/punycode.py b/punycode/punycode.py index 980ece7..7e57952 100644 --- a/punycode/punycode.py +++ b/punycode/punycode.py @@ -1,10 +1,13 @@ import idna -def convert(input_text:str): +def convert(input_text:str, ascii_only=False): if not isinstance(input_text, str): raise ValueError("Input must be string.") if (input_text[:4] == "xn--") | (".xn--" in input_text): - output_text = idna.decode(input_text) + if ascii_only: + output_text = idna.encode(input_text).decode('utf-8') + else: + output_text = idna.decode(input_text) else: output_text = idna.encode(input_text).decode('utf-8') return output_text \ No newline at end of file diff --git a/setup.py b/setup.py index 9143476..5feae59 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ def _requirements(): setup( name=package_name, description='Punycode Converter Library for Python', - version='0.1.0', + version='0.2.0', long_description=long_description, long_description_content_type='text/markdown', url='https://github.com/Yutsuro/punycode',