Skip to content

Commit

Permalink
puml parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
localgod committed Sep 7, 2023
1 parent 87e054c commit bce2679
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/catalyst.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Mx } from './mx/Mx.mjs'
import { MxGeometry } from "./mx/MxGeometry.mjs"
import { Svg } from './svg/Svg.mjs'
import { PlantUmlPipe } from "plantuml-pipe";
import { PumlParser } from "./pumlParser.mjs";

async function svg2mx(svg: Svg): Promise<string> {
const output = new Mx(svg.getDocumentHeight(), svg.getDocumentWidth());
Expand All @@ -22,7 +23,7 @@ async function svg2mx(svg: Svg): Promise<string> {
as: 'geometry',
},
};
console.log(element)
console.log(element?.text)
await output.addMxC4Object(g, name, 'kurt', 'be awesome');
}
}
Expand Down Expand Up @@ -56,18 +57,25 @@ const program = new Command()
program.description('An application for converting plantuml diagrams to draw.io xml')
program.requiredOption('-i, --input <path>', 'path to input file')
program.requiredOption('-o, --output <path>', 'path to output file')
program.action(async () => {
const svg = new Svg()
const svgData = await puml2Svg('diagram.puml')
await svg.load(svgData)
const data = await svg2mx(svg)

try {
fs.writeFileSync('diagram.drawio', data);
fs.writeFileSync('diagram.svg', svgData);
console.log('File written successfully.');
} catch (error) {
console.error('Error writing file:', error);
program.action(async (options) => {
if (fs.existsSync(options.input)) {
const svg = new Svg()
const puml = await fs.promises.readFile(options.input, 'utf-8')
const parser = new PumlParser(puml)
console.log(parser.parse())
const svgData = await puml2Svg(options.input)
await svg.load(svgData)
const data = await svg2mx(svg)

try {
fs.writeFileSync(options.output, data);
// fs.writeFileSync('diagram.svg', svgData);
console.log('File written successfully.');
} catch (error) {
console.error('Error writing file:', error);
}
} else {
throw new Error('Input file does not exist')
}
})

Expand Down
25 changes: 25 additions & 0 deletions src/pumlParser.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type Element = { type: string, alias: string, label: string, description: string }
class PumlParser {
readonly elementPattern = /(System|Container|Component)\((\w+),\s?"([^"]*)?",\s?"([^"]*)?",\s?"([^"]*)?"/;
private input: string
private elements: Element[] = []

constructor(input: string) {
this.input = input
}

parse(): Element[] {
const lines = this.input.split('\n');
for (const line of lines) {

const match = line.match(this.elementPattern);
if (match) {
// Something wrong... only finds "Container"
this.elements.push({ type: match[1], alias: match[2], label: match[3], description: match[4] })
}
}
return this.elements;
}
}

export { PumlParser }

0 comments on commit bce2679

Please sign in to comment.