Skip to content

Instantly share code, notes, and snippets.

@lgessler
Created February 17, 2016 06:05
Show Gist options
  • Select an option

  • Save lgessler/aa6fba6c7a4dfcb353f6 to your computer and use it in GitHub Desktop.

Select an option

Save lgessler/aa6fba6c7a4dfcb353f6 to your computer and use it in GitHub Desktop.
TamperMonkey script that lets open my browser console and generate concordances from my MiniMongo collection over a regex
// ==UserScript==
// @name Bindi Assistant
// @namespace lgessler
// @version 0.1
// @description Offer convenience functions
// @author Luke Gessler
// @match https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo/related?hl=en
// @grant unsafeWindow
// ==/UserScript==
/* jshint -W097 */
'use strict';
unsafeWindow.on = true;
// Your code here...
unsafeWindow.grep = function(pat) {
var docs = Forms.find({phonetic: new RegExp(pat, "g")}).fetch();
_.pluck(docs, 'phonetic').forEach(function(form) {
console.log(form);
});
}
/*
var combining = [
' ̥',
' ̃',
' ̞',
'̘',
'̃'
]
*/
var combining = [
'\u031E', // shift down
'\u0318', // shift left
'\u0303', // nasalized
'\u0325' // devoiced
];
unsafeWindow.pgrep = function(pat) {
var regex = new RegExp(pat, "g");
var occ;
var results = [];
var maxInd = 0;
var docs = Forms.find({phonetic: regex}).fetch();
_.pluck(docs, 'phonetic').forEach(function(form) {
while((occ = regex.exec(form)) != null) {
if (maxInd < occ.index) maxInd = occ.index;
results.push(occ);
}
});
//just do this so we get consistent results
maxInd = 20;
console.log(" ".repeat(maxInd) + "v");
results.forEach(function(occ) {
var padding = (" ".repeat(maxInd-occ.index));
// account for combining characters
var beforeMatch = occ.input.substring(0, occ.index);
combining.forEach(function(c) {
padding += " ".repeat((beforeMatch.match(new RegExp(c, "g"))||[]).length);
});
console.log(padding + occ.input);
//console.log(padding + occ.input.substring(0, occ.index) + " " + occ.input.substring(occ.index, occ.index+1) + " " + occ.input.substring(occ.index+1));
});
console.log(" ".repeat(maxInd) + "^");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment