caracal 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +20 -0
- data/README.md +750 -675
- data/Rakefile +5 -0
- data/caracal.gemspec +2 -1
- data/lib/caracal/core/images.rb +2 -3
- data/lib/caracal/core/list_styles.rb +2 -1
- data/lib/caracal/core/lists.rb +2 -2
- data/lib/caracal/core/models/base_model.rb +1 -1
- data/lib/caracal/core/models/border_model.rb +1 -1
- data/lib/caracal/core/models/image_model.rb +1 -1
- data/lib/caracal/core/models/list_item_model.rb +2 -2
- data/lib/caracal/core/models/list_model.rb +3 -3
- data/lib/caracal/core/models/list_style_model.rb +1 -1
- data/lib/caracal/core/models/margin_model.rb +1 -1
- data/lib/caracal/core/models/page_number_model.rb +1 -1
- data/lib/caracal/core/models/page_size_model.rb +1 -1
- data/lib/caracal/core/models/paragraph_model.rb +6 -6
- data/lib/caracal/core/models/style_model.rb +1 -1
- data/lib/caracal/core/models/table_cell_model.rb +7 -3
- data/lib/caracal/core/models/table_model.rb +3 -3
- data/lib/caracal/core/page_numbers.rb +1 -1
- data/lib/caracal/core/page_settings.rb +2 -2
- data/lib/caracal/core/relationships.rb +1 -1
- data/lib/caracal/core/rules.rb +1 -1
- data/lib/caracal/core/styles.rb +1 -1
- data/lib/caracal/core/tables.rb +1 -1
- data/lib/caracal/core/text.rb +3 -4
- data/lib/caracal/renderers/document_renderer.rb +30 -19
- data/lib/caracal/version.rb +1 -1
- metadata +19 -4
data/Rakefile
CHANGED
data/caracal.gemspec
CHANGED
@@ -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.
|
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
|
data/lib/caracal/core/images.rb
CHANGED
@@ -16,9 +16,8 @@ module Caracal
|
|
16
16
|
# Public Methods
|
17
17
|
#-------------------------------------------------------------
|
18
18
|
|
19
|
-
def img(
|
20
|
-
|
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(
|
51
|
+
def list_style(options = {}, &block)
|
51
52
|
model = Caracal::Core::Models::ListStyleModel.new(options, &block)
|
52
53
|
|
53
54
|
if model.valid?
|
data/lib/caracal/core/lists.rb
CHANGED
@@ -18,7 +18,7 @@ module Caracal
|
|
18
18
|
|
19
19
|
#============== ATTRIBUTES ==========================
|
20
20
|
|
21
|
-
def ol(
|
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(
|
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(
|
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)
|
@@ -56,7 +56,7 @@ module Caracal
|
|
56
56
|
#=============== SUB-METHODS ===========================
|
57
57
|
|
58
58
|
# .ol
|
59
|
-
def ol(
|
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(
|
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(
|
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(
|
95
|
-
options.merge!({ content:
|
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
|
|
@@ -27,7 +27,7 @@ module Caracal
|
|
27
27
|
attr_reader :paragraph_underline
|
28
28
|
|
29
29
|
# initialization
|
30
|
-
def initialize(
|
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(
|
97
|
-
options.merge!({ content:
|
98
|
-
options.merge!({ href:
|
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(
|
110
|
-
options.merge!({ content:
|
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?
|
@@ -25,7 +25,7 @@ module Caracal
|
|
25
25
|
attr_reader :cell_margins
|
26
26
|
|
27
27
|
# initialization
|
28
|
-
def initialize(
|
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(
|
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
|
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(
|
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,
|
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
|
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,
|
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(
|
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(
|
56
|
+
def page_size(options = {}, &block)
|
57
57
|
model = Caracal::Core::Models::PageSizeModel.new(options, &block)
|
58
58
|
|
59
59
|
if model.valid?
|
data/lib/caracal/core/rules.rb
CHANGED
data/lib/caracal/core/styles.rb
CHANGED
data/lib/caracal/core/tables.rb
CHANGED
@@ -16,7 +16,7 @@ module Caracal
|
|
16
16
|
# Public Methods
|
17
17
|
#-------------------------------------------------------------
|
18
18
|
|
19
|
-
def table(data,
|
19
|
+
def table(data, options = {}, &block)
|
20
20
|
options.merge!({ data: data })
|
21
21
|
|
22
22
|
model = Caracal::Core::Models::TableModel.new(options, &block)
|
data/lib/caracal/core/text.rb
CHANGED
@@ -18,9 +18,8 @@ module Caracal
|
|
18
18
|
|
19
19
|
#============== PARAGRAPHS ==========================
|
20
20
|
|
21
|
-
def p(
|
22
|
-
|
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
|
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
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
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
|
-
|
256
|
-
|
257
|
-
|
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' =>
|
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
|
|