Fix backreferencing/replacement issues

Add in a way to short-circuit missing language definitions
This commit is contained in:
eston
2011-01-18 23:08:08 -08:00
parent 56bd0b9757
commit 05b53462df
@@ -13,7 +13,8 @@
EditorMode: 'code', EditorMode: 'code',
NewFile: false, NewFile: false,
HasFunctionBar: true, HasFunctionBar: true,
Debug: false Debug: false,
NoDefinitionsFor: []
}; };
var ActiveOptions = {}; var ActiveOptions = {};
@@ -61,13 +62,14 @@
// get form fields // get form fields
var oldAction = $('#gollum-editor form').attr('action'); var oldAction = $('#gollum-editor form').attr('action');
var $form = $($('#gollum-editor form').get(0)); var $form = $($('#gollum-editor form').get(0));
$form.attr('action', '/preview'); $form.attr('action', this.href);
$form.attr('target', '_blank'); $form.attr('target', '_blank');
$form.submit(); $form.submit();
$form.attr('action', oldAction); $form.attr('action', oldAction);
$form.removeAttr('target'); $form.removeAttr('target');
return false;
}); });
} }
@@ -217,8 +219,21 @@
* @return void * @return void
*/ */
loadFor: function( markup_name, on_complete ) { loadFor: function( markup_name, on_complete ) {
// Keep us from hitting 404s on our site, check the definition blacklist
if ( ActiveOptions.NoDefinitionsFor.length ) {
for ( var i=0; i < ActiveOptions.NoDefinitionsFor.length; i++ ) {
if ( markup_name == ActiveOptions.NoDefinitionsFor[i] ) {
// we don't have this. get out.
if ( typeof on_complete == 'function' ) {
on_complete( null, 'error' );
return;
}
}
}
}
// attempt to load the definition for this language // attempt to load the definition for this language
var script_uri = '/javascript/gollum-editor/langs/' + markup_name + '.js'; var script_uri = '/javascripts/editor/langs/' + markup_name + '.js';
$.ajax({ $.ajax({
url: script_uri, url: script_uri,
dataType: 'script', dataType: 'script',
@@ -475,27 +490,21 @@
if ( definitionObject.search && if ( definitionObject.search &&
typeof definitionObject.search == 'object' ) { typeof definitionObject.search == 'object' ) {
debug('Replacing search Regex'); debug('Replacing search Regex');
searchExp = definitionObject.search; searchExp = null;
searchExp = new RegExp ( definitionObject.search );
debug( searchExp ); debug( searchExp );
} }
debug(repText); debug('repText is ' + '"' + repText + '"');
// replace text // replace text
if ( definitionObject.replace && if ( definitionObject.replace &&
typeof definitionObject.replace == 'string' ) { typeof definitionObject.replace == 'string' ) {
debug('Running replacement - using ' + definitionObject.replace); debug('Running replacement - using ' + definitionObject.replace);
var rt = definitionObject.replace; var rt = definitionObject.replace;
var matches = searchExp.exec( repText ); repText = repText.replace( searchExp, rt );
if ( matches && matches.length > 1 ) { // remove backreferences
debug(matches); repText = repText.replace( /\$[\d]/g, '' );
for ( var i = 1; i < matches.length; i++ ) {
var searchStr = '$' + i; if ( repText === '' ) {
debug('searching for ' + searchStr + ' to replace with ' + matches[i]);
rt = rt.replace( searchStr, matches[i] );
}
// remove any excess backreferences from the replace string
rt = rt.replace( /\$[\d]/g, '' );
repText = rt;
} else if ( repText === '' ) {
debug('Search string is empty'); debug('Search string is empty');
// find position of $1 - this is where we will place the cursor // find position of $1 - this is where we will place the cursor
@@ -620,6 +629,7 @@
refresh: function() { refresh: function() {
if ( EditorHas.functionBar() ) { if ( EditorHas.functionBar() ) {
debug('Refreshing function bar');
if ( LanguageDefinition.isValid() ) { if ( LanguageDefinition.isValid() ) {
$('#gollum-editor-function-bar a.function-button').unbind('click'); $('#gollum-editor-function-bar a.function-button').unbind('click');
FunctionBar.activate(); FunctionBar.activate();
@@ -982,6 +992,9 @@
if ( $('#function-help').length ) { if ( $('#function-help').length ) {
$('#function-help').addClass('disabled'); $('#function-help').addClass('disabled');
} }
if ( Help.isShown() ) {
Help.hide();
}
} else { } else {
Help._ACTIVE_HELP_LANG = name; Help._ACTIVE_HELP_LANG = name;
if ( $("#function-help").length ) { if ( $("#function-help").length ) {