-
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 6 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,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); | ||
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) { | ||
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. Why is this if-else-block is needed? Both block execute the same commands |
||
if(this.commandAvailable("asciidoctor") && this.commandAvailable("pandoc")) { | ||
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. 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"); | ||
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. Couldn't you use |
||
} | ||
}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) { | ||
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. 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 commentThe 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()); | ||
} | ||
} | ||
} |
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.
If you pass the encoding parameter you dont have to convert it to a string afterwards.