Class: Pekky::Format::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/pekky/format.rb

Overview

Transformer class for the formatters and filters. A subclass can actually be both a formatter – takes text and makes HTML – or a filter – takes HTML and modifies it in some way. The reason for this is that the logic for doing both is often shared. For example in the code formatting, we sometimes want to render code directly from text, or we want to modify existing markup – like some embedded in textile output.

Direct Known Subclasses

Erb, Haml, Links, Markdown, Rdoc, Textile

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loadedObject (readonly)

Returns the value of attribute loaded.



109
110
111
# File 'lib/pekky/format.rb', line 109

def loaded
  @loaded
end

Class Method Details

._filter(text) ⇒ Object

Wraps around the #filter method defined in the subclasses. It does some preflight stuff like loading requirements and setting up the Nokogiri document.



160
161
162
163
164
165
# File 'lib/pekky/format.rb', line 160

def self._filter(text)
  check_for_requirements
  doc = Nokogiri::HTML.fragment(text)
  filter(doc, text)
  doc.to_html
end

._format(text, opts = {}) ⇒ Object

This method wraps the actual #format method defined by the subclass. This is so we can make sure the dependencies are loaded and then run any of the specified filters – or the defaults.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pekky/format.rb', line 135

def self._format(text, opts = {})
  check_for_requirements
  
  output = if method(:format).arity > 1
    format(text, opts)
  else
    format(text)
  end
  
  filters = opts.delete(:filters) || Filters.defaults
  
  if filters.empty?
    output
  else
    doc = Nokogiri::HTML.fragment(output)
    filters.each do |name|
      Filters[name].filter(doc, output)
    end
    doc.to_html
  end
end

.nameObject

Returns the snake case version of this class name.



128
129
130
# File 'lib/pekky/format.rb', line 128

def self.name 
  @name ||= self.to_s.match(/::(\w+)$/)[1].downcase.to_sym
end

.singleton_method_added(method_name) ⇒ Object

This hook is being used to see if the subclasses define either the #format or #filter methods and thus need to be added to the relevant wrappers.



120
121
122
123
124
125
# File 'lib/pekky/format.rb', line 120

def self.singleton_method_added(method_name)
  case method_name
    when :format then Formatters.add(name, self)
    when :filter then Filters.add(name, self)
  end
end