Reduce highlights further.

This commit is contained in:
John-David Dalton
2016-09-05 10:11:45 -07:00
parent d1abde7d1a
commit bc13666222

View File

@@ -11,28 +11,23 @@ const basePath = path.join(__dirname, '..', '..');
const docPath = path.join(basePath, 'doc'); const docPath = path.join(basePath, 'doc');
const readmePath = path.join(docPath, 'README.md'); const readmePath = path.join(docPath, 'README.md');
const highlights = [ const highlights = {
'html': [
'string'
],
'js': [
'comment', 'comment',
'constant', 'console',
'delimiter', 'delimiter',
'method', 'method',
'modifier', 'modifier',
'name',
'numeric', 'numeric',
'string', 'string',
'support',
'type' 'type'
]; ]
};
const hlSources = [
'highlight',
'source',
'text'
];
const hlTypes = [
'html',
'js',
'shell'
];
/** /**
* Converts Lodash method references into documentation links. * Converts Lodash method references into documentation links.
@@ -120,31 +115,46 @@ function repairMarkyHeaders($) {
* @param {Object} $ The Cheerio object. * @param {Object} $ The Cheerio object.
*/ */
function tidyHighlights($) { function tidyHighlights($) {
$('.highlight').each(function() {
let $spans;
const $parent = $(this);
const ext = $parent.find('.source,.text').first().attr('class').split(' ').pop();
$parent.addClass(ext);
// Remove line indicators for single line snippets.
$parent.children('pre').each(function() {
const $divs = $(this).children('div');
if ($divs.length == 1) {
$divs.replaceWith($divs.html());
}
});
// Remove extraneous class names. // Remove extraneous class names.
$('.highlight [class]').each(function() { $parent.find('[class]').each(function() {
const $element = $(this); const $element = $(this);
const classes = $element.attr('class').split(' '); const classes = $element.attr('class').split(' ');
if (!_.isEmpty(_.intersection(classes, hlSources)) && const attr = _.intersection(classes, highlights[ext]).join(' ');
!_.isEmpty(_.intersection(classes, hlTypes))) {
return;
}
const attr = _.intersection(classes, highlights).join(' ');
$element.attr('class', attr || null); $element.attr('class', attr || null);
}); });
// Unwrap elements containing only text. // Collapse nested highlights.
$('.highlight :not([class])').each(function() { _.each(['comment', 'string'], function(cls) {
let element = $(this); $parent.find(`[class~="${ cls }"] > [class~="${ cls }"]`).each(function() {
while (element && !element.children.length) {
const $element = $(element);
$element.replaceWith($element.text());
element = element.parent;
}
});
// Collapse comments.
$('.highlight [class~="comment"] > [class~="comment"]').each(function() {
const $parent = $(this).parent(); const $parent = $(this).parent();
$parent.text($parent.text()); $parent.text($parent.text());
}); });
});
// Collapse nested spans.
while (($spans = $parent.find('span:not([class])')).length) {
$spans.each(function() {
let $span = $(this);
while ($span[0] && $span[0].name == 'span' && !$span.attr('class')) {
const $parent = $span.parent();
$span.replaceWith($span.html());
$span = $parent;
}
});
}
});
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/