Skip to content

Commit

Permalink
Better support for single quotation and double quotation marks ('"‘’“”)
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Mar 13, 2024
1 parent 3a507a8 commit 4fa3d83
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
11 changes: 9 additions & 2 deletions pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ def non_ascii_to_octal(argstr):
'@%34%\\41@%%@%34%\\176@%%@%34%\\241@%%@%34%\\376@%%'
>>> non_ascii_to_octal("ABC ±120° DEF α ♥")
'ABC \\261120\\260 DEF @~\\141@~ @%34%\\252@%%'
>>> non_ascii_to_octal("'‘’\"“”")
'\\234\\140\\47\\042\\216\\217'
""" # noqa: RUF002
# Dictionary mapping non-ASCII characters to octal codes
mapping = {}
Expand Down Expand Up @@ -299,19 +301,24 @@ def non_ascii_to_octal(argstr):
c: "\\" + format(i, "o")
for c, i in zip(
"•…™—–fiž" # \03x. \030 is undefined
"’‘" # \047 and \140
"š" # \177
"Œ†‡Ł⁄‹Š›œŸŽł‰„“”" # \20x-\21x
"ı`´ˆ˜¯˘˙¨‚˚¸'˝˛ˇ", # \22x-\23x
[*range(25, 32), *range(127, 160)],
[*range(25, 32), 39, 96, *range(127, 160)],
strict=True,
)
}
)
# \240-\377
mapping.update({chr(i): "\\" + format(i, "o") for i in range(160, 256)})

# Remove any printable characters
# Remove any printable characters.
mapping = {k: v for k, v in mapping.items() if k not in string.printable}
# Mapping single quote ' and double quote " to octal codes because they often cause
# troubles.
mapping['"'] = "\\042"
mapping["'"] = "\\234" # Not \047
return argstr.translate(str.maketrans(mapping))


Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/baseline/test_text_nonascii.png.dvc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
outs:
- md5: 5442ca4a23e43b9f3e4d5794ccb5448a
size: 17286
- md5: 41e76a73a9eb2e443fed0acc81010305
size: 17595
hash: md5
path: test_text_nonascii.png
2 changes: 1 addition & 1 deletion pygmt/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def test_text_nonascii():
"""
fig = Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c", frame=True)
fig.text(position="TL", text="position-text:°α") # noqa: RUF001
fig.text(position="TL", text="position-text:°α'‘’\"“”", offset="j0.2c") # noqa: RUF001
fig.text(x=1, y=1, text="xytext:°α") # noqa: RUF001
fig.text(x=[5, 5], y=[3, 5], text=["xytext1:αζΔ❡", "xytext2:∑π∇✉"])
return fig

0 comments on commit 4fa3d83

Please sign in to comment.