refactor
This commit is contained in:
parent
bd1e889e5c
commit
07032da486
3 changed files with 19 additions and 21 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package d2cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
|
|
@ -10,23 +9,21 @@ type exportExtension string
|
|||
const GIF exportExtension = ".gif"
|
||||
const PNG exportExtension = ".png"
|
||||
const PPTX exportExtension = ".pptx"
|
||||
const PPT exportExtension = ".ppt"
|
||||
const PDF exportExtension = ".pdf"
|
||||
const SVG exportExtension = ".svg"
|
||||
|
||||
var SUPPORTED_EXTENSIONS = []exportExtension{SVG, PNG, PDF, PPTX, GIF}
|
||||
var SUPPORTED_EXTENSIONS = []exportExtension{SVG, PNG, PDF, PPTX, GIF, PPT}
|
||||
|
||||
func getExportExtension(outputPath string) (exportExtension, error) {
|
||||
func getExportExtension(outputPath string) exportExtension {
|
||||
ext := filepath.Ext(outputPath)
|
||||
if ext == ".ppt" {
|
||||
return "", fmt.Errorf("D2 does not support ppt exports, did you mean \"pptx\"?")
|
||||
}
|
||||
for _, kext := range SUPPORTED_EXTENSIONS {
|
||||
if kext == exportExtension(ext) {
|
||||
return exportExtension(ext), nil
|
||||
return exportExtension(ext)
|
||||
}
|
||||
}
|
||||
// default is svg
|
||||
return exportExtension(SVG), nil
|
||||
return exportExtension(SVG)
|
||||
}
|
||||
|
||||
func (ex exportExtension) supportsAnimation() bool {
|
||||
|
|
|
|||
|
|
@ -57,6 +57,14 @@ func TestOutputFormat(t *testing.T) {
|
|||
requiresAnimationInterval: false,
|
||||
requiresPngRender: true,
|
||||
},
|
||||
{
|
||||
outputPath: "/out.ppt",
|
||||
extension: PPT,
|
||||
supportsDarkTheme: false,
|
||||
supportsAnimation: false,
|
||||
requiresAnimationInterval: false,
|
||||
requiresPngRender: false,
|
||||
},
|
||||
{
|
||||
outputPath: "/out.pdf",
|
||||
extension: PDF,
|
||||
|
|
@ -78,17 +86,11 @@ func TestOutputFormat(t *testing.T) {
|
|||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.outputPath, func(t *testing.T) {
|
||||
extension, err := getExportExtension(tc.outputPath)
|
||||
assert.NoError(t, err)
|
||||
extension := getExportExtension(tc.outputPath)
|
||||
assert.Equal(t, tc.extension, extension)
|
||||
assert.Equal(t, tc.supportsAnimation, extension.supportsAnimation())
|
||||
assert.Equal(t, tc.supportsDarkTheme, extension.supportsDarkTheme())
|
||||
assert.Equal(t, tc.requiresPngRender, extension.requiresPNGRenderer())
|
||||
})
|
||||
}
|
||||
|
||||
// unsupported format
|
||||
_, err := getExportExtension("/out.ppt")
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, "D2 does not support ppt exports, did you mean \"pptx\"?", err.Error())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -187,9 +187,9 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
inputPath = filepath.Join(inputPath, "index.d2")
|
||||
}
|
||||
}
|
||||
outputFormat, err := getExportExtension(outputPath)
|
||||
if err != nil {
|
||||
return xmain.UsageErrorf(err.Error())
|
||||
outputFormat := getExportExtension(outputPath)
|
||||
if outputFormat == PPT {
|
||||
return xmain.UsageErrorf("D2 does not support ppt exports, did you mean \"pptx\"?")
|
||||
}
|
||||
if outputPath != "-" {
|
||||
outputPath = ms.AbsPath(outputPath)
|
||||
|
|
@ -356,7 +356,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, rende
|
|||
return nil, false, err
|
||||
}
|
||||
|
||||
ext, _ := getExportExtension(outputPath)
|
||||
ext := getExportExtension(outputPath)
|
||||
switch ext {
|
||||
case GIF:
|
||||
svg, pngs, err := renderPNGsForGIF(ctx, ms, plugin, renderOpts, ruler, page, diagram)
|
||||
|
|
@ -625,8 +625,7 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug
|
|||
}
|
||||
|
||||
func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts d2svg.RenderOpts, outputPath string, bundle, forceAppendix bool, page playwright.Page, ruler *textmeasure.Ruler, diagram *d2target.Diagram) ([]byte, error) {
|
||||
ext, _ := getExportExtension(outputPath)
|
||||
toPNG := ext == PNG
|
||||
toPNG := getExportExtension(outputPath) == PNG
|
||||
svg, err := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||
Pad: opts.Pad,
|
||||
Sketch: opts.Sketch,
|
||||
|
|
|
|||
Loading…
Reference in a new issue