Module: Caracal::Core::Text

Included in:
Document
Defined in:
lib/caracal/core/text.rb

Overview

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

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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