Last active
April 10, 2019 01:53
-
-
Save rossgoodwin/836df11d9e692eecbeb74566afabd9b9 to your computer and use it in GitHub Desktop.
Revisions
-
rossgoodwin revised this gist
Apr 10, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ const stopRe = /\band\b|\bor\b|\bbut\b|\bnor\b|\bfor\b|\byet\b|\bso\b|\bin\b|\bon\b|\bto\b|\bof\b|\binto\b|\bwith\b|[\.\!\?]+|[,;:]/igm; export default { } -
rossgoodwin revised this gist
Apr 10, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,7 +32,7 @@ function shortenLine(line, maxLineLength=64) { let stopIx = -1; if (stopIxsCeil.length > 0) stopIx = Math.max(...stopIxsCeil); else { let consolation = lineTrim.slice(0, maxLineLength-3).replace(/\s[^\s]*$/m, ''); return [consolation+'...', false]; } // console.log(stopIx); -
rossgoodwin created this gist
Apr 10, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ const stopRe = /\band\b|\bor\b|\bbut\b|\bnor\b|\bfor\b|\byet\b|\bso\b|\bin\b|\bon\b|\bto\b|\bof\b|\binto\b|[\.\!\?]+|[,;:]/igm; export default { } // function puncStrip(text) { // return text; // no processing // // return text.match(puncStripRe)[1]; // kyle's processing // } function shortenLine(line, maxLineLength=64) { // reduce and normalize whitespace let lineWs = line.replace(/\s+/igm, ' ').trim(); // if last char not punctuation or whitespace // assume last token is an incomplete word and drop it let isNotLastCharPunc = !lineWs.match(/\W$/m); let lineTrim = lineWs; if (isNotLastCharPunc) lineTrim = lineWs.replace(/\s[^\s]*$/m, ''); // reduce line if greater than maxLineLength let curLineLength = lineTrim.length; if (curLineLength <= maxLineLength) return [lineTrim, isNotLastCharPunc]; let match, stopIxs = []; while ((match = stopRe.exec(lineTrim)) != null) stopIxs.push(match.index); // console.log(stopIxs); let stopIxsCeil = stopIxs.filter(n=>n<=maxLineLength); // console.log(stopIxsCeil); let stopIx = -1; if (stopIxsCeil.length > 0) stopIx = Math.max(...stopIxsCeil); else { let consolation = lineTrim.slice(0, maxLineLength).replace(/\s[^\s]*$/m, ''); return [consolation+'...', false]; } // console.log(stopIx); let outLine = lineTrim.slice(0,stopIx).trim(); return [outLine, !outLine.match(/\W$/m)]; } export function capitalize(text) { return text.charAt(0).toUpperCase() + text.substr(1) } export function cleanPoem(line1, line2) { let line1tup = shortenLine(line1), line2tup = shortenLine(line2); let line1short = line1tup[0], line1punc = line1tup[1]; let line2short = line2tup[0], line2punc = line2tup[1]; return [ capitalize(line1short) + (line1punc?',':''), line2short + (line2punc?'.':'') ] }