add addSpacingBetweenCnAndEn

This commit is contained in:
rexebin 2016-11-24 20:42:38 +00:00
parent b5486dff72
commit d913092f99
2 changed files with 138 additions and 118 deletions

View File

@ -1357,6 +1357,8 @@ block dart-map-alternative
Framework developers may take this approach when they Framework developers may take this approach when they
must acquire services generically and dynamically. must acquire services generically and dynamically.
在框架程序员必须采用泛型或者动态方式获取服务时,他们可能采用这个方法。
+ifDocsFor('ts') +ifDocsFor('ts')
.l-main-section#one-class-per-file .l-main-section#one-class-per-file
:marked :marked

View File

@ -1,135 +1,153 @@
// TODO: refactor me! // TODO: refactor me!
var sourceVisible = localStorage.getItem('source-visible') === 'true'; var sourceVisible = localStorage.getItem('source-visible') === 'true';
(function ($) { (function ($) {
var content = document.querySelector('article'); var content = document.querySelector('article');
var footer = document.querySelector('.main-footer'); var footer = document.querySelector('.main-footer');
processContainer(content); processContainer(content);
processContainer(footer); processContainer(footer);
if (!sourceVisible) { if (!sourceVisible) {
var nodes = document.querySelectorAll('.original-english'); var nodes = document.querySelectorAll('.original-english');
_.each(nodes, function (node) { _.each(nodes, function (node) {
$(node).addClass('hidden'); $(node).addClass('hidden');
}); });
} }
// restore // restore
content.style.display = 'block'; content.style.display = 'block';
footer.style.display = 'block'; footer.style.display = 'block';
/** /**
* Process container recursively. * Process container recursively.
* @param container * @param container
*/ */
function processContainer(container) { function processContainer(container) {
var count = 0; var count = 0;
for (var i = 0; i < container.children.length; i++) { for (var i = 0; i < container.children.length; i++) {
var node = container.children[i]; var node = container.children[i];
var ignoredTagNames = ['CODE-EXAMPLE', 'SCRIPT', 'CODE', 'EM'];
// ignore example code. // ignore example code.
if (node.classList.contains('code-example') || if (node.classList.contains('code-example') ||
node.tagName === 'CODE-EXAMPLE' || ignoredTagNames.indexOf(node.tagName) >= 0) {
node.tagName === 'SCRIPT' || node.tagName === 'CODE') { continue;
continue;
}
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':
processContainer(node);
break;
default:
if (processContainer(node) <= 1) {
if (processBlock(node)) {
i++;
count++;
} }
}
break;
}
}
return count; switch (node.tagName) {
} case 'P':
case 'H1':
/** case 'H2':
* Process block elements. The first element is original english, the case 'H3':
* second element is translated one. case 'H4':
* @param current the first element. case 'H5':
* @returns {boolean} Is success? case 'H6':
*/ case 'HEADER':
function processBlock(current) { count++;
var sibling = current.nextElementSibling; if (processBlock(node)) {
var $current = $(current); i++;
var $sibling = $(sibling); count++;
}
if (sibling) { break;
if (isClonedNode(current, sibling)) { case 'TD':
if (isPureEnglish(current.textContent)) { case 'TH':
if (sibling.children) { case 'UL':
processContainer(sibling); case 'OL':
} case 'DIV':
$current.addClass('original-english'); processContainer(node);
$sibling.addClass('translated'); break;
$sibling.addClass('translated-cn'); default:
$sibling.after($current); if (processContainer(node) <= 1) {
$sibling.on('click', function (event) { if (processBlock(node)) {
// for nested structure. i++;
event.stopPropagation(); count++;
$current.toggleClass('hidden'); }
}); }
return true; break;
}
} }
console.error('Error: ' + current.innerText); return count;
}
} }
return false; /**
} * 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;
var $current = $(current);
var $sibling = $(sibling);
function isPureEnglish(text) { if (sibling) {
if(text){ if (isClonedNode(current, sibling)) {
text = text.replace('在线例子', ''); if (isPureEnglish(current.textContent)) {
return /^[\1-\255—“”ç®…à\u200B]*$/.test(text); if (sibling.children) {
} processContainer(sibling);
return false; }
$current.addClass('original-english');
$sibling.addClass('translated');
$sibling.addClass('translated-cn');
addSpacingBetweenCnAndEn(sibling);
$sibling.after($current);
$sibling.on('click', function (event) {
// for nested structure.
event.stopPropagation();
$current.toggleClass('hidden');
});
return true;
}
} console.error('Error: ' + current.innerText);
}
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 false;
return node1.tagName === node2.tagName && }
attributesToString(node1) === attributesToString(node2);
} function isPureEnglish(text) {
if (text) {
text = text.replace('在线例子', '');
return /^[\1-\255—“”ç®…à\u200B]*$/.test(text);
}
return false;
}
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 &&
attributesToString(node1) === attributesToString(node2);
}
function addSpacingBetweenCnAndEn(nodeCn) {
if (nodeCn.children) {
return;
}
var text = nodeCn.textContent;
console.log(text);
text = text.replace(/([\x20-\xff]+)/g, function (word) {
if (!word.replace(/\s/, '')) {
return '';
} else if (/<[^>]*>/.test(word)) {
return ' ' + word + ' ';
} else {
return ' ' + word + ' ';
}
});
nodeCn.textContent = text;
}
})(angular.element); })(angular.element);