caracal 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
2
7
 
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency 'rubyzip', '~> 1.1'
23
23
  spec.add_dependency 'tilt', '~> 1.4'
24
24
 
25
- spec.add_development_dependency 'bundler', '~> 1.6'
25
+ spec.add_development_dependency 'bundler', '~> 1.3'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
26
27
  spec.add_development_dependency 'rspec', '~> 3.0'
27
28
  end
@@ -16,9 +16,8 @@ module Caracal
16
16
  # Public Methods
17
17
  #-------------------------------------------------------------
18
18
 
19
- def img(*text, **options, &block)
20
- text.flatten!
21
- options.merge!( { url: text[0] }) unless text[0].nil?
19
+ def img(url = nil, options={}, &block)
20
+ options.merge!({ url: url }) if url
22
21
 
23
22
  model = Caracal::Core::Models::ImageModel.new(options, &block)
24
23
  if model.valid?
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'caracal/core/models/list_style_model'
2
3
  require 'caracal/errors'
3
4
 
@@ -47,7 +48,7 @@ module Caracal
47
48
 
48
49
  #============== ATTRIBUTES ==========================
49
50
 
50
- def list_style(**options, &block)
51
+ def list_style(options = {}, &block)
51
52
  model = Caracal::Core::Models::ListStyleModel.new(options, &block)
52
53
 
53
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
@@ -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,8 @@ module Caracal
91
91
  #=============== SUB-METHODS ===========================
92
92
 
93
93
  # .li
94
- def li(*args, **options, &block)
95
- options.merge!({ content: args[0] }) unless args[0].nil?
94
+ def li(content = nil, options = {}, &block)
95
+ options.merge!({ content: content }) if content
96
96
  options.merge!({ type: list_type })
97
97
  options.merge!({ level: list_level })
98
98
 
@@ -32,7 +32,7 @@ module Caracal
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
38
  @style_line = DEFAULT_STYLE_LINE
@@ -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,9 @@ module Caracal
93
93
  #=============== SUB-METHODS ===========================
94
94
 
95
95
  # .link
96
- def link(*args, **options, &block)
97
- options.merge!({ content: args[0] }) unless args[0].nil?
98
- options.merge!({ href: args[1] }) unless args[1].nil?
96
+ def link(content = nil, href = nil, options = {}, &block)
97
+ options.merge!({ content: content }) if content
98
+ options.merge!({ href: href }) if href
99
99
 
100
100
  model = Caracal::Core::Models::LinkModel.new(options, &block)
101
101
  if model.valid?
@@ -106,8 +106,8 @@ module Caracal
106
106
  end
107
107
 
108
108
  # .text
109
- def text(*args, **options, &block)
110
- options.merge!({ content: args[0] }) unless args[0].nil?
109
+ def text(content = nil, options = {}, &block)
110
+ options.merge!({ content: content }) if content
111
111
 
112
112
  model = Caracal::Core::Models::TextModel.new(options, &block)
113
113
  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,7 +57,11 @@ module Caracal
57
57
  #
58
58
  # In all cases, invalid options will simply be ignored.
59
59
  #
60
- def apply_styles(**options)
60
+ def apply_styles(opts = {})
61
+ # make dup of options so we don't
62
+ # harm any siblings
63
+ options = opts.dup
64
+
61
65
  # first, try apply to self
62
66
  options.each do |(k,v)|
63
67
  send(k, v) if respond_to?(k)
@@ -119,7 +123,7 @@ module Caracal
119
123
 
120
124
  # models
121
125
  [:margins].each do |m|
122
- define_method "#{ m }" do |**options, &block|
126
+ define_method "#{ m }" do |options = {}, &block|
123
127
  instance_variable_set("@cell_#{ m }", Caracal::Core::Models::MarginModel.new(options, &block))
124
128
  end
125
129
  end
@@ -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
@@ -129,7 +129,7 @@ module Caracal
129
129
 
130
130
  # models
131
131
  [:top, :bottom, :left, :right, :horizontal, :vertical].each do |m|
132
- define_method "border_#{ m }" do |**options, &block|
132
+ define_method "border_#{ m }" do |options = {}, &block|
133
133
  options.merge!({ type: m })
134
134
  instance_variable_set("@table_border_#{ m }", Caracal::Core::Models::BorderModel.new(options, &block))
135
135
  end
@@ -31,7 +31,7 @@ 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)
34
+ def page_numbers(show = nil, options = {}, &block)
35
35
  options.merge!({ show: !!show })
36
36
  model = Caracal::Core::Models::PageNumberModel.new(options, &block)
37
37
 
@@ -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?
@@ -37,7 +37,7 @@ module Caracal
37
37
 
38
38
  #============== ATTRIBUTES ==========================
39
39
 
40
- def style(**options, &block)
40
+ def style(options = {}, &block)
41
41
  model = Caracal::Core::Models::StyleModel.new(options, &block)
42
42
 
43
43
  if model.valid?
@@ -16,7 +16,7 @@ module Caracal
16
16
  # Public Methods
17
17
  #-------------------------------------------------------------
18
18
 
19
- def table(data, **options, &block)
19
+ def table(data, options = {}, &block)
20
20
  options.merge!({ data: data })
21
21
 
22
22
  model = Caracal::Core::Models::TableModel.new(options, &block)
@@ -18,9 +18,8 @@ module Caracal
18
18
 
19
19
  #============== PARAGRAPHS ==========================
20
20
 
21
- def p(*text, **options, &block)
22
- text.flatten!
23
- options.merge!( { content: text[0] }) unless text[0].nil?
21
+ def p(text = nil, options = {}, &block)
22
+ options.merge!({ content: text }) if text
24
23
 
25
24
  model = Caracal::Core::Models::ParagraphModel.new(options, &block)
26
25
  if model.valid?
@@ -38,7 +37,7 @@ module Caracal
38
37
  # model with an explicitly set style class.
39
38
  #
40
39
  [:h1, :h2, :h3, :h4, :h5, :h6].each do |cmd|
41
- define_method "#{ cmd }" do |*text, **options, &block|
40
+ define_method "#{ cmd }" do |text = nil, options = {}, &block|
42
41
  options.merge!({ style: style_id_for_header(cmd) })
43
42
  p(text, options, &block)
44
43
  end
@@ -66,20 +66,24 @@ module Caracal
66
66
  # This method renders a standard node of run properties based on the
67
67
  # model's attributes.
68
68
  #
69
- def render_run_attributes(xml, model)
69
+ def render_run_attributes(xml, model, paragraph_level=false)
70
70
  if model.respond_to? :run_attributes
71
71
  attrs = model.run_attributes.delete_if { |k, v| v.nil? }
72
-
73
- xml.send 'w:rPr' do
74
- unless attrs.empty?
75
- xml.send 'w:rStyle', { 'w:val' => attrs[:style] } unless attrs[:style].nil?
76
- xml.send 'w:color', { 'w:val' => attrs[:color] } unless attrs[:color].nil?
77
- xml.send 'w:sz', { 'w:val' => attrs[:size] } unless attrs[:size].nil?
78
- xml.send 'w:b', { 'w:val' => (attrs[:bold] ? '1' : '0') } unless attrs[:bold].nil?
79
- xml.send 'w:i', { 'w:val' => (attrs[:italic] ? '1' : '0') } unless attrs[:italic].nil?
80
- xml.send 'w:u', { 'w:val' => (attrs[:underline] ? 'single' : 'none') } unless attrs[:underline].nil?
72
+
73
+ if paragraph_level && attrs.empty?
74
+ # skip
75
+ else
76
+ xml.send 'w:rPr' do
77
+ unless attrs.empty?
78
+ xml.send 'w:rStyle', { 'w:val' => attrs[:style] } unless attrs[:style].nil?
79
+ xml.send 'w:color', { 'w:val' => attrs[:color] } unless attrs[:color].nil?
80
+ xml.send 'w:sz', { 'w:val' => attrs[:size] } unless attrs[:size].nil?
81
+ xml.send 'w:b', { 'w:val' => (attrs[:bold] ? '1' : '0') } unless attrs[:bold].nil?
82
+ xml.send 'w:i', { 'w:val' => (attrs[:italic] ? '1' : '0') } unless attrs[:italic].nil?
83
+ xml.send 'w:u', { 'w:val' => (attrs[:underline] ? 'single' : 'none') } unless attrs[:underline].nil?
84
+ end
85
+ xml.send 'w:rtl', { 'w:val' => '0' }
81
86
  end
82
- xml.send 'w:rtl', { 'w:val' => '0' }
83
87
  end
84
88
  end
85
89
  end
@@ -160,7 +164,7 @@ module Caracal
160
164
 
161
165
  xml.send 'w:hyperlink', { 'r:id' => rel.formatted_id } do
162
166
  xml.send 'w:r', run_options do
163
- render_run_attributes(xml, model)
167
+ render_run_attributes(xml, model, false)
164
168
  xml.send 'w:t', { 'xml:space' => 'preserve' }, model.link_content
165
169
  end
166
170
  end
@@ -216,7 +220,7 @@ module Caracal
216
220
  xml.send 'w:pStyle', { 'w:val' => model.paragraph_style } unless model.paragraph_style.nil?
217
221
  xml.send 'w:contextualSpacing', { 'w:val' => '0' }
218
222
  xml.send 'w:jc', { 'w:val' => model.paragraph_align } unless model.paragraph_align.nil?
219
- render_run_attributes(xml, model)
223
+ render_run_attributes(xml, model, true)
220
224
  end
221
225
  model.runs.each do |run|
222
226
  method = render_method_for_model(run)
@@ -239,12 +243,16 @@ module Caracal
239
243
 
240
244
  def render_text(xml, model)
241
245
  xml.send 'w:r', run_options do
242
- render_run_attributes(xml, model)
246
+ render_run_attributes(xml, model, false)
243
247
  xml.send 'w:t', { 'xml:space' => 'preserve' }, model.text_content
244
248
  end
245
249
  end
246
250
 
247
251
  def render_table(xml, model)
252
+ borders = %w(top left bottom right horizontal vertical).select do |m|
253
+ model.send("table_border_#{ m }_size") > 0
254
+ end
255
+
248
256
  xml.send 'w:tbl' do
249
257
  xml.send 'w:tblPr' do
250
258
  xml.send 'w:tblStyle', { 'w:val' => 'DefaultTable' }
@@ -252,14 +260,13 @@ module Caracal
252
260
  xml.send 'w:tblW', { 'w:w' => model.table_width.to_f, 'w:type' => 'dxa' }
253
261
  xml.send 'w:tblInd', { 'w:w' => '0.0', 'w:type' => 'dxa' }
254
262
  xml.send 'w:jc', { 'w:val' => model.table_align }
255
- xml.send 'w:tblBorders' do
256
- %w(top left bottom right horizontal vertical).each do |m|
257
- size = model.send("table_border_#{ m }_size")
258
- if size > 0
263
+ unless borders.empty?
264
+ xml.send 'w:tblBorders' do
265
+ borders.each do |m|
259
266
  options = {
260
267
  'w:color' => model.send("table_border_#{ m }_color"),
261
268
  'w:val' => model.send("table_border_#{ m }_line"),
262
- 'w:sz' => size,
269
+ 'w:sz' => model.send("table_border_#{ m }_size"),
263
270
  'w:space' => model.send("table_border_#{ m }_spacing")
264
271
  }
265
272
  xml.send "w:#{ Caracal::Core::Models::BorderModel.formatted_type(m) }", options
@@ -302,6 +309,10 @@ module Caracal
302
309
  end
303
310
  end
304
311
  end
312
+
313
+ # don't know why this is needed, but it prevents a
314
+ # rendering error.
315
+ render_linebreak(xml, Caracal::Core::Models::LineBreakModel.new)
305
316
  end
306
317
 
307
318