User:Alien333/poemise.js
Jump to navigation
Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
- Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
For details and instructions about other browsers, see Wikipedia:Bypass your cache.
Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the central discussion page, Scriptorium. The code will be executed when previewing this page under some skins, including Monobook. You can in the interim if you wish to refresh the content sooner under another skin. |
This script seems to have a documentation page at User:Alien333/poemise. |
/* global mw,$ */
"use strict";
mw.loader.using(['mediawiki.util'], () => { $(() => {
if (mw.config.get('wgCanonicalNamespace') == 'Page' && mw.config.get('wgPageContentModel') == 'proofread-page' && ['edit', 'submit'].includes(mw.config.get('wgAction'))) {
var poemisebtn = mw.util.addPortletLink('p-tb', '#', 'Poemise', 'poemise-btn', 'Poemise.');
$(poemisebtn).click(event => { event.preventDefault(); poemise() });
}
})});
var concat = (ll) => [].concat.apply([], ll); // merge array array
var multiplyArray = (arr, length) => Array.from({ length }, () => arr).flat();
var ins = (s, c) => s.indexOf(c) != -1;
var ind = (n) => ":".repeat(n);
var pr = (s) => // Process Rhyme pattern
(s=="")?[]:
(ins(s, ";"))?concat(s.split(";").map(pr)):
(ins(s, "*"))?multiplyArray(pr(s.split("*").slice(1).join("*")), Number(s.split("*")[0])):
(ins(s, ","))?concat(s.split(",").map(pr)):
(s[0]=="!")?[ind(Number(s.slice(1)))]: // multiple-digint indent
(s[0]=="'")?pr(s.slice(1)+"*0;."):
(s[0]==":")?pr(s.slice(1)+"*01;."):
(s[0]==".")?["."].concat(pr(s.slice(1))):
(s[0]=="[")?[s.match(/.*?\]/)[0].slice(1, -1)].concat(pr(s.match(/\].+/)[0].slice(1))):
(/^[0-9]/.test(s))?[ind(Number(s[0]))].concat(pr(s.slice(1))):["!"];
function poemise() { // apply poem formatting
var [l, rhyme, ri] = [$('#wpTextbox1').val().split("\n"), [""], 0]; // list of lines, indent list, rhyme index
var ei = l.findIndex((ll) => ll[0] == "-"); // end index
ei = (ei != -1)?ei:l.length;
var after = l.slice(ei).join("\n").slice(1);
l = l.slice(0, ei);
var si = l.findIndex((ll) => ll[0] == "+"); // start index
si = (si != -1)?si:0;
var before = l.slice(0, si).join("\n");
var anend = !l.some((ll) => ll[0] == "$" || (ll[1] == "$" && ll[0] == "+"));
l = ("\n" + l.slice(si).join("\n") + "\n")
.replace(/\n[+$]+/, "\n")
.trim().split("\n");
l.forEach((ll) => {
if (ll[0] == "*") {
rhyme = pr(ll.slice(1).trim());
} else if (ll[0] == "/") {
ri = Number(ll.slice(1));
}
});
l = l.filter((ll) => ll[0] != "/" && ll[0] != "*");
if (rhyme.includes("!")) {
alert("Poemise: Invalid input: unrecognized characters in indent pattern");
return;
} else if (!rhyme.some((e) => e != ".")) { // would go into infinite loop else, which is a bit annoying
alert("Poemise: Invalid input: the indent pattern is only composed of stanza breaks");
return;
}
before = (before)?before:("{{ppoem\n|start="+((rhyme[(ri < 1)?(rhyme.length+ri-1):(ri-1)] == ".")?"stanza|":"follow|"));
var [poem, li] = ["", 0]; // Line Index
ri %= rhyme.length;
while (li < l.length) {
while (rhyme[ri] == ".") {
poem += "\n";
ri = (ri+1)%rhyme.length;
}
poem += rhyme[ri]+l[li];
li += 1;
while (li < l.length && (/[a-z]/.test(l[li][0]) || l[li].length < 3)) {
poem += l[li];
li ++;
}
poem += "\n";
ri = (ri+1) % rhyme.length;
}
after = (after)?after:((anend)?("|end="+((rhyme[ri] == ".")?"stanza\n":"follow\n")):""+"}}");
$('#wpTextbox1').val(before + "\n" + poem + after);
}