-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.js
85 lines (67 loc) · 3.23 KB
/
custom.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
75
76
77
78
79
80
81
82
83
84
85
function getClosingTagString(aTagName) {
return '</' + aTagName + '>';
}
function getNextClosingTagIndex(aString, aStartingPoint, aTagName) {
return aString.indexOf(getClosingTagString(aTagName), aStartingPoint)
}
function getNestedCount(aString, aSubstring, aStartIndex, aEndIndex) {
return aString.substring(aStartIndex, aEndIndex).split(aSubstring).length - 1;
}
function getNestedOpenTagCount(aString, aTagName, aStartIndex, aEndIndex) {
return getNestedCount(aString, '<' + aTagName, aStartIndex, aEndIndex);
}
function getNestedCloseTagCount(aString, aTagName, aStartIndex, aEndIndex) {
return getNestedCount(aString, getClosingTagString(aTagName), aStartIndex, aEndIndex);
}
function getOpenTagMinusCloseTagCount(aString, aTagName, aStartIndex, aEndIndex) {
return getNestedOpenTagCount(aString, aTagName, aStartIndex, aEndIndex) - getNestedCloseTagCount(aString, aTagName, aStartIndex, aEndIndex);
}
function findNthClosingTagFromPosition(aNum, aString, aTagName, aPosition) {
while (aNum--) {
aPosition = getNextClosingTagIndex(aString, aPosition + getClosingTagString(aTagName).length, aTagName)
}
return aPosition;
}
function findCorrectClosingTagPosition(aString, aStartingPoint, aTagName) {
var tNextClosingTagIndex = getNextClosingTagIndex(aString, aStartingPoint, aTagName),
tOpenTagDiffCount;
function getOpenTagDiffCount() {
tOpenTagDiffCount = getOpenTagMinusCloseTagCount(aString, aTagName, aStartingPoint, tNextClosingTagIndex - 1);
return tOpenTagDiffCount;
}
while (getOpenTagDiffCount() > 0) {
//console.log("while getOpenTagMinusCloseTagCount > 0", tNextClosingTagIndex);
tNextClosingTagIndex = findNthClosingTagFromPosition(tOpenTagDiffCount, aString, aTagName, tNextClosingTagIndex);
if(tNextClosingTagIndex == -1) {
throw new Error('Malformed html, cannot refactor:\n' + aString);
}
}
return tNextClosingTagIndex;
}
function replaceContentDivWithArticle(aString) {
var tOpenTagStartFragment = '<div class="column-two-thirds"',
tOpenTagStart = aString.indexOf(tOpenTagStartFragment),
tOpenTagEnd,
tClosingTagStart,
tClosingTagEnd,
tContentPreDiv,
tInnerContent,
tContentPostDiv,
tStringToReturn;
if (tOpenTagStart !== -1) {
tOpenTagEnd = aString.indexOf('>', tOpenTagStart);
tClosingTagStart = findCorrectClosingTagPosition(aString, tOpenTagEnd + 1, 'div');
tClosingTagEnd = aString.indexOf('>', tClosingTagStart);
tContentPreDiv = aString.substring(0, tOpenTagStart - 1);
tInnerContent = aString.substring(tOpenTagEnd + 1, tClosingTagStart);
tContentPostDiv = aString.substring(tClosingTagEnd + 1);
tStringToReturn = tContentPreDiv + '<article class="content__body">' + tInnerContent + '</article>' + tContentPostDiv;
}
//console.log('tOpenTagStart',tOpenTagStart);
//console.log('tOpenTagEnd',tOpenTagEnd);
//console.log('tNextClosingTag',tClosingTagStart);
//console.log('tNextOpeningTag',tClosingTagEnd);
//console.log('innerHtml',tTemplate.substring(tOpenTagEnd + 1, tClosingTagStart));
return tStringToReturn || aString;
}
module.exports = replaceContentDivWithArticle;