add exportExtension
This commit is contained in:
parent
cce35e5d05
commit
22f03b8f48
3 changed files with 111 additions and 7 deletions
30
d2cli/export.go
Normal file
30
d2cli/export.go
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
package d2cli
|
||||||
|
|
||||||
|
import "path/filepath"
|
||||||
|
|
||||||
|
type exportExtension string
|
||||||
|
|
||||||
|
var KNOWN_EXTENSIONS = []string{".svg", ".png", ".pptx", ".pdf"}
|
||||||
|
|
||||||
|
func getExportExtension(outputPath string) exportExtension {
|
||||||
|
ext := filepath.Ext(outputPath)
|
||||||
|
for _, kext := range KNOWN_EXTENSIONS {
|
||||||
|
if kext == ext {
|
||||||
|
return exportExtension(ext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// default is svg
|
||||||
|
return exportExtension(".svg")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ex exportExtension) supportsAnimation() bool {
|
||||||
|
return ex == ".svg"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ex exportExtension) requiresPngRenderer() bool {
|
||||||
|
return ex == ".png" || ex == ".pdf" || ex == ".pptx"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ex exportExtension) supportsDarkTheme() bool {
|
||||||
|
return ex == ".svg"
|
||||||
|
}
|
||||||
73
d2cli/export_test.go
Normal file
73
d2cli/export_test.go
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
package d2cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestOutputFormat(t *testing.T) {
|
||||||
|
type testCase struct {
|
||||||
|
outputPath string
|
||||||
|
extension exportExtension
|
||||||
|
supportsDarkTheme bool
|
||||||
|
supportsAnimation bool
|
||||||
|
requiresPngRender bool
|
||||||
|
}
|
||||||
|
testCases := []testCase{
|
||||||
|
{
|
||||||
|
outputPath: "/out.svg",
|
||||||
|
extension: ".svg",
|
||||||
|
supportsDarkTheme: true,
|
||||||
|
supportsAnimation: true,
|
||||||
|
requiresPngRender: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// assumes SVG by default
|
||||||
|
outputPath: "/out",
|
||||||
|
extension: ".svg",
|
||||||
|
supportsDarkTheme: true,
|
||||||
|
supportsAnimation: true,
|
||||||
|
requiresPngRender: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
outputPath: "-",
|
||||||
|
extension: ".svg",
|
||||||
|
supportsDarkTheme: true,
|
||||||
|
supportsAnimation: true,
|
||||||
|
requiresPngRender: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
outputPath: "/out.png",
|
||||||
|
extension: ".png",
|
||||||
|
supportsDarkTheme: false,
|
||||||
|
supportsAnimation: false,
|
||||||
|
requiresPngRender: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
outputPath: "/out.pptx",
|
||||||
|
extension: ".pptx",
|
||||||
|
supportsDarkTheme: false,
|
||||||
|
supportsAnimation: false,
|
||||||
|
requiresPngRender: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
outputPath: "/out.pdf",
|
||||||
|
extension: ".pdf",
|
||||||
|
supportsDarkTheme: false,
|
||||||
|
supportsAnimation: false,
|
||||||
|
requiresPngRender: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
t.Run(tc.outputPath, func(t *testing.T) {
|
||||||
|
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())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -186,14 +186,13 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
|
||||||
inputPath = filepath.Join(inputPath, "index.d2")
|
inputPath = filepath.Join(inputPath, "index.d2")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
outputFormat := getExportExtension(outputPath)
|
||||||
if outputPath != "-" {
|
if outputPath != "-" {
|
||||||
outputPath = ms.AbsPath(outputPath)
|
outputPath = ms.AbsPath(outputPath)
|
||||||
if *animateIntervalFlag > 0 {
|
if *animateIntervalFlag > 0 && !outputFormat.supportsAnimation() {
|
||||||
// Not checking for extension == "svg", because users may want to write SVG data to a non-svg-extension file
|
|
||||||
if filepath.Ext(outputPath) == ".png" || filepath.Ext(outputPath) == ".pdf" || filepath.Ext(outputPath) == ".pptx" {
|
|
||||||
return xmain.UsageErrorf("-animate-interval can only be used when exporting to SVG.\nYou provided: %s", filepath.Ext(outputPath))
|
return xmain.UsageErrorf("-animate-interval can only be used when exporting to SVG.\nYou provided: %s", filepath.Ext(outputPath))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match := d2themescatalog.Find(*themeFlag)
|
match := d2themescatalog.Find(*themeFlag)
|
||||||
|
|
@ -236,12 +235,14 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
|
||||||
}
|
}
|
||||||
ms.Log.Debug.Printf("using layout plugin %s (%s)", *layoutFlag, plocation)
|
ms.Log.Debug.Printf("using layout plugin %s (%s)", *layoutFlag, plocation)
|
||||||
|
|
||||||
var pw png.Playwright
|
if !outputFormat.supportsDarkTheme() {
|
||||||
if filepath.Ext(outputPath) == ".png" || filepath.Ext(outputPath) == ".pdf" || filepath.Ext(outputPath) == ".pptx" {
|
|
||||||
if darkThemeFlag != nil {
|
if darkThemeFlag != nil {
|
||||||
ms.Log.Warn.Printf("--dark-theme cannot be used while exporting to another format other than .svg")
|
ms.Log.Warn.Printf("--dark-theme cannot be used while exporting to another format other than .svg")
|
||||||
darkThemeFlag = nil
|
darkThemeFlag = nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
var pw png.Playwright
|
||||||
|
if outputFormat.requiresPngRenderer() {
|
||||||
pw, err = png.InitPlaywright()
|
pw, err = png.InitPlaywright()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue