Skip to content

Instantly share code, notes, and snippets.

@ROpdebee
Created February 5, 2021 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ROpdebee/159f2acb83ca545c7f60b9497555608e to your computer and use it in GitHub Desktop.
Save ROpdebee/159f2acb83ca545c7f60b9497555608e to your computer and use it in GitHub Desktop.
PoC userscript for a “Guess Punctuation” button, based on @kellnerd’s bookmarklet
// ==UserScript==
// @name Guess Punctuation
// @namespace Violentmonkey Scripts
// @match https://*.musicbrainz.org/release/*/edit
// @grant none
// @version 1.0
// @author ROpdebee
// @description 03/02/2021, 22:18:56
// ==/UserScript==
// Credits for the guess punctuation functionality to kellnerd @ https://community.metabrainz.org/t/bookmarklet-to-guess-unicode-punctuation-of-titles/517356
(() => {
/**
* Transforms the values of the selected input fields using the given substitution rules.
* @param {string} inputSelector CSS selector of the input fields.
* @param {(string|RegExp)[][]} substitutionRules Pairs of values for search & replace.
*/
function transformInputValues(inputSelector, substitutionRules) {
$(inputSelector).css('background-color', ''); // disable possible previously highlighted changes
$(inputSelector).each((_index, input) => {
let value = input.value;
substitutionRules.forEach(([searchValue, newValue]) => {
value = value.replace(searchValue, newValue);
console.debug(value);
});
if (value != input.value) { // update and highlight changed values
$(input).val(value)
.css('background-color', 'yellow');
}
});
}
const transformationRules = [
[/(?<=\W|^)"(.+?)"(?=\W|$)/g, '“$1”'], // double quoted text
[/(?<=\W|^)'(.+?)'(?=\W|$)/g, '‘$1’'], // single quoted text
// ... which is enclosed by non-word characters or at the beginning/end of the title
[/(\d+)"/g, '$1″'], // double primes, e.g. for 12″
[/(\d+)'(\d+)/g, '$1′$2'], // single primes, e.g. for 3′42″ but not for 70’s
[/'/g, '’'], // ... and finally the apostrophes should be remaining
[/(?<!\.)\.{3}(?!\.)/g, '…'], // horizontal ellipsis (but not more than three dots)
[/ - /g, ' – '], // en dash as separator
[/(\d+)-(\d+)/g, '$1–$2'], // en dash for ranges where it means "to", e.g. 1965–1972
[/-/g, '‐'], // ... and finally the hyphens should be remaining
// difficult to find rules for: em dash (rare), minus (very rare), figure dash (very rare)
// TODO: localize quotes using release/lyrics language
];
$(document).ready(() => {
let btn = document.createElement('button');
btn.innerText = 'Guess Punctuation';
$(btn).click((e) => {
e.preventDefault();
transformInputValues('input.track-name', transformationRules);
});
$('.guesscase .buttons').append(btn)
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment