caracal 1.0.1 → 1.2.0
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.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/CHANGELOG.md +95 -5
- data/README.md +171 -35
- data/caracal.gemspec +4 -4
- data/lib/caracal/core/custom_properties.rb +49 -0
- data/lib/caracal/core/iframes.rb +42 -0
- data/lib/caracal/core/ignorables.rb +47 -0
- data/lib/caracal/core/models/custom_property_model.rb +60 -0
- data/lib/caracal/core/models/iframe_model.rb +148 -0
- data/lib/caracal/core/models/namespace_model.rb +65 -0
- data/lib/caracal/core/models/page_break_model.rb +50 -4
- data/lib/caracal/core/models/page_number_model.rb +50 -24
- data/lib/caracal/core/models/page_size_model.rb +34 -25
- data/lib/caracal/core/models/paragraph_model.rb +11 -4
- data/lib/caracal/core/models/style_model.rb +44 -15
- data/lib/caracal/core/models/table_cell_model.rb +61 -50
- data/lib/caracal/core/models/table_model.rb +1 -1
- data/lib/caracal/core/models/text_model.rb +42 -20
- data/lib/caracal/core/namespaces.rb +89 -0
- data/lib/caracal/core/page_numbers.rb +22 -16
- data/lib/caracal/core/page_settings.rb +18 -16
- data/lib/caracal/document.rb +75 -58
- data/lib/caracal/renderers/content_types_renderer.rb +18 -17
- data/lib/caracal/renderers/core_renderer.rb +1 -1
- data/lib/caracal/renderers/custom_renderer.rb +64 -0
- data/lib/caracal/renderers/document_renderer.rb +205 -118
- data/lib/caracal/renderers/fonts_renderer.rb +2 -2
- data/lib/caracal/renderers/footer_renderer.rb +41 -20
- data/lib/caracal/renderers/numbering_renderer.rb +27 -26
- data/lib/caracal/renderers/package_relationships_renderer.rb +14 -13
- data/lib/caracal/renderers/settings_renderer.rb +5 -5
- data/lib/caracal/renderers/styles_renderer.rb +88 -73
- data/lib/caracal/renderers/xml_renderer.rb +20 -20
- data/lib/caracal/version.rb +1 -1
- data/lib/caracal.rb +11 -2
- data/spec/lib/caracal/core/iframes_spec.rb +29 -0
- data/spec/lib/caracal/core/ignorables_spec.rb +79 -0
- data/spec/lib/caracal/core/models/iframe_model_spec.rb +83 -0
- data/spec/lib/caracal/core/models/image_model_spec.rb +60 -62
- data/spec/lib/caracal/core/models/link_model_spec.rb +12 -11
- data/spec/lib/caracal/core/models/namespace_model_spec.rb +107 -0
- data/spec/lib/caracal/core/models/page_number_model_spec.rb +66 -31
- data/spec/lib/caracal/core/models/page_size_model_spec.rb +42 -32
- data/spec/lib/caracal/core/models/style_model_spec.rb +32 -1
- data/spec/lib/caracal/core/models/table_cell_model_spec.rb +10 -1
- data/spec/lib/caracal/core/models/text_model_spec.rb +18 -10
- data/spec/lib/caracal/core/namespaces_spec.rb +116 -0
- data/spec/lib/caracal/core/page_numbers_spec.rb +32 -23
- data/spec/lib/caracal/core/page_settings_spec.rb +33 -25
- data/spec/lib/caracal/core/relationships_spec.rb +47 -47
- data/spec/support/_fixtures/snippet.docx +0 -0
- metadata +28 -8
|
@@ -8,60 +8,61 @@ require 'caracal/renderers/xml_renderer'
|
|
|
8
8
|
module Caracal
|
|
9
9
|
module Renderers
|
|
10
10
|
class NumberingRenderer < XmlRenderer
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
#-------------------------------------------------------------
|
|
13
13
|
# Public Methods
|
|
14
14
|
#-------------------------------------------------------------
|
|
15
|
-
|
|
16
|
-
# This method produces the xml required for the `word/numbering.xml`
|
|
15
|
+
|
|
16
|
+
# This method produces the xml required for the `word/numbering.xml`
|
|
17
17
|
# sub-document.
|
|
18
18
|
#
|
|
19
19
|
def to_xml
|
|
20
20
|
builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml|
|
|
21
|
-
xml
|
|
21
|
+
xml['w'].numbering root_options do
|
|
22
22
|
|
|
23
23
|
# add abstract definitions
|
|
24
24
|
document.toplevel_lists.each_with_index do |model, i|
|
|
25
|
-
xml
|
|
25
|
+
xml['w'].abstractNum({ 'w:abstractNumId' => i + 1 }) do
|
|
26
|
+
xml['w'].multiLevelType({ 'w:val' => 'hybridMultilevel' })
|
|
26
27
|
model.level_map.each do |(level, type)|
|
|
27
28
|
if s = document.find_list_style(type, level)
|
|
28
|
-
xml
|
|
29
|
-
xml
|
|
30
|
-
xml
|
|
31
|
-
xml
|
|
32
|
-
xml
|
|
33
|
-
xml
|
|
34
|
-
xml
|
|
35
|
-
xml
|
|
29
|
+
xml['w'].lvl({ 'w:ilvl' => s.style_level }) do
|
|
30
|
+
xml['w'].start({ 'w:val' => s.style_start })
|
|
31
|
+
xml['w'].numFmt({ 'w:val' => s.style_format })
|
|
32
|
+
xml['w'].lvlRestart({ 'w:val' => s.style_restart })
|
|
33
|
+
xml['w'].lvlText({ 'w:val' => s.style_value })
|
|
34
|
+
xml['w'].lvlJc({ 'w:val' => s.style_align })
|
|
35
|
+
xml['w'].pPr do
|
|
36
|
+
xml['w'].ind({ 'w:left' => s.style_left, 'w:firstLine' => s.style_indent })
|
|
36
37
|
end
|
|
37
|
-
xml
|
|
38
|
-
xml
|
|
38
|
+
xml['w'].rPr do
|
|
39
|
+
xml['w'].u({ 'w:val' => 'none' })
|
|
39
40
|
end
|
|
40
41
|
end
|
|
41
42
|
end
|
|
42
43
|
end
|
|
43
44
|
end
|
|
44
45
|
end
|
|
45
|
-
|
|
46
|
+
|
|
46
47
|
# bind individual tables to abstract definitions
|
|
47
48
|
document.toplevel_lists.each_with_index do |model, i|
|
|
48
|
-
xml
|
|
49
|
-
xml
|
|
49
|
+
xml['w'].num({ 'w:numId' => i + 1 }) do
|
|
50
|
+
xml['w'].abstractNumId({ 'w:val' => i + 1 })
|
|
50
51
|
end
|
|
51
52
|
end
|
|
52
53
|
end
|
|
53
|
-
|
|
54
|
+
|
|
54
55
|
end
|
|
55
56
|
builder.to_xml(save_options)
|
|
56
57
|
end
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
60
61
|
#-------------------------------------------------------------
|
|
61
62
|
# Private Methods
|
|
62
|
-
#-------------------------------------------------------------
|
|
63
|
+
#-------------------------------------------------------------
|
|
63
64
|
private
|
|
64
|
-
|
|
65
|
+
|
|
65
66
|
def root_options
|
|
66
67
|
{
|
|
67
68
|
'xmlns:mc' => 'http://schemas.openxmlformats.org/markup-compatibility/2006',
|
|
@@ -81,7 +82,7 @@ module Caracal
|
|
|
81
82
|
'xmlns:dgm' => 'http://schemas.openxmlformats.org/drawingml/2006/diagram'
|
|
82
83
|
}
|
|
83
84
|
end
|
|
84
|
-
|
|
85
|
+
|
|
85
86
|
end
|
|
86
87
|
end
|
|
87
|
-
end
|
|
88
|
+
end
|
|
@@ -6,12 +6,12 @@ require 'caracal/renderers/xml_renderer'
|
|
|
6
6
|
module Caracal
|
|
7
7
|
module Renderers
|
|
8
8
|
class PackageRelationshipsRenderer < XmlRenderer
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
#-------------------------------------------------------------
|
|
11
11
|
# Public Methods
|
|
12
12
|
#-------------------------------------------------------------
|
|
13
|
-
|
|
14
|
-
# This method produces the xml required for the `word/settings.xml`
|
|
13
|
+
|
|
14
|
+
# This method produces the xml required for the `word/settings.xml`
|
|
15
15
|
# sub-document.
|
|
16
16
|
#
|
|
17
17
|
def to_xml
|
|
@@ -24,27 +24,28 @@ module Caracal
|
|
|
24
24
|
end
|
|
25
25
|
builder.to_xml(save_options)
|
|
26
26
|
end
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
|
|
28
|
+
|
|
29
29
|
#-------------------------------------------------------------
|
|
30
30
|
# Private Methods
|
|
31
|
-
#-------------------------------------------------------------
|
|
31
|
+
#-------------------------------------------------------------
|
|
32
32
|
private
|
|
33
|
-
|
|
33
|
+
|
|
34
34
|
def relationship_data
|
|
35
35
|
[
|
|
36
|
-
['docProps/app.xml',
|
|
37
|
-
['docProps/core.xml',
|
|
38
|
-
['
|
|
36
|
+
['docProps/app.xml', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties'],
|
|
37
|
+
['docProps/core.xml', 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties'],
|
|
38
|
+
['docProps/custom.xml', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties'],
|
|
39
|
+
['word/document.xml', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument']
|
|
39
40
|
]
|
|
40
41
|
end
|
|
41
|
-
|
|
42
|
+
|
|
42
43
|
def root_options
|
|
43
44
|
{
|
|
44
45
|
'xmlns' => 'http://schemas.openxmlformats.org/package/2006/relationships'
|
|
45
46
|
}
|
|
46
47
|
end
|
|
47
|
-
|
|
48
|
+
|
|
48
49
|
end
|
|
49
50
|
end
|
|
50
|
-
end
|
|
51
|
+
end
|
|
@@ -16,11 +16,11 @@ module Caracal
|
|
|
16
16
|
#
|
|
17
17
|
def to_xml
|
|
18
18
|
builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml|
|
|
19
|
-
xml
|
|
20
|
-
xml
|
|
21
|
-
xml
|
|
22
|
-
xml
|
|
23
|
-
xml
|
|
19
|
+
xml['w'].settings root_options do
|
|
20
|
+
xml['w'].displayBackgroundShape({ 'w:val' => '1' })
|
|
21
|
+
xml['w'].defaultTabStop({ 'w:val' => '720' })
|
|
22
|
+
xml['w'].compat do
|
|
23
|
+
xml['w'].compatSetting({ 'w:val' => '14', 'w:name' => 'compatibilityMode', 'w:uri' => 'http://schemas.microsoft.com/office/word' })
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
26
|
end
|
|
@@ -8,103 +8,104 @@ module Caracal
|
|
|
8
8
|
module Renderers
|
|
9
9
|
class StylesRenderer < XmlRenderer
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
#----------------------------------------------------
|
|
12
12
|
# Public Methods
|
|
13
|
-
|
|
13
|
+
#----------------------------------------------------
|
|
14
14
|
|
|
15
15
|
# This method produces the xml required for the `word/styles.xml`
|
|
16
16
|
# sub-document.
|
|
17
17
|
#
|
|
18
18
|
def to_xml
|
|
19
19
|
builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml|
|
|
20
|
-
xml
|
|
20
|
+
xml['w'].styles root_options do
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
#========== DEFAULT STYLES ====================
|
|
23
23
|
|
|
24
24
|
unless s = document.default_style
|
|
25
25
|
raise Caracal::Errors::NoDefaultStyleError 'Document must declare a default paragraph style.'
|
|
26
26
|
end
|
|
27
|
-
xml
|
|
28
|
-
xml
|
|
29
|
-
xml
|
|
30
|
-
xml
|
|
31
|
-
xml
|
|
32
|
-
xml
|
|
33
|
-
xml
|
|
34
|
-
xml
|
|
35
|
-
xml
|
|
36
|
-
xml
|
|
37
|
-
xml
|
|
38
|
-
xml
|
|
39
|
-
xml
|
|
27
|
+
xml['w'].docDefaults do
|
|
28
|
+
xml['w'].rPrDefault do
|
|
29
|
+
xml['w'].rPr do
|
|
30
|
+
xml['w'].rFonts font_options(s)
|
|
31
|
+
xml['w'].b({ 'w:val' => (s.style_bold ? '1' : '0') })
|
|
32
|
+
xml['w'].i({ 'w:val' => (s.style_italic ? '1' : '0') })
|
|
33
|
+
xml['w'].caps({ 'w:val' => (s.style_caps ? '1' : '0') })
|
|
34
|
+
xml['w'].smallCaps({ 'w:val' => '0' })
|
|
35
|
+
xml['w'].strike({ 'w:val' => '0' })
|
|
36
|
+
xml['w'].color({ 'w:val' => s.style_color })
|
|
37
|
+
xml['w'].sz({ 'w:val' => s.style_size })
|
|
38
|
+
xml['w'].u({ 'w:val' => (s.style_underline ? 'single' : 'none') })
|
|
39
|
+
xml['w'].vertAlign({ 'w:val' => 'baseline' })
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
|
-
xml
|
|
43
|
-
xml
|
|
44
|
-
xml
|
|
45
|
-
xml
|
|
46
|
-
xml
|
|
47
|
-
xml
|
|
48
|
-
xml
|
|
49
|
-
xml
|
|
42
|
+
xml['w'].pPrDefault do
|
|
43
|
+
xml['w'].pPr do
|
|
44
|
+
xml['w'].keepNext({ 'w:val' => '0' })
|
|
45
|
+
xml['w'].keepLines({ 'w:val' => '0' })
|
|
46
|
+
xml['w'].widowControl({ 'w:val' => '1' })
|
|
47
|
+
xml['w'].spacing(spacing_options(s, true))
|
|
48
|
+
xml['w'].ind(indentation_options(s, true))
|
|
49
|
+
xml['w'].jc({ 'w:val' => s.style_align.to_s })
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
52
|
end
|
|
53
|
-
xml
|
|
54
|
-
xml
|
|
53
|
+
xml['w'].style({ 'w:styleId' => s.style_id, 'w:type' => 'paragraph', 'w:default' => '1' }) do
|
|
54
|
+
xml['w'].name({ 'w:val' => s.style_name })
|
|
55
55
|
end
|
|
56
|
-
xml
|
|
57
|
-
xml
|
|
58
|
-
xml
|
|
59
|
-
xml
|
|
56
|
+
xml['w'].style({ 'w:styleId' => 'TableNormal', 'w:type' => 'table', 'w:default' => '1' }) do
|
|
57
|
+
xml['w'].name({ 'w:val' => 'Table Normal'})
|
|
58
|
+
xml['w'].pPr do
|
|
59
|
+
xml['w'].spacing({ 'w:lineRule' => 'auto', 'w:line' => (s.style_size * 20 * 1.15), 'w:before' => '0', 'w:after' => '0' })
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
default_id = s.style_id
|
|
63
63
|
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
#========== PARA/CHAR STYLES ==================
|
|
66
66
|
|
|
67
67
|
document.styles.reject { |s| s.style_id == default_id }.each do |s|
|
|
68
|
-
xml
|
|
69
|
-
xml
|
|
70
|
-
xml
|
|
71
|
-
xml
|
|
72
|
-
xml
|
|
73
|
-
xml
|
|
74
|
-
xml
|
|
75
|
-
xml
|
|
76
|
-
xml
|
|
77
|
-
xml
|
|
78
|
-
xml
|
|
68
|
+
xml['w'].style({ 'w:styleId' => s.style_id, 'w:type' => s.style_type }) do
|
|
69
|
+
xml['w'].name({ 'w:val' => s.style_name })
|
|
70
|
+
xml['w'].basedOn({ 'w:val' => s.style_base })
|
|
71
|
+
xml['w'].next({ 'w:val' => s.style_next })
|
|
72
|
+
xml['w'].pPr do
|
|
73
|
+
xml['w'].keepNext({ 'w:val' => '0' })
|
|
74
|
+
xml['w'].keepLines({ 'w:val' => '0' })
|
|
75
|
+
xml['w'].widowControl({ 'w:val' => '1' })
|
|
76
|
+
xml['w'].spacing(spacing_options(s)) unless spacing_options(s).nil?
|
|
77
|
+
xml['w'].contextualSpacing({ 'w:val' => '1' })
|
|
78
|
+
xml['w'].jc({ 'w:val' => s.style_align.to_s }) unless s.style_align.nil?
|
|
79
|
+
xml['w'].ind(indentation_options(s)) unless indentation_options(s).nil?
|
|
79
80
|
end
|
|
80
|
-
xml
|
|
81
|
-
xml
|
|
82
|
-
xml
|
|
83
|
-
xml
|
|
84
|
-
xml
|
|
85
|
-
xml
|
|
86
|
-
xml
|
|
87
|
-
xml
|
|
81
|
+
xml['w'].rPr do
|
|
82
|
+
xml['w'].rFonts(font_options(s)) unless s.style_font.nil?
|
|
83
|
+
xml['w'].b({ 'w:val' => (s.style_bold ? '1' : '0') } ) unless s.style_bold.nil?
|
|
84
|
+
xml['w'].i({ 'w:val' => (s.style_italic ? '1' : '0') }) unless s.style_italic.nil?
|
|
85
|
+
xml['w'].caps({ 'w:val' => (s.style_caps ? '1' : '0') }) unless s.style_caps.nil?
|
|
86
|
+
xml['w'].color({ 'w:val' => s.style_color }) unless s.style_color.nil?
|
|
87
|
+
xml['w'].sz({ 'w:val' => s.style_size }) unless s.style_size.nil?
|
|
88
|
+
xml['w'].u({ 'w:val' => (s.style_underline ? 'single' : 'none') }) unless s.style_underline.nil?
|
|
88
89
|
end
|
|
89
90
|
end
|
|
90
91
|
end
|
|
91
92
|
|
|
92
|
-
|
|
93
|
+
#========== TABLE STYLES ======================
|
|
93
94
|
|
|
94
|
-
xml
|
|
95
|
-
xml
|
|
96
|
-
xml
|
|
97
|
-
xml
|
|
98
|
-
xml
|
|
95
|
+
xml['w'].style({ 'w:styleId' => 'DefaultTable', 'w:type' => 'table' }) do
|
|
96
|
+
xml['w'].basedOn({ 'w:val' => 'TableNormal' })
|
|
97
|
+
xml['w'].tblPr do
|
|
98
|
+
xml['w'].tblStyleRowBandSize({ 'w:val' => '1' })
|
|
99
|
+
xml['w'].tblStyleColBandSize({ 'w:val' => '1' })
|
|
99
100
|
end
|
|
100
101
|
%w(band1Horz band1Vert band2Horz band2Vert).each do |type|
|
|
101
|
-
xml
|
|
102
|
+
xml['w'].tblStylePr({ 'w:type' => type })
|
|
102
103
|
end
|
|
103
104
|
%w(firstCol firstRow lastCol lastRow).each do |type|
|
|
104
|
-
xml
|
|
105
|
+
xml['w'].tblStylePr({ 'w:type' => type })
|
|
105
106
|
end
|
|
106
107
|
%w(neCell nwCell seCell swCell).each do |type|
|
|
107
|
-
xml
|
|
108
|
+
xml['w'].tblStylePr({ 'w:type' => type })
|
|
108
109
|
end
|
|
109
110
|
end
|
|
110
111
|
|
|
@@ -115,9 +116,9 @@ module Caracal
|
|
|
115
116
|
|
|
116
117
|
|
|
117
118
|
|
|
118
|
-
|
|
119
|
+
#----------------------------------------------------
|
|
119
120
|
# Private Methods
|
|
120
|
-
|
|
121
|
+
#----------------------------------------------------
|
|
121
122
|
private
|
|
122
123
|
|
|
123
124
|
def font_options(style)
|
|
@@ -125,18 +126,16 @@ module Caracal
|
|
|
125
126
|
{ 'w:cs' => name, 'w:hAnsi' => name, 'w:eastAsia' => name, 'w:ascii' => name }
|
|
126
127
|
end
|
|
127
128
|
|
|
128
|
-
def
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
129
|
+
def indentation_options(style, default=false)
|
|
130
|
+
left = (default) ? style.style_indent_left.to_i : style.style_indent_left
|
|
131
|
+
right = (default) ? style.style_indent_right.to_i : style.style_indent_right
|
|
132
|
+
first = (default) ? style.style_indent_first.to_i : style.style_indent_first
|
|
133
133
|
options = nil
|
|
134
|
-
if [
|
|
135
|
-
options
|
|
136
|
-
options['w:
|
|
137
|
-
options['w:
|
|
138
|
-
options['w:
|
|
139
|
-
options['w:line'] = line unless line.nil?
|
|
134
|
+
if [left, right, first].compact.size > 0
|
|
135
|
+
options = {}
|
|
136
|
+
options['w:left'] = left unless left.nil?
|
|
137
|
+
options['w:right'] = right unless right.nil?
|
|
138
|
+
options['w:firstLine'] = first unless first.nil?
|
|
140
139
|
end
|
|
141
140
|
options
|
|
142
141
|
end
|
|
@@ -161,6 +160,22 @@ module Caracal
|
|
|
161
160
|
}
|
|
162
161
|
end
|
|
163
162
|
|
|
163
|
+
def spacing_options(style, default=false)
|
|
164
|
+
top = (default) ? style.style_top.to_i : style.style_top
|
|
165
|
+
bottom = (default) ? style.style_bottom.to_i : style.style_bottom
|
|
166
|
+
line = style.style_line
|
|
167
|
+
|
|
168
|
+
options = nil
|
|
169
|
+
if [top, bottom, line].compact.size > 0
|
|
170
|
+
options = {}
|
|
171
|
+
options['w:lineRule'] = 'auto'
|
|
172
|
+
options['w:before'] = top unless top.nil?
|
|
173
|
+
options['w:after'] = bottom unless bottom.nil?
|
|
174
|
+
options['w:line'] = line unless line.nil?
|
|
175
|
+
end
|
|
176
|
+
options
|
|
177
|
+
end
|
|
178
|
+
|
|
164
179
|
end
|
|
165
180
|
end
|
|
166
181
|
end
|
|
@@ -4,80 +4,80 @@ require 'nokogiri'
|
|
|
4
4
|
module Caracal
|
|
5
5
|
module Renderers
|
|
6
6
|
class XmlRenderer
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
#-------------------------------------------------------------
|
|
9
9
|
# Configuration
|
|
10
10
|
#-------------------------------------------------------------
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
attr_reader :document
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
|
|
14
|
+
|
|
15
15
|
#-------------------------------------------------------------
|
|
16
16
|
# Class Methods
|
|
17
17
|
#-------------------------------------------------------------
|
|
18
|
-
|
|
19
|
-
# This method produces xml output for the given document
|
|
18
|
+
|
|
19
|
+
# This method produces xml output for the given document
|
|
20
20
|
# according to the rules of this renderer object.
|
|
21
21
|
#
|
|
22
22
|
def self.render(doc)
|
|
23
23
|
renderer = new(doc)
|
|
24
24
|
renderer.to_xml
|
|
25
25
|
end
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
|
|
27
|
+
|
|
28
28
|
#-------------------------------------------------------------
|
|
29
29
|
# Public Methods
|
|
30
30
|
#-------------------------------------------------------------
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
# This method instantiates a new verison of this renderer.
|
|
33
33
|
#
|
|
34
34
|
def initialize(doc)
|
|
35
35
|
unless doc.is_a?(Caracal::Document)
|
|
36
36
|
raise NoDocumentError, 'renderers must receive a reference to a valid Caracal document object.'
|
|
37
37
|
end
|
|
38
|
-
|
|
38
|
+
|
|
39
39
|
@document = doc
|
|
40
40
|
end
|
|
41
|
-
|
|
41
|
+
|
|
42
42
|
# This method converts data in the specified document to XML.
|
|
43
43
|
# A concrete implementation must be provided by the subclass.
|
|
44
44
|
#
|
|
45
45
|
def to_xml
|
|
46
46
|
raise NotImplementedError, 'renderers must implement the method :to_xml.'
|
|
47
47
|
end
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
|
|
49
|
+
|
|
50
50
|
#-------------------------------------------------------------
|
|
51
51
|
# Private Methods
|
|
52
52
|
#-------------------------------------------------------------
|
|
53
53
|
private
|
|
54
|
-
|
|
55
|
-
# This method returns a Nokogiri::XML object that contains the
|
|
54
|
+
|
|
55
|
+
# This method returns a Nokogiri::XML object that contains the
|
|
56
56
|
# specific declaration we want.
|
|
57
57
|
#
|
|
58
58
|
def declaration_xml
|
|
59
59
|
::Nokogiri::XML('<?xml version = "1.0" encoding = "UTF-8" standalone ="yes"?>')
|
|
60
60
|
end
|
|
61
|
-
|
|
61
|
+
|
|
62
62
|
# This method returns a commonly used set of attributes for paragraph nodes.
|
|
63
63
|
#
|
|
64
64
|
def paragraph_options
|
|
65
65
|
{ 'w:rsidP' => '00000000', 'w:rsidRDefault' => '00000000' }.merge(run_options)
|
|
66
66
|
end
|
|
67
|
-
|
|
67
|
+
|
|
68
68
|
# This method returns a commonly used set of attributes for text run nodes.
|
|
69
69
|
#
|
|
70
70
|
def run_options
|
|
71
71
|
{ 'w:rsidR' => '00000000', 'w:rsidRPr' => '00000000', 'w:rsidDel' => '00000000' }
|
|
72
72
|
end
|
|
73
|
-
|
|
73
|
+
|
|
74
74
|
# These save options force Nokogiri to remove indentation and
|
|
75
75
|
# line feeds from the output.
|
|
76
76
|
#
|
|
77
77
|
def save_options
|
|
78
78
|
{ save_with: Nokogiri::XML::Node::SaveOptions::AS_XML }
|
|
79
79
|
end
|
|
80
|
-
|
|
80
|
+
|
|
81
81
|
end
|
|
82
82
|
end
|
|
83
|
-
end
|
|
83
|
+
end
|
data/lib/caracal/version.rb
CHANGED
data/lib/caracal.rb
CHANGED
|
@@ -18,8 +18,17 @@ require 'caracal/document'
|
|
|
18
18
|
# Extra Setup
|
|
19
19
|
#------------------------------------------------
|
|
20
20
|
|
|
21
|
-
#
|
|
22
|
-
#
|
|
21
|
+
# Convenience method for finding root directory.
|
|
22
|
+
#
|
|
23
|
+
module Caracal
|
|
24
|
+
def self.root
|
|
25
|
+
File.dirname __dir__
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# Add functions to table cell model. we do this here to
|
|
31
|
+
# avoid a circular require between Caracal::Core::Tables
|
|
23
32
|
# and Caracal::Core::Models::TableCellModel.
|
|
24
33
|
#
|
|
25
34
|
Caracal::Core::Models::TableCellModel.class_eval do
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Caracal::Core::IFrames do
|
|
4
|
+
subject { Caracal::Document.new }
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
#-------------------------------------------------------------
|
|
8
|
+
# Public Methods
|
|
9
|
+
#-------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
describe 'public method tests' do
|
|
12
|
+
|
|
13
|
+
# .img
|
|
14
|
+
describe '.iframes' do
|
|
15
|
+
let!(:size) { subject.contents.size }
|
|
16
|
+
|
|
17
|
+
before do
|
|
18
|
+
path = File.join(Caracal.root, 'spec', 'support', '_fixtures', 'snippet.docx')
|
|
19
|
+
data = File.open(path).read
|
|
20
|
+
subject.iframe data: data
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it { expect(subject.contents.size).to eq size + 1 }
|
|
24
|
+
it { expect(subject.contents.last).to be_a(Caracal::Core::Models::IFrameModel) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Caracal::Core::Ignorables do
|
|
4
|
+
subject { Caracal::Document.new }
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
#-------------------------------------------------------------
|
|
8
|
+
# Public Methods
|
|
9
|
+
#-------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
describe 'public method tests' do
|
|
12
|
+
|
|
13
|
+
#============== ATTRIBUTES =====================
|
|
14
|
+
|
|
15
|
+
# .ignorable
|
|
16
|
+
describe '.ignorable' do
|
|
17
|
+
it 'delegates to registration method' do
|
|
18
|
+
expect(subject).to receive(:register_ignorable)
|
|
19
|
+
subject.ignorable('dummy')
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
#============== GETTERS ========================
|
|
25
|
+
|
|
26
|
+
# .ignorables
|
|
27
|
+
describe '.ignorables' do
|
|
28
|
+
it { expect(subject.ignorables).to be_a(Array) }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
#============== REGISTRATION ========================
|
|
33
|
+
|
|
34
|
+
# .register_ignorable
|
|
35
|
+
describe '.register_ignorable' do
|
|
36
|
+
let(:default_length) { 0 }
|
|
37
|
+
|
|
38
|
+
describe 'when not already registered' do
|
|
39
|
+
before do
|
|
40
|
+
subject.register_ignorable('dummy')
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it { expect(subject.ignorables.size).to eq default_length + 1 }
|
|
44
|
+
end
|
|
45
|
+
describe 'when already registered' do
|
|
46
|
+
before do
|
|
47
|
+
subject.register_ignorable('dummy')
|
|
48
|
+
subject.register_ignorable('dummy')
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it { expect(subject.ignorables.size).to eq default_length + 1 }
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# .unregister_ignorable
|
|
56
|
+
describe '.unregister_ignorable' do
|
|
57
|
+
let(:default_length) { 0 }
|
|
58
|
+
|
|
59
|
+
describe 'when registered' do
|
|
60
|
+
before do
|
|
61
|
+
subject.register_ignorable('dummy')
|
|
62
|
+
subject.unregister_ignorable('dummy')
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it { expect(subject.ignorables.size).to eq default_length }
|
|
66
|
+
end
|
|
67
|
+
describe 'when not registered' do
|
|
68
|
+
before do
|
|
69
|
+
subject.register_ignorable('dummy')
|
|
70
|
+
subject.unregister_ignorable('bogus')
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it { expect(subject.ignorables.size).to eq default_length + 1 }
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
end
|