class Nanoc::Polly::Backend

Protected Instance Methods

create!(identifier, about, json) click to toggle source
# File lib/nanoc/polly/backend.rb, line 186
def create! identifier, about, json
  sanitized_content = sanitize_content(json['content'])
  begin
    @data_source.create_item(sanitized_content, {
      about: about,
      title: json['title'] ? json['title'] : 'Title',
      layout: 'default'}, identifier)
    recompile!
  rescue Nanoc::Errors::Generic => e
    # rollback, i.e. delete the newly created file
    # we assume the file was created in this request as we should have
    # passed the "page already exists" test in the post handler
    new_items = @data_source.items.select do |item|
      item.identifier == identifier
    end
    path = File.expand_path(new_items.first.raw_filename)
    if File.file? path
      File.delete path
    end
    puts "[ERR] #{e}"
    recompile!
    return [ 400, {}, {
      about: identifier,
      message: "Invalid request. #{e} Rolled back."
    }.to_json ]
  end

  return [ 202, {}, {
    about: identifier,
    message: "Page creation request accepted.",
    content: sanitized_content
  }.to_json ]
end
recompile!() click to toggle source

TODO maybe as a nonblocking background task

# File lib/nanoc/polly/backend.rb, line 178
def recompile!
  # difficult to cache site because of nanoc internal caching (freeze)
  # TODO optimize recompilation
  # assuming we are running in a nanoc site dir
  site = Nanoc::Site.new('.')
  site.compile
end
sanitize_about(about) click to toggle source

@TODO

# File lib/nanoc/polly/backend.rb, line 159
def sanitize_about about
  return about
end
sanitize_attributes(attributes) click to toggle source
# File lib/nanoc/polly/backend.rb, line 172
def sanitize_attributes attributes
  blacklist = [:mtime, :file, :filename, :extension, :meta_filename, :content_filename]
  return attributes.delete_if { |k,v| blacklist.include? k }
end
sanitize_content(content = '') click to toggle source

@TODO TODO maybe as a nonblocking background task

# File lib/nanoc/polly/backend.rb, line 165
def sanitize_content content = ''
  # see https://github.com/flavorjones/loofah
  sanitized_content = Loofah.fragment(content).scrub!(:prune).to_s
  beautified_output = HtmlBeautifier.beautify(sanitized_content)
  return beautified_output
end
update!(identifier, about, attributes, json) click to toggle source
# File lib/nanoc/polly/backend.rb, line 220
def update! identifier, about, attributes, json
  sanitized_content = sanitize_content(json['content'])
  attributes.merge!(json['attributes']) if json['attributes']
  # make sure that about is not overriden in PUT
  # we always want this to be the value given in the request path
  attributes[:about] = about
  begin
    @data_source.create_item(sanitized_content, attributes, identifier)
    recompile!
  rescue Nanoc::Errors::Generic => e
    @data_source.create_item(@content_bak, @attributes_bak, identifier)
    recompile!
    puts "[ERR] #{e}"
    return [ 400, {}, {
      about: identifier,
      message: "Invalid request. #{e} Rolled back."
    }.to_json ]
  end

  return [ 202, {}, {
    about: identifier,
    message: "Page update request accepted.",
    content: sanitized_content
  }.to_json ]
end