Skip to content

Commit

Permalink
resolves #13 support colist (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Jul 3, 2023
1 parent 81de5e1 commit fad1662
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 32 deletions.
55 changes: 36 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ class JupyterConverter {
const languageVersion = node.getAttribute('jupyter-language-version', '3.9.1')
const blocks = node.getBlocks()
const cells = []
let lastCell = {}
for (const block of blocks) {
const result = block.convert()
if (cells.length === 0) {
const firstCell = result[0]
// attach document title
if (node.hasHeader() && node.getDocumentTitle()) {
if (firstCell && firstCell.cell_type === 'markdown') {
firstCell.source.unshift(`# ${node.getDocumentTitle()}\n\n`)
} else {
cells.push({
cell_type: 'markdown',
source: [`# ${node.getDocumentTitle()}\n\n`],
metadata: {}
})
if (lastCell.cell_type === 'markdown') {
lastCell = this.mergeAdjacentMarkdownCells(result, lastCell, cells)
} else {
if (cells.length === 0) {
const firstCell = result[0]
// attach document title
if (node.hasHeader() && node.getDocumentTitle()) {
if (firstCell && firstCell.cell_type === 'markdown') {
firstCell.source.unshift(`# ${node.getDocumentTitle()}\n\n`)
} else {
cells.push({
cell_type: 'markdown',
source: [`# ${node.getDocumentTitle()}\n\n`],
metadata: {}
})
}
}
}
cells.push(...result)
if (cells.length > 0) {
lastCell = cells[cells.length - 1]
}
}
cells.push(...result)
}
const result = {
cells,
Expand Down Expand Up @@ -116,9 +124,7 @@ class JupyterConverter {
}
if (nodeName === 'listing') {
const lines = node.lines
const length = lines.length
const source = lines
.map((l, index) => length === index + 1 ? l : l + '\n')
const source = lines.map((l, index) => l + '\n')
const language = node.getAttribute('language')
if (language === 'python' || language === 'py') {
return [{
Expand All @@ -135,7 +141,7 @@ class JupyterConverter {
} else {
return [{
cell_type: 'markdown',
source: ['```' + language, ...source, '```'],
source: ['```' + (language || '') + '\n', ...source, '```'],
metadata: {}
}]
}
Expand Down Expand Up @@ -319,7 +325,6 @@ class JupyterConverter {
}
return image
}

if (nodeName === 'dlist') {
let source = ''
for (const [terms, dd] of node.getItems()) {
Expand All @@ -337,7 +342,7 @@ ${dd.getText()}
for (const block of dd.getBlocks()) {
const content = block.convert()
if (content && content.length === 1 && content[0].cell_type === 'markdown') {
source += ` ${content[0].source.join('\n ')}\n`
source += ` ${content[0].source.join(' ')}\n`
} else {
// ignore
this.ignoredNodes.push({ name: `dlist>${block.getNodeName()}` })
Expand All @@ -353,6 +358,18 @@ ${dd.getText()}
metadata: {}
}]
}
if (nodeName === 'colist') {
const source = []
for (const [index, item] of node.getItems().entries()) {
source.push(`\n${index + 1}. ${item.text}`)
}
source.push('')
return [{
cell_type: 'markdown',
source,
metadata: {}
}]
}

this.ignoredNodes.push({ name: nodeName })
return ''
Expand Down
41 changes: 30 additions & 11 deletions test/converter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ describe('Jupyter converter', () => {
const ipynb = JSON.parse(result)
expect(ipynb.metadata.language_info.name).is.equal('python')
expect(ipynb.metadata.language_info.version).is.equal('2.7.10')
expect(ipynb.cells.length).is.equal(35)
expect(ipynb.cells.length).is.equal(32)
const codeCells = ipynb.cells.filter(cell => cell.cell_type === 'code')
expect(codeCells.length).is.equal(21)
expect(codeCells[0].source.join('')).is.equal(`from py2neo import Graph
graph = Graph()`)
graph = Graph()
`)
})
it('should convert an exercise guide to ipynb', async () => {
const inputFile = path.join(__dirname, 'fixtures', 'intro-neo4j-guides-01.adoc')
Expand All @@ -48,7 +49,7 @@ graph = Graph()`)
const ipynb = JSON.parse(result)
expect(ipynb.metadata.language_info.name).is.equal('python')
expect(ipynb.metadata.language_info.version).is.equal('3.9.1')
expect(ipynb.cells.length).is.equal(11)
expect(ipynb.cells.length).is.equal(1)
})
it('should convert stem blocks to ipynb', async () => {
const inputFile = path.join(__dirname, 'fixtures', 'lorenz-differential-equations.adoc')
Expand All @@ -61,7 +62,7 @@ graph = Graph()`)
const ipynb = JSON.parse(result)
expect(ipynb.metadata.language_info.name).is.equal('python')
expect(ipynb.metadata.language_info.version).is.equal('3.7.8')
expect(ipynb.cells.length).is.equal(10)
expect(ipynb.cells.length).is.equal(7)
await debug(result, 'lorenz-differential-equations.ipynb')
})
it('should convert an exercise guide with a complex list to ipynb', async () => {
Expand Down Expand Up @@ -156,13 +157,14 @@ If set X is a subset of Y or vice versa then the overlap coefficient is equal to
const ipynb = JSON.parse(result)
expect(ipynb.metadata.language_info.name).is.equal('python')
expect(ipynb.metadata.language_info.version).is.equal('3.9.1')
expect(ipynb.cells.length).is.equal(6)
expect(ipynb.cells[0].source.join('')).is.equal('*Note:* An admonition draws attention to auxiliary information.\n')
expect(ipynb.cells[1].source.join('')).is.equal('Here are the other built-in admonition types:\n')
expect(ipynb.cells[2].source.join('')).is.equal('*Tip:* Pro tip…​\n')
expect(ipynb.cells[3].source.join('')).is.equal('*Important:* Don’t forget…​\n')
expect(ipynb.cells[4].source.join('')).is.equal('*Warning:* Watch out for…​\n')
expect(ipynb.cells[5].source.join('')).is.equal('*Caution:* Ensure that…​\n')
expect(ipynb.cells.length).is.equal(1)
expect(ipynb.cells[0].source.join('')).is.equal(`*Note:* An admonition draws attention to auxiliary information.
Here are the other built-in admonition types:
*Tip:* Pro tip…​
*Important:* Don’t forget…​
*Warning:* Watch out for…​
*Caution:* Ensure that…​
`)
})
it('should convert an exercise guide with an admonition block to ipynb', async () => {
const inputFile = path.join(__dirname, 'fixtures', 'admonition-block.adoc')
Expand Down Expand Up @@ -318,4 +320,21 @@ It performs operations on an external data source, usually memory or some other
Permanent storage for operating system and/or user files.
`)
})
it('should convert colist', async () => {
const inputFile = path.join(__dirname, 'fixtures', 'colist.adoc')
const result = asciidoctor.convertFile(inputFile, {
safe: 'safe',
backend: 'jupyter',
to_file: false
})
expect(result).is.not.empty()
const ipynb = JSON.parse(result)
await debug(result, 'colist.ipynb')
expect(ipynb.cells[0].source.join('')).is.equal(`\`\`\`
git clone https://github.com/feelpp/book.feelpp.org.git # <1>
git clone https://github.com/feelpp/toolbox.git # <2>
\`\`\`
1. clone the source for the website
2. clone the source for toolbox cases`)
})
})
6 changes: 6 additions & 0 deletions test/fixtures/colist.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
----
git clone https://github.com/feelpp/book.feelpp.org.git # <1>
git clone https://github.com/feelpp/toolbox.git # <2>
----
<1> clone the source for the website
<2> clone the source for toolbox cases
5 changes: 3 additions & 2 deletions test/register.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ describe('Jupyter converter', () => {
const ipynb = JSON.parse(result)
expect(ipynb.metadata.language_info.name).is.equal('python')
expect(ipynb.metadata.language_info.version).is.equal('2.7.10')
expect(ipynb.cells.length).is.equal(35)
expect(ipynb.cells.length).is.equal(32)
const codeCells = ipynb.cells.filter(cell => cell.cell_type === 'code')
expect(codeCells.length).is.equal(21)
expect(codeCells[0].source.join('')).is.equal(`from py2neo import Graph
graph = Graph()`)
graph = Graph()
`)
} finally {
asciidoctor.Extensions.unregisterAll()
}
Expand Down

0 comments on commit fad1662

Please sign in to comment.