2016-06-19 04:46:50 -04:00
|
|
|
|
// TODO: refactor me!
|
|
|
|
|
var sourceVisible = localStorage.getItem('source-visible') === 'true';
|
|
|
|
|
(function ($) {
|
2016-11-26 09:53:33 -05:00
|
|
|
|
var content = document.querySelector('article');
|
|
|
|
|
var footer = document.querySelector('.main-footer');
|
2016-06-19 04:46:50 -04:00
|
|
|
|
|
2016-11-27 06:39:31 -05:00
|
|
|
|
processContainer(content, true);
|
2016-11-26 09:53:33 -05:00
|
|
|
|
processContainer(footer);
|
2016-11-23 22:45:57 -05:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
if (!sourceVisible) {
|
|
|
|
|
var nodes = document.querySelectorAll('.original-english');
|
|
|
|
|
_.each(nodes, function (node) {
|
|
|
|
|
$(node).addClass('hidden');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// restore
|
|
|
|
|
if (content) {
|
2016-11-24 15:42:38 -05:00
|
|
|
|
content.style.display = 'block';
|
2016-11-26 09:53:33 -05:00
|
|
|
|
}
|
|
|
|
|
footer.style.display = 'block';
|
2016-11-23 22:45:57 -05:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
/**
|
|
|
|
|
* Process container recursively.
|
|
|
|
|
* @param container
|
|
|
|
|
*/
|
2016-11-27 06:39:31 -05:00
|
|
|
|
function processContainer(container, isContent) {
|
|
|
|
|
if (!container || (isContent && isPureEnglish(container.textContent))) {
|
2016-11-26 09:53:33 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var count = 0;
|
|
|
|
|
for (var i = 0; i < container.children.length; i++) {
|
|
|
|
|
var node = container.children[i];
|
2016-11-29 06:02:21 -05:00
|
|
|
|
var ignoredTagNames = ['CODE-EXAMPLE', 'SCRIPT', 'CODE', 'EM', 'STRONG'];
|
2016-11-26 09:53:33 -05:00
|
|
|
|
// ignore example code.
|
|
|
|
|
if (node.classList.contains('code-example') ||
|
|
|
|
|
ignoredTagNames.indexOf(node.tagName) >= 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-11-23 22:45:57 -05:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
switch (node.tagName) {
|
|
|
|
|
case 'P':
|
|
|
|
|
case 'H1':
|
|
|
|
|
case 'H2':
|
|
|
|
|
case 'H3':
|
|
|
|
|
case 'H4':
|
|
|
|
|
case 'H5':
|
|
|
|
|
case 'H6':
|
|
|
|
|
case 'HEADER':
|
|
|
|
|
count++;
|
|
|
|
|
if (processBlock(node)) {
|
|
|
|
|
i++;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'TD':
|
|
|
|
|
case 'TH':
|
|
|
|
|
case 'UL':
|
|
|
|
|
case 'OL':
|
|
|
|
|
case 'DIV':
|
2016-11-27 06:39:31 -05:00
|
|
|
|
processContainer(node, ['TD','TH'].indexOf(node.tagName)!== -1);
|
2016-11-26 09:53:33 -05:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if (processContainer(node) <= 1) {
|
|
|
|
|
if (processBlock(node)) {
|
|
|
|
|
i++;
|
|
|
|
|
count++;
|
2016-11-24 12:15:27 -05:00
|
|
|
|
}
|
2016-11-26 09:53:33 -05:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-11-23 22:45:57 -05:00
|
|
|
|
}
|
2016-11-24 12:15:27 -05:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
return count;
|
|
|
|
|
}
|
2016-11-24 16:15:22 -05:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
/**
|
|
|
|
|
* Process block elements. The first element is original english, the
|
|
|
|
|
* second element is translated one.
|
|
|
|
|
* @param current the first element.
|
|
|
|
|
* @returns {boolean} Is success?
|
|
|
|
|
*/
|
|
|
|
|
function processBlock(current) {
|
|
|
|
|
var sibling = current.nextElementSibling;
|
2016-07-16 06:29:42 -04:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
var $current = $(current);
|
|
|
|
|
var $sibling = $(sibling);
|
2016-06-19 04:46:50 -04:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
if (sibling) {
|
|
|
|
|
if (isClonedNode(current, sibling)) {
|
|
|
|
|
if (isPureEnglish(current.textContent)) {
|
|
|
|
|
if (sibling.children) {
|
|
|
|
|
processContainer(sibling);
|
2016-11-27 06:39:31 -05:00
|
|
|
|
}
|
2016-11-27 06:00:11 -05:00
|
|
|
|
$current.addClass('original-english');
|
|
|
|
|
$sibling.addClass('translated');
|
|
|
|
|
$sibling.addClass('translated-cn');
|
|
|
|
|
$sibling.after($current);
|
|
|
|
|
$sibling.on('click', function (event) {
|
|
|
|
|
// for nested structure.
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
$current.toggleClass('hidden');
|
|
|
|
|
});
|
|
|
|
|
// addSpacingBetweenCnAndEn(sibling);
|
|
|
|
|
return true;
|
2016-11-27 06:39:31 -05:00
|
|
|
|
|
2016-11-27 06:00:11 -05:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-23 22:45:57 -05:00
|
|
|
|
}
|
2016-06-19 04:46:50 -04:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-19 04:46:50 -04:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
function isPureEnglish(text) {
|
|
|
|
|
if (text) {
|
|
|
|
|
text = text.replace('在线例子', '');
|
|
|
|
|
return /^[\1-\255—’“”ç®…à\u200B]*$/.test(text);
|
2016-06-19 04:46:50 -04:00
|
|
|
|
}
|
2016-11-26 09:53:33 -05:00
|
|
|
|
return false;
|
2016-06-19 04:46:50 -04:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
}
|
2016-11-24 09:16:23 -05:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
function attributesToString(node) {
|
|
|
|
|
return _.chain(node.attributes)
|
|
|
|
|
.map(function (value) {
|
|
|
|
|
if (value.name === 'id') {
|
|
|
|
|
return '';
|
|
|
|
|
} else {
|
|
|
|
|
return value.name + '=' + value.value;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.sortBy()
|
|
|
|
|
.value()
|
|
|
|
|
.join(';');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isClonedNode(node1, node2) {
|
|
|
|
|
return node1.tagName === node2.tagName && node1.tagName !== 'TR' &&
|
|
|
|
|
attributesToString(node1) === attributesToString(node2);
|
|
|
|
|
}
|
2016-11-24 09:16:23 -05:00
|
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
|
// function addSpacingBetweenCnAndEn(nodeCn) {
|
|
|
|
|
// var text = nodeCn.innerHTML;
|
|
|
|
|
// text = text.replace(/([\x20-\xff]+)/g, function (word) {
|
|
|
|
|
// if (!word.replace(/\s/, '')) {
|
|
|
|
|
// return '';
|
|
|
|
|
// } else if (/<[^>]*>/.test(word)) {
|
|
|
|
|
// return ' ' + word + ' ';
|
|
|
|
|
// } else {
|
|
|
|
|
// return ' ' + word + ' ';
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// nodeCn.innerHTML = text;
|
|
|
|
|
// }
|
2016-06-19 04:46:50 -04:00
|
|
|
|
})(angular.element);
|