Skip to content

Commit

Permalink
Added support for arbitrary fractions.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdulcet committed Dec 10, 2024
1 parent 46b6bf6 commit 7669348
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 34 deletions.
6 changes: 1 addition & 5 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -404,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 @@ -414,10 +414,6 @@
"code_arrow": {
"content": "$2",
"example": "<code>--></code>"
},
"code_fraction": {
"content": "$3",
"example": "<code>1/4</code>"
}
}
},
Expand Down
8 changes: 6 additions & 2 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 @@ -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 Down Expand Up @@ -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
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
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
76 changes: 54 additions & 22 deletions src/content_scripts/autocorrect.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
"use strict";

const fractions = Object.freeze({
"¼": 1 / 4,
"½": 1 / 2,
"¾": 3 / 4,
"⅐": 1 / 7,
"⅑": 1 / 9,
"⅒": 1 / 10,
"⅓": 1 / 3,
"⅔": 2 / 3,
"⅕": 1 / 5,
"⅖": 2 / 5,
"⅗": 3 / 5,
"⅘": 4 / 5,
"⅙": 1 / 6,
"⅚": 5 / 6,
"⅛": 1 / 8,
"⅜": 3 / 8,
"⅝": 5 / 8,
"⅞": 7 / 8
const afractions = Object.freeze({
"¼": [1, 4],
"½": [1, 2],
"¾": [3, 4],
"⅐": [1, 7],
"⅑": [1, 9],
"⅒": [1, 10],
"⅓": [1, 3],
"⅔": [2, 3],
"⅕": [1, 5],
"⅖": [2, 5],
"⅗": [3, 5],
"⅘": [4, 5],
"⅙": [1, 6],
"⅚": [5, 6],
"⅛": [1, 8],
"⅜": [3, 8],
"⅝": [5, 8],
"⅞": [7, 8]
});

const fractions = Object.freeze(Object.fromEntries(Object.entries(afractions).map(([f, [n, d]]) => [f, n / d])));

const constants = Object.freeze({
π: Math.PI,
e: Math.E
Expand All @@ -41,6 +43,7 @@ let lastCaretPosition; // Last caret position
let enabled = false;
let quotes = true;
let fracts = true;
let numbers = true;

let autocorrections = {};

Expand Down Expand Up @@ -281,7 +284,7 @@ function autocorrect(event) {
running = true;
const { target } = event;
const caretposition = getCaretPosition(target);
if (caretposition) {
if (caretposition != null) {
const value = target.value || target.innerText;
let deletecount = 0;
let insert = ["insertLineBreak", "insertParagraph"].includes(event.inputType) ? "\n" : event.data;
Expand Down Expand Up @@ -314,11 +317,39 @@ function autocorrect(event) {
output = true;
}
} else {
// Convert fractions and mathematical constants to Unicode characters
// Convert fractions to Unicode characters
if (!output && fracts) {
const fractionRegex = /(?<!\/\d*)(?<numerator>\d+)\/(?<denominator>\d+)$/u;
const previousText = value.slice(0, caretposition);
const regexResult = fractionRegex.exec(previousText);
if (regexResult && insert !== "/") {
const text = value.slice(0, caretposition) + inserted;
const aregexResult = fractionRegex.exec(text);
if (!aregexResult) {
const [fraction] = regexResult;
const numerator = Number.parseInt(regexResult.groups.numerator, 10);
const denominator = Number.parseInt(regexResult.groups.denominator, 10);
const result = Object.entries(afractions).find(([, [anumerator, adenominator]]) => anumerator === numerator && adenominator === denominator);
let label;
if (result) {
[label] = result;
} else {
label = `${numerator}\u2044${denominator}`;
}
const index = firstDifferenceIndex(label, fraction);
if (index >= 0) {
insert = label.slice(index) + inserted;
deletecount = fraction.length - index;
output = true;
}
}
}
}
// Convert numbers with fractions and mathematical constants to Unicode characters
if (!output && numbers) {
// Numbers regular expression: https://regex101.com/r/7jUaSP/10
// Do not match version numbers: https://github.com/rugk/unicodify/issues/40
const numberRegex = /(?<!\.)\d+(?<fractionpart>\.\d+)?$/u;
const numberRegex = /(?<!\.\d*)\d+(?<fractionpart>\.\d+)?$/u;
const previousText = value.slice(0, caretposition);
const regexResult = numberRegex.exec(previousText);
if (regexResult && insert !== ".") {
Expand Down Expand Up @@ -408,6 +439,7 @@ function handleResponse(message, _sender) {
enabled,
quotes,
fracts,
numbers,
autocorrections,
longest,
symbolpatterns,
Expand Down
12 changes: 10 additions & 2 deletions src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ <h1 data-i18n="__MSG_titleUnicodeAutocorrection__">Unicode autocorrection</h1>
<label
data-i18n="__MSG_optionAutocorrectSymbols__" for="autocorrectSymbols">Autocorrect Unicode symbols</label>
</div>
<span data-i18n="__MSG_optionAutocorrectSymbolsDescr__" data-opt-i18n-keep-children class="line indent helper-text">For example, this will replace <code>--</code> with –, <code>--></code> with ⟶ and <code>1/4</code> with ¼.</span>
<span data-i18n="__MSG_optionAutocorrectSymbolsDescr__" data-opt-i18n-keep-children class="line indent helper-text">For example, this will replace <code>--</code> with – and <code>--></code> with ⟶.</span>
</li>

<li>
Expand All @@ -103,7 +103,15 @@ <h1 data-i18n="__MSG_titleUnicodeAutocorrection__">Unicode autocorrection</h1>
<li>
<div class="line">
<input class="setting save-on-change" type="checkbox" id="autocorrectUnicodeFracts" data-optiongroup="autocorrect" name="autocorrectUnicodeFracts">
<label data-i18n="__MSG_optionConvertFractions__" data-opt-i18n-keep-children for="autocorrectUnicodeFracts">Convert fractions and mathematical constants to Unicode characters</label>
<label data-i18n="" data-opt-i18n-keep-children for="autocorrectUnicodeFracts">Convert fractions to Unicode characters</label>
</div>
<span data-i18n="" data-opt-i18n-keep-children class="line indent helper-text">For example, this will replace <code>1/4</code> with ¼.</span>
</li>

<li>
<div class="line">
<input class="setting save-on-change" type="checkbox" id="autocorrectUnicodeNumbers" data-optiongroup="autocorrect" name="autocorrectUnicodeNumbers">
<label data-i18n="__MSG_optionConvertFractions__" data-opt-i18n-keep-children for="autocorrectUnicodeNumbers">Convert numbers with fractions and mathematical constants to Unicode characters</label>
</div>
<span data-i18n="__MSG_optionConvertFractionsDescr__" data-opt-i18n-keep-children class="line indent helper-text">For example, this will replace <code>1234.25</code> with 1234¼.</span>
</li>
Expand Down

0 comments on commit 7669348

Please sign in to comment.