|
// gl "global logger" set to true in order to get enable logging |
|
var gl = false; |
|
|
|
function onOpen() { |
|
var ui = DocumentApp.getUi(); |
|
ui.createMenu('Blanker') |
|
.addItem('Blanify', 'clicked_on_blankify') |
|
.addToUi(); |
|
|
|
ui.alert( |
|
'How this works:', |
|
'You type or paste in any text, and I will blank out every x number of words you specify. Click on "Blanker" -> "Blankify"', |
|
ui.ButtonSet.OK); |
|
} |
|
|
|
/* |
|
tokenize |
|
@param s: string |
|
@return an array of strings |
|
*/ |
|
function tokenize(s){ |
|
s = s.replace(/(^\s*)|(\s*$)/gi,""); //exclude start and end white-space |
|
s = s.replace(/[ ]{2,}/gi," "); //2 or more space to 1 |
|
return s.split(' '); |
|
} |
|
|
|
/* |
|
blankify |
|
@param txt: string |
|
@param every: integer. How many words to skip over |
|
@return string |
|
*/ |
|
function blankify(txt, every) { |
|
(gl || log) && Logger.log(txt + every); |
|
|
|
var tokens = tokenize(txt); // tokens gives us an array with each element representing a word |
|
var tally = 0; |
|
var output = []; // array that will be put back onto the document |
|
|
|
for (var i = 0; i < tokens.length; i++) { |
|
var word = tokens[i]; |
|
tally += 1; |
|
//(gl || log) && Logger.log(tally); |
|
|
|
// don't count if it's a small word |
|
if (word.length <= 3) { |
|
(gl || log) && Logger.log(' <=3: ' + word); |
|
output.push(word); |
|
continue; |
|
} |
|
|
|
if (tally >= every) { |
|
(gl || log) && Logger.log('blankify: '+word); |
|
output.push("***"); |
|
tally = 0; |
|
} else { |
|
(gl || log) && Logger.log(' : ' + word); |
|
output.push(word) |
|
} |
|
} |
|
|
|
return output.join(" "); // this will convert the array into a string where a space separates each element |
|
} |
|
|
|
function test_blankify() { |
|
var log = false; |
|
|
|
var test_material = "I wonder what happens now.\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." |
|
var output = blankify(test_material, 10); |
|
update_document(output); |
|
} |
|
|
|
/* |
|
update_document |
|
@param txt: array of words |
|
*/ |
|
function update_document(txt) { |
|
var doc = DocumentApp.getActiveDocument(); |
|
var body = doc.getBody(); |
|
|
|
body.clear(); |
|
body.appendParagraph(txt); |
|
} |
|
|
|
/* |
|
clicked_on_blankify |
|
Called when user clicks on the menu |
|
*/ |
|
function clicked_on_blankify() { |
|
var log = true; |
|
(gl || log) && Logger.log('clicked_on_blankify'); |
|
|
|
var doc = DocumentApp.getActiveDocument(); |
|
var body = doc.getBody(); |
|
var ui = DocumentApp.getUi(); |
|
var result = ui.prompt( |
|
'Blank out every _ word', |
|
'Please enter the number to skip (words <= 3 are not counted at all)', |
|
ui.ButtonSet.OK_CANCEL); |
|
|
|
// Process the user's response. |
|
var button = result.getSelectedButton(); |
|
var text = result.getResponseText(); |
|
|
|
// When user clicks okay, go through the document text, adding to the a new array (output) |
|
if (button == ui.Button.OK) { |
|
|
|
var every = parseInt(text); |
|
|
|
var output = blankify(body.getText(), every); |
|
|
|
update_document(output); |
|
} |
|
} |
|
|