Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
offline asciidoctor.js
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreau committed Feb 10, 2014
1 parent f33b368 commit 05bf117
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/main/webapp/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ app.controller("RCEAdocCtrl", function($scope, $rootScope, JsonService, DocRESTS

$scope.editor.commands.addCommand({
name: 'sendAsciidocToServer',
bindKey: {win: 'Altl-R', mac: 'Option-R'},
bindKey: {win: 'Alt-R', mac: 'Option-R'},
exec: function(editor) {
if ($scope.isDiffOnEditor === true){
$scope.rceAdocs[spaceID].state = "Diff are loaded, apply it or unload it by click on Compute diff.";
Expand Down Expand Up @@ -155,8 +155,8 @@ app.controller("RCEAdocCtrl", function($scope, $rootScope, JsonService, DocRESTS
WebSocketService.sendAdocSource(idAdoc, $scope.rceAdocs[idAdoc].adocSrc, $scope.rceAdocs[idAdoc].author);
}
else {
$scope.rceAdocs[idAdoc].state = "You work on OFFLINE MODE !!. You need to CONNECT to do this action.";
$scope.addAlert("danger", $scope.rceAdocs[idAdoc].state);
$scope.rceAdocs[idAdoc].state = "You work on OFFLINE MODE !";
$scope.addAlert("warning", $scope.rceAdocs[idAdoc].state);
$scope.rceAdocs[idAdoc].html5.output = OfflineService.getOfflineHTML5($scope.rceAdocs[idAdoc].adocSrc);
}
};
Expand Down Expand Up @@ -296,6 +296,7 @@ app.controller("RCEAdocCtrl", function($scope, $rootScope, JsonService, DocRESTS
//Disconnect from the server, work offline ?
$scope.disconnect = function(adSpaceID) {
$scope.rceAdocs[adSpaceID].author = "";
$scope.rceAdocs[adSpaceID].notification.writers = {};
WebSocketService.disconnect(adSpaceID);
//Activate the offline mode with storage
$scope.rceAdocs[adSpaceID].state = "You are working on Offline mode, don't forget to Backup locally !";
Expand Down
89 changes: 84 additions & 5 deletions src/main/webapp/js/offlineService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ app.factory('OfflineService', function($rootScope, $window, WebSocketService, ID

var service = {};
var LIST_O_STUFF = "adocStore";

service.addItem = function(item){
IDB.put(LIST_O_STUFF, item);
};
Expand Down Expand Up @@ -90,6 +90,7 @@ app.factory('OfflineService', function($rootScope, $window, WebSocketService, ID


service.getOfflineHTML5 = function (adocSrc){
var asciidoctorOptions = "Opal.hash2(['attributes'], {'attributes': '" + service.buildAsciidoctorOptions() + "' }) ";
var strVar="";
strVar += "<!DOCTYPE html>";
strVar += " <html>";
Expand All @@ -100,21 +101,99 @@ app.factory('OfflineService', function($rootScope, $window, WebSocketService, ID
strVar += " <\/head>";
strVar += " <body>";
strVar += " <div id=\"content\">";
strVar += adocSrc;
strVar += " <\/div>";
strVar += " <script src=\"offline\/opal.js\"><\/script>";
strVar += " <script src=\"offline\/asciidoctor.js\"><\/script>";
strVar += " <script>";
strVar += " var adoc = document.getElementById('content');";
strVar += " Opal.hash2(['attributes'], {'attributes': ['notitle!']}); ";
strVar += " document.getElementById('content').innerHTML = Opal.Asciidoctor.$render(adoc.innerHTML);";
strVar += " var generatedHtml = undefined;";
strVar += " try{ ";
strVar += " asciidoctorDocument = Opal.Asciidoctor.$load(\"";
strVar += service.stringEncode(adocSrc) ;
strVar += "\", ";
strVar += asciidoctorOptions;
strVar += ");";
strVar += " generatedHtml = asciidoctorDocument.$render();";
strVar += " }catch (e) {generatedHtml='Rendering error : ' + e.name + ':' + e.message};";
strVar += " document.getElementById('content').innerHTML = generatedHtml;";
strVar += " <\/script>";
strVar += " <\/body>";
strVar += "<\/html>";
strVar += "";
return strVar;

};

/**
* Build Asciidoctor options
*/
service.buildAsciidoctorOptions = function (items) {
var customAttributes = '';
if (items){
customAttributes = items['CUSTOM_ATTRIBUTES_KEY'];
}
var defaultAttributes = 'showtitle toc2 showauthor icons=font@';
if (customAttributes) {
attributes = defaultAttributes.concat(' ').concat(customAttributes);
} else {
attributes = defaultAttributes;
}
return attributes;
};

service.stringEncode = function (preescape) {
var escaped="";
var i=0;
for(i=0;i<preescape.length;i++)
{
escaped=escaped+service.encodeCharx(preescape.charAt(i));
}
return escaped;
};


service.encodeCharx = function(original) {
var hex=new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
var found=true;
var thecharchar=original.charAt(0);
var thechar=original.charCodeAt(0);
switch(thecharchar) {
case '\n': return "\\n"; break; //newline
case '\r': return "\\r"; break; //Carriage return
case '\'': return "\\'"; break;
case '"': return "\\\""; break;
case '\&': return "\\&"; break;
case '\\': return "\\\\"; break;
case '\t': return "\\t"; break;
case '\b': return "\\b"; break;
case '\f': return "\\f"; break;
case '/': return "\\x2F"; break;
case '<': return "\\x3C"; break;
case '>': return "\\x3E"; break;
default:
found=false;
break;
}
if(!found)
{
if(thechar>127) {
var c=thechar;
var a4=c%16;
c=Math.floor(c/16);
var a3=c%16;
c=Math.floor(c/16);
var a2=c%16;
c=Math.floor(c/16);
var a1=c%16;
// alert(a1);
return "\\u"+hex[a1]+hex[a2]+hex[a3]+hex[a4]+"";
}
else
{
return original;
}
}
};


return service;
});

0 comments on commit 05bf117

Please sign in to comment.