Class: Kms::TemplateProcessor

Inherits:
Object
  • Object
show all
Defined in:
app/services/kms/template_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ TemplateProcessor

Returns a new instance of TemplateProcessor.



3
4
5
# File 'app/services/kms/template_processor.rb', line 3

def initialize(template)
  @template = template
end

Instance Method Details

#processObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/kms/template_processor.rb', line 7

def process
  template_document = Nokogiri::HTML @template.content
  template_document.search('script, link[rel="stylesheet"]').each do |tag|
    url = tag['src'] || tag['href']
    next unless url
    tag.replace "{{ '#{File.basename(url)}' | asset_tag }}"
  end
  # can't use Nokogiri for images because of Nokogiri escaping
  template_document.search('img').each do |tag|
    tag.replace %Q(<img src="{{ '#{File.basename(tag['src'])}' | asset_path }}" alt="#{tag['alt']}">)
  end
  template_document.to_html.gsub(/(%7B|%7D|%20|%7C)/,'%7B' => '{', '%7D' => '}', '%20' => ' ', '%7C' => '|')
end