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 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
83 changes: 83 additions & 0 deletions engine/converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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 commandsAvailable: boolean;

constructor() {

// Create temporary directory in which files can be created & modified
this.createTempDir();
this.commandsAvailable = this.commandAvailable("asciidoctor -v") && this.commandAvailable("pandoc");
}

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 = fs.readFileSync(file_path, "utf8");
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.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 {
markdownString = asciidocString;
}
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.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