Skip to content

Instantly share code, notes, and snippets.

@kurtcagle
Created October 22, 2008 21:33
Show Gist options
  • Save kurtcagle/18807 to your computer and use it in GitHub Desktop.
Save kurtcagle/18807 to your computer and use it in GitHub Desktop.
var $ = jQuery;
CmdUtils.CreateCommand({
name:"macro",
author: { name: "Kurt Cagle", email: "kurt@oreilly.com"},
contributors: ["Kurt Cagle"],
license: "Apache License, v.2",
homepage: "http://www.xforms.org/xrx/?q=ubiquity",
description: "Performs regular expression replacements of web page content.",
help: "The macro verb takes the URL of a dictionary file (which can be set up as a default using the defaultURL property) and uses it to replace each regex term with its corresponding replacement value in either a web page (if no content is selected) or within a given selection (if one has been).",
defaultURL:"http://localhost:8080/exist/rest//db/sandbox/dictionary.xml",
takes:{"sourceURL":noun_arb_text},
preview:function(pblock,sourceURL){
this._src = CmdUtils.getHtmlSelection();
var table=<table/>;
try{
this._parseEntries(sourceURL,this._src,function(entries,str){
for each (var entry in entries){
table.tr += <tr><td>{entry.term}</td><td>{entry.expr}</td></tr>;
}
pblock.innerHTML = table.toXMLString();
});
}
catch(e){}
},
execute:function(sourceURL){
this._src = CmdUtils.getHtmlSelection();
if(this._src==null){
var doc = CmdUtils.getDocumentInsecure();
this._src=doc.body.innerHTML;
this._parseEntries(sourceURL,this._src,function(entries,str){doc.body.innerHTML=str});
}
else {
this._parseEntries(sourceURL,this._src,function(entries,str){CmdUtils.setSelection(str,{});});
}
},
_src:'',
_parseEntries:function(sourceURL,src,fn){
if (sourceURL.text == ""){
sourceURL.text = this.defaultURL;
}
jQuery.get(sourceURL.text, function(dictionaryXML){
var dictionary=$(dictionaryXML);
var entries=[];
dictionary.find('entry').each(function(){
var entry = {term:$(this).find('term').text(),
expr:$(this).find('expr').text(),
isGlobal:($(this).attr('global')=='yes')?'g':'',
caseSensitive:($(this).attr('casesensitive')=='yes')?'':'i'};
var entryRE = new RegExp(entry.term,entry.isGlobal+entry.caseSensitive);
entries.push(entry);
str = src.replace(entryRE,entry.expr);
src=str;
});
fn(entries,src);
});
this._src=src;
}
});
/* The pageLoad_macro will give replacement capability upon startup, but is unstable with pages that perform an inline page reload. It's likely that this will be replaced with code built into the component in the next iteration */
/* pageLoad_macro = function(){
var sourceURL = {text:"http://localhost:8080/exist/rest//db/sandbox/dictionary.xml"};
var doc = Application.activeWindow.activeTab.document;
var src=doc.body.innerHTML;
jQuery.get(sourceURL.text, function(dictionaryXML){
var dictionary=$(dictionaryXML);
var entries=[];
dictionary.find('entry').each(function(){
var entry = {term:$(this).find('term').text(),
expr:$(this).find('expr').text(),
isGlobal:($(this).attr('global')=='yes')?'g':'',
caseSensitive:($(this).attr('casesensitive')=='yes')?'':'i'};
var entryRE = new RegExp(entry.term,entry.isGlobal+entry.caseSensitive);
entries.push(entry);
//var arr=src.split(entry.term);
//var str =arr.join(entry.expr);
var str = src.replace(entryRE,entry.expr);
src=str;
});
doc.body.innerHTML=src
});
} */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment