From 167c5783cdc4afbb1d8a9c85fc1eccedd83d598a Mon Sep 17 00:00:00 2001 From: derochs Date: Thu, 10 Jun 2021 20:05:56 +0200 Subject: [PATCH 1/6] Add Converter for Asciidoc to Markdown conversion --- engine/converter.ts | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 engine/converter.ts diff --git a/engine/converter.ts b/engine/converter.ts new file mode 100644 index 00000000..cf7df6f3 --- /dev/null +++ b/engine/converter.ts @@ -0,0 +1,82 @@ +import { ConsolePlatform } from "../runners/console/consoleInterfaces"; +import { ConsoleUtils } from "../runners/console/consoleUtils"; +import * as child_process from "child_process" +import * as path from "path" +import * as fs from "fs" +import { pathToFileURL } from "url"; +import { fstat } from "fs"; +import { FileContains } from "../assertions/fileContains"; + +export class Converter { + +private readonly TMP_DIR = "tmp_convert"; + +private platform: ConsolePlatform; + + constructor() { + // Determine platform for script execution + if(process.platform == "win32") { + this.platform = ConsolePlatform.WINDOWS; + }else { + this.platform = ConsolePlatform.LINUX; + } + + // Create temporary directory in which files can be created & modified + this.createTempDir(); + } + + createTempDir(): void { + // Create tempDir if not already existing + let dir = this.getTempDir(); + if(!fs.existsSync(dir)) { + fs.mkdirSync(dir); + } + } + + + createTempFile(filename: string, content: string) { + fs.writeFileSync(path.join(this.getTempDir(),filename), content); + } + + readTempFile(filename: string): string { + let file_path = path.join(this.getTempDir(), filename); + let fileContent = ""; + let file = fs.readFileSync(file_path); + fileContent = file.toString() + return fileContent; + } + + getTempDir(): string { + // Return tempDir + return path.join(__dirname, this.TMP_DIR); + } + + convertAsciidocToMarkdown(asciidocString: string): string { + /** + * 1. Write asciidocString to temp.adoc file + * 2. Use AsciiDoctor to convert AsciiDoc String to DocBook XML file + * 3. Use Pandoc to convert DocBook XML file to Markdown File + * 4. Read Markdown file (eventually combine with step 2) + */ + + this.createTempFile("temp.adoc", asciidocString); + + let markdownString = ""; + if(this.platform == ConsolePlatform.WINDOWS) { + this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); + this.executeCommand("pandoc -f docbook -t markdown temp.xml -o temp.md", this.getTempDir()); + markdownString = this.readTempFile("temp.md"); + }else { + + } + return markdownString; + } + + executeCommand(command: string, directory: string) { + let process = child_process.spawnSync(command, {shell: true, cwd: directory}); + if(process.status != 0) { + console.log("Error executing command: " + command + " (exit code: )" + process.status + ")"); + console.log(process.stderr.toString(), process.stdout.toString()); + } + } +} \ No newline at end of file From 174bc76c4834cd6be4e998bfe9ddcee45f5df383 Mon Sep 17 00:00:00 2001 From: derochs Date: Thu, 10 Jun 2021 23:03:43 +0200 Subject: [PATCH 2/6] Add script for Linux environment --- engine/converter.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/engine/converter.ts b/engine/converter.ts index cf7df6f3..5c3ee28b 100644 --- a/engine/converter.ts +++ b/engine/converter.ts @@ -56,7 +56,7 @@ private platform: ConsolePlatform; * 1. Write asciidocString to temp.adoc file * 2. Use AsciiDoctor to convert AsciiDoc String to DocBook XML file * 3. Use Pandoc to convert DocBook XML file to Markdown File - * 4. Read Markdown file (eventually combine with step 2) + * 4. Read Markdown file (eventually combine with step 2?) */ this.createTempFile("temp.adoc", asciidocString); @@ -67,7 +67,9 @@ private platform: ConsolePlatform; this.executeCommand("pandoc -f docbook -t markdown temp.xml -o temp.md", this.getTempDir()); markdownString = this.readTempFile("temp.md"); }else { - + this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); + this.executeCommand("pandoc -f docbook -t markdown temp.xml -o temp.md", this.getTempDir()); + markdownString = this.readTempFile("temp.md"); } return markdownString; } From 3283b0db07e1871e55f84f18643799ec57aa0d3b Mon Sep 17 00:00:00 2001 From: derochs Date: Tue, 15 Jun 2021 15:52:37 +0200 Subject: [PATCH 3/6] Convert text and textAfter into Markdown Syntax --- engine/converter.ts | 38 ++++++++++++++++++++------------------ runners/katacoda/index.ts | 11 +++++++++++ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/engine/converter.ts b/engine/converter.ts index 5c3ee28b..1d084e02 100644 --- a/engine/converter.ts +++ b/engine/converter.ts @@ -52,26 +52,28 @@ private platform: ConsolePlatform; } convertAsciidocToMarkdown(asciidocString: string): string { - /** - * 1. Write asciidocString to temp.adoc file - * 2. Use AsciiDoctor to convert AsciiDoc String to DocBook XML file - * 3. Use Pandoc to convert DocBook XML file to Markdown File - * 4. Read Markdown file (eventually combine with step 2?) - */ + if(asciidocString !== undefined) { + /** + * 1. Write asciidocString to temp.adoc file + * 2. Use AsciiDoctor to convert AsciiDoc String to DocBook XML file + * 3. Use Pandoc to convert DocBook XML file to Markdown File + * 4. Read Markdown file (eventually combine with step 2?) + */ - this.createTempFile("temp.adoc", asciidocString); + this.createTempFile("temp.adoc", asciidocString); - let markdownString = ""; - if(this.platform == ConsolePlatform.WINDOWS) { - this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); - this.executeCommand("pandoc -f docbook -t markdown temp.xml -o temp.md", this.getTempDir()); - markdownString = this.readTempFile("temp.md"); - }else { - this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); - this.executeCommand("pandoc -f docbook -t markdown temp.xml -o temp.md", this.getTempDir()); - markdownString = this.readTempFile("temp.md"); - } - return markdownString; + let markdownString = ""; + if(this.platform == ConsolePlatform.WINDOWS) { + this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); + this.executeCommand("pandoc -f docbook -t gfm temp.xml -o temp.md", this.getTempDir()); + markdownString = this.readTempFile("temp.md"); + }else { + this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); + this.executeCommand("pandoc -f docbook -t markdown temp.xml -o temp.md", this.getTempDir()); + markdownString = this.readTempFile("temp.md"); + } + return markdownString; + } } executeCommand(command: string, directory: string) { diff --git a/runners/katacoda/index.ts b/runners/katacoda/index.ts index 2a831a2f..c58636b7 100644 --- a/runners/katacoda/index.ts +++ b/runners/katacoda/index.ts @@ -9,6 +9,7 @@ import { DirUtils } from "./dirUtils"; import * as path from 'path'; import * as ejs from 'ejs'; import * as fs from 'fs'; +import { Converter } from "../../engine/converter"; export class Katacoda extends Runner { @@ -24,6 +25,7 @@ export class Katacoda extends Runner { private terminalCounter: number = 1; private showVsCodeIde: boolean = false; private terminals: KatacodaTerminals[] = [{function: "default", terminalId: 1}]; + private converter: Converter; init(playbook: Playbook): void { // create directory for katacoda tutorials if not exist @@ -48,6 +50,8 @@ export class Katacoda extends Runner { //set working direktory this.setVariable(this.WORKSPACE_DIRECTORY, path.join("/root")); + this.converter = new Converter(); + this.assetManager = new KatacodaAssetManager(path.join(this.outputPathTutorial, "assets")); playbook.steps.forEach(step => { @@ -463,6 +467,13 @@ export class Katacoda extends Runner { } private renderTemplate(name: string, targetPath: string, variables) { + if("text" in variables) { + variables["text"] = this.converter.convertAsciidocToMarkdown(variables["text"]); + } + if("textAfter" in variables) { + variables["textAfter"] = this.converter.convertAsciidocToMarkdown(variables["textAfter"]); + } + let template = fs.readFileSync(path.join(this.getRunnerDirectory(),"templates", name), 'utf8'); let result = ejs.render(template, variables); fs.writeFileSync(targetPath, result, {flag: "a"}); From 94a3c4e4b7aa39b0a4facf3879315ad44e33b4d2 Mon Sep 17 00:00:00 2001 From: derochs Date: Mon, 21 Jun 2021 11:21:28 +0200 Subject: [PATCH 4/6] Convert playbook conclusion and description to Markdown --- runners/katacoda/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runners/katacoda/index.ts b/runners/katacoda/index.ts index c58636b7..ea9da3e0 100644 --- a/runners/katacoda/index.ts +++ b/runners/katacoda/index.ts @@ -65,8 +65,8 @@ export class Katacoda extends Runner { async destroy(playbook: Playbook): Promise { let tutorialDirectoryName = path.basename(playbook.path); - this.renderTemplate("intro.md", path.join(this.outputPathTutorial, "intro.md"), { description: playbook.description, tutorialPath: tutorialDirectoryName }); - fs.writeFileSync(this.outputPathTutorial + 'finish.md', playbook.conclusion); + this.renderTemplate("intro.md", path.join(this.outputPathTutorial, "intro.md"), { description: this.converter.convertAsciidocToMarkdown(playbook.description), tutorialPath: tutorialDirectoryName }); + fs.writeFileSync(this.outputPathTutorial + 'finish.md', this.converter.convertAsciidocToMarkdown(playbook.conclusion)); // create and configure required files for the setup process this.renderTemplate(path.join("scripts", "intro_foreground.sh"), path.join(this.outputPathTutorial, "intro_foreground.sh"), { }); From c7d3604f80737cd531c3052f393fd5b011cf69ca Mon Sep 17 00:00:00 2001 From: derochs Date: Thu, 1 Jul 2021 16:10:46 +0200 Subject: [PATCH 5/6] Add exception if commands are unavailable --- engine/converter.ts | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/engine/converter.ts b/engine/converter.ts index 1d084e02..c5ab1cfb 100644 --- a/engine/converter.ts +++ b/engine/converter.ts @@ -64,18 +64,32 @@ private platform: ConsolePlatform; let markdownString = ""; if(this.platform == ConsolePlatform.WINDOWS) { - this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); - this.executeCommand("pandoc -f docbook -t gfm temp.xml -o temp.md", this.getTempDir()); - markdownString = this.readTempFile("temp.md"); + if(this.commandAvailable("asciidoctor") && this.commandAvailable("pandoc")) { + this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); + this.executeCommand("pandoc -f docbook -t gfm temp.xml -o temp.md", this.getTempDir()); + markdownString = this.readTempFile("temp.md"); + }else { + markdownString = this.readTempFile("temp.adoc"); + } }else { - this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); - this.executeCommand("pandoc -f docbook -t markdown temp.xml -o temp.md", this.getTempDir()); - markdownString = this.readTempFile("temp.md"); + if(this.commandAvailable("asciidoctor") && this.commandAvailable("pandoc")) { + this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); + this.executeCommand("pandoc -f docbook -t gfm temp.xml -o temp.md", this.getTempDir()); + markdownString = this.readTempFile("temp.md"); + }else { + markdownString = this.readTempFile("temp.adoc"); + } } return markdownString; } } + commandAvailable(command: string): boolean { + let isAvailable = false; + let process = child_process.spawnSync(command, {shell: true, cwd: this.getTempDir()}); + return process.status == 0; + } + executeCommand(command: string, directory: string) { let process = child_process.spawnSync(command, {shell: true, cwd: directory}); if(process.status != 0) { From 5c4f5322d51151027ca84ed879a36bd74a26c17e Mon Sep 17 00:00:00 2001 From: derochs Date: Wed, 14 Jul 2021 14:11:50 +0200 Subject: [PATCH 6/6] Clean up code --- engine/converter.ts | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/engine/converter.ts b/engine/converter.ts index c5ab1cfb..b7c2855e 100644 --- a/engine/converter.ts +++ b/engine/converter.ts @@ -9,20 +9,15 @@ import { FileContains } from "../assertions/fileContains"; export class Converter { -private readonly TMP_DIR = "tmp_convert"; + private readonly TMP_DIR = "tmp_convert"; -private platform: ConsolePlatform; + private commandsAvailable: boolean; constructor() { - // Determine platform for script execution - if(process.platform == "win32") { - this.platform = ConsolePlatform.WINDOWS; - }else { - this.platform = ConsolePlatform.LINUX; - } // Create temporary directory in which files can be created & modified this.createTempDir(); + this.commandsAvailable = this.commandAvailable("asciidoctor -v") && this.commandAvailable("pandoc"); } createTempDir(): void { @@ -40,9 +35,7 @@ private platform: ConsolePlatform; readTempFile(filename: string): string { let file_path = path.join(this.getTempDir(), filename); - let fileContent = ""; - let file = fs.readFileSync(file_path); - fileContent = file.toString() + let fileContent = fs.readFileSync(file_path, "utf8"); return fileContent; } @@ -63,23 +56,13 @@ private platform: ConsolePlatform; this.createTempFile("temp.adoc", asciidocString); let markdownString = ""; - if(this.platform == ConsolePlatform.WINDOWS) { - if(this.commandAvailable("asciidoctor") && this.commandAvailable("pandoc")) { - this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); - this.executeCommand("pandoc -f docbook -t gfm temp.xml -o temp.md", this.getTempDir()); - markdownString = this.readTempFile("temp.md"); - }else { - markdownString = this.readTempFile("temp.adoc"); - } + if(this.commandsAvailable) { + this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); + this.executeCommand("pandoc -f docbook -t gfm temp.xml -o temp.md", this.getTempDir()); + markdownString = this.readTempFile("temp.md"); }else { - if(this.commandAvailable("asciidoctor") && this.commandAvailable("pandoc")) { - this.executeCommand("asciidoctor -b docbook temp.adoc", this.getTempDir()); - this.executeCommand("pandoc -f docbook -t gfm temp.xml -o temp.md", this.getTempDir()); - markdownString = this.readTempFile("temp.md"); - }else { - markdownString = this.readTempFile("temp.adoc"); - } - } + markdownString = asciidocString; + } return markdownString; } } @@ -94,7 +77,7 @@ private platform: ConsolePlatform; let process = child_process.spawnSync(command, {shell: true, cwd: directory}); if(process.status != 0) { console.log("Error executing command: " + command + " (exit code: )" + process.status + ")"); - console.log(process.stderr.toString(), process.stdout.toString()); + console.log(process.stderr.toString(), process.toString()); } } } \ No newline at end of file