caracal 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +334 -702
  3. data/lib/caracal.rb +1 -0
  4. data/lib/caracal/core/file_name.rb +1 -1
  5. data/lib/caracal/core/fonts.rb +2 -2
  6. data/lib/caracal/core/images.rb +3 -2
  7. data/lib/caracal/core/list_styles.rb +20 -20
  8. data/lib/caracal/core/lists.rb +2 -2
  9. data/lib/caracal/core/models/base_model.rb +1 -1
  10. data/lib/caracal/core/models/border_model.rb +1 -1
  11. data/lib/caracal/core/models/image_model.rb +1 -1
  12. data/lib/caracal/core/models/link_model.rb +11 -0
  13. data/lib/caracal/core/models/list_item_model.rb +2 -2
  14. data/lib/caracal/core/models/list_model.rb +4 -3
  15. data/lib/caracal/core/models/list_style_model.rb +7 -18
  16. data/lib/caracal/core/models/margin_model.rb +1 -1
  17. data/lib/caracal/core/models/page_number_model.rb +1 -1
  18. data/lib/caracal/core/models/page_size_model.rb +1 -1
  19. data/lib/caracal/core/models/paragraph_model.rb +8 -6
  20. data/lib/caracal/core/models/style_model.rb +1 -1
  21. data/lib/caracal/core/models/table_cell_model.rb +3 -3
  22. data/lib/caracal/core/models/table_model.rb +2 -2
  23. data/lib/caracal/core/page_numbers.rb +4 -3
  24. data/lib/caracal/core/page_settings.rb +2 -2
  25. data/lib/caracal/core/relationships.rb +1 -1
  26. data/lib/caracal/core/rules.rb +1 -1
  27. data/lib/caracal/core/styles.rb +1 -1
  28. data/lib/caracal/core/tables.rb +3 -2
  29. data/lib/caracal/core/text.rb +6 -4
  30. data/lib/caracal/document.rb +10 -10
  31. data/lib/caracal/renderers/document_renderer.rb +1 -1
  32. data/lib/caracal/renderers/numbering_renderer.rb +2 -2
  33. data/lib/caracal/utilities.rb +23 -0
  34. data/lib/caracal/version.rb +1 -1
  35. data/spec/lib/caracal/core/models/link_model_spec.rb +6 -0
  36. data/spec/lib/caracal/core/models/list_style_model_spec.rb +15 -29
  37. metadata +3 -2
data/lib/caracal.rb CHANGED
@@ -7,6 +7,7 @@ require 'tilt'
7
7
 
8
8
  # odds & ends
9
9
  require 'caracal/errors'
10
+ require 'caracal/utilities'
10
11
  require 'caracal/version'
11
12
 
12
13
  # document
@@ -26,7 +26,7 @@ module Caracal
26
26
  # This method sets the name of the output file. Defaults
27
27
  # to the name of the library.
28
28
  #
29
- def file_name(value = nil)
29
+ def file_name(value=nil)
30
30
  v = value.to_s.strip
31
31
  @name = (v == '') ? self.class::DEFAULT_FILE_NAME : v
32
32
  end
@@ -30,8 +30,8 @@ module Caracal
30
30
 
31
31
  #============== ATTRIBUTES ==========================
32
32
 
33
- def font(opts, &block)
34
- model = Caracal::Core::Models::FontModel.new(opts, &block)
33
+ def font(options={}, &block)
34
+ model = Caracal::Core::Models::FontModel.new(options, &block)
35
35
 
36
36
  if model.valid?
37
37
  register_font(model)
@@ -16,8 +16,9 @@ module Caracal
16
16
  # Public Methods
17
17
  #-------------------------------------------------------------
18
18
 
19
- def img(url = nil, options={}, &block)
20
- options.merge!({ url: url }) if url
19
+ def img(*args, &block)
20
+ options = Caracal::Utilities.extract_options!(args)
21
+ options.merge!({ url: args.first }) if args.first
21
22
 
22
23
  model = Caracal::Core::Models::ImageModel.new(options, &block)
23
24
  if model.valid?
@@ -19,25 +19,25 @@ module Caracal
19
19
 
20
20
  def self.default_list_styles
21
21
  [
22
- { type: :ordered, level: 0, format: 'decimal', value: '%1.', left: 720, line: 360 },
23
- { type: :ordered, level: 1, format: 'lowerLetter', value: '%2.', left: 1440, line: 1080 },
24
- { type: :ordered, level: 2, format: 'lowerRoman', value: '%3.', left: 2160, line: 1800, align: :right },
25
- { type: :ordered, level: 3, format: 'decimal', value: '%4.', left: 2880, line: 2520 },
26
- { type: :ordered, level: 4, format: 'lowerLetter', value: '%5.', left: 3600, line: 3240 },
27
- { type: :ordered, level: 5, format: 'lowerRoman', value: '%6.', left: 4320, line: 3960, align: :right },
28
- { type: :ordered, level: 6, format: 'decimal', value: '%7.', left: 5040, line: 4680 },
29
- { type: :ordered, level: 7, format: 'lowerLetter', value: '%8.', left: 5760, line: 5400 },
30
- { type: :ordered, level: 8, format: 'lowerRoman', value: '%9.', left: 6480, line: 6120, align: :right },
31
-
32
- { type: :unordered, level: 0, format: 'bullet', value: '●', left: 720, line: 360 },
33
- { type: :unordered, level: 1, format: 'bullet', value: '○', left: 1440, line: 1080 },
34
- { type: :unordered, level: 2, format: 'bullet', value: '■', left: 2160, line: 1800 },
35
- { type: :unordered, level: 3, format: 'bullet', value: '●', left: 2880, line: 2520 },
36
- { type: :unordered, level: 4, format: 'bullet', value: '○', left: 3600, line: 3240 },
37
- { type: :unordered, level: 5, format: 'bullet', value: '■', left: 4320, line: 3960 },
38
- { type: :unordered, level: 6, format: 'bullet', value: '●', left: 5040, line: 4680 },
39
- { type: :unordered, level: 7, format: 'bullet', value: '○', left: 5760, line: 5400 },
40
- { type: :unordered, level: 8, format: 'bullet', value: '■', left: 6480, line: 6120 }
22
+ { type: :ordered, level: 0, format: 'decimal', value: '%1.', left: 720, indent: 360 },
23
+ { type: :ordered, level: 1, format: 'lowerLetter', value: '%2.', left: 1440, indent: 1080 },
24
+ { type: :ordered, level: 2, format: 'lowerRoman', value: '%3.', left: 2160, indent: 1800, align: :right },
25
+ { type: :ordered, level: 3, format: 'decimal', value: '%4.', left: 2880, indent: 2520 },
26
+ { type: :ordered, level: 4, format: 'lowerLetter', value: '%5.', left: 3600, indent: 3240 },
27
+ { type: :ordered, level: 5, format: 'lowerRoman', value: '%6.', left: 4320, indent: 3960, align: :right },
28
+ { type: :ordered, level: 6, format: 'decimal', value: '%7.', left: 5040, indent: 4680 },
29
+ { type: :ordered, level: 7, format: 'lowerLetter', value: '%8.', left: 5760, indent: 5400 },
30
+ { type: :ordered, level: 8, format: 'lowerRoman', value: '%9.', left: 6480, indent: 6120, align: :right },
31
+
32
+ { type: :unordered, level: 0, format: 'bullet', value: '●', left: 720, indent: 360 },
33
+ { type: :unordered, level: 1, format: 'bullet', value: '○', left: 1440, indent: 1080 },
34
+ { type: :unordered, level: 2, format: 'bullet', value: '■', left: 2160, indent: 1800 },
35
+ { type: :unordered, level: 3, format: 'bullet', value: '●', left: 2880, indent: 2520 },
36
+ { type: :unordered, level: 4, format: 'bullet', value: '○', left: 3600, indent: 3240 },
37
+ { type: :unordered, level: 5, format: 'bullet', value: '■', left: 4320, indent: 3960 },
38
+ { type: :unordered, level: 6, format: 'bullet', value: '●', left: 5040, indent: 4680 },
39
+ { type: :unordered, level: 7, format: 'bullet', value: '○', left: 5760, indent: 5400 },
40
+ { type: :unordered, level: 8, format: 'bullet', value: '■', left: 6480, indent: 6120 }
41
41
  ]
42
42
  end
43
43
 
@@ -48,7 +48,7 @@ module Caracal
48
48
 
49
49
  #============== ATTRIBUTES ==========================
50
50
 
51
- def list_style(options = {}, &block)
51
+ def list_style(options={}, &block)
52
52
  model = Caracal::Core::Models::ListStyleModel.new(options, &block)
53
53
 
54
54
  if model.valid?
@@ -18,7 +18,7 @@ module Caracal
18
18
 
19
19
  #============== ATTRIBUTES ==========================
20
20
 
21
- def ol(options = {}, &block)
21
+ def ol(options={}, &block)
22
22
  options.merge!({ type: :ordered, level: 0 })
23
23
 
24
24
  model = Caracal::Core::Models::ListModel.new(options, &block)
@@ -30,7 +30,7 @@ module Caracal
30
30
  model
31
31
  end
32
32
 
33
- def ul(options = {}, &block)
33
+ def ul(options={}, &block)
34
34
  options.merge!({ type: :unordered, level: 0 })
35
35
 
36
36
  model = Caracal::Core::Models::ListModel.new(options, &block)
@@ -12,7 +12,7 @@ module Caracal
12
12
  #-------------------------------------------------------------
13
13
 
14
14
  # initialization
15
- def initialize(options = {}, &block)
15
+ def initialize(options={}, &block)
16
16
  options.keep_if { |k,v| option_keys.include? k }
17
17
  options.each do |(key, value)|
18
18
  send(key, value)
@@ -29,7 +29,7 @@ module Caracal
29
29
  attr_reader :border_type
30
30
 
31
31
  # initialization
32
- def initialize(options = {}, &block)
32
+ def initialize(options={}, &block)
33
33
  @border_color = DEFAULT_BORDER_COLOR
34
34
  @border_line = DEFAULT_BORDER_LINE
35
35
  @border_size = DEFAULT_BORDER_SIZE
@@ -34,7 +34,7 @@ module Caracal
34
34
 
35
35
 
36
36
  # initialization
37
- def initialize(options = {}, &block)
37
+ def initialize(options={}, &block)
38
38
  @image_width = DEFAULT_IMAGE_WIDTH
39
39
  @image_height = DEFAULT_IMAGE_HEIGHT
40
40
  @image_align = DEFAULT_IMAGE_ALIGN
@@ -14,6 +14,10 @@ module Caracal
14
14
  # Configuration
15
15
  #-------------------------------------------------------------
16
16
 
17
+ # constants
18
+ const_set(:DEFAULT_LINK_COLOR, '1155cc')
19
+ const_set(:DEFAULT_LINK_UNDERLINE, true)
20
+
17
21
  # readers (create aliases for superclass methods to conform
18
22
  # to expected naming convention.)
19
23
  attr_reader :link_href
@@ -25,6 +29,13 @@ module Caracal
25
29
  alias_method :link_italic, :text_italic
26
30
  alias_method :link_underline, :text_underline
27
31
 
32
+ # initialization
33
+ def initialize(options={}, &block)
34
+ @text_color = DEFAULT_LINK_COLOR
35
+ @text_underline = DEFAULT_LINK_UNDERLINE
36
+
37
+ super options, &block
38
+ end
28
39
 
29
40
 
30
41
  #-------------------------------------------------------------
@@ -56,7 +56,7 @@ module Caracal
56
56
  #=============== SUB-METHODS ===========================
57
57
 
58
58
  # .ol
59
- def ol(options = {}, &block)
59
+ def ol(options={}, &block)
60
60
  options.merge!({ type: :ordered, level: list_item_level + 1 })
61
61
 
62
62
  model = Caracal::Core::Models::ListModel.new(options, &block)
@@ -68,7 +68,7 @@ module Caracal
68
68
  end
69
69
 
70
70
  # .ul
71
- def ul(options = {}, &block)
71
+ def ul(options={}, &block)
72
72
  options.merge!({ type: :unordered, level: list_item_level + 1 })
73
73
 
74
74
  model = Caracal::Core::Models::ListModel.new(options, &block)
@@ -26,7 +26,7 @@ module Caracal
26
26
 
27
27
 
28
28
  # initialization
29
- def initialize(options = {}, &block)
29
+ def initialize(options={}, &block)
30
30
  @list_type = DEFAULT_LIST_TYPE
31
31
  @list_level = DEFAULT_LIST_LEVEL
32
32
 
@@ -91,8 +91,9 @@ module Caracal
91
91
  #=============== SUB-METHODS ===========================
92
92
 
93
93
  # .li
94
- def li(content = nil, options = {}, &block)
95
- options.merge!({ content: content }) if content
94
+ def li(*args, &block)
95
+ options = Caracal::Utilities.extract_options!(args)
96
+ options.merge!({ content: args.first }) if args.first
96
97
  options.merge!({ type: list_type })
97
98
  options.merge!({ level: list_level })
98
99
 
@@ -14,10 +14,10 @@ module Caracal
14
14
  # constants
15
15
  const_set(:TYPE_MAP, { ordered: 1, unordered: 2 })
16
16
  const_set(:DEFAULT_STYLE_LEFT, 720) # units in twips
17
- const_set(:DEFAULT_STYLE_LINE, 360) # units in twips
17
+ const_set(:DEFAULT_STYLE_INDENT, 360) # units in twips
18
18
  const_set(:DEFAULT_STYLE_ALIGN, :left)
19
19
  const_set(:DEFAULT_STYLE_START, 1)
20
- const_set(:DEFAULT_STYLE_RESTART, true)
20
+ const_set(:DEFAULT_STYLE_RESTART, 1)
21
21
 
22
22
  # accessors
23
23
  attr_reader :style_type
@@ -27,15 +27,15 @@ module Caracal
27
27
  attr_reader :style_start
28
28
  attr_reader :style_align
29
29
  attr_reader :style_left
30
- attr_reader :style_line
30
+ attr_reader :style_indent
31
31
  attr_reader :style_restart
32
32
 
33
33
 
34
34
  # initialization
35
- def initialize(options = {}, &block)
35
+ def initialize(options={}, &block)
36
36
  @style_align = DEFAULT_STYLE_ALIGN
37
37
  @style_left = DEFAULT_STYLE_LEFT
38
- @style_line = DEFAULT_STYLE_LINE
38
+ @style_indent = DEFAULT_STYLE_INDENT
39
39
  @style_start = DEFAULT_STYLE_START
40
40
  @style_restart = DEFAULT_STYLE_RESTART
41
41
 
@@ -62,22 +62,11 @@ module Caracal
62
62
  self.class.formatted_type(style_type)
63
63
  end
64
64
 
65
- def formatted_restart
66
- v = style_restart ? '1' : '0'
67
- end
68
-
69
65
 
70
66
  #=============== SETTERS ==============================
71
67
 
72
- # booleans
73
- [:restart].each do |m|
74
- define_method "#{ m }" do |value|
75
- instance_variable_set("@style_#{ m }", !!value)
76
- end
77
- end
78
-
79
68
  # integers
80
- [:level, :left, :line, :start].each do |m|
69
+ [:level, :left, :indent, :start, :restart].each do |m|
81
70
  define_method "#{ m }" do |value|
82
71
  instance_variable_set("@style_#{ m }", value.to_i)
83
72
  end
@@ -119,7 +108,7 @@ module Caracal
119
108
  private
120
109
 
121
110
  def option_keys
122
- [:type, :level, :format, :value, :align, :left, :line, :start]
111
+ [:type, :level, :format, :value, :align, :left, :indent, :start]
123
112
  end
124
113
 
125
114
  end
@@ -28,7 +28,7 @@ module Caracal
28
28
 
29
29
 
30
30
  # initialization
31
- def initialize(options = {}, &block)
31
+ def initialize(options={}, &block)
32
32
  @margin_top = DEFAULT_MARGIN_TOP
33
33
  @margin_bottom = DEFAULT_MARGIN_BOTTOM
34
34
  @margin_left = DEFAULT_MARGIN_LEFT
@@ -23,7 +23,7 @@ module Caracal
23
23
  attr_reader :page_number_show
24
24
 
25
25
  # initialization
26
- def initialize(options = {}, &block)
26
+ def initialize(options={}, &block)
27
27
  @page_number_show = DEFAULT_PAGE_NUMBER_SHOW
28
28
  @page_number_align = DEFAULT_PAGE_NUMBER_ALIGN
29
29
 
@@ -23,7 +23,7 @@ module Caracal
23
23
  attr_reader :page_height
24
24
 
25
25
  # initialization
26
- def initialize(options = {}, &block)
26
+ def initialize(options={}, &block)
27
27
  @page_width = DEFAULT_PAGE_WIDTH
28
28
  @page_height = DEFAULT_PAGE_HEIGHT
29
29
 
@@ -27,7 +27,7 @@ module Caracal
27
27
  attr_reader :paragraph_underline
28
28
 
29
29
  # initialization
30
- def initialize(options = {}, &block)
30
+ def initialize(options={}, &block)
31
31
  if content = options.delete(:content)
32
32
  text content, options.dup
33
33
  end
@@ -93,9 +93,10 @@ module Caracal
93
93
  #=============== SUB-METHODS ===========================
94
94
 
95
95
  # .link
96
- def link(content = nil, href = nil, options = {}, &block)
97
- options.merge!({ content: content }) if content
98
- options.merge!({ href: href }) if href
96
+ def link(*args, &block)
97
+ options = Caracal::Utilities.extract_options!(args)
98
+ options.merge!({ content: args[0] }) if args[0]
99
+ options.merge!({ href: args[1] }) if args[1]
99
100
 
100
101
  model = Caracal::Core::Models::LinkModel.new(options, &block)
101
102
  if model.valid?
@@ -106,8 +107,9 @@ module Caracal
106
107
  end
107
108
 
108
109
  # .text
109
- def text(content = nil, options = {}, &block)
110
- options.merge!({ content: content }) if content
110
+ def text(*args, &block)
111
+ options = Caracal::Utilities.extract_options!(args)
112
+ options.merge!({ content: args.first }) if args.first
111
113
 
112
114
  model = Caracal::Core::Models::TextModel.new(options, &block)
113
115
  if model.valid?
@@ -46,7 +46,7 @@ module Caracal
46
46
 
47
47
 
48
48
  # initialization
49
- def initialize(options = {}, &block)
49
+ def initialize(options={}, &block)
50
50
  @style_default = false
51
51
  @style_base = DEFAULT_STYLE_BASE
52
52
  @style_next = DEFAULT_STYLE_NEXT
@@ -25,7 +25,7 @@ module Caracal
25
25
  attr_reader :cell_margins
26
26
 
27
27
  # initialization
28
- def initialize(options = {}, &block)
28
+ def initialize(options={}, &block)
29
29
  @cell_background = DEFAULT_CELL_BACKGROUND
30
30
  @cell_margins = DEFAULT_CELL_MARGINS
31
31
 
@@ -57,9 +57,9 @@ module Caracal
57
57
  #
58
58
  # In all cases, invalid options will simply be ignored.
59
59
  #
60
- def apply_styles(opts = {})
60
+ def apply_styles(opts={})
61
61
  # make dup of options so we don't
62
- # harm any siblings
62
+ # harm args sent to sibling cells
63
63
  options = opts.dup
64
64
 
65
65
  # first, try apply to self
@@ -38,7 +38,7 @@ module Caracal
38
38
  attr_reader :table_border_vertical # returns border model
39
39
 
40
40
  # initialization
41
- def initialize(options = {}, &block)
41
+ def initialize(options={}, &block)
42
42
  @table_align = DEFAULT_TABLE_ALIGN
43
43
  @table_border_color = DEFAULT_TABLE_BORDER_COLOR
44
44
  @table_border_line = DEFAULT_TABLE_BORDER_LINE
@@ -94,7 +94,7 @@ module Caracal
94
94
  # t.cell_style t.rows[0], background: '3366cc', color: 'ffffff', bold: true
95
95
  # end
96
96
  #
97
- def cell_style(models, options = {})
97
+ def cell_style(models, options={})
98
98
  [models].flatten.each do |m|
99
99
  m.apply_styles(options)
100
100
  end
@@ -31,10 +31,11 @@ module Caracal
31
31
  # This method controls whether and how page numbers are displayed
32
32
  # on the document.
33
33
  #
34
- def page_numbers(show = nil, options = {}, &block)
35
- options.merge!({ show: !!show })
36
- model = Caracal::Core::Models::PageNumberModel.new(options, &block)
34
+ def page_numbers(*args, &block)
35
+ options = Caracal::Utilities.extract_options!(args)
36
+ options.merge!({ show: !!args.first }) unless args.first.nil? # careful: falsey value
37
37
 
38
+ model = Caracal::Core::Models::PageNumberModel.new(options, &block)
38
39
  if model.valid?
39
40
  @page_number_show = model.page_number_show
40
41
  @page_number_align = model.page_number_align
@@ -33,7 +33,7 @@ module Caracal
33
33
  # This method controls the physical margins of the printed page. Defaults
34
34
  # to 1in on each side.
35
35
  #
36
- def page_margins(options = {}, &block)
36
+ def page_margins(options={}, &block)
37
37
  model = Caracal::Core::Models::MarginModel.new(options, &block)
38
38
 
39
39
  if model.valid?
@@ -53,7 +53,7 @@ module Caracal
53
53
  # This method controls the physical width and height of the printed page. Defaults
54
54
  # to US standard A4 portrait size.
55
55
  #
56
- def page_size(options = {}, &block)
56
+ def page_size(options={}, &block)
57
57
  model = Caracal::Core::Models::PageSizeModel.new(options, &block)
58
58
 
59
59
  if model.valid?
@@ -40,7 +40,7 @@ module Caracal
40
40
 
41
41
  #============== ATTRIBUTES ==========================
42
42
 
43
- def relationship(options = {}, &block)
43
+ def relationship(options={}, &block)
44
44
  id = relationship_counter.to_i + 1
45
45
  options.merge!({ id: id })
46
46
 
@@ -16,7 +16,7 @@ module Caracal
16
16
  # Public Methods
17
17
  #-------------------------------------------------------------
18
18
 
19
- def hr(options = {}, &block)
19
+ def hr(options={}, &block)
20
20
  model = Caracal::Core::Models::RuleModel.new(options, &block)
21
21
 
22
22
  if model.valid?