class Kramdown::PlantUml::Theme

Provides theming support for PlantUML

Attributes

directory[R]
name[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/kramdown-plantuml/theme.rb, line 11
def initialize(options = {})
  @logger = Logger.init
  @name, @directory = theme_options(options)
end

Public Instance Methods

apply(plantuml) click to toggle source
# File lib/kramdown-plantuml/theme.rb, line 16
def apply(plantuml)
  if plantuml.nil? || !plantuml.is_a?(String) || plantuml.empty?
    @logger.debug ' kramdown-plantuml: Empty diagram or not a String.'
    return plantuml
  end

  if @name.nil? || @name.empty?
    @logger.debug ' kramdown-plantuml: No theme to apply.'
    return plantuml
  end

  theme(plantuml)
end

Private Instance Methods

symbolize_keys(options) click to toggle source
# File lib/kramdown-plantuml/theme.rb, line 46
def symbolize_keys(options)
  return options if options.nil?

  array = options.map do |key, value|
    value = value.is_a?(Hash) ? symbolize_keys(value) : value
    [key.to_sym, value]
  end

  array.to_h
end
theme(plantuml) click to toggle source
# File lib/kramdown-plantuml/theme.rb, line 57
def theme(plantuml)
  startuml = '@startuml'
  startuml_index = plantuml.index(startuml) + startuml.length

  return plantuml if startuml_index.nil?

  theme_string = "\n!theme #{@name}"
  theme_string << " from #{@directory}" unless @directory.nil?

  @logger.debug " kramdown-plantuml: Applying #{theme_string.strip}"

  /@startuml.*/.match(plantuml) do |match|
    return plantuml.insert match.end(0), theme_string
  end

  plantuml
end
theme_options(options) click to toggle source
# File lib/kramdown-plantuml/theme.rb, line 32
def theme_options(options)
  options = symbolize_keys(options)

  @logger.debug " kramdown-plantuml: Options: #{options}"

  return nil if options.nil? || !options.key?(:theme)

  theme = options[:theme] || {}
  name = theme.key?(:name) ? theme[:name] : nil
  directory = theme.key?(:directory) ? theme[:directory] : nil

  [name, directory]
end