-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
46 lines (42 loc) · 1.51 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict'
const processor = require('bytefield-svg'); // The byte field diagram generator.
// Register as a block processor of type 'bytefield'
module.exports = function (registry) {
function toBlock(attrs, parent, source, line_info, self) {
if (typeof attrs === 'object' && '$$smap' in attrs) {
attrs = fromHash(attrs)
}
const doc = parent.getDocument()
const subs = attrs.subs
if (subs) {
source = doc.$apply_subs(source, doc.$resolve_subs(subs))
}
var svgText
try {
svgText = processor(source)
} catch (err) {
console.log(`error after ${line_info}: ${err.toString()}`)
svgText = `error after ${line_info}: ${err.toString()}`
}
const idAttr = attrs.id ? ` id="${attrs.id}"` : ''
const classAttr = attrs.role ? `${attrs.role} imageblock bytefield` : `imageblock bytefield`
const title_el = attrs.title ? `\n<div class="title">${attrs.title}</div>` : ''
const svgBlock = self.$create_pass_block(
parent,
`<div${idAttr} class="${classAttr}">\n<div class="content">${svgText}</div>${title_el}\n</div>`,
// eslint-disable-next-line no-undef
Opal.hash({})
)
return svgBlock
}
registry.block(function () {
const self = this
self.named('bytefield')
self.onContext(['listing', 'literal'])
self.process(function (parent, reader, attrs) {
const line_info = reader.$line_info()
var source = reader.getLines().join('\n')
return toBlock(attrs, parent, source, line_info, self)
})
})
}