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

Added support for converting arbitrary fractions #96

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For instance, it converts quotes like `"these"` to `“these”`, which are typo
Additionally, you can convert text into more than 40 different font styles and casing changes.
You can enable and disable any features in the options and adjust more settings regarding the behavior of the add-on.

This extension works with modern Firefox v112 or higher, Chromium/Chrome and Thunderbird v112 or higher.
This extension works with modern Firefox v125 or higher, Chromium/Chrome and Thunderbird v125 or higher.

## Download

Expand Down
2 changes: 1 addition & 1 deletion assets/texts/en/amoDescription.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
The add-on is free/libre open-source software and developed on GitHub. <a href="https://github.com/rugk/unicodify">Fork it on GitHub</a> and contribute.
<a href="https://github.com/rugk/unicodify/contribute">There are some easy issues to start with.</a>

This extension works with modern Firefox and Thunderbird v112 or higher.
This extension works with modern Firefox and Thunderbird v125 or higher.


<b>🙋‍♀️ Contribute 🙋‍♀️</b>
Expand Down
2 changes: 1 addition & 1 deletion assets/texts/en/atnDescription.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
The add-on is free/libre open-source software and developed on GitHub. <a href="https://github.com/rugk/unicodify">Fork it on GitHub</a> and contribute.
<a href="https://github.com/rugk/unicodify/contribute">There are some easy issues to start with.</a>

This extension works with modern Firefox and Thunderbird v112 or higher.
This extension works with modern Firefox and Thunderbird v125 or higher.


<b>Contribute</b>
Expand Down
2 changes: 1 addition & 1 deletion scripts/manifests/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"browser_specific_settings": {
"gecko": {
"id": "unicodify@rugk.github.io",
"strict_min_version": "112.0"
"strict_min_version": "125.0"
}
}
}
2 changes: 1 addition & 1 deletion scripts/manifests/firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"browser_specific_settings": {
"gecko": {
"id": "unicodify@rugk.github.io",
"strict_min_version": "112.0"
"strict_min_version": "125.0"
}
}
}
2 changes: 1 addition & 1 deletion scripts/manifests/thunderbirdmanifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"browser_specific_settings": {
"gecko": {
"id": "unicodify@rugk.github.io",
"strict_min_version": "112.0"
"strict_min_version": "125.0"
}
}
}
32 changes: 25 additions & 7 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@
"message": "Change casing",
"description": "An entry in the context menu. This is an entry for the case."
},
"menuCaseSentenceCase": {
"message": "Sentence case.",
"description": "An entry in the context menu. This is an entry for the case."
},
"menuCaseLowercase": {
"message": "Lowercase",
"description": "An entry in the context menu. This is an entry for the case."
Expand Down Expand Up @@ -264,6 +268,10 @@
"message": "Double-struck",
"description": "An entry in the context menu. This is an entry for the Unicode font."
},
"menuFontOutlined": {
"message": "Outlined",
"description": "An entry in the context menu. This is an entry for the Unicode font."
},
"menuFontOther": {
"message": "Other/Emojis",
"description": "An entry in the context menu. This is an entry for the Unicode font."
Expand Down Expand Up @@ -400,7 +408,7 @@
"description": "This is an option shown in the add-on settings."
},
"optionAutocorrectSymbolsDescr": {
"message": "For example, this will replace $CODE_HYPHEN$ with –, $CODE_ARROW$ with ⟶ and $CODE_FRACTION$ with ¼.",
"message": "For example, this will replace $CODE_HYPHEN$ with – and $CODE_ARROW$ with .",
"description": "This is an option shown in the add-on settings. It describes the optionAutocorrectSymbols setting.",
"placeholders": {
"code_hyphen": {
Expand All @@ -410,10 +418,6 @@
"code_arrow": {
"content": "$2",
"example": "<code>--></code>"
},
"code_fraction": {
"content": "$3",
"example": "<code>1/4</code>"
}
}
},
Expand All @@ -426,12 +430,26 @@
"description": "This is an option shown in the add-on settings. It describes the optionReplaceQuotes setting."
},
"optionConvertFractions": {
"message": "Convert fractions and mathematical constants to Unicode characters",
"message": "Convert fractions to Unicode characters",
"description": "This is an option shown in the add-on settings."
},
"optionConvertFractionsDescr": {
"message": "For example, this will replace $CODE_NUMBER$ with 1234¼.",
"message": "For example, this will replace $CODE_FRACTION$ with ¼.",
"description": "This is an option shown in the add-on settings. It describes the optionConvertFractions setting.",
"placeholders": {
"code_fraction": {
"content": "$1",
"example": "<code>1/4</code>"
}
}
},
"optionConvertNumbers": {
"message": "Convert numbers with fractions and mathematical constants to Unicode characters",
"description": "This is an option shown in the add-on settings."
},
"optionConvertNumbersDescr": {
"message": "For example, this will replace $CODE_NUMBER$ with 1234¼.",
"description": "This is an option shown in the add-on settings. It describes the optionConvertNumbers setting.",
"placeholders": {
"code_number": {
"content": "$1",
Expand Down
20 changes: 12 additions & 8 deletions src/background/modules/AutocorrectHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import * as symbols from "/common/modules/data/Symbols.js";

const settings = {
enabled: null,
autocorrectEmojis: null,
autocorrectSymbols: null,
quotes: null,
fracts: null
fracts: null,
numbers: null
};

// Leaf node
Expand Down Expand Up @@ -44,14 +45,14 @@ function createRegEx(tree) {

for (const char in tree) {
if (char) {
const escaptedChar = char.replace(regExSpecialChars, "\\$&");
const escaptedChar = RegExp.escape ? RegExp.escape(char) : char.replaceAll(regExSpecialChars, String.raw`\$&`);

const atree = tree[char];
if (!(LEAF in atree && Object.keys(atree).length === 0)) {
if (LEAF in atree && Object.keys(atree).length === 0) {
characterClass.push(escaptedChar);
} else {
const recurse = createRegEx(atree);
alternatives.push(recurse + escaptedChar);
} else {
characterClass.push(escaptedChar);
}
}
}
Expand Down Expand Up @@ -139,7 +140,7 @@ function applySettings() {
continue;
}
const aindex = x.indexOf(y);
if (aindex >= 0) {
if (aindex !== -1) {
if (aindex < index) {
index = aindex;
length = y.length;
Expand Down Expand Up @@ -188,6 +189,7 @@ function setSettings(autocorrect) {
settings.autocorrectSymbols = autocorrect.autocorrectSymbols;
settings.quotes = autocorrect.autocorrectUnicodeQuotes;
settings.fracts = autocorrect.autocorrectUnicodeFracts;
settings.numbers = autocorrect.autocorrectUnicodeNumbers;

if (settings.enabled) {
applySettings();
Expand All @@ -213,6 +215,7 @@ function sendSettings(autocorrect) {
enabled: settings.enabled,
quotes: settings.quotes,
fracts: settings.fracts,
numbers: settings.numbers,
autocorrections,
longest,
symbolpatterns: IS_CHROME ? symbolpatterns.source : symbolpatterns,
Expand All @@ -235,7 +238,7 @@ export async function init() {
setSettings(autocorrect);

// Thunderbird
// Remove if part 3 of https://bugzilla.mozilla.org/show_bug.cgi?id=1630786#c4 is ever done
// Cannot register scripts in manifest.json file: https://bugzilla.mozilla.org/show_bug.cgi?id=1902843
if (browser.composeScripts) {
browser.composeScripts.register({
js: [
Expand All @@ -260,6 +263,7 @@ browser.runtime.onMessage.addListener((message) => {
enabled: settings.enabled,
quotes: settings.quotes,
fracts: settings.fracts,
numbers: settings.numbers,
autocorrections,
longest,
symbolpatterns: IS_CHROME ? symbolpatterns.source : symbolpatterns,
Expand Down
29 changes: 23 additions & 6 deletions src/common/modules/UnicodeTransformationHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { fontLetters, formats, CASE_ID_PREFIX, CODE_CASE_ID_PREFIX, FONT_ID_PREFIX, FORMAT_ID_PREFIX, TRANSFORMATION_TYPE } from "/common/modules/data/Fonts.js";

const segmenter = new Intl.Segmenter();
tdulcet marked this conversation as resolved.
Show resolved Hide resolved
const segmenterWord = new Intl.Segmenter([], { granularity: "word" });
const segmenterSentence = new Intl.Segmenter([], { granularity: "sentence" });

/**
* Transforms the given text according to the given transformation.
*
Expand Down Expand Up @@ -67,11 +71,23 @@ export function getTransformationType(transformationId) {
* @returns {string}
*/
function capitalizeEachWord(text) {
// Regular expression Unicode property escapes and lookbehind assertions require Firefox/Thunderbird 78
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#bcd:javascript.builtins.RegExp
// Intl.Segmenter is not yet supported by Firefox/Thunderbird: https://bugzilla.mozilla.org/show_bug.cgi?id=1423593
// \p{Alphabetic}
return text.replaceAll(/(?<=^|\P{Alpha})\p{Alpha}\S*/gu, ([h, ...t]) => h.toLocaleUpperCase() + t.join(""));
return Array.from(segmenterWord.segment(text), ({ segment, isWordLike }) => {
if (isWordLike) {
const [h, ...t] = segment;
return h.toLocaleUpperCase() + t.join("");
}
return segment;
}).join("");
}

/**
* Sentence Case.
*
* @param {string} text
* @returns {string}
*/
function sentenceCase(text) {
return Array.from(segmenterSentence.segment(text), ({ segment: [h, ...t] }) => h.toLocaleUpperCase() + t.join("")).join("");
}

/**
Expand Down Expand Up @@ -132,7 +148,7 @@ function changeFormat(text, chosenFormat) {
throw new Error(`Format ${chosenFormat} could not be processed.`);
}

return Array.from(text, (letter) => letter + format).join("");
return Array.from(segmenter.segment(text), ({ segment }) => segment + format).join("");
}

/**
Expand Down Expand Up @@ -166,6 +182,7 @@ function toggleCase(atext) {
* @type {Object.<string, function(string): string>}
*/
const changeCase = Object.freeze({
SentenceCase: (str) => sentenceCase(str.toLocaleLowerCase()),
Lowercase: (str) => str.toLocaleLowerCase(),
Uppercase: (str) => str.toLocaleUpperCase(),
CapitalizeEachWord: (str) => capitalizeEachWord(str.toLocaleLowerCase()),
Expand Down
3 changes: 2 additions & 1 deletion src/common/modules/data/DefaultSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const defaultSettings = {
enabled: false,
autocorrectSymbols: true,
autocorrectUnicodeQuotes: true,
autocorrectUnicodeFracts: true
autocorrectUnicodeFracts: true,
autocorrectUnicodeNumbers: true
},
unicodeFont: {
changeFont: true,
Expand Down
6 changes: 5 additions & 1 deletion src/common/modules/data/Fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export const menuStructure = Object.freeze({
`${FONT_ID_PREFIX}Monospace`
],
[`${FONT_ID_PREFIX}DoubleStruck`]: [
`${FONT_ID_PREFIX}DoubleStruck`
`${FONT_ID_PREFIX}DoubleStruck`,
`${FONT_ID_PREFIX}Outlined`
],
[`${SEPARATOR_ID_PREFIX}2`]: [],
[`${FONT_ID_PREFIX}Other`]: [
Expand All @@ -132,6 +133,7 @@ export const menuStructure = Object.freeze({
},
[TRANSFORMATION_TYPE.CASING]: {
[`${CASE_ID_PREFIX}Casing`]: [
`${CASE_ID_PREFIX}SentenceCase`,
`${CASE_ID_PREFIX}Lowercase`,
`${CASE_ID_PREFIX}Uppercase`,
`${CASE_ID_PREFIX}CapitalizeEachWord`,
Expand Down Expand Up @@ -176,6 +178,8 @@ const fonts = Object.freeze({
FrakturBold: "𝕬𝕭𝕮𝕯𝕰𝕱𝕲𝕳𝕴𝕵𝕶𝕷𝕸𝕹𝕺𝕻𝕼𝕽𝕾𝕿𝖀𝖁𝖂𝖃𝖄𝖅𝖆𝖇𝖈𝖉𝖊𝖋𝖌𝖍𝖎𝖏𝖐𝖑𝖒𝖓𝖔𝖕𝖖𝖗𝖘𝖙𝖚𝖛𝖜𝖝𝖞𝖟",
Monospace: "𝙰𝙱𝙲𝙳𝙴𝙵𝙶𝙷𝙸𝙹𝙺𝙻𝙼𝙽𝙾𝙿𝚀𝚁𝚂𝚃𝚄𝚅𝚆𝚇𝚈𝚉𝚊𝚋𝚌𝚍𝚎𝚏𝚐𝚑𝚒𝚓𝚔𝚕𝚖𝚗𝚘𝚙𝚚𝚛𝚜𝚝𝚞𝚟𝚠𝚡𝚢𝚣𝟶𝟷𝟸𝟹𝟺𝟻𝟼𝟽𝟾𝟿",
DoubleStruck: "𝔸𝔹ℂ𝔻𝔼𝔽𝔾ℍ𝕀𝕁𝕂𝕃𝕄ℕ𝕆ℙℚℝ𝕊𝕋𝕌𝕍𝕎𝕏𝕐ℤ𝕒𝕓𝕔𝕕𝕖𝕗𝕘𝕙𝕚𝕛𝕜𝕝𝕞𝕟𝕠𝕡𝕢𝕣𝕤𝕥𝕦𝕧𝕨𝕩𝕪𝕫𝟘𝟙𝟚𝟛𝟜𝟝𝟞𝟟𝟠𝟡",
// https://en.wikipedia.org/wiki/Symbols_for_Legacy_Computing_Supplement
Outlined: "𜳖𜳗𜳘𜳙𜳚𜳛𜳜𜳝𜳞𜳟𜳠𜳡𜳢𜳣𜳤𜳥𜳦𜳧𜳨𜳩𜳪𜳫𜳬𜳭𜳮𜳯𜳰𜳱𜳲𜳳𜳴𜳵𜳶𜳷𜳸𜳹",
Circled: " !\"#$%&'()⊛⊕,⊖⊙⊘⓪①②③④⑤⑥⑦⑧⑨:;⧀⊜⧁?@ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏ[⦸]^_`ⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ{⦶}~",
CircledBlack: "🅐🅑🅒🅓🅔🅕🅖🅗🅘🅙🅚🅛🅜🅝🅞🅟🅠🅡🅢🅣🅤🅥🅦🅧🅨🅩⓿❶❷❸❹❺❻❼❽❾",
Squared: " !\"#$%&'()⧆⊞,⊟⊡⧄0123456789:;<=>?@🄰🄱🄲🄳🄴🄵🄶🄷🄸🄹🄺🄻🄼🄽🄾🄿🅀🅁🅂🅃🅄🅅🅆🅇🅈🅉[⧅]^_`🄰🄱🄲🄳🄴🄵🄶🄷🄸🄹🄺🄻🄼🄽🄾🄿🅀🅁🅂🅃🅄🅅🅆🅇🅈🅉{|}~",
Expand Down
4 changes: 2 additions & 2 deletions src/common/modules/data/Symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const symbols = Object.freeze({
"<=>": "⇔",

// Fractions
"1/4": "¼",
/* "1/4": "¼",
"1/2": "½",
"3/4": "¾",
"1/7": "⅐",
Expand All @@ -42,7 +42,7 @@ export const symbols = Object.freeze({
"1/8": "⅛",
"3/8": "⅜",
"5/8": "⅝",
"7/8": "⅞",
"7/8": "⅞", */

// Fira Code (https://github.com/tonsky/FiraCode)
"<--": "⟵",
Expand Down
Loading