Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversion for sub/super-script #2

Closed
zyv opened this issue Feb 24, 2021 · 6 comments
Closed

Add conversion for sub/super-script #2

zyv opened this issue Feb 24, 2021 · 6 comments

Comments

@zyv
Copy link
Contributor

zyv commented Feb 24, 2021

We use ^superscript^ and ~subscript~ in our projects.

Could you please add translation according to the following rules:

  1. If unicode conversion of the complete marked text possible, then do it: https://gist.github.com/molomby/9bc092e4a125f529ae362de7e46e8176
  2. If no unicode conversion is possible, then wrap in <sup> & <sub> tags

Thanks!

@catcombo
Copy link
Owner

catcombo commented Feb 24, 2021

I vote for the second rule only. The first one needs much more attention to implement and to maintain:

  1. Create mapping of all available characters and it's subscript/superscript counterparts. Need to keep the mapping up to date as the new unicode sub/super-script characters are added.
  2. After conversion with str.translate need to compare it char by char with the original string to confirm the 100% conversion. At the moment, I don't know how to implement this efficiently.

@zyv
Copy link
Contributor Author

zyv commented Feb 24, 2021

In as far as mapping is concerned, I have linked a good one and we can assume nothing changes in the future in the relevant code planes.

As to comparison, I though that's actually quite easy:

FROM = "..."
TO = "..."

translated = content.translate(str.maketrans(FROM, TO))

if all(src != dst for src, dst in zip(content, translated)):
    return content
else:
    return f"<sup>{content}</sup>"

We have enough cores to not to care about efficiency :)

@catcombo
Copy link
Owner

catcombo commented Feb 25, 2021

Ok, I am ready to exchange efficiency for beautiful and elegant code :)

@zyv
Copy link
Contributor Author

zyv commented Feb 25, 2021

👍

@catcombo
Copy link
Owner

I can't help thinking this solution will lead me to the unpredictable bugs. I would prefer to implement the simplest solution so that you can extend it as you see fit.

catcombo added a commit that referenced this issue Feb 25, 2021
catcombo added a commit that referenced this issue Feb 25, 2021
@zyv
Copy link
Contributor Author

zyv commented Feb 26, 2021

Fine, I can live with that, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants