module Caracal::Core::Text

This module encapsulates all the functionality related to adding text to the document.

Public Class Methods

included(base) click to toggle source
# File lib/caracal/core/text.rb, line 13
def self.included(base)
  base.class_eval do
    
    #-------------------------------------------------------------
    # Public Methods
    #-------------------------------------------------------------
    
    #============== PARAGRAPHS ==========================
    
    def p(*args, &block)
      options = Caracal::Utilities.extract_options!(args)
      options.merge!({ content: args.first }) if args.first
      
      model = Caracal::Core::Models::ParagraphModel.new(options, &block)
      if model.valid?
        contents << model
      else
        raise Caracal::Errors::InvalidModelError, 'Paragraphs and headings, which delegate to the :p command, require at least one text string.'
      end
      model
    end
    
    
    #============== HEADINGS ============================
    
    # All heading methods simply delegate to the paragraph
    # model with an explicitly set style class.
    #
    [:h1, :h2, :h3, :h4, :h5, :h6].each do |cmd|
      define_method "#{ cmd }" do |*args, &block|
        options = Caracal::Utilities.extract_options!(args)
        options.merge!({ style: style_id_for_header(cmd) })
        p(args.first, options, &block)
      end
    end
    
    
    #-------------------------------------------------------------
    # Private Methods
    #-------------------------------------------------------------
    private
    
    # This method translates the html-like command to the
    # corresponding style id.
    #
    def style_id_for_header(command)
      case command.to_s
        when 'h1' then 'Heading1'
        when 'h2' then 'Heading2'
        when 'h3' then 'Heading3'
        when 'h4' then 'Heading4'
        when 'h5' then 'Heading5'
        when 'h6' then 'Heading6'
        else 'Normal'
      end
    end
    
  end
end

Public Instance Methods

p(*args, &block) click to toggle source
PARAGRAPHS ==========================
# File lib/caracal/core/text.rb, line 22
def p(*args, &block)
  options = Caracal::Utilities.extract_options!(args)
  options.merge!({ content: args.first }) if args.first
  
  model = Caracal::Core::Models::ParagraphModel.new(options, &block)
  if model.valid?
    contents << model
  else
    raise Caracal::Errors::InvalidModelError, 'Paragraphs and headings, which delegate to the :p command, require at least one text string.'
  end
  model
end
style_id_for_header(command) click to toggle source

This method translates the html-like command to the corresponding style id.

# File lib/caracal/core/text.rb, line 58
def style_id_for_header(command)
  case command.to_s
    when 'h1' then 'Heading1'
    when 'h2' then 'Heading2'
    when 'h3' then 'Heading3'
    when 'h4' then 'Heading4'
    when 'h5' then 'Heading5'
    when 'h6' then 'Heading6'
    else 'Normal'
  end
end