Skip to content

Commit

Permalink
Merge pull request #1 from Yutsuro/ascii_only
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
Yutsuro authored Mar 27, 2023
2 parents d438d68 + 351b3a7 commit aa553d4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ dmypy.json
.history

# Test
test.py
test/
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
7 changes: 5 additions & 2 deletions punycode/punycode.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit aa553d4

Please sign in to comment.