-
Notifications
You must be signed in to change notification settings - Fork 0
/
unicode-file-to-htm-text-converter.js
74 lines (59 loc) · 1.5 KB
/
unicode-file-to-htm-text-converter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
UnicodeFileToHtmTextConverter = function(fileBlob) {
this._fileBlob = fileBlob;
};
UnicodeFileToHtmTextConverter.prototype = {
convertToHtml: function (callback) {
var self = this;
var fileReader = new FileReader();
var text;
fileReader.onload = function(evt) {
text = evt.target.result;
var htmlLines = self._basicHtmlEncode(text);
if (callback) {
callback(htmlLines);
}
};
fileReader.readAsText(this._fileBlob);
},
_basicHtmlEncode: function (source) {
var stashNextCharacterAndAdvanceThePointer = function () {
var c = source.charAt(i);
i += 1;
return c;
};
var addANewLine = function () {
convertedLine = convertedLine.join('');
result.push(convertedLine);
convertedLine = [];
};
var pushACharacterToTheOutput = function () {
convertedLine.push(characterToConvert);
};
var i = 0;
var result = [];
var convertedLine = [];
var characterToConvert = stashNextCharacterAndAdvanceThePointer();
while (i <= source.length) {
switch (characterToConvert) {
case '<':
convertedLine.push('<');
break;
case '>':
convertedLine.push('>');
break;
case '&':
convertedLine.push('&');
break;
case '\n':
addANewLine();
break;
default:
pushACharacterToTheOutput();
}
characterToConvert = stashNextCharacterAndAdvanceThePointer();
}
addANewLine();
result = result.join('<br />');
return result;
}
};