# frozen_string_literal: true module HoTModuLe class View < Bridgetown::ERBView attr_accessor :child_nodes end class ComponentRenderer < Bridgetown::Builder def build render_html_modules end # rubocop:todo Metrics def render_html_modules inspect_html do |doc, resource| view_context = HoTModuLe::View.new(resource) HoTModuLe.registered_elements.each do |component| tag_name = component.tag_name doc.xpath("//#{tag_name}").reverse.each do |node| if node["hmod:ignore"] node.attribute("hmod:ignore").remove next end attrs = node.attributes.transform_values(&:value) attrs.reject! { |k| k.start_with?("hmod:") } new_attrs = {} attrs.each do |k, v| next unless k.start_with?("arg:") new_key = k.delete_prefix("arg:") attrs.delete(k) new_attrs[new_key] = resource.instance_eval(v) end attrs.merge!(new_attrs) attrs.transform_keys!(&:to_sym) view_context.child_nodes = node.children new_node = node.replace( component.new(**attrs).render_in(view_context) { node.children.to_html.html_safe } ) new_node.first.attribute("hmod:ignore")&.remove end rescue StandardError => e Bridgetown.logger.error "Unable to render <#{tag_name}> (#{component}) in #{resource.path}" raise e end end end # rubocop:enable Metrics end end