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 2 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"
}
}
}
10 changes: 5 additions & 5 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 @@ -400,7 +404,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 +414,6 @@
"code_arrow": {
"content": "$2",
"example": "<code>--></code>"
},
"code_fraction": {
"content": "$3",
"example": "<code>1/4</code>"
}
}
},
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 segmenter1 = new Intl.Segmenter([], { granularity: "word" });
const segmenter2 = new Intl.Segmenter([], { granularity: "sentence" });
tdulcet marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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(segmenter1.segment(text), ({ segment, isWordLike }) => {
tdulcet marked this conversation as resolved.
Show resolved Hide resolved
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(segmenter2.segment(text), ({ segment: [h, ...t] }) => h.toLocaleUpperCase() + t.join("")).join("");
tdulcet marked this conversation as resolved.
Show resolved Hide resolved
}

/**
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
1 change: 1 addition & 0 deletions src/common/modules/data/Fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,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
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