class SlideRenderer
Renders a single slide using its XRB template.
A plain Ruby object (not a Live view) that resolves templates via a
class Presently::Templates instance and produces HTML for a given class Presently::Slide.
Definitions
def initialize(css_class: "slide", templates: Templates.for)
Initialize a new slide renderer.
Signature
-
parameter
css_classString The CSS class for the slide container element.
-
parameter
templatesTemplates The template resolver to use.
Implementation
def initialize(css_class: "slide", templates: Templates.for)
@css_class = css_class
@templates = templates
end
def render_to_html(slide, extra_class: nil)
Render a slide to an HTML string.
Signature
-
parameter
slideSlide The slide to render.
-
parameter
extra_classString | Nil An additional CSS class for the container.
-
returns
XRB::MarkupString The rendered HTML, safe for embedding.
Implementation
def render_to_html(slide, extra_class: nil)
builder = XRB::Builder.new
render(builder, slide, extra_class: extra_class)
XRB::MarkupString.raw(builder.to_s)
end
def render(builder, slide, extra_class: nil)
Render a slide into the given builder.
Signature
-
parameter
builderXRB::Builder The HTML builder.
-
parameter
slideSlide The slide to render.
-
parameter
extra_classString | Nil An additional CSS class for the container.
Implementation
def render(builder, slide, extra_class: nil)
return unless slide
template = @templates.resolve(slide.template)
scope = TemplateScope.new(slide)
html = template.to_string(scope)
classes = [@css_class, extra_class]
classes << "slide-scripted" unless slide.scripts.empty?
classes = classes.compact.join(" ")
path = Stylesheet.encode_path(slide.path)
background = slide.background
if background
# Escape the CSS string independently of the builder's HTML escaping:
url = background.gsub(/["\\\x00-\x1f\x7f]/){|character| "\\#{character.ord.to_s(16)} "}
style = %(background-image: url("#{url}"); background-size: #{slide.background_size};)
end
builder.tag(:div, class: "slide-surface", data: {template: slide.template, background: background}, style: style) do
builder.tag(:div, class: classes, data: {template: slide.template}, "data-slide-path": path) do
builder.raw(html)
slide.scripts.each do |script|
builder.tag(:script, type: "text/slide-script") do
builder.raw(script)
end
end
end
end
end