class Neruda::Templater

Insert custom part inside generated HTML files.

Public Class Methods

customize_output(file_name, source = nil) click to toggle source
# File lib/neruda/templater.rb, line 36
def customize_output(file_name, source = nil)
  templates_to_apply = filter_templates(file_name)
  return if templates_to_apply.empty?
  if source.nil?
    sourcepath = Neruda::OrgFile.source_for_target(file_name)
    source = Neruda::OrgFile.new(sourcepath)
  end
  dom = open_dom(file_name)
  templates_to_apply.each do |t|
    tpl = Neruda::Templater.new(source, dom, t)
    next if tpl.in_head?
    tpl.apply
  end
  write_dom(file_name, dom)
end
new(source, dom, opts = {}) click to toggle source
# File lib/neruda/templater.rb, line 10
def initialize(source, dom, opts = {})
  @dom = dom
  @org_file = source
  @position = opts['type'] || 'after'
  @content = extract_content opts
  @element = @dom.css(opts['selector'])
  digest = Digest::MD5.hexdigest(@content)
  @check_line = " Neruda Template: #{digest} "
end

Private Class Methods

check_path(file_name, pathes) click to toggle source
# File lib/neruda/templater.rb, line 73
def check_path(file_name, pathes)
  pub_folder = Neruda::Config.settings['public_folder']
  if pathes.is_a?(Array)
    pathes.each do |tp|
      return true if File.fnmatch?("#{pub_folder}#{tp}",
                                   file_name, File::FNM_DOTMATCH)
    end
    return false
  end
  File.fnmatch?("#{pub_folder}#{pathes}",
                file_name, File::FNM_DOTMATCH)
end
check_required_keys(opts, file_name) click to toggle source
# File lib/neruda/templater.rb, line 86
def check_required_keys(opts, file_name)
  return false unless opts.has_key?('selector')
  return false unless opts.has_key?('content') || opts.has_key?('source')
  return check_path(file_name, opts['path']) if opts.has_key?('path')
  true
end
filter_templates(file_name) click to toggle source
# File lib/neruda/templater.rb, line 54
def filter_templates(file_name)
  templates = Neruda::Config.settings['templates']
  return [] if templates.nil? || templates.empty?
  templates.filter { |t| check_required_keys(t, file_name) }
end
open_dom(file_name) click to toggle source
# File lib/neruda/templater.rb, line 60
def open_dom(file_name)
  file = File.new file_name, 'r'
  dom = Nokogiri::HTML file
  file.close
  dom
end
write_dom(file_name, dom) click to toggle source
# File lib/neruda/templater.rb, line 67
def write_dom(file_name, dom)
  file = File.new file_name, 'w'
  dom.write_to file
  file.close
end

Public Instance Methods

apply() click to toggle source
# File lib/neruda/templater.rb, line 20
def apply
  flag_head
  content = @org_file.format(@content)
  @element.each do |e|
    insert_new_node_at e, content
  end
end
in_head?() click to toggle source
# File lib/neruda/templater.rb, line 28
def in_head?
  @dom.xpath('//head').children.to_a.filter(&:comment?).each do |c|
    return true if c.text == @check_line
  end
  false
end

Private Instance Methods

extract_content(opts) click to toggle source
# File lib/neruda/templater.rb, line 111
def extract_content(opts)
  return opts['content'] if opts['content']
  # If we don't have a content option, then we must have a source
  # one.
  @dom.css(opts['source']).unlink.to_s
end
flag_head() click to toggle source
# File lib/neruda/templater.rb, line 96
def flag_head
  @dom.xpath('//head').first.prepend_child("<!--#{@check_line}-->\n")
end
insert_new_node_at(elem, content) click to toggle source
# File lib/neruda/templater.rb, line 100
def insert_new_node_at(elem, content)
  case @position
  when 'before'
    elem.add_previous_sibling content
  when 'replace'
    elem.replace content
  else
    elem.add_next_sibling content
  end
end