Skip to content

Commit

Permalink
fixes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
prudhomm authored Oct 2, 2023
1 parent e356210 commit 66d11bf
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/make-dynamic-notebook-processor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* global Opal */
const child_process = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');

module.exports.register = function register(registry) {
const logger = Opal.Asciidoctor.LoggerManager.getLogger();

registry.treeProcessor(function () {
const self = this;
self.process(function (doc) {
const blocks = doc.findBy({ context: 'listing', style: 'source' })
.filter((b) => b.getAttribute('language') === 'makefile' && b.isOption('dynamic'));
doc.findBy({ context: 'listing', style: 'source' }).filter((b) => console.log("language attr: ", b.getAttribute('language'), " ", b.isOption('dynamic')));
if (blocks && blocks.length > 0) {
for (const block of blocks) {
try {
console.log("[makefile] Extracted Attributes:", block.getAttributes());

const adocbasefname = path.parse(doc.getAttribute('docfile')).name;
const parent = block.getParent()
const parentBlocks = parent.getBlocks()
const blockIndex = parentBlocks['$find_index'](block) + 1
const code = block.getSource();

// Use filename from attribute or fallback to a timestamped name
const filename = block.getAttribute('filename', `Makefile`);
let cppDirPath = path.join('cpp', adocbasefname);
const tmpFilePath = path.join(cppDirPath, filename);
fs.mkdirSync(cppDirPath, { recursive: true });
console.log(`[makefile] Writing Makefile to ${tmpFilePath}`);
fs.writeFileSync(tmpFilePath, code);
} catch (err) {
logger.error(`[makefile] Error processing Makefile block: ${err.message}`);
}
}
}
return doc;
});
});
}

0 comments on commit 66d11bf

Please sign in to comment.