class Mako::Article

Attributes

published[R]
summary[R]
title[R]
uri[R]

Public Class Methods

new(args) click to toggle source
# File lib/mako/article.rb, line 7
def initialize(args)
  @title = args.fetch(:title, '')
  @published = args.fetch(:published)
  @uri = URI.parse(args.fetch(:url))
  @summary = sanitize(args.fetch(:summary))
end

Public Instance Methods

formatted_published() click to toggle source

Converts published Time object to formatted string

@return [String]

# File lib/mako/article.rb, line 17
def formatted_published
  @published.strftime('%A, %d %B %Y at %I:%M %P')
end
url() click to toggle source

Converts URI object into string

@return [String]

# File lib/mako/article.rb, line 24
def url
  uri.to_s
end

Private Instance Methods

sanitize(html) click to toggle source

Transforms img tags into a tags (if configured) and transforms h1 tags into p tags with the class bold

@param [String] html an html document string @return [String] a sanitized html document string

# File lib/mako/article.rb, line 35
def sanitize(html)
  doc = Nokogiri::HTML::DocumentFragment.parse(html)
  if Mako.config.sanitize_images
    doc.css('img').each do |n|
      begin
        n.name = 'a'
        n.content = n['alt'] ? "📷 #{n['alt']}" : '📷 Image'
        n['href'] = URI.parse(n['src']).absolutize!(uri)
      rescue URI::InvalidURIError
        # if there's a problem, just ignore it
        next
      end
    end
  end
  doc.css('h1,h2,h3,h4,h5,h6').each { |n| n.name = 'p'; n.set_attribute('class', 'bold') }
  doc.to_s
end