Skip to content

Commit

Permalink
bugfix: Entity encoding issues wanasit#15
Browse files Browse the repository at this point in the history
  • Loading branch information
ctgnauh committed Feb 4, 2016
1 parent a9ef57f commit 83101e2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 58 deletions.
103 changes: 53 additions & 50 deletions enml.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,56 @@
var byteLength = bytes.byteLength
var byteRemainder = byteLength % 3
var mainLength = byteLength - byteRemainder

var a, b, c, d
var chunk

// Main loop deals with bytes in chunks of 3
for (var i = 0; i < mainLength; i = i + 3) {
// Combine the three bytes into a single integer
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]

// Use bitmasks to extract 6-bit segments from the triplet
a = (chunk & 16515072) >> 18 // 16515072 = (2^6 - 1) << 18
b = (chunk & 258048) >> 12 // 258048 = (2^6 - 1) << 12
c = (chunk & 4032) >> 6 // 4032 = (2^6 - 1) << 6
d = chunk & 63 // 63 = 2^6 - 1

// Convert the raw binary segments to the appropriate ASCII encoding
base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
}

// Deal with the remaining bytes and padding
if (byteRemainder == 1) {
chunk = bytes[mainLength]

a = (chunk & 252) >> 2 // 252 = (2^6 - 1) << 2

// Set the 4 least significant bits to zero
b = (chunk & 3) << 4 // 3 = 2^2 - 1

base64 += encodings[a] + encodings[b] + '=='
} else if (byteRemainder == 2) {
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]

a = (chunk & 64512) >> 10 // 64512 = (2^6 - 1) << 10
b = (chunk & 1008) >> 4 // 1008 = (2^6 - 1) << 4

// Set the 2 least significant bits to zero
c = (chunk & 15) << 2 // 15 = 2^4 - 1

base64 += encodings[a] + encodings[b] + encodings[c] + '='
}

return base64
}


/**
* ENMLOfPlainText
* @param { string } text (Plain)
* @return string - ENML
*/
* ENMLOfPlainText
* @param { string } text (Plain)
* @return string - ENML
*/
function ENMLOfPlainText(text){

var writer = new XMLWriter();
Expand Down Expand Up @@ -91,19 +91,22 @@
}

/**
* PlainTextOfENML
* @param { string } text (ENML)
* @return string - text
*/
* PlainTextOfENML
* @param { string } text (ENML)
* @return string - text
*/
function PlainTextOfENML(enml){

var entities = require('entities');

var text = enml || '';
text = text.replace(/(\r\n|\n|\r)/gm," ");
text = text.replace(/(<\/(div|ui|li|p|table|tr|dl)>)/ig,"\n");
text = text.replace(/^\s/gm,"");
text = text.replace(/(<(li)>)/ig," - ");
text = text.replace(/(<([^>]+)>)/ig,"");
text = text.trim()
text = entities.decodeHTML(text);
text = text.trim();

return text;
}
Expand All @@ -112,23 +115,23 @@


/**
* HTMLOfENML
* Convert ENML into HTML for showing in web browsers.
*
* @param { string } text (ENML)
* @param { Map <string (hash), url (string) || { url: (string), title: (string) } >, Optional } resources
* @return string - HTML
*/
* HTMLOfENML
* Convert ENML into HTML for showing in web browsers.
*
* @param { string } text (ENML)
* @param { Map <string (hash), url (string) || { url: (string), title: (string) } >, Optional } resources
* @return string - HTML
*/
function HTMLOfENML(text, resources){

resources = resources || [];

var resource_map = {}
resources.forEach(function(resource){

var hex = [].map.call( resource.data.bodyHash,
function(v) { str = v.toString(16);
return str.length < 2 ? "0" + str : str; }).join("");
var hex = [].map.call( resource.data.bodyHash,
function(v) { str = v.toString(16);
return str.length < 2 ? "0" + str : str; }).join("");

resource_map[hex] = resource;
})
Expand Down Expand Up @@ -176,18 +179,18 @@
});

var resource = resource_map[hash];

if(!resource) return;
var resourceTitle = resource.title || '';

if(type.match('image')) {

writer.startElement('img');
writer.writeAttribute('title', resourceTitle);

} else if(type.match('audio')) {


writer.writeElement('p', resourceTitle);
writer.startElement('audio');
writer.writeAttribute('controls', '');
Expand All @@ -207,7 +210,7 @@
linkTagStarted = true;
linkTitle = resourceTitle;
}

if(resource.data.body) {
var b64encoded = base64ArrayBuffer(resource.data.body);
var src = 'data:'+type+';base64,'+b64encoded;
Expand Down Expand Up @@ -269,12 +272,12 @@


/**
* TodosOfENML
* Extract data of all TODO(s) in ENML text.
*
* @param { string } text (ENML)
* @return { Array [ { text: (string), done: (bool) } ] } -
*/
* TodosOfENML
* Extract data of all TODO(s) in ENML text.
*
* @param { string } text (ENML)
* @return { Array [ { text: (string), done: (bool) } ] } -
*/
function TodosOfENML(text){

var todos = [];
Expand Down Expand Up @@ -330,14 +333,14 @@
}

/**
* CheckTodoInENML
* Rewrite ENML content by changing check/uncheck value of the TODO in given position.
*
* @param { string } text (ENML)
* @param { int } index
* @param { bool } check
* @return string - ENML (the new content)
*/
* CheckTodoInENML
* Rewrite ENML content by changing check/uncheck value of the TODO in given position.
*
* @param { string } text (ENML)
* @param { int } index
* @param { bool } check
* @return string - ENML (the new content)
*/
function CheckTodoInENML(text, index, check){

var todo_cout = 0;
Expand Down
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
"name": "enml-js",
"description": "Evernote's ENML library in Javascript",
"homepage": "https://github.com/berryboy/enml-js",
"repository": {
"type" : "git",
"url" : "https://github.com/berryboy/enml-js.git"
},
"repository": {
"type": "git",
"url": "https://github.com/berryboy/enml-js.git"
},
"version": "0.1.4",
"directories" : {
"lib" : "./lib",
"examples" : "./examples"
},
"directories": {
"lib": "./lib",
"examples": "./examples"
},
"main": "./enml",
"devDependencies": {
"evernote": "latest",
"should": "3.1.X",
"mocha": "1.17.X",
"async": "0.2.X"
},
"dependencies": {
"entities": "^1.1.1"
}
}

0 comments on commit 83101e2

Please sign in to comment.