Skip to content

Instantly share code, notes, and snippets.

@danyill
Created January 24, 2020 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danyill/fb49453752e13c9133d33302ca57f296 to your computer and use it in GitHub Desktop.
Save danyill/fb49453752e13c9133d33302ca57f296 to your computer and use it in GitHub Desktop.
converter
// const asciidoctor = require('asciidoctor.js')()
// console.log('Loaded SectionConverter')
class SectionConverter {
constructor() {
const self = this
self.baseConverter = Opal.Asciidoctor.Html5Converter.$new()
self.templates = {
section: (node) => SectionConverter.convertSection(node)
}
}
static 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]
}
static getFormat(date) {
const formatter = new Intl.DateTimeFormat('en', {
month: 'short',
year: '2-digit'
})
return formatter.format(date)
}
static convertSection(node) {
// This is ugly but mostly a copy the convert_section method from Ruby
let doc_attrs = node.getDocument().getAttributes()
let level = node.getLevel()
let title
let caption = node.getCaption()
if (caption !== undefined) {
title = node.getCaptionedTitle()
} else if (node.isNumbered() && level <= (parseInt(doc_attrs['sectnumlevels']) || 3)) {
if (level < 2 && node.getDocument().getDoctype === 'book') {
if (node.getSectionName() === 'chapter') {
let signifier = doc_attrs['chapter-signifier']
title = `${signifier ? signifier + ' ' : ''}${node.getNumeral()} ${node.getTitle()}`
} else if (node.getSectionName() === 'part') {
let signifier = doc_attrs['part-signifier']
title = `${signifier ? signifier + ' ': ''}${node.getNumeral() === undefined} ${node.getTitle()} weird`
} else {
title = `${node.getNumeral()} ${node.getTitle()}`
}
} else {
title = `${node.getNumeral()} ${node.getTitle()}`
}
} else {
title = node.getTitle()
}
let id_attr
if (node.getId()) {
let id = node.getId()
id_attr = ` id="${id}"`
if ('sectlinks' in doc_attrs) {
title = `<a class="link" href="#${id}">${title}</a>`
}
if ('sectanchors' in doc_attrs) {
if (doc_attrs['sectanchors'] === 'after') {
title = `<a class="anchor" href="#${id}"></a>${title}`
} else {
title = `<a class="anchor" href="#${id}"></a>${title}`
}
}
} else {
id_attr = ''
}
// - data_attributes = Hash[(attributes.select {|k| k.to_s.start_with?('data-')}).map { |k, v| [k.sub(/^data-/, ''), v] }]
let node_attribs = node.getAttributes()
let data_attributes = Object.keys(node_attribs).filter(attr => attr.startsWith('data-'))
let data_dash = data_attributes.map(attr => `${attr}="${node_attribs[attr]}"`).join(' ')
// console.log(data_dash)
// console.log(test)
// test = test.filter(attr => attr.beginsWith("data-"));
// console.log(test)
if (level === 0) {
let role = node.getRole()
let result = `<h1${id_attr} class="sect0$${role ?' ${role}' : ''}">${title}</h1>
${node.getContent()}`
return result
} else {
let role = node.hasRole() ? node.getRole() : undefined
const statii = ['new', 'updated', 'embedded', 'current']
let labelme = ''
statii.forEach(function (status) {
if (node.getAttribute('cl-status') === status) {
// console.log('status: ' + status)
labelme = `<span class="label ${status}">${status}</span>`
}
})
let result = `<div class="sect${level}${role !== undefined ? `${role}` : ``}" ${data_dash}>
<h${level + 1}${id_attr}>${title}</h${level + 1}>${labelme}
${level == 1 ? `<div class="sectionbody">
${node.getContent()}
</div>` : node.getContent()} </div>`
return result
}
}
convert(node, transform, opts) {
// console.log("I was called")
const template = this.templates[transform || node.node_name]
if (template) {
return template(node)
}
return this.baseConverter.convert(node, transform, opts)
}
}
module.exports.register = function register() {
Opal.Asciidoctor.ConverterFactory.register(new SectionConverter(), ['html5']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment