Class: Lutaml::Xsd::Spa::HtmlDocumentBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xsd/spa/html_document_builder.rb

Overview

HTML document builder (Builder Pattern)

Provides a fluent interface for building complete HTML documents with proper structure, meta tags, styles, and scripts. This builder separates the construction of a complex HTML document from its representation.

Examples:

Build a single-file HTML document

builder = HtmlDocumentBuilder.new
html = builder
  .title("My Schema")
  .meta_tag("description", "Schema documentation")
  .inline_style(css_content)
  .inline_script(js_content)
  .body_content(main_content)
  .build

Build a multi-file HTML document

builder = HtmlDocumentBuilder.new
html = builder
  .title("My Schema")
  .external_stylesheet("css/styles.css")
  .external_script("js/app.js")
  .body_content(main_content)
  .build

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HtmlDocumentBuilder

Initialize HTML document builder

Parameters:

  • options (Hash) (defaults to: {})

    Initial options



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 36

def initialize(options = {})
  @options = default_options.merge(options)
  @meta_tags = []
  @stylesheets = []
  @inline_styles = []
  @scripts = []
  @inline_scripts = []
  @body_classes = []
  @body_attributes = {}
  @head_content = []
  @body_content = ""
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



31
32
33
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 31

def options
  @options
end

Instance Method Details

#body_attribute(name, value) ⇒ self

Add body attribute

Parameters:

  • name (String)

    Attribute name

  • value (String)

    Attribute value

Returns:

  • (self)

    Builder instance for chaining



147
148
149
150
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 147

def body_attribute(name, value)
  @body_attributes[name] = value
  self
end

#body_class(class_name) ⇒ self

Add body class

Parameters:

  • class_name (String)

    CSS class name

Returns:

  • (self)

    Builder instance for chaining



137
138
139
140
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 137

def body_class(class_name)
  @body_classes << class_name
  self
end

#body_content(content) ⇒ self

Set body content

Parameters:

  • content (String)

    HTML content for body

Returns:

  • (self)

    Builder instance for chaining



165
166
167
168
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 165

def body_content(content)
  @body_content = content
  self
end

#buildString

Build complete HTML document

Returns:

  • (String)

    Complete HTML document



182
183
184
185
186
187
188
189
190
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 182

def build
  parts = []
  parts << doctype
  parts << html_open_tag
  parts << build_head
  parts << build_body
  parts << html_close_tag
  parts.join("\n")
end

#charset(charset = "UTF-8") ⇒ self

Add charset meta tag

Parameters:

  • charset (String) (defaults to: "UTF-8")

    Character encoding (default: UTF-8)

Returns:

  • (self)

    Builder instance for chaining



81
82
83
84
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 81

def charset(charset = "UTF-8")
  @options[:charset] = charset
  self
end

#external_script(src, options = {}) ⇒ self

Add external script

Parameters:

  • src (String)

    Script URL

  • options (Hash) (defaults to: {})

    Script options (defer, async, module)

Returns:

  • (self)

    Builder instance for chaining



118
119
120
121
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 118

def external_script(src, options = {})
  @scripts << { src: src, options: options }
  self
end

#external_stylesheet(href) ⇒ self

Add external stylesheet link

Parameters:

  • href (String)

    Stylesheet URL

Returns:

  • (self)

    Builder instance for chaining



99
100
101
102
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 99

def external_stylesheet(href)
  @stylesheets << href
  self
end

#head_content(content) ⇒ self

Add content to head section

Parameters:

  • content (String)

    HTML content for head

Returns:

  • (self)

    Builder instance for chaining



156
157
158
159
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 156

def head_content(content)
  @head_content << content
  self
end

#inline_script(js, options = {}) ⇒ self

Add inline script content

Parameters:

  • js (String)

    JavaScript content

  • options (Hash) (defaults to: {})

    Script options

Returns:

  • (self)

    Builder instance for chaining



128
129
130
131
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 128

def inline_script(js, options = {})
  @inline_scripts << { content: js, options: options }
  self
end

#inline_style(css) ⇒ self

Add inline style content

Parameters:

  • css (String)

    CSS content

Returns:

  • (self)

    Builder instance for chaining



108
109
110
111
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 108

def inline_style(css)
  @inline_styles << css
  self
end

#language(value) ⇒ self

Set document language

Parameters:

  • value (String)

    Language code (e.g., “en”, “ja”)

Returns:

  • (self)

    Builder instance for chaining



62
63
64
65
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 62

def language(value)
  @options[:lang] = value
  self
end

#meta_tag(name, content) ⇒ self

Add meta tag

Parameters:

  • name (String)

    Meta tag name

  • content (String)

    Meta tag content

Returns:

  • (self)

    Builder instance for chaining



72
73
74
75
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 72

def meta_tag(name, content)
  @meta_tags << { name: name, content: content }
  self
end

#resetself

Reset builder to initial state

Returns:

  • (self)

    Builder instance for chaining



195
196
197
198
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 195

def reset
  initialize(@options.slice(:lang, :charset, :viewport))
  self
end

#theme(theme) ⇒ self

Set theme (adds data-theme attribute to body)

Parameters:

  • theme (String)

    Theme name (e.g., “light”, “dark”)

Returns:

  • (self)

    Builder instance for chaining



174
175
176
177
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 174

def theme(theme)
  body_attribute("data-theme", theme)
  self
end

#title(value) ⇒ self

Set document title

Parameters:

  • value (String)

    Document title

Returns:

  • (self)

    Builder instance for chaining



53
54
55
56
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 53

def title(value)
  @options[:title] = value
  self
end

#viewport(content = "width=device-width, initial-scale=1.0") ⇒ self

Add viewport meta tag

Parameters:

  • content (String) (defaults to: "width=device-width, initial-scale=1.0")

    Viewport content

Returns:

  • (self)

    Builder instance for chaining



90
91
92
93
# File 'lib/lutaml/xsd/spa/html_document_builder.rb', line 90

def viewport(content = "width=device-width, initial-scale=1.0")
  @options[:viewport] = content
  self
end