Module: Markdownizer::DSL
- Defined in:
- lib/markdownizer.rb
Overview
The Markdownizer DSL is the public interface of the gem, and can be called from any ActiveRecord model.
Instance Method Summary collapse
-
#markdownize!(attribute, options = {}) ⇒ Object
Calling ‘markdownize! :attribute` (where `:attribute` can be any database attribute with type `text`) will treat this field as Markdown.
Instance Method Details
#markdownize!(attribute, options = {}) ⇒ Object
Calling ‘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.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/markdownizer.rb', line 175 def markdownize! attribute, = {} # 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 Markdownizer 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 = .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 self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(self.send(attribute), ), hierarchy)) end end |