Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for %open and add a fail-on-error attribute #6

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jobs:
- name: Install dependencies
run: |
npm ci
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- run: pip install ipython
- name: Test
run: |
npm t
4 changes: 4 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:
- name: Install dependencies
run: |
npm ci
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- run: pip install ipython
- name: Test
run: |
npm t
Expand Down
92 changes: 92 additions & 0 deletions spec/dynamic-notebook.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* global describe it */
const chai = require('chai')
const expect = chai.expect

const dynamicNotebookExt = require('../src/dynamic-notebook-processor.js')
const asciidoctor = require('@asciidoctor/core')()

describe('Dynamic Notebook', () => {
describe('When extension is registered', () => {
it('should execute code using Python', () => {
const input = `
:dynamic-blocks:

[%dynamic%open,python]
----
print('hello')
----
`
const registry = asciidoctor.Extensions.create()
dynamicNotebookExt.register(registry)
const doc = asciidoctor.load(input, {extension_registry: registry})
const html = doc.convert()
expect(html).to.equal(`<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-python" data-lang="python">print('hello')</code></pre>
</div>
</div>
<details open>
<summary class="title">Results</summary>
<div class="content">
<div class="literalblock dynamic-py-result">
<div class="content">
<pre>hello</pre>
</div>
</div>
</div>
</details>`)
})
it('should fail on error when fail-on-error attribute is defined', () => {
const input = `
:dynamic-blocks:

[%dynamic%open,python,fail-on-error=]
----
invalid_python
----
`
const registry = asciidoctor.Extensions.create()
dynamicNotebookExt.register(registry)
try {
const doc = asciidoctor.load(input, {extension_registry: registry})
expect.fail('Should throw an error when fail-on-error attribute is define')
} catch (err) {
// OK
}
})
it('should ignore error when fail-on-error attribute is not defined', () => {
const input = `
:dynamic-blocks:

[%dynamic%open,python]
----
invalid_python
----
`
const registry = asciidoctor.Extensions.create()
dynamicNotebookExt.register(registry)
const doc = asciidoctor.load(input, {extension_registry: registry})
const html = doc.convert()
expect(html).to.equal(`<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-python" data-lang="python">invalid_python</code></pre>
</div>
</div>
<details open>
<summary class="title">Results</summary>
<div class="content">
<div class="literalblock dynamic-py-result">
<div class="content">
<pre>---------------------------------------------------------------------------
NameError Traceback (most recent call last)
File <ipython-input-1-79628dfcbf60>:1
----> 1 invalid_python

NameError: name 'invalid_python' is not defined</pre>
</div>
</div>
</div>
</details>`)
})
})
})
28 changes: 25 additions & 3 deletions src/dynamic-notebook-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ sys.stderr.write(json.dumps(results))
`
}

class ExecutionError extends Error {
constructor(message) {
super(message)
this.name = "ExecutionError"
}
}

/**
* Executes Python code blocks that have the "dynamic" option.
* The code blocks are executed in the order of their definition in the AsciiDoc document.
Expand Down Expand Up @@ -87,10 +94,19 @@ sys.stdout.write(plotter.export_html(None).getvalue())`)
const parent = block.getParent()
const parentBlocks = parent.getBlocks()
const blockIndex = parentBlocks['$find_index'](block) + 1
const exampleBlock = self.createExampleBlock(block, '', {'collapsible-option': ''}, {'content_model': 'compound'})
const opts = Object.fromEntries(Object.entries(block.getAttributes()).filter(([key, _]) => key.endsWith('-option')))
const attrs = {
...opts,
'collapsible-option': ''
}
const exampleBlock = self.createExampleBlock(block, '', attrs, {'content_model': 'compound'})
exampleBlock.setTitle('Results')
// option for raw content (Plotly)
let source = response[index].stdout.toString('utf8')
const result = response[index]
let source = result.stdout.toString('utf8')
if (result.success === false && block.hasAttribute("fail-on-error")) {
throw new ExecutionError(result.stderr.toString('utf8') + " " + result.stdout.toString('utf8'))
}
if (block.isOption('raw')) {
if (block.getAttribute('output') === 'pyvista') {
logger.debug(source)
Expand Down Expand Up @@ -119,7 +135,13 @@ ${script}`, {role: 'dynamic-py-result'}))
}
parentBlocks.splice(blockIndex, 0, exampleBlock)
} catch (err) {
logger.error({ err })
if (err instanceof ExecutionError) {
throw err
} else {
const errorMessage = { err }
errorMessage['$inspect'] = () => err.toString()
logger.error(errorMessage)
}
}
}
}
Expand Down