Skip to content

Instantly share code, notes, and snippets.

@copitz
Last active November 4, 2017 04:53
Show Gist options
  • Save copitz/2243a2050d8d3c435df280acf4fce4b2 to your computer and use it in GitHub Desktop.
Save copitz/2243a2050d8d3c435df280acf4fce4b2 to your computer and use it in GitHub Desktop.
User script to fix missing examples on Apache Camel Docs - use with e.g. Tampermonkey
// ==UserScript==
// @name Fix Apache Camel Docs Snippets
// @namespace http://netresearch.de/
// @version 0.1
// @description Fix missing examples on Apache Camel Docs
// @author Christian Opitz
// @include http://camel.apache.org/*
// @include https://raw.githubusercontent.com/apache/camel/master/*
// @require http://camel.apache.org/styles/highlighter/scripts/shCore.js
// @require http://camel.apache.org/styles/highlighter/scripts/shBrushJava.js
// @require http://camel.apache.org/styles/highlighter/scripts/shBrushXml.js
// @require http://camel.apache.org/styles/highlighter/scripts/shBrushPlain.js
// @resource shCoreCamel http://camel.apache.org/styles/highlighter/styles/shCoreCamel.css
// @resource shThemeCamel http://camel.apache.org/styles/highlighter/styles/shThemeCamel.css
// @grant GM_addStyle
// @grant GM_getResourceText
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
GM_addStyle(GM_getResourceText("shCoreCamel"));
GM_addStyle(GM_getResourceText("shThemeCamel"));
GM_addStyle(
'.camel-snippet { border: 1px dashed #3c78b5; margin: 1em 0; position: relative; } ' +
'.camel-snippet .syntaxhighlighter { padding: 10px 5px; box-sizing: border-box; margin: 0 !important; } ' +
'.camel-snippet > a { position: absolute; top: 0; right: 5px; }'
);
var urls = {};
var remaining = 0;
function resolveSnippet(element, config, source) {
var regexp = new RegExp('START SNIPPET:\\s+' + config.id + '(\\n|.)+END SNIPPET:\\s+' + config.id);
var match = regexp.exec(source);
var href = "https://github.com/apache/camel/blob/master/" + config.path;
var wrapper = document.createElement('div');
wrapper.classList.add('camel-snippet');
if (match) {
href += "#L" + ((source.substring(0, match.index).match(/\n/g) || []).length + 2);
var lines = match[0].split("\n");
lines.shift();
lines.pop();
var indention;
for (var l = 0; l < lines.length; l++) {
var line = lines[l].replace(/\t/, ' ');
if (l === 0) {
var indentionMatch = /^(\s+)/.exec(line);
if (indentionMatch) {
indention = new RegExp('^' + indentionMatch[1]);
}
}
if (indention) {
lines[l] = line.replace(indention, '');
}
}
var code = document.createElement('pre');
wrapper.appendChild(code);
code.setAttribute('class', 'gutter: false toolbar: false brush: ' + config.lang);
code.innerText = lines.join("\n");
code.innerHTML = code.innerHTML.replace(/<br\s*\/?>/g, "\n");
} else {
var error = document.createElement('div');
error.innerText = "Could not find snippet " + config.id + " on " + config.url;
error.classList.add('camel-snippet-error');
wrapper.appendChild(innerText);
}
var link = document.createElement('a');
link.setAttribute('href', href);
link.setAttribute('target', '_blank');
link.setAttribute('title', 'Open source file');
link.innerText = '↗';
wrapper.appendChild(link);
element.parentNode.insertBefore(wrapper, element);
element.parentNode.removeChild(element);
}
function loadSnippet(element, config) {
if (!config.url || config.url.substr(0, 12) !== 'camel/trunk/' || !config.id) {
return;
}
config.path = config.url.substr(12);
remaining++;
if (urls[config.url]) {
urls[config.url].push({element: element, config: config});
return;
}
urls[config.url] = [{element: element, config: config}];
GM_xmlhttpRequest({
method: "GET",
url: "https://raw.githubusercontent.com/apache/camel/master/" + config.path,
onload: function(response) {
if (response.status === 200) {
for (var i = 0; i < urls[config.url].length; i++) {
remaining--;
resolveSnippet(urls[config.url][i].element, urls[config.url][i].config, response.responseText);
}
} else {
console.log("Error", response);
}
if (!remaining) {
SyntaxHighlighter.all();
}
}
});
}
var codeElements = document.querySelectorAll('plain-text-body');
for (var i = 0; i < codeElements.length; i++) {
var codeElement = codeElements[i];
var snippet = codeElement.innerText;
if (snippet.substr(0, 9) === '{snippet:') {
var config = {};
var parts = snippet.substring(9, snippet.length - 1).split('|');
for (var j = 0; j < parts.length; j++) {
var part = parts[j];
var equalsPos = part.indexOf('=');
if (equalsPos > 0) {
config[part.substr(0, equalsPos)] = part.substr(equalsPos + 1);
}
}
loadSnippet(codeElement, config);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment