Sha256: 892d4fe83537ef6806a24a6058148ad5fd65b1f557461a8e353c436b47147925

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

require_relative "error"

module Marmerdo
  # This class generates a markdown or mermaid file from a mermaid diagram.
  class OutputGenerator
    WARNING_COMMENT = "This file was generated by Marmerdo. Do not edit it manually.".freeze

    class UnknownOutputExtensionError < Error
      def initialize(output_extension)
        super("Unknown output extension #{output_extension}.\nSupported filetypes are markdown and mermaid.")
      end
    end

    def initialize(output_path, domain_diagram)
      @output_path = output_path
      @domain_diagram = domain_diagram
    end

    # @return [String]
    def generate
      case output_filetype
      when :markdown
        [
          "<!-- #{WARNING_COMMENT} -->",
          "",
          "```mermaid",
          @domain_diagram,
          "```"
        ].join("\n")
      when :mermaid
        [
          "%% #{WARNING_COMMENT}",
          "",
          @domain_diagram
        ].join("\n")
      end
    end

    private

    def output_extension
      File.extname(@output_path)
    end

    def output_filetype
      case output_extension
      when ".markdown", ".md"
        :markdown
      when ".mermaid", ".mmd"
        :mermaid
      else
        raise UnknownOutputExtensionError, output_extension
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
marmerdo-0.3.1 lib/marmerdo/output_generator.rb
marmerdo-0.2.3 lib/marmerdo/output_generator.rb
marmerdo-0.2.2 lib/marmerdo/output_generator.rb
marmerdo-0.2.1 lib/marmerdo/output_generator.rb
marmerdo-0.2.0 lib/marmerdo/output_generator.rb