Created
January 24, 2020 18:35
-
-
Save danyill/f63d0d68be15562c49370fe86c6ce4df to your computer and use it in GitHub Desktop.
treeprocessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// asciidoctor-pdf -r ./build_tools/sectionTreeProcessor.js --template-require ./build_tools/sectionConverter.js -a purpose=pdf P_and_A_Update.adoc | |
// console.log('Loaded SectionTreeProcessor') | |
function removeElement(arr, e) { | |
arr.splice(arr.indexOf(e), 1) | |
} | |
function last(array) { | |
return array[array.length - 1]; | |
} | |
function getDates(date) { | |
const formatter = new Intl.DateTimeFormat('en', { | |
month: 'short', | |
year: '2-digit' | |
}) | |
const new_date = new Date(date.split('/').reverse()) | |
return [formatter.format(new_date), new_date] | |
} | |
function getFormat(date) { | |
const formatter = new Intl.DateTimeFormat('en', { | |
month: 'short', | |
year: '2-digit' | |
}) | |
return formatter.format(date) | |
} | |
module.exports.register = function register(registry) { | |
if (typeof registry.register === 'function') { | |
registry.register(function () { | |
this.treeProcessor(function () { | |
let self = this | |
self.process(function (doc) { | |
doc.findBy({ | |
'context': 'section' | |
}).forEach(function (node) { | |
let sectAttribs = node.getAttributes() | |
let conv_date | |
let created_date | |
let modified_dates | |
let date_att | |
if ('data-type' in sectAttribs && sectAttribs['data-type'] === 'issue') { | |
if ('data-date-created' in sectAttribs) { | |
[created_date, conv_date] = getDates(sectAttribs['data-date-created']); | |
} | |
if ('data-date-modified' in sectAttribs) { | |
date_att = sectAttribs['data-date-modified'].split(',').map(x => getDates(x)[1]) | |
if (conv_date !== undefined) { | |
date_att = date_att.filter(item => item.getTime() !== conv_date.getTime()) | |
} | |
// console.log(sectAttribs['data-date-modified'], date_att) | |
if (date_att.length > 0) { | |
modified_dates = date_att.sort().map(x => getFormat(x)) | |
modified_dates = modified_dates.join('; ') | |
} | |
} | |
const bits = node.getTitle().trim().split(" ") | |
let date_info | |
if (created_date !== undefined) { | |
date_info = created_date | |
} | |
if (modified_dates !== undefined) { | |
date_info += " updated: " + modified_dates | |
} | |
// console.log(node.getTitle(), created_date, modified_dates) | |
date_info = ' [' + date_info + '] ' | |
// console.log(date_info) | |
let new_sect_title = bits[0] + date_info + bits.slice(1, bits.length).join(" ") | |
let data_groups = sectAttribs['data-groups'].split(',') | |
const statii = ['new', 'updated', 'embedded', 'current'] | |
statii.forEach(function (status) { | |
if (data_groups.includes(status)) { | |
// new_sect_title = `{cl-${status}} ` + new_sect_title | |
node.setAttribute(name='cl-status', value=status) | |
} | |
}) | |
const removals = ['new', 'updated', 'embedded', 'current', 'process', 'circuit', 'settings', 'panel'] | |
removals.forEach(function (tag) { | |
if (data_groups.includes(tag)) { | |
removeElement(data_groups, tag) | |
} | |
}) | |
const tags = data_groups.map(tag => `[.tag]#${tag}#`).join(' ') | |
// work around until we have node.setTitle | |
// https://github.com/asciidoctor/asciidoctor.js/issues/736#issuecomment-504217213 | |
node['$title='](new_sect_title) | |
// console.log(node.getTitle()) | |
if (tags !== '') { | |
let parent = node.getBlocks() | |
parent = parent[parent.length - 1] // last element | |
let block = self.createBlock(parent, 'paragraph', tags, {}, { | |
'subs': 'normal' | |
}) | |
node.append(block) | |
} | |
} | |
}) | |
return doc | |
}) | |
}) | |
}) | |
} | |
return registry | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment