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
|
package d2cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -10,23 +9,21 @@ type exportExtension string
|
||||||
const GIF exportExtension = ".gif"
|
const GIF exportExtension = ".gif"
|
||||||
const PNG exportExtension = ".png"
|
const PNG exportExtension = ".png"
|
||||||
const PPTX exportExtension = ".pptx"
|
const PPTX exportExtension = ".pptx"
|
||||||
|
const PPT exportExtension = ".ppt"
|
||||||
const PDF exportExtension = ".pdf"
|
const PDF exportExtension = ".pdf"
|
||||||
const SVG exportExtension = ".svg"
|
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)
|
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 {
|
for _, kext := range SUPPORTED_EXTENSIONS {
|
||||||
if kext == exportExtension(ext) {
|
if kext == exportExtension(ext) {
|
||||||
return exportExtension(ext), nil
|
return exportExtension(ext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// default is svg
|
// default is svg
|
||||||
return exportExtension(SVG), nil
|
return exportExtension(SVG)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ex exportExtension) supportsAnimation() bool {
|
func (ex exportExtension) supportsAnimation() bool {
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,14 @@ func TestOutputFormat(t *testing.T) {
|
||||||
requiresAnimationInterval: false,
|
requiresAnimationInterval: false,
|
||||||
requiresPngRender: true,
|
requiresPngRender: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
outputPath: "/out.ppt",
|
||||||
|
extension: PPT,
|
||||||
|
supportsDarkTheme: false,
|
||||||
|
supportsAnimation: false,
|
||||||
|
requiresAnimationInterval: false,
|
||||||
|
requiresPngRender: false,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
outputPath: "/out.pdf",
|
outputPath: "/out.pdf",
|
||||||
extension: PDF,
|
extension: PDF,
|
||||||
|
|
@ -78,17 +86,11 @@ func TestOutputFormat(t *testing.T) {
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
tc := tc
|
tc := tc
|
||||||
t.Run(tc.outputPath, func(t *testing.T) {
|
t.Run(tc.outputPath, func(t *testing.T) {
|
||||||
extension, err := getExportExtension(tc.outputPath)
|
extension := getExportExtension(tc.outputPath)
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, tc.extension, extension)
|
assert.Equal(t, tc.extension, extension)
|
||||||
assert.Equal(t, tc.supportsAnimation, extension.supportsAnimation())
|
assert.Equal(t, tc.supportsAnimation, extension.supportsAnimation())
|
||||||
assert.Equal(t, tc.supportsDarkTheme, extension.supportsDarkTheme())
|
assert.Equal(t, tc.supportsDarkTheme, extension.supportsDarkTheme())
|
||||||
assert.Equal(t, tc.requiresPngRender, extension.requiresPNGRenderer())
|
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")
|
inputPath = filepath.Join(inputPath, "index.d2")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outputFormat, err := getExportExtension(outputPath)
|
outputFormat := getExportExtension(outputPath)
|
||||||
if err != nil {
|
if outputFormat == PPT {
|
||||||
return xmain.UsageErrorf(err.Error())
|
return xmain.UsageErrorf("D2 does not support ppt exports, did you mean \"pptx\"?")
|
||||||
}
|
}
|
||||||
if outputPath != "-" {
|
if outputPath != "-" {
|
||||||
outputPath = ms.AbsPath(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
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ext, _ := getExportExtension(outputPath)
|
ext := getExportExtension(outputPath)
|
||||||
switch ext {
|
switch ext {
|
||||||
case GIF:
|
case GIF:
|
||||||
svg, pngs, err := renderPNGsForGIF(ctx, ms, plugin, renderOpts, ruler, page, diagram)
|
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) {
|
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 := getExportExtension(outputPath) == PNG
|
||||||
toPNG := ext == PNG
|
|
||||||
svg, err := d2svg.Render(diagram, &d2svg.RenderOpts{
|
svg, err := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||||
Pad: opts.Pad,
|
Pad: opts.Pad,
|
||||||
Sketch: opts.Sketch,
|
Sketch: opts.Sketch,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue