Created
April 16, 2012 16:47
-
-
Save shawnchin/2399902 to your computer and use it in GitHub Desktop.
UserScript to load SyntaxHighlighter on-demand within the CCPForge CVS/SVN code browser
This file contains 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 characters
// ==UserScript== | |
// @name GForge SCM Syntax Highlighter | |
// @version 1.1.1 | |
// @namespace http://shawnchin.github.com | |
// @description Provides syntax highlighting for CVS/SVN/Git Code Viewer | |
// @include http://*/gf/project/*/scm*/?action=browse&*view=markup* | |
// @include http://*/gf/project/*/scmgit/*;a=blob;* | |
// ==/UserScript== | |
// Note that GForge already loads jQuery (a rather old version!) | |
function with_jquery(f) { | |
var script = document.createElement("script"); | |
script.type = "text/javascript"; | |
script.textContent = "(" + f.toString() + ")(jQuery)"; | |
document.body.appendChild(script); | |
}; | |
with_jquery(function ($) { | |
$(function () { | |
//** selfupdatingscript starts here | |
// (see https://gist.github.com/raw/874058/selfupdatingscript.user.js) | |
var VERSION = '1.1.1'; //<<<<<<<<<<<<** DON'T FORGET TO UPDATE THIS!!!! ***** | |
var URL = "https://gist.github.com/raw/2399902/GForgeCodeSyntaxHighlight.user.js"; | |
if(window["selfUpdaterCallback:" + URL]) { | |
window["selfUpdaterCallback:" + URL](VERSION); | |
return; | |
} | |
function updateCheck(notifier) { | |
window["selfUpdaterCallback:" + URL] = function (newver) { | |
if(newver > VERSION) notifier(newver, VERSION, URL); | |
} | |
$("<script />").attr("src", URL).appendTo("head"); | |
} | |
//**selfupdatingscript ends here (except for call to updateCheck further down in code) | |
function is_scmgit() { | |
return (location.href.split("/")[6] == "scmgit"); | |
} | |
// Convert divs created by scmgit to a plain <pre> as used by CVS/SVN viewer | |
function convert_scmgit($pre) { | |
$(".page_body:first").children("div.pre").each(function() { | |
$this = jQuery(this); | |
$this.find("a").remove(); | |
$pre.append($this.text().replace(/ /g, " ") + "\n"); | |
$this.remove(); | |
}); | |
} | |
// Inject links to remote CSS files | |
function load_css(base) { | |
var c1 = document.createElement("link"); | |
c1.type = 'text/css'; | |
c1.rel = 'stylesheet'; | |
c1.href = base + 'styles/shCore.css'; | |
var c2 = document.createElement("link"); | |
c2.type = 'text/css'; | |
c2.rel = 'stylesheet'; | |
c2.href = base + 'styles/shThemeDefault.css'; | |
document.getElementsByTagName('head')[0].appendChild(c1).appendChild(c2); | |
} | |
// Convert paths for autoloading SyntaxHighlighter brushes | |
function path() { | |
var args = arguments, result = []; | |
for(var i = 0; i < args.length; i++) | |
result.push(args[i].replace('@', 'http://alexgorbatchev.com/pub/sh/current/scripts/')); | |
return result | |
}; | |
// Dynamically load and initialise SyntaxHighlighter | |
function load_syntaxhighlighter($, callback) { | |
var base_url = 'http://alexgorbatchev.com/pub/sh/current/'; | |
var url_core = base_url + 'scripts/shCore.js'; | |
var url_loader = base_url + 'scripts/shAutoloader.js'; | |
load_css(base_url); | |
$.getScript(url_core, function(data, textStatus, jqxhr) { | |
$.getScript(url_loader, function(data, textStatus, jqxhr) { | |
console.log("loaded"); | |
SyntaxHighlighter.autoloader.apply(null, path( | |
//'applescript @shBrushAppleScript.js', | |
//'actionscript3 as3 @shBrushAS3.js', | |
'bash shell @shBrushBash.js', | |
//'coldfusion cf @shBrushColdFusion.js', | |
'cpp c @shBrushCpp.js', | |
'c# c-sharp csharp @shBrushCSharp.js', | |
'css @shBrushCss.js', | |
'delphi pascal @shBrushDelphi.js', | |
'diff patch pas @shBrushDiff.js', | |
'erl erlang @shBrushErlang.js', | |
'fortran https://gist.github.com/raw/517349/shBrushFortran.js', | |
//'groovy @shBrushGroovy.js', | |
'java @shBrushJava.js', | |
//'jfx javafx @shBrushJavaFX.js', | |
'js jscript javascript @shBrushJScript.js', | |
'perl pl @shBrushPerl.js', | |
'php @shBrushPhp.js', | |
//'text plain @shBrushPlain.js', | |
'py python @shBrushPython.js', | |
'ruby rails ror rb @shBrushRuby.js', | |
//'sass scss @shBrushSass.js', | |
//'scala @shBrushScala.js', | |
'sql @shBrushSql.js', | |
'vb vbnet @shBrushVb.js', | |
'xml xhtml xslt html @shBrushXml.js' | |
)); | |
SyntaxHighlighter.defaults.toolbar = false; | |
SyntaxHighlighter.defaults.gutter = false; | |
SyntaxHighlighter.all(); | |
callback(); | |
}); | |
}); | |
} | |
if (is_scmgit()) { | |
var $banner = $("div.page_path:first"); | |
var $code_container = $("<div></div>").insertAfter($banner); | |
var $code_pre = $("<pre></pre>").appendTo($code_container); | |
} else { | |
var $code_container = $('#vc_markup'); | |
var $code_pre = $code_container.find("pre:first-child"); | |
var $banner = $code_container.prev(".vc_summary"); | |
} | |
var choices = { | |
"bash" : "Bash / Shell", | |
"cpp" : "C / C++", | |
"csharp" : "C#", | |
"css" : "CSS", | |
"delphi" : "Delphi / Pascal", | |
"diff" : "diff / patch", | |
"erlang" : "Erlang", | |
"fortran" : "Fortran", | |
"java" : "Java", | |
"js" : "Javascript", | |
"perl" : "Perl", | |
"php" : "PHP", | |
"python" : "Python", | |
"ruby" : "Ruby / Rails", | |
"sql" : "SQL", | |
"vb" : "VB / VB.Net", | |
"xml" : "XML / XHTML / XSLT / HTML" | |
}; | |
var $dropdown = $("<select><option value=''>" + | |
" ( Syntax Highlighting ) </option></select>"); | |
for (var key in choices) { | |
$("<option value='" + key + "'>" + choices[key] + "</option>") | |
.appendTo($dropdown); | |
} | |
$dropdown.css("float", "right").change(function() { | |
$dropdown.attr("disabled", "disabled") | |
$selected = $dropdown.find("option:selected"); | |
$selected.text($selected.text() + " (loading ...)"); | |
if (is_scmgit()) convert_scmgit($code_pre); | |
$code_pre.addClass("brush: " + this.value); | |
load_syntaxhighlighter($, function() { | |
$dropdown.slideUp(); | |
}); | |
}).insertBefore($code_container); | |
// Check for script updates. Display message if new version available. | |
updateCheck(function(newver, oldver, url) { | |
var msg = "A new version of the GForgeCodeSyntaxHighlight userscript is now available (" + | |
newver + ", your installed version is " + oldver + "). <br /><br />" + | |
"The latest version is available here : <a href='" + url + "'>" + url + "</a>."; | |
$msgbox = $("<div></div>").html(msg) | |
.css("border", "2px solid rgb(255, 200, 200)") | |
.css("padding", "10px") | |
.css("margin", "3px") | |
.css("color", "rgb(200, 10, 10)") | |
.hide().insertBefore($dropdown).slideDown(); | |
$("<a href='#' style='display:block; float:right;'>close</a>").click(function(e) { | |
e.preventDefault(); | |
$msgbox.slideUp(); | |
}).appendTo($msgbox); | |
}); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment