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

support for '\unicode' #76

Open
lopo12123 opened this issue May 31, 2023 · 0 comments
Open

support for '\unicode' #76

lopo12123 opened this issue May 31, 2023 · 0 comments

Comments

@lopo12123
Copy link

This library does not support rendering \unicode{xxx} into unicode characters, you can match such formulas before parsing and replace them with unicode characters to display

Here I use regular expressions to filter and replace, it should be able to support \unicode{xxx}, hope you can add it if you have time

/// replace all unicode directives before parsing
///
/// rule of '\unicode{xxx}'
/// - \unicode{ [^x][0-9a-zA-Z]* }  -- decimal
/// - \unicode{ x[0-9a-zA-Z]* } -- hexadecimal
String unicodeFilter(String raw) {
  var reg = RegExp(
    r'\${1,2}\\unicode\{(?<isHex>x)?(?<val>[0-9a-eA-E]*)\}\${1,2}',
    dotAll: true,
  );

  String replaced = '';
  int p = 0;

  reg.allMatches(raw).forEach((part) {
    if (part.start > p) {
      replaced += raw.substring(p, part.start);
    }

    bool isHex = part.namedGroup('isHex') == 'x';
    int? v = int.tryParse(part.namedGroup('val') ?? '', radix: isHex ? 16 : 10);

    if (v != null) replaced += String.fromCharCode(v);

    p = part.end;
  });

  return replaced;
}

image

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

1 participant