Skip to content

Commit

Permalink
Merge pull request asciidoctor#454 from danyill/issue-444-use-kroki-s…
Browse files Browse the repository at this point in the history
…etting-2

Issue asciidoctor#444 - Run Kroki extension within class
  • Loading branch information
danyill committed Oct 3, 2021
2 parents d603d9e + 52327c7 commit 23f80e6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Unreleased

- Make `use_kroki` setting change effective without VS code restart (closes #444)

## 2.8.9

- Fix links in preview (closes #397)
Expand Down
2 changes: 1 addition & 1 deletion src/asciidocEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as vscode from 'vscode'
import { AsciidocContributions } from './asciidocExtensions'
import { Slugifier } from './slugify'
import { AsciidocParser } from './text-parser'
import { AsciidocParser } from './asciidocParser'

const FrontMatterRegex = /^---\s*[^]*?(-{3}|\.{3})\s*/

Expand Down
48 changes: 25 additions & 23 deletions src/text-parser.ts → src/asciidocParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ import * as vscode from 'vscode'
import * as path from 'path'
import { spawn } from 'child_process'

const asciidoctor = require('@asciidoctor/core')()
const asciidoctor = require('@asciidoctor/core')
const docbook = require('@asciidoctor/docbook-converter')
const kroki = require('asciidoctor-kroki')
const highlightjsBuiltInSyntaxHighlighter = asciidoctor.SyntaxHighlighter.for('highlight.js')
const highlightjsBuiltInSyntaxHighlighter = asciidoctor().SyntaxHighlighter.for('highlight.js')
const highlightjsAdapter = require('./highlightjs-adapter')

const useKroki = vscode.workspace.getConfiguration('asciidoc', null).get('use_kroki')
if (useKroki) {
kroki.register(asciidoctor.Extensions)
}

export class AsciidocParser {
public html: string = ''
public document = null
public processor = null
private extPath = vscode.extensions.getExtension('asciidoctor.asciidoctor-vscode').extensionPath
private stylesdir = path.join(this.extPath, 'media')

constructor (private readonly filename: string, private errorCollection: vscode.DiagnosticCollection = null) {
this.filename = filename
this.errorCollection = errorCollection
this.processor = asciidoctor()
}

public getAttribute (name: string) {
Expand Down Expand Up @@ -57,8 +52,16 @@ export class AsciidocParser {
this.errorCollection.clear()
}

const memoryLogger = asciidoctor.MemoryLogger.create()
asciidoctor.LoggerManager.setLogger(memoryLogger)
const memoryLogger = this.processor.MemoryLogger.create()
this.processor.LoggerManager.setLogger(memoryLogger)

const registry = this.processor.Extensions.create()

const useKroki = vscode.workspace.getConfiguration('asciidoc', null).get('use_kroki')

if (useKroki) {
kroki.register(registry)
}

if (context && editor) {
highlightjsAdapter.register(highlightjsBuiltInSyntaxHighlighter, context, editor)
Expand Down Expand Up @@ -120,18 +123,17 @@ export class AsciidocParser {
baseDir: baseDir,
sourcemap: true,
backend: backend,
extension_registry: registry,
}
try {
const asciiDoc = asciidoctor.load(text, options)
this.document = asciiDoc
const blocksWithLineNumber = asciiDoc.findBy(function (b) {
this.document = this.processor.load(text, options)
const blocksWithLineNumber = this.document.findBy(function (b) {
return typeof b.getLineNumber() !== 'undefined'
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
blocksWithLineNumber.forEach(function (block, key, myArray) {
blocksWithLineNumber.forEach(function (block) {
block.addRole('data-line-' + block.getLineNumber())
})
const resultHTML = asciiDoc.convert(options)
const resultHTML = this.document.convert(options)
//let result = this.fixLinks(resultHTML);
if (enableErrorDiagnostics) {
const diagnostics = []
Expand Down Expand Up @@ -284,9 +286,9 @@ export class AsciidocParser {
adocCmdArgs.push('-a', 'env-vscode')

adocCmdArgs.push('-q', '-B', '"' + baseDir + '"', '-o', '-', '-')
const asciidoctor = spawn(adocCmd, adocCmdArgs, options)
spawn(adocCmd, adocCmdArgs, options)

asciidoctor.stderr.on('data', (data) => {
this.processor.stderr.on('data', (data) => {
let errorMessage = data.toString()
console.error(errorMessage)
errorMessage += errorMessage.replace('\n', '<br><br>')
Expand All @@ -299,15 +301,15 @@ export class AsciidocParser {
})
let resultData = Buffer.from('')
/* with large outputs we can receive multiple calls */
asciidoctor.stdout.on('data', (data) => {
this.processor.stdout.on('data', (data) => {
resultData = Buffer.concat([resultData, data as Buffer])
})
asciidoctor.on('close', (_code) => {
this.processor.on('close', () => {
//var result = this.fixLinks(result_data.toString());
resolve(resultData.toString())
})
asciidoctor.stdin.write(text)
asciidoctor.stdin.end()
this.processor.stdin.write(text)
this.processor.stdin.end()
})
}

Expand Down
9 changes: 5 additions & 4 deletions src/commands/exportAsPDF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import * as vscode from 'vscode'
import * as fs from 'fs'
import * as path from 'path'
import { exec, spawn } from 'child_process'
import * as tmp from 'tmp'
import * as zlib from 'zlib'
import { https } from 'follow-redirects'
import { AsciidocParser } from '../text-parser'
import { Command } from '../commandManager'

import { AsciidocParser } from '../asciidocParser'
import { AsciidocEngine } from '../asciidocEngine'
import * as tmp from 'tmp'
import { Command } from '../commandManager'

import url = require('url');
import url = require('url')
import { Logger } from '../logger'

export class ExportAsPDF implements Command {
Expand Down
2 changes: 1 addition & 1 deletion src/features/attributeCompleter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode'

import { AsciidocParser } from '../text-parser'
import { AsciidocParser } from '../asciidocParser'

export class AttributeCompleter {
provideCompletionItems (document: vscode.TextDocument, _position: vscode.Position) {
Expand Down
2 changes: 1 addition & 1 deletion src/image-paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spawn } from 'child_process'
import * as moment from 'moment'
import * as fs from 'fs'

import { AsciidocParser } from './text-parser'
import { AsciidocParser } from './asciidocParser'

export namespace Import {

Expand Down

0 comments on commit 23f80e6

Please sign in to comment.