Module: ExtendedMarkdownizer::DSL

Defined in:
lib/extended_markdownizer.rb

Overview

The ExtendedMarkdownizer DSL is the public interface of the gem, and can be called from any ActiveRecord model.

Instance Method Summary collapse

Instance Method Details

#extended_markdownize!(attribute, options = {}) ⇒ Object

Calling ‘extended_markdownize! :attribute` (where `:attribute` can be any database attribute with type `text`) will treat this field as Markdown. You can pass an `options` hash for CodeRay. An example option would be:

* `:line_numbers => :table` (or `:inline`)

You can check other available options in CodeRay’s documentation.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/extended_markdownizer.rb', line 222

def extended_markdownize! attribute, options = {}
  # Check that both `:attribute` and `:rendered_attribute` columns exist.
  # If they don't, it raises an error indicating that the user should generate
  # a migration.
  unless self.column_names.include?(attribute.to_s) &&
          self.column_names.include?("rendered_#{attribute}")
    raise "#{self.name} doesn't have required attributes :#{attribute} and :rendered_#{attribute}\nPlease generate a migration to add these attributes -- both should have type :text."
  end

  # The `:hierarchy` option tells ExtendedMarkdownizer the smallest header tag that
  # precedes the Markdown text. If you have a blogpost with an H1 (title) and
  # an H2 (some kind of tagline), then your hierarchy is 2, and the biggest
  # header found the markdown text will be translated directly to an H3. This
  # allows for semantical coherence within the context where the markdown text
  # is to be introduced.
  hierarchy = options.delete(:hierarchy) || 0

  # Create a `before_save` callback which will convert plain text to
  # Markdownized html every time the model is saved.
  self.before_save :"render_#{attribute}"

  # Define the converter method, which will assign the rendered html to the
  # `:rendered_attribute` field.
  define_method :"render_#{attribute}" do
    processed_attribute = ExtendedMarkdownizer.youtube_embedded_videos(self.send(attribute)) 
    processed_attribute = ExtendedMarkdownizer.vimeo_embedded_videos(processed_attribute) 
    processed_attribute = ExtendedMarkdownizer.detect_images(processed_attribute) 
    processed_attribute = ExtendedMarkdownizer.urls_into_anchors(processed_attribute) 
    self.send(:"rendered_#{attribute}=", ExtendedMarkdownizer.markdown(ExtendedMarkdownizer.coderay(processed_attribute, options), hierarchy))
  end
end