-
Notifications
You must be signed in to change notification settings - Fork 18
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
base: main
Are you sure you want to change the base?
Changes from all commits
167c578
174bc76
3283b0d
94a3c4e
fa5e79e
c7d3604
5c4f532
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
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()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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 => { | ||
|
@@ -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"), { }); | ||
|
@@ -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"]); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"}); | ||
|
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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)