// Package exporter provides diagram export functionality package exporter import ( "fmt" "io" "os" "path/filepath" "strings" "mermaid-go/pkg/ast" ) // ExportFormat represents the export format type ExportFormat string const ( FormatSVG ExportFormat = "svg" ) // ExportOptions contains export configuration type ExportOptions struct { Format ExportFormat Width int Height int DPI int Theme string } // DefaultExportOptions returns default export options func DefaultExportOptions() *ExportOptions { return &ExportOptions{ Format: FormatSVG, Width: 800, Height: 600, DPI: 96, Theme: "default", } } // DiagramExporter provides unified diagram export functionality type DiagramExporter struct { svgExporter *SVGExporter } // NewDiagramExporter creates a new diagram exporter func NewDiagramExporter() *DiagramExporter { return &DiagramExporter{ svgExporter: NewSVGExporter(), } } // Export exports a diagram with the given options func (e *DiagramExporter) Export(diagram ast.Diagram, options *ExportOptions) ([]byte, error) { if options == nil { options = DefaultExportOptions() } switch options.Format { case FormatSVG: return e.exportSVG(diagram, options) default: return nil, fmt.Errorf("unsupported export format: %s", options.Format) } } // ExportToFile exports a diagram to a file func (e *DiagramExporter) ExportToFile(diagram ast.Diagram, filename string, options *ExportOptions) error { // Infer format from filename if not specified if options == nil { options = DefaultExportOptions() ext := strings.ToLower(filepath.Ext(filename)) switch ext { case ".svg": options.Format = FormatSVG default: options.Format = FormatSVG } } data, err := e.Export(diagram, options) if err != nil { return fmt.Errorf("failed to export diagram: %w", err) } err = os.WriteFile(filename, data, 0644) if err != nil { return fmt.Errorf("failed to write file: %w", err) } return nil } // ExportToWriter exports a diagram to an io.Writer func (e *DiagramExporter) ExportToWriter(diagram ast.Diagram, writer io.Writer, options *ExportOptions) error { data, err := e.Export(diagram, options) if err != nil { return fmt.Errorf("failed to export diagram: %w", err) } _, err = writer.Write(data) if err != nil { return fmt.Errorf("failed to write data: %w", err) } return nil } // exportSVG exports to SVG format func (e *DiagramExporter) exportSVG(diagram ast.Diagram, options *ExportOptions) ([]byte, error) { e.svgExporter.SetSize(options.Width, options.Height) e.svgExporter.SetTheme(options.Theme) svg, err := e.svgExporter.ExportToSVG(diagram) if err != nil { return nil, err } return []byte(svg), nil } // GetSupportedFormats returns supported export formats func (e *DiagramExporter) GetSupportedFormats() []ExportFormat { return []ExportFormat{FormatSVG} } // GetSupportedDiagramTypes returns supported diagram types for export func (e *DiagramExporter) GetSupportedDiagramTypes() []ast.DiagramType { return []ast.DiagramType{ ast.DiagramTypeFlowchart, ast.DiagramTypeSequence, ast.DiagramTypeClassDiagram, ast.DiagramTypeStateDiagram, ast.DiagramTypeERDiagram, ast.DiagramTypePie, ast.DiagramTypeGantt, ast.DiagramTypeTimeline, ast.DiagramTypeUserJourney, ast.DiagramTypeArchitecture, ast.DiagramTypeOrganization, ast.DiagramTypeBPMN, } } // IsFormatSupported checks if a format is supported func (e *DiagramExporter) IsFormatSupported(format ExportFormat) bool { for _, f := range e.GetSupportedFormats() { if f == format { return true } } return false } // IsDiagramTypeSupported checks if a diagram type is supported for export func (e *DiagramExporter) IsDiagramTypeSupported(diagramType ast.DiagramType) bool { for _, dt := range e.GetSupportedDiagramTypes() { if dt == diagramType { return true } } return false }