minor refactor

This commit is contained in:
Júlio César Batista 2023-04-06 11:04:17 -03:00
parent e4bbc26959
commit 4b2041f759
No known key found for this signature in database
GPG key ID: 10C4B861BF314878

View file

@ -360,15 +360,12 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, rende
ms.Log.Success.Printf("successfully compiled %s to %s in %s", ms.HumanPath(inputPath), ms.HumanPath(outputPath), dur)
return pdf, true, nil
case ".pptx":
ext := filepath.Ext(outputPath)
trimmedPath := strings.TrimSuffix(outputPath, ext)
splitPath := strings.Split(trimmedPath, "/")
rootName := splitPath[len(splitPath)-1]
var username string
if user, err := user.Current(); err == nil {
username = user.Username
}
description := "Presentation auto-generated by D2 - https://d2lang.com/"
rootName := getFileName(outputPath)
p := pptx.NewPresentation(rootName, description, rootName, username, version.OnlyNumbers())
err := renderPPTX(ctx, ms, p, plugin, renderOpts, outputPath, page, diagram, nil)
if err != nil {
@ -673,11 +670,7 @@ func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opt
var currBoardPath []string
// Root board doesn't have a name, so we use the output filename
if diagram.Name == "" {
ext := filepath.Ext(outputPath)
trimmedPath := strings.TrimSuffix(outputPath, ext)
splitPath := strings.Split(trimmedPath, "/")
rootName := splitPath[len(splitPath)-1]
currBoardPath = append(boardPath, rootName)
currBoardPath = append(boardPath, getFileName(outputPath))
} else {
currBoardPath = append(boardPath, diagram.Name)
}
@ -764,11 +757,7 @@ func renderPPTX(ctx context.Context, ms *xmain.State, presentation *pptx.Present
var currBoardPath []string
// Root board doesn't have a name, so we use the output filename
if diagram.Name == "" {
ext := filepath.Ext(outputPath)
trimmedPath := strings.TrimSuffix(outputPath, ext)
splitPath := strings.Split(trimmedPath, "/")
rootName := splitPath[len(splitPath)-1]
currBoardPath = append(boardPath, rootName)
currBoardPath = append(boardPath, getFileName(outputPath))
} else {
currBoardPath = append(boardPath, diagram.Name)
}
@ -844,6 +833,13 @@ func renameExt(fp string, newExt string) string {
}
}
func getFileName(path string) string {
ext := filepath.Ext(path)
trimmedPath := strings.TrimSuffix(path, ext)
splitPath := strings.Split(trimmedPath, "/")
return splitPath[len(splitPath)-1]
}
// TODO: remove after removing slog
func DiscardSlog(ctx context.Context) context.Context {
return ctxlog.With(ctx, slog.Make(sloghuman.Sink(io.Discard)))