Class: JekyllReact::Render

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll_react/render.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, document, site_payload = nil) ⇒ Render

Returns a new instance of Render.



13
14
15
16
17
# File 'lib/jekyll_react/render.rb', line 13

def initialize(site, document, site_payload = nil)
  @site     = site
  @document = document
  @payload  = site_payload
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



10
11
12
# File 'lib/jekyll_react/render.rb', line 10

def document
  @document
end

#layoutsObject

The list of layouts registered for this Renderer. It can be written with #layouts=(new_layouts) Falls back to site.layouts if no layouts are registered.

Returns a Hash of String => Jekyll::Layout identified as basename without the extension name.



34
35
36
# File 'lib/jekyll_react/render.rb', line 34

def layouts
  @layouts || site.layouts
end

#payloadObject

Fetches the payload used in Liquid rendering. It can be written with #payload=(new_payload) Falls back to site.site_payload if no payload is set.

Returns a Jekyll::Drops::UnifiedPayloadDrop



24
25
26
# File 'lib/jekyll_react/render.rb', line 24

def payload
  @payload ||= site.site_payload
end

#siteObject (readonly)

Returns the value of attribute site.



10
11
12
# File 'lib/jekyll_react/render.rb', line 10

def site
  @site
end

Instance Method Details

#convert(content) ⇒ Object

Convert the given content using the converters which match this renderer’s document.

content - the raw, unconverted content

Returns the converted content.



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/jekyll_react/render.rb', line 96

def convert(content)
  converters.reduce(content) do |output, converter|
    begin
      converter.convert output
    rescue => e
      Jekyll.logger.error "Conversion error:",
                          "#{converter.class} encountered an error while "\
        "converting '#{document.relative_path}':"
      Jekyll.logger.error("", e.to_s)
      raise e
    end
  end
end

#convertersObject

Determine which converters to use based on this document’s extension.

Returns an array of Converter instances.



42
43
44
# File 'lib/jekyll_react/render.rb', line 42

def converters
  @converters ||= site.converters.select { |c| c.matches(document.extname) }.sort
end

#invalid_layout?(layout) ⇒ Boolean

Checks if the layout specified in the document actually exists

layout - the layout to check

Returns true if the layout is invalid, false if otherwise

Returns:

  • (Boolean)


138
139
140
# File 'lib/jekyll_react/render.rb', line 138

def invalid_layout?(layout)
  !document.data["layout"].nil? && layout.nil? && !(document.is_a? Jekyll::Excerpt)
end

#output_extObject

Determine the extname the outputted file should have

Returns the output extname including the leading period.



49
50
51
# File 'lib/jekyll_react/render.rb', line 49

def output_ext
  @output_ext ||= (permalink_ext || converter_output_ext)
end

#place_in_layouts(content, payload, info) ⇒ Object

Render layouts and place given content inside.

content - the content to be placed in the layout

Returns the content placed in the Liquid-rendered layouts



148
149
150
151
152
153
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
179
180
181
182
183
184
185
186
187
# File 'lib/jekyll_react/render.rb', line 148

def place_in_layouts(content, payload, info)
  output = content.dup
  layout = layouts[document.data["layout"]]

  Jekyll.logger.warn(
      "Build Warning:",
      "Layout '#{document.data["layout"]}' requested in "\
    "#{document.relative_path} does not exist."
  ) if invalid_layout? layout

  used = Set.new([layout])

  # Reset the payload layout data to ensure it starts fresh for each page.
  payload["layout"] = nil

  while layout
    payload["content"] = output
    payload["layout"]  = Jekyll::Utils.deep_merge_hashes(layout.data, payload["layout"] || {})

    output = render_liquid(
        layout.content,
        payload,
        info,
        layout.relative_path
    )

    # Add layout to dependency tree
    site.regenerator.add_dependency(
        site.in_source_dir(document.path),
        site.in_source_dir(layout.path)
    ) if document.write?

    if (layout = layouts[layout.data["layout"]])
      break if used.include?(layout)
      used << layout
    end
  end

  output
end

#render_liquid(content, payload, info, path = nil) ⇒ Object

Render the given content with the payload and info

content - payload - info - path - (optional) the path to the file, for use in ex

Returns the content, rendered by Liquid.



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/jekyll_react/render.rb', line 118

def render_liquid(content, payload, info, path = nil)
  template = site.liquid_renderer.file(path).parse(content)
  template.warnings.each do |e|
    Jekyll.logger.warn "Liquid Warning:",
                       LiquidRenderer.format_error(e, path || document.relative_path)
  end
  template.render!(payload, info)
    # rubocop: disable RescueException
rescue Exception => e
  Jekyll.logger.error "Liquid Exception:",
                      LiquidRenderer.format_error(e, path || document.relative_path)
  raise e
end

#runObject

DAT RENDER THO



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jekyll_react/render.rb', line 57

def run
  Jekyll.logger.debug "Rendering:", document.relative_path

  payload["page"] = document.to_liquid

  if document.respond_to? :pager
    payload["paginator"] = document.pager.to_liquid
  end

  # if document.is_a?(Jekyll::Document) && document.collection.label == "posts"
  # payload["site"]["related_posts"] = document.related_posts
  # else
  # payload["site"]["related_posts"] = nil
  # end

  # render and transform content (this becomes the final content of the object)
  payload["highlighter_prefix"] = converters.first.highlighter_prefix
  payload["highlighter_suffix"] = converters.first.highlighter_suffix

  Jekyll.logger.debug "Pre-Render Hooks:", document.relative_path
  document.trigger_hooks(:pre_render, payload)

  info = {
      :registers => { :site => site, :page => payload["page"] }
  }

  output = document.content

  if document.render_with_liquid?
    Jekyll.logger.debug "Rendering Liquid:", document.relative_path
    output = render_liquid(output, payload, info, document.path)
  end
end