exporter.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Package exporter provides diagram export functionality
  2. package exporter
  3. import (
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "mermaid-go/pkg/ast"
  10. )
  11. // ExportFormat represents the export format
  12. type ExportFormat string
  13. const (
  14. FormatSVG ExportFormat = "svg"
  15. )
  16. // ExportOptions contains export configuration
  17. type ExportOptions struct {
  18. Format ExportFormat
  19. Width int
  20. Height int
  21. DPI int
  22. Theme string
  23. }
  24. // DefaultExportOptions returns default export options
  25. func DefaultExportOptions() *ExportOptions {
  26. return &ExportOptions{
  27. Format: FormatSVG,
  28. Width: 800,
  29. Height: 600,
  30. DPI: 96,
  31. Theme: "default",
  32. }
  33. }
  34. // DiagramExporter provides unified diagram export functionality
  35. type DiagramExporter struct {
  36. svgExporter *SVGExporter
  37. }
  38. // NewDiagramExporter creates a new diagram exporter
  39. func NewDiagramExporter() *DiagramExporter {
  40. return &DiagramExporter{
  41. svgExporter: NewSVGExporter(),
  42. }
  43. }
  44. // Export exports a diagram with the given options
  45. func (e *DiagramExporter) Export(diagram ast.Diagram, options *ExportOptions) ([]byte, error) {
  46. if options == nil {
  47. options = DefaultExportOptions()
  48. }
  49. switch options.Format {
  50. case FormatSVG:
  51. return e.exportSVG(diagram, options)
  52. default:
  53. return nil, fmt.Errorf("unsupported export format: %s", options.Format)
  54. }
  55. }
  56. // ExportToFile exports a diagram to a file
  57. func (e *DiagramExporter) ExportToFile(diagram ast.Diagram, filename string, options *ExportOptions) error {
  58. // Infer format from filename if not specified
  59. if options == nil {
  60. options = DefaultExportOptions()
  61. ext := strings.ToLower(filepath.Ext(filename))
  62. switch ext {
  63. case ".svg":
  64. options.Format = FormatSVG
  65. default:
  66. options.Format = FormatSVG
  67. }
  68. }
  69. data, err := e.Export(diagram, options)
  70. if err != nil {
  71. return fmt.Errorf("failed to export diagram: %w", err)
  72. }
  73. err = os.WriteFile(filename, data, 0644)
  74. if err != nil {
  75. return fmt.Errorf("failed to write file: %w", err)
  76. }
  77. return nil
  78. }
  79. // ExportToWriter exports a diagram to an io.Writer
  80. func (e *DiagramExporter) ExportToWriter(diagram ast.Diagram, writer io.Writer, options *ExportOptions) error {
  81. data, err := e.Export(diagram, options)
  82. if err != nil {
  83. return fmt.Errorf("failed to export diagram: %w", err)
  84. }
  85. _, err = writer.Write(data)
  86. if err != nil {
  87. return fmt.Errorf("failed to write data: %w", err)
  88. }
  89. return nil
  90. }
  91. // exportSVG exports to SVG format
  92. func (e *DiagramExporter) exportSVG(diagram ast.Diagram, options *ExportOptions) ([]byte, error) {
  93. e.svgExporter.SetSize(options.Width, options.Height)
  94. e.svgExporter.SetTheme(options.Theme)
  95. svg, err := e.svgExporter.ExportToSVG(diagram)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return []byte(svg), nil
  100. }
  101. // GetSupportedFormats returns supported export formats
  102. func (e *DiagramExporter) GetSupportedFormats() []ExportFormat {
  103. return []ExportFormat{FormatSVG}
  104. }
  105. // GetSupportedDiagramTypes returns supported diagram types for export
  106. func (e *DiagramExporter) GetSupportedDiagramTypes() []ast.DiagramType {
  107. return []ast.DiagramType{
  108. ast.DiagramTypeFlowchart,
  109. ast.DiagramTypeSequence,
  110. ast.DiagramTypeClassDiagram,
  111. ast.DiagramTypeStateDiagram,
  112. ast.DiagramTypeERDiagram,
  113. ast.DiagramTypePie,
  114. ast.DiagramTypeGantt,
  115. ast.DiagramTypeTimeline,
  116. ast.DiagramTypeUserJourney,
  117. ast.DiagramTypeArchitecture,
  118. ast.DiagramTypeOrganization,
  119. ast.DiagramTypeBPMN,
  120. }
  121. }
  122. // IsFormatSupported checks if a format is supported
  123. func (e *DiagramExporter) IsFormatSupported(format ExportFormat) bool {
  124. for _, f := range e.GetSupportedFormats() {
  125. if f == format {
  126. return true
  127. }
  128. }
  129. return false
  130. }
  131. // IsDiagramTypeSupported checks if a diagram type is supported for export
  132. func (e *DiagramExporter) IsDiagramTypeSupported(diagramType ast.DiagramType) bool {
  133. for _, dt := range e.GetSupportedDiagramTypes() {
  134. if dt == diagramType {
  135. return true
  136. }
  137. }
  138. return false
  139. }