Skip to content

Commit

Permalink
add graeco to power()
Browse files Browse the repository at this point in the history
  • Loading branch information
gdraheim committed Apr 8, 2024
1 parent d58e1ed commit e545fa0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ often in the field of mathematics. The sans and mono are rarely used however.
15 1/4 km/h
15³
x₁
Ξ₁³ +¼
X₁³ +¼
rewsna
ɐusʍǝɹ
ɹǝʍsuɐ
Expand Down
35 changes: 31 additions & 4 deletions unicoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__copyright__ = "(C) 2021-2024 Guido U. Draheim, licensed under the APLv2"
__version__ = "1.2.3152"

from typing import List, Dict, Generator, Tuple
from typing import List, Dict, Generator, Tuple, Optional
from io import StringIO
import sys
import logging
Expand Down Expand Up @@ -456,10 +456,28 @@ def superscript(text: str) -> str:
def power(text: str) -> str:
out = StringIO()
power = False
graec: Optional[str] = None
for x, c in enumerate(text):
ch = ord(c)
if graec is not None:
if norm_base_a <= ch and ch <= norm_base_z:
graec += c
continue
if norm_base_A <= ch and ch <= norm_base_Z:
graec += c
continue
if graec:
out.write(greek(graec))
graec = ""
if c in norm_power_signs and x + 1 < len(text) and (text[x + 1] in norm_super_numbers or text[x + 1] in norm_super_before):
power = True
continue # drop the power sign
if c in norm_power_signs and x + 1 < len(text) and (norm_base_a <= ord(text[x + 1]) and ord(text[x + 1]) <= norm_base_z):
graec = "" # graec += text[x+1] # in next loop
continue
if c in norm_power_signs and x + 1 < len(text) and (norm_base_A <= ord(text[x + 1]) and ord(text[x + 1]) <= norm_base_Z):
graec = "" # graec += text[x+1] # in next loop
continue
if power:
if c in norm_super_numbers:
out.write(chr(norm_super_numbers[c]))
Expand All @@ -469,9 +487,18 @@ def power(text: str) -> str:
out.write(chr(norm_super_after[c]))
else:
power = False
if norm_base_a <= ch and ch <= norm_base_z:
graec = c
continue
if norm_base_A <= ch and ch <= norm_base_Z:
graec = c
continue
out.write(c)
else:
out.write(c)
if graec:
out.write(greek(graec))
graec = ""
return out.getvalue()

norm_sub_numbers: Dict[str, int] = {
Expand Down Expand Up @@ -1358,7 +1385,7 @@ def convert(cmd: str, text: str) -> str:
text = rune(text)
if "viking" in cmd or "futo" in cmd or "futho" in cmd:
text = viking(text)
if "greek" in cmd or "math" in cmd:
if "greek" in cmd or "graec" in cmd:
text = greek(text)
if "black" in cmd or "frak" in cmd:
text = fraktur(text)
Expand All @@ -1383,7 +1410,7 @@ def helpinfo() -> str:
*fat* *bold* convert to math fat symbols
*ital* *name* convert to math slanted symbols
*round* *script* convert to math writing symbols
*greek* *math* convert to math greek
*greek* *graec* convert to math greek
*frak* *black* convert to math fraktur
*doub* *wide* convert to math double stroke
*cour* *type* convert to math courier monospace
Expand All @@ -1395,7 +1422,7 @@ def helpinfo() -> str:
*fract* *vect* convert fractional values
*subi* *below* convert numbers to subscript (and +/-)
*super* *above* convert numbers to superscript (and +/-)
*power* *dim* convert numbers after ^ to superscript
*power* *dim* convert numbers after ^ to superscript (alpha to greek)
*index* *idx* convert numbers after _ to subscript
*math* nobr+frac+power+index
*turned* *down* turn each character (upside-down)
Expand Down
64 changes: 49 additions & 15 deletions unicoder.py.tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_019_opt_scan(self) -> None:
self.assertEqual(opt.text, "arg3")
def test_051_helpinfo(self) -> None:
text = unicoder.helpinfo()
self.assertIn("futark", text)
self.assertIn("futa", text)
self.assertIn("italboldgreek", text)
#
def test_110_bold_base(self) -> None:
Expand Down Expand Up @@ -1093,19 +1093,43 @@ def test_706_norm_rune(self) -> None:
def test_707_norm_rune(self) -> None:
uni = unicoder.rune(base_ABCDEFGHIJKLMNOPQRSTUVWXYZ)
self.assertEqual(uni, ":ᚨᛒᚳᛞᛖᚠᚷᚺᛁᛡᚳᛚᛗᚾᛟᛈᚳᚱᛋᛏᚹᚹᛕᚳᛋᛇᛉ")
def test_710_norm_rune(self) -> None:
uni = unicoder.convert("fix", base_abcdefghijklmnopqrstuvwxyz)
self.assertEqual(uni, base_abcdefghijklmnopqrstuvwxyz)
def test_711_norm_rune(self) -> None:
uni = unicoder.convert("viking", base_abcdefghijklmnopqrstuvwxyz)
self.assertEqual(uni, ":ᚨᛒᚳᛏᛁᚠᚳᚺᛁᛇᚳᛚᛗᚾᚨᛒᚳᛉᛋᛏᚢᚢᚢᚳᛋᛇᛉ")
def test_712_norm_rune(self) -> None:
uni = unicoder.convert("futork", base_abcdefghijklmnopqrstuvwxyz)
self.assertEqual(uni, ":ᚨᛒᚳᛏᛁᚠᚳᚺᛁᛇᚳᛚᛗᚾᚨᛒᚳᛉᛋᛏᚢᚢᚢᚳᛋᛇᛉ")
def test_713_norm_rune(self) -> None:
uni = unicoder.convert("viking", base_ABCDEFGHIJKLMNOPQRSTUVWXYZ)
self.assertEqual(uni, ":ᚨᛒᚳᛏᛁᚠᚳᚺᛁᛇᚳᛚᛗᚾᚨᛒᚳᛉᛋᛏᚢᚢᚢᚳᛋᛇᛉ")
def test_714_norm_rune(self) -> None:
uni = unicoder.convert("futork", base_ABCDEFGHIJKLMNOPQRSTUVWXYZ)
self.assertEqual(uni, ":ᚨᛒᚳᛏᛁᚠᚳᚺᛁᛇᚳᛚᛗᚾᚨᛒᚳᛉᛋᛏᚢᚢᚢᚳᛋᛇᛉ")
def test_715_norm_rune(self) -> None:
uni = unicoder.viking(base_abcdefghijklmnopqrstuvwxyz)
self.assertEqual(uni, ":ᚨᛒᚳᛏᛁᚠᚳᚺᛁᛇᚳᛚᛗᚾᚨᛒᚳᛉᛋᛏᚢᚢᚢᚳᛋᛇᛉ")
def test_716_norm_rune(self) -> None:
uni = unicoder.viking(base_abcdefghijklmnopqrstuvwxyz)
self.assertEqual(uni, ":ᚨᛒᚳᛏᛁᚠᚳᚺᛁᛇᚳᛚᛗᚾᚨᛒᚳᛉᛋᛏᚢᚢᚢᚳᛋᛇᛉ")
def test_717_norm_rune(self) -> None:
uni = unicoder.viking(base_ABCDEFGHIJKLMNOPQRSTUVWXYZ)
self.assertEqual(uni, ":ᚨᛒᚳᛏᛁᚠᚳᚺᛁᛇᚳᛚᛗᚾᚨᛒᚳᛉᛋᛏᚢᚢᚢᚳᛋᛇᛉ")
def test_741_norm_rune_quaengeln(self) -> None:
uni = unicoder.rune(":quaengeln")
self.assertEqual(uni, ":ᚳᚨᛖᛜᛖᛚᚾ")
def test_742_norm_rune_quaengeln(self) -> None:
uni = unicoder.rune(":QUAENGELN")
self.assertEqual(uni, ":ᚳᚨᛖᛜᛖᛚᚾ")
def test_748_norm_rune_notfound(self) -> None:
def test_788_norm_rune_notfound(self) -> None:
old = unicoder.norm_rune_lower
unicoder.norm_rune_lower = unicoder.norm_greek_upper
uni = unicoder.rune(":FOOBAR")
unicoder.norm_rune_lower = old
self.assertEqual(uni, ":foobar")
def test_749_norm_rune_notfound(self) -> None:
def test_789_norm_rune_notfound(self) -> None:
old = unicoder.norm_rune_lower
unicoder.norm_rune_lower = unicoder.norm_greek_upper
uni = unicoder.rune(":foobar")
Expand Down Expand Up @@ -1198,58 +1222,68 @@ def test_867_power(self) -> None:
uni = unicoder.power(txt)
self.assertEqual(uni, "⁽⁻²⁰⁾ (X)")
self.assertNotEqual(uni, txt)
def test_870_subscript(self) -> None:
def test_877_power(self) -> None:
txt = "^(-20) (^X)"
uni = unicoder.power(txt)
self.assertEqual(uni, "⁽⁻²⁰⁾ (Ξ)")
self.assertNotEqual(uni, txt)
def test_878_power(self) -> None:
txt = "^(-20a) (^X)"
uni = unicoder.power(txt)
self.assertEqual(uni, "⁽⁻²⁰α) (Ξ)")
self.assertNotEqual(uni, txt)
def test_880_subscript(self) -> None:
txt = "+-0123456789"
uni = unicoder.convert("subi", txt)
self.assertEqual(uni, "₊₋₀₁₂₃₄₅₆₇₈₉")
self.assertNotEqual(uni, txt)
def test_871_subscript(self) -> None:
def test_881_subscript(self) -> None:
txt = "+-0123456789"
uni = unicoder.subscript(txt)
self.assertEqual(uni, "₊₋₀₁₂₃₄₅₆₇₈₉")
self.assertNotEqual(uni, txt)
def test_874_subscript(self) -> None:
def test_884_subscript(self) -> None:
txt = "(-20)(X)"
uni = unicoder.convert("subi", txt)
self.assertEqual(uni, "₍₋₂₀₎(X)")
self.assertNotEqual(uni, txt)
def test_875_subscript(self) -> None:
def test_885_subscript(self) -> None:
txt = "(-20)(X)"
uni = unicoder.subscript(txt)
self.assertEqual(uni, "₍₋₂₀₎(X)")
self.assertNotEqual(uni, txt)
def test_880_index(self) -> None:
def test_890_index(self) -> None:
txt = "+-0123456789"
uni = unicoder.convert("index", txt)
self.assertEqual(uni, txt)
def test_881_index(self) -> None:
def test_891_index(self) -> None:
txt = "+-0123456789"
uni = unicoder.indexed(txt)
self.assertEqual(uni, txt)
def test_882_index(self) -> None:
def test_892_index(self) -> None:
txt = "_+-0123456789"
uni = unicoder.convert("index", txt)
self.assertEqual(uni, "₊₋₀₁₂₃₄₅₆₇₈₉")
self.assertNotEqual(uni, txt)
def test_883_index(self) -> None:
def test_893_index(self) -> None:
txt = "_+-0123456789"
uni = unicoder.indexed(txt)
self.assertEqual(uni, "₊₋₀₁₂₃₄₅₆₇₈₉")
self.assertNotEqual(uni, txt)
def test_884_index(self) -> None:
def test_894_index(self) -> None:
txt = "(-20)(X)"
uni = unicoder.convert("index", txt)
self.assertEqual(uni, txt)
def test_885_index(self) -> None:
def test_895_index(self) -> None:
txt = "(-20)(X)"
uni = unicoder.indexed(txt)
self.assertEqual(uni, txt)
def test_886_index(self) -> None:
def test_896_index(self) -> None:
txt = "_(-20) (X)"
uni = unicoder.convert("index", txt)
self.assertEqual(uni, "₍₋₂₀₎ (X)")
self.assertNotEqual(uni, txt)
def test_887_index(self) -> None:
def test_897_index(self) -> None:
txt = "_(-20) (X)"
uni = unicoder.indexed(txt)
self.assertEqual(uni, "₍₋₂₀₎ (X)")
Expand Down

0 comments on commit e545fa0

Please sign in to comment.