Class: Coradoc::Html::Spa::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/html/spa.rb

Overview

Configuration for SPA HTML output

Plain Ruby configuration class with accessors and defaults.

Constant Summary collapse

VALID_THEME_VARIANTS =

Valid theme variants

%i[glass minimal vibrant].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Configuration

Initialize configuration with options

Parameters:

  • options (Hash)

    Configuration options



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/coradoc/html/spa.rb', line 111

def initialize(**options)
  @asset_delivery = options[:asset_delivery] || :embedded
  @theme_variant = options[:theme_variant] || :glass
  @primary_color = options[:primary_color] || '#6366f1'
  @accent_color = options[:accent_color] || '#8b5cf6'
  @theme_toggle = options.fetch(:theme_toggle, true)
  @reading_progress = options.fetch(:reading_progress, true)
  @back_to_top = options.fetch(:back_to_top, true)
  @toc_sticky = options.fetch(:toc_sticky, true)
  @copy_code_buttons = options.fetch(:copy_code_buttons, true)
  @toc_levels = options[:toc_levels] || 2
  @toc_title = options[:toc_title] || 'Table of Contents'
  @enable_animations = options.fetch(:enable_animations, true)
  @animation_duration = options[:animation_duration] || '300ms'
  @lazy_load_images = options.fetch(:lazy_load_images, true)
  @max_width = options[:max_width] || '1200px'
  @content_width = options[:content_width] || '65ch'
  @sidebar_width = options[:sidebar_width] || '280px'
  @lang = options[:lang] || 'en'
  @meta_description = options[:meta_description]
  @meta_keywords = options[:meta_keywords]
  @open_graph = options.fetch(:open_graph, false)
end

Instance Attribute Details

#accent_colorObject

Accent color (hex string, e.g., ‘#8b5cf6’)



52
53
54
# File 'lib/coradoc/html/spa.rb', line 52

def accent_color
  @accent_color
end

#animation_durationObject

Animation duration (CSS value, e.g., ‘300ms’)



79
80
81
# File 'lib/coradoc/html/spa.rb', line 79

def animation_duration
  @animation_duration
end

#asset_deliveryObject

How to deliver assets (:embedded always for SPA)



43
44
45
# File 'lib/coradoc/html/spa.rb', line 43

def asset_delivery
  @asset_delivery
end

#back_to_topObject

Whether to show back to top button



61
62
63
# File 'lib/coradoc/html/spa.rb', line 61

def back_to_top
  @back_to_top
end

#content_widthObject

Content width (CSS value)



88
89
90
# File 'lib/coradoc/html/spa.rb', line 88

def content_width
  @content_width
end

#copy_code_buttonsObject

Whether to add copy buttons to code blocks



67
68
69
# File 'lib/coradoc/html/spa.rb', line 67

def copy_code_buttons
  @copy_code_buttons
end

#enable_animationsObject

Whether to enable animations



76
77
78
# File 'lib/coradoc/html/spa.rb', line 76

def enable_animations
  @enable_animations
end

#langObject

Language attribute for HTML



94
95
96
# File 'lib/coradoc/html/spa.rb', line 94

def lang
  @lang
end

#lazy_load_imagesObject

Whether to lazy load images



82
83
84
# File 'lib/coradoc/html/spa.rb', line 82

def lazy_load_images
  @lazy_load_images
end

#max_widthObject

Maximum width of the container (CSS value)



85
86
87
# File 'lib/coradoc/html/spa.rb', line 85

def max_width
  @max_width
end

#meta_descriptionObject

Custom meta description



97
98
99
# File 'lib/coradoc/html/spa.rb', line 97

def meta_description
  @meta_description
end

#meta_keywordsObject

Custom meta keywords



100
101
102
# File 'lib/coradoc/html/spa.rb', line 100

def meta_keywords
  @meta_keywords
end

#open_graphObject

Enable Open Graph meta tags



103
104
105
# File 'lib/coradoc/html/spa.rb', line 103

def open_graph
  @open_graph
end

#primary_colorObject

Primary color (hex string, e.g., ‘#6366f1’)



49
50
51
# File 'lib/coradoc/html/spa.rb', line 49

def primary_color
  @primary_color
end

#reading_progressObject

Whether to show reading progress bar



58
59
60
# File 'lib/coradoc/html/spa.rb', line 58

def reading_progress
  @reading_progress
end

Sidebar width for TOC (CSS value)



91
92
93
# File 'lib/coradoc/html/spa.rb', line 91

def sidebar_width
  @sidebar_width
end

#theme_toggleObject

Whether to enable theme toggle (dark/light mode)



55
56
57
# File 'lib/coradoc/html/spa.rb', line 55

def theme_toggle
  @theme_toggle
end

#theme_variantObject

Theme appearance variant (:glass, :minimal, :vibrant)



46
47
48
# File 'lib/coradoc/html/spa.rb', line 46

def theme_variant
  @theme_variant
end

#toc_levelsObject

TOC levels to include (1-5)



70
71
72
# File 'lib/coradoc/html/spa.rb', line 70

def toc_levels
  @toc_levels
end

#toc_stickyObject

Whether TOC should be sticky



64
65
66
# File 'lib/coradoc/html/spa.rb', line 64

def toc_sticky
  @toc_sticky
end

#toc_titleObject

TOC title text



73
74
75
# File 'lib/coradoc/html/spa.rb', line 73

def toc_title
  @toc_title
end

Class Method Details

.defaultsConfiguration

Default configuration

Returns:



138
139
140
# File 'lib/coradoc/html/spa.rb', line 138

def self.defaults
  new
end

Instance Method Details

#merge(other) ⇒ Configuration

Merge with another configuration or hash

Parameters:

Returns:



146
147
148
149
# File 'lib/coradoc/html/spa.rb', line 146

def merge(other)
  other_hash = other.is_a?(Configuration) ? other.to_h : other.to_h.transform_keys(&:to_sym)
  self.class.new(**to_h.merge(other_hash))
end

#to_hHash

Convert to hash

Returns:

  • (Hash)

    Configuration as hash



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/coradoc/html/spa.rb', line 154

def to_h
  {
    asset_delivery: @asset_delivery,
    theme_variant: @theme_variant,
    primary_color: @primary_color,
    accent_color: @accent_color,
    theme_toggle: @theme_toggle,
    reading_progress: @reading_progress,
    back_to_top: @back_to_top,
    toc_sticky: @toc_sticky,
    copy_code_buttons: @copy_code_buttons,
    toc_levels: @toc_levels,
    toc_title: @toc_title,
    enable_animations: @enable_animations,
    animation_duration: @animation_duration,
    lazy_load_images: @lazy_load_images,
    max_width: @max_width,
    content_width: @content_width,
    sidebar_width: @sidebar_width,
    lang: @lang,
    meta_description: @meta_description,
    meta_keywords: @meta_keywords,
    open_graph: @open_graph
  }
end

#to_renderer_optionsHash

Convert to options hash for ModernRenderer

Returns:

  • (Hash)

    Options hash for the modern renderer



206
207
208
209
210
211
212
213
214
# File 'lib/coradoc/html/spa.rb', line 206

def to_renderer_options
  {
    modern: to_h,
    lang: @lang,
    toc: @toc_sticky,
    toclevels: @toc_levels,
    toc_title: @toc_title
  }
end

#validate!Object

Validate configuration

Raises:



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/coradoc/html/spa.rb', line 183

def validate!
  validate_hex_color(@primary_color, 'primary_color')
  validate_hex_color(@accent_color, 'accent_color')
  validate_css_value(@max_width, 'max_width')
  validate_css_value(@content_width, 'content_width')
  validate_css_value(@sidebar_width, 'sidebar_width')
  validate_css_value(@animation_duration, 'animation_duration')

  unless VALID_THEME_VARIANTS.include?(@theme_variant.to_sym)
    raise ConverterBase::ValidationError,
          "Invalid theme variant: #{@theme_variant}. " \
          "Valid variants: #{VALID_THEME_VARIANTS.join(', ')}"
  end

  return if @toc_levels.is_a?(Integer) && @toc_levels.between?(1, 5)

  raise ConverterBase::ValidationError,
        'TOC levels must be an integer between 1 and 5'
end