Skip to content

Instantly share code, notes, and snippets.

@bozhink
Created February 9, 2017 11:21
Show Gist options
  • Save bozhink/43555999ccd9c6260636ef14eac948bb to your computer and use it in GitHub Desktop.
Save bozhink/43555999ccd9c6260636ef14eac948bb to your computer and use it in GitHub Desktop.
(function (window, document) {
'use strict';
var $ = window.$;
// ==================================================================
window.process = function () {
console.log('process');
removeEmptyElements('p');
removeEmptyElements('span');
replaceElement('.ZAG-1, .zag-1, .backZAG1', 'h2');
replaceElement('.ZAG1-Ref', 'h2', {class: 'ref'});
replaceElement('.ZAG1-ack', 'h2', {class: 'ack'});
replaceElement('.ZAG-2', 'h3');
replaceElement('.References', 'ref');
removeClass('p.NORMAL', 'NORMAL');
removeClass('p.normal-0', 'normal-0');
processByStyle('font-style', 'italic', 'i');
processByStyle('font-weight', 'bold', 'b');
processByStyle('text-decoration', 'underline', 'underline');
processByStyle('text-decoration', 'overline', 'overline');
processByStyle('text-decoration', 'line-through', 'strike');
processByStyle('font-family', 'monospace', 'monospace');
createSectionStructure();
setFormattingToInlineStyles('p');
replaceElement('span', '');
console.log('done');
}
// ==================================================================
function removeEmptyElements(selector) {
$(selector).filter(function () { return $(this).html() == '' }).remove();
}
function processByStyle(cssPropertyName, cssPropertyValue, tagName) {
var $elements = $('*').filter(function () { return $(this).css(cssPropertyName) == cssPropertyValue });
$elements.each(function (i, value) {
if (value.innerHTML) {
value.innerHTML = `<${tagName}>${value.innerHTML}</${tagName}>`;
}
})
}
function replaceElement(selector, tagName, attributes) {
tagName = tagName || '';
$(selector).replaceWith(function() {
var content, key;
if (tagName === '') {
return $(this).html();
} else {
content = '<' + tagName;
if (attributes) {
for (key in attributes) {
content += ` ${key}="${attributes[key]}"`;
}
}
content += '>' + $(this).html() + `</${tagName}>`;
return content;
}
});
}
function removeClass(selector, className) {
$(selector).removeClass(className);
}
function setFormattingToInlineStyles(selector) {
$(selector).each(function (i, value) {
var $this = $(this);
// $this.css({
// 'margin-left': $this.css('margin-left'),
// 'margin-right': $this.css('margin-right'),
// 'padding-left': $this.css('padding-left'),
// 'padding-right': $this.css('padding-right'),
// 'text-indent': $this.css('text-indent'),
// 'border': $this.css('border'),
// });
$this.removeClass();
});
}
function createSectionStructure() {
var $textFrames = $('.Basic-Text-Frame');
$textFrames.each(function () {
var frame = this,
innerHTML = frame.innerHTML;
if (!innerHTML) {
return;
}
innerHTML = innerHTML.replace(/(<h3\b[^>]*>[\s\S]*?)(?=<h[23]\b)/g, '<section>$1</section>')
.replace(/(<\/section>)\s*(<h3\b[^>]*>[\s\S]*)/, '$1<section>$2</section>')
.replace(/(<h2\b[^>]*>[\s\S]*?)(?=<h2\b)/g, '<section>$1</section>')
.replace(/(<\/section>)\s*(<h2\b[^>]*>[\s\S]*)/, '$1<section>$2</section>');
frame.innerHTML = innerHTML;
});
}
}(window, document))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment