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

Enhancement/convert asciidoc #288

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
100 changes: 100 additions & 0 deletions engine/converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you pass the encoding parameter you dont have to convert it to a string afterwards.

fileContent = file.toString()
return fileContent;
}

getTempDir(): string {
// Return tempDir
return path.join(__dirname, this.TMP_DIR);
}

convertAsciidocToMarkdown(asciidocString: string): string {
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);

let markdownString = "";
if(this.platform == ConsolePlatform.WINDOWS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this if-else-block is needed? Both block execute the same commands

if(this.commandAvailable("asciidoctor") && this.commandAvailable("pandoc")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to test if both commands are available in the constructor once. Otherwise you will always test and execute the commands when you want to convert something

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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you use markdownString = asciidocString in this case? Then you do not need to call the function

}
}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");
}
}
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the function executeCommandSync() we already got in ConsoleUtils for this ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically yes, but the executeCommandSync() from ConsoleUtils requires parameteres that I cannot provide (e.g. RunResult, env)

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());
}
}
}
15 changes: 13 additions & 2 deletions runners/katacoda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
import { Assertions } from "../../assertions";

export class Katacoda extends Runner {
Expand All @@ -25,6 +26,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
Expand All @@ -49,6 +51,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 => {
Expand All @@ -62,8 +66,8 @@ export class Katacoda extends Runner {

async destroy(playbook: Playbook): Promise<void> {
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"), { });
Expand Down Expand Up @@ -464,6 +468,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"]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also check for playbook.description and convert it to markdown. Now we don´t have a problem with that but somebody could also use asciidoc formating in the description in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I implemented it correctly, the description conversion can be found here


let template = fs.readFileSync(path.join(this.getRunnerDirectory(),"templates", name), 'utf8');
let result = ejs.render(template, variables);
fs.writeFileSync(targetPath, result, {flag: "a"});
Expand Down