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.
Files changed (53) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +95 -5
  4. data/README.md +171 -35
  5. data/caracal.gemspec +4 -4
  6. data/lib/caracal/core/custom_properties.rb +49 -0
  7. data/lib/caracal/core/iframes.rb +42 -0
  8. data/lib/caracal/core/ignorables.rb +47 -0
  9. data/lib/caracal/core/models/custom_property_model.rb +60 -0
  10. data/lib/caracal/core/models/iframe_model.rb +148 -0
  11. data/lib/caracal/core/models/namespace_model.rb +65 -0
  12. data/lib/caracal/core/models/page_break_model.rb +50 -4
  13. data/lib/caracal/core/models/page_number_model.rb +50 -24
  14. data/lib/caracal/core/models/page_size_model.rb +34 -25
  15. data/lib/caracal/core/models/paragraph_model.rb +11 -4
  16. data/lib/caracal/core/models/style_model.rb +44 -15
  17. data/lib/caracal/core/models/table_cell_model.rb +61 -50
  18. data/lib/caracal/core/models/table_model.rb +1 -1
  19. data/lib/caracal/core/models/text_model.rb +42 -20
  20. data/lib/caracal/core/namespaces.rb +89 -0
  21. data/lib/caracal/core/page_numbers.rb +22 -16
  22. data/lib/caracal/core/page_settings.rb +18 -16
  23. data/lib/caracal/document.rb +75 -58
  24. data/lib/caracal/renderers/content_types_renderer.rb +18 -17
  25. data/lib/caracal/renderers/core_renderer.rb +1 -1
  26. data/lib/caracal/renderers/custom_renderer.rb +64 -0
  27. data/lib/caracal/renderers/document_renderer.rb +205 -118
  28. data/lib/caracal/renderers/fonts_renderer.rb +2 -2
  29. data/lib/caracal/renderers/footer_renderer.rb +41 -20
  30. data/lib/caracal/renderers/numbering_renderer.rb +27 -26
  31. data/lib/caracal/renderers/package_relationships_renderer.rb +14 -13
  32. data/lib/caracal/renderers/settings_renderer.rb +5 -5
  33. data/lib/caracal/renderers/styles_renderer.rb +88 -73
  34. data/lib/caracal/renderers/xml_renderer.rb +20 -20
  35. data/lib/caracal/version.rb +1 -1
  36. data/lib/caracal.rb +11 -2
  37. data/spec/lib/caracal/core/iframes_spec.rb +29 -0
  38. data/spec/lib/caracal/core/ignorables_spec.rb +79 -0
  39. data/spec/lib/caracal/core/models/iframe_model_spec.rb +83 -0
  40. data/spec/lib/caracal/core/models/image_model_spec.rb +60 -62
  41. data/spec/lib/caracal/core/models/link_model_spec.rb +12 -11
  42. data/spec/lib/caracal/core/models/namespace_model_spec.rb +107 -0
  43. data/spec/lib/caracal/core/models/page_number_model_spec.rb +66 -31
  44. data/spec/lib/caracal/core/models/page_size_model_spec.rb +42 -32
  45. data/spec/lib/caracal/core/models/style_model_spec.rb +32 -1
  46. data/spec/lib/caracal/core/models/table_cell_model_spec.rb +10 -1
  47. data/spec/lib/caracal/core/models/text_model_spec.rb +18 -10
  48. data/spec/lib/caracal/core/namespaces_spec.rb +116 -0
  49. data/spec/lib/caracal/core/page_numbers_spec.rb +32 -23
  50. data/spec/lib/caracal/core/page_settings_spec.rb +33 -25
  51. data/spec/lib/caracal/core/relationships_spec.rb +47 -47
  52. data/spec/support/_fixtures/snippet.docx +0 -0
  53. metadata +28 -8
@@ -0,0 +1,60 @@
1
+ require 'caracal/core/models/base_model'
2
+
3
+
4
+ module Caracal
5
+ module Core
6
+ module Models
7
+
8
+ # This class encapsulates the logic needed to store and manipulate
9
+ # custom properties
10
+ #
11
+ class CustomPropertyModel < BaseModel
12
+
13
+ # accessors
14
+ attr_reader :custom_property_name
15
+ attr_reader :custom_property_value
16
+ attr_reader :custom_property_type
17
+
18
+
19
+ #-------------------------------------------------------------
20
+ # Public Instance Methods
21
+ #-------------------------------------------------------------
22
+
23
+
24
+ #=================== SETTERS =============================
25
+
26
+ def name(value)
27
+ @custom_property_name = value.to_s
28
+ end
29
+
30
+ def value(value)
31
+ @custom_property_value = value.to_s
32
+ end
33
+
34
+ def type(value)
35
+ @custom_property_type = value.to_s
36
+ end
37
+
38
+
39
+ #=============== VALIDATION ===========================
40
+
41
+ def valid?
42
+ required = option_keys
43
+ required.all? { |m| !send("custom_property_#{ m }").nil? }
44
+ end
45
+
46
+
47
+ #-------------------------------------------------------------
48
+ # Private Instance Methods
49
+ #-------------------------------------------------------------
50
+ private
51
+
52
+ def option_keys
53
+ [:name, :value, :type]
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,148 @@
1
+ require 'caracal/core/models/base_model'
2
+
3
+ module Caracal
4
+ module Core
5
+ module Models
6
+
7
+ # This class handles block options passed to the img method.
8
+ #
9
+ class IFrameModel < BaseModel
10
+
11
+ #--------------------------------------------------
12
+ # Configuration
13
+ #--------------------------------------------------
14
+
15
+ # accessors
16
+ attr_reader :iframe_url
17
+ attr_reader :iframe_data
18
+ attr_reader :iframe_ignorables
19
+ attr_reader :iframe_namespaces
20
+ attr_reader :iframe_relationships
21
+
22
+ # initialization
23
+ def initialize(options={}, &block)
24
+ super options, &block
25
+ end
26
+
27
+
28
+ #--------------------------------------------------
29
+ # Public Methods
30
+ #--------------------------------------------------
31
+
32
+ #=============== PROCESSING =======================
33
+
34
+ def preprocess!
35
+ ::Zip::File.open(file) do |zip|
36
+ # locate relationships xml
37
+ entry = zip.glob('word/_rels/document.xml.rels').first
38
+ content = entry.get_input_stream.read
39
+ rel_xml = Nokogiri::XML(content)
40
+
41
+ # locate document xml
42
+ entry = zip.glob('word/document.xml').first
43
+ content = entry.get_input_stream.read
44
+ doc_xml = Nokogiri::XML(content)
45
+
46
+ # master nodesets
47
+ rel_nodes = rel_xml.children.first.children
48
+ doc_root = doc_xml.at_xpath('//w:document')
49
+ pic_nodes = doc_xml.xpath('//pic:pic', { pic: 'http://schemas.openxmlformats.org/drawingml/2006/picture' })
50
+
51
+ # namespaces
52
+ @iframe_namespaces = doc_root.namespaces
53
+
54
+ # ignorable namespaces
55
+ if a = doc_root.attributes['Ignorable']
56
+ @iframe_ignorables = a.value.split(/\s+/)
57
+ end
58
+
59
+ # relationships
60
+ media_map = rel_nodes.reduce({}) do |hash, node|
61
+ type = node.at_xpath('@Type').value
62
+ if type.slice(-5, 5) == 'image'
63
+ id = node.at_xpath('@Id').value
64
+ path = "word/#{ node.at_xpath('@Target').value }"
65
+ hash[id] = path
66
+ end
67
+ hash
68
+ end
69
+ @iframe_relationships = pic_nodes.reduce([]) do |array, node|
70
+ r_node = node.children[1].children[0]
71
+ r_id = r_node.attributes['embed'].value.to_s
72
+ r_media = media_map[r_id]
73
+
74
+ p_node = node.children[0].children[0]
75
+ p_id = p_node.attributes['id'].to_s.to_i
76
+ p_name = p_node.attributes['name'].to_s
77
+ p_data = zip.glob(r_media).first.get_input_stream.read
78
+
79
+ # register relationship
80
+ array << { id: r_id, type: 'image', target: p_name, data: p_data }
81
+ array
82
+ end
83
+ end
84
+ end
85
+
86
+
87
+ #=============== GETTERS ==========================
88
+
89
+ def file
90
+ @file ||= begin
91
+ if iframe_url.nil?
92
+ file = Tempfile.new('iframe')
93
+ file.write iframe_data
94
+ file.rewind
95
+ else
96
+ file = open(iframe_url)
97
+ end
98
+ file
99
+ end
100
+ end
101
+
102
+ def ignorables
103
+ @iframe_ignorables || []
104
+ end
105
+
106
+ def namespaces
107
+ @iframe_namespaces || {}
108
+ end
109
+
110
+ def relationships
111
+ @iframe_relationships || []
112
+ end
113
+
114
+
115
+ #=============== SETTERS ==========================
116
+
117
+ # strings
118
+ [:data, :url].each do |m|
119
+ define_method "#{ m }" do |value|
120
+ instance_variable_set("@iframe_#{ m }", value.to_s)
121
+ end
122
+ end
123
+
124
+
125
+ #=============== VALIDATION =======================
126
+
127
+ def valid?
128
+ vals = option_keys.map { |m| send("iframe_#{ m }") }.compact
129
+ vals = vals.reject { |v| v.size == 0 }
130
+ vals.size > 0
131
+ end
132
+
133
+
134
+
135
+ #--------------------------------------------------
136
+ # Private Methods
137
+ #--------------------------------------------------
138
+ private
139
+
140
+ def option_keys
141
+ [:url, :data]
142
+ end
143
+
144
+ end
145
+
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,65 @@
1
+ require 'caracal/core/models/base_model'
2
+
3
+
4
+ module Caracal
5
+ module Core
6
+ module Models
7
+
8
+ # This class encapsulates the logic needed to store and manipulate
9
+ # namespace data.
10
+ #
11
+ class NamespaceModel < BaseModel
12
+
13
+ #-------------------------------------------------------------
14
+ # Configuration
15
+ #-------------------------------------------------------------
16
+
17
+ # accessors
18
+ attr_reader :namespace_prefix
19
+ attr_reader :namespace_href
20
+
21
+
22
+
23
+ #-------------------------------------------------------------
24
+ # Public Instance Methods
25
+ #-------------------------------------------------------------
26
+
27
+ #=================== SETTERS =============================
28
+
29
+ # strings
30
+ [:href, :prefix].each do |m|
31
+ define_method "#{ m }" do |value|
32
+ instance_variable_set("@namespace_#{ m }", value.to_s)
33
+ end
34
+ end
35
+
36
+
37
+ #=================== STATE ===============================
38
+
39
+ def matches?(str)
40
+ namespace_prefix == str.to_s
41
+ end
42
+
43
+
44
+ #=============== VALIDATION ===========================
45
+
46
+ def valid?
47
+ required = [:href, :prefix]
48
+ required.all? { |m| !send("namespace_#{ m }").nil? }
49
+ end
50
+
51
+
52
+ #-------------------------------------------------------------
53
+ # Private Instance Methods
54
+ #-------------------------------------------------------------
55
+ private
56
+
57
+ def option_keys
58
+ [:prefix, :href]
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -4,12 +4,58 @@ require 'caracal/core/models/base_model'
4
4
  module Caracal
5
5
  module Core
6
6
  module Models
7
-
7
+
8
8
  # This class encapsulates the logic needed to store and manipulate
9
9
  # page break data.
10
10
  #
11
- class PageBreakModel < BaseModel; end
12
-
11
+ # The :wrap option is not described in the project's README because
12
+ # it exists purely as an internal Caracal concern. Page breaks
13
+ # at the document level must be wrapped in a paragraph node; page breaks
14
+ # within paragraph-like container simply add a run. There's no need to
15
+ # trouble end users with this issue.
16
+ #
17
+ class PageBreakModel < BaseModel
18
+
19
+ #-------------------------------------------------------------
20
+ # Configuration
21
+ #-------------------------------------------------------------
22
+
23
+ # constants
24
+ const_set(:DEFAULT_PAGE_BREAK_WRAP, true)
25
+
26
+ # accessors
27
+ attr_reader :page_break_wrap
28
+
29
+ # initialization
30
+ def initialize(options={}, &block)
31
+ @page_break_wrap = DEFAULT_PAGE_BREAK_WRAP
32
+
33
+ super options, &block
34
+ end
35
+
36
+
37
+ #-------------------------------------------------------------
38
+ # Public Methods
39
+ #-------------------------------------------------------------
40
+
41
+ #=============== SETTERS ==============================
42
+
43
+ def wrap(value)
44
+ @page_break_wrap = !!value
45
+ end
46
+
47
+
48
+ #-------------------------------------------------------------
49
+ # Private Instance Methods
50
+ #-------------------------------------------------------------
51
+ private
52
+
53
+ def option_keys
54
+ [:wrap]
55
+ end
56
+
57
+ end
58
+
13
59
  end
14
60
  end
15
- end
61
+ end
@@ -4,66 +4,92 @@ require 'caracal/core/models/base_model'
4
4
  module Caracal
5
5
  module Core
6
6
  module Models
7
-
7
+
8
8
  # This class handles block options passed to the page_numbers
9
9
  # method.
10
10
  #
11
11
  class PageNumberModel < BaseModel
12
-
12
+
13
13
  #-------------------------------------------------------------
14
14
  # Configuration
15
15
  #-------------------------------------------------------------
16
-
16
+
17
17
  # constants
18
- const_set(:DEFAULT_PAGE_NUMBER_SHOW, false)
19
18
  const_set(:DEFAULT_PAGE_NUMBER_ALIGN, :center)
20
-
19
+ const_set(:DEFAULT_PAGE_NUMBER_SHOW, false)
20
+
21
21
  # accessors
22
22
  attr_reader :page_number_align
23
+ attr_reader :page_number_label
24
+ attr_reader :page_number_label_size
25
+ attr_reader :page_number_number_size
23
26
  attr_reader :page_number_show
24
-
27
+
25
28
  # initialization
26
29
  def initialize(options={}, &block)
27
- @page_number_show = DEFAULT_PAGE_NUMBER_SHOW
28
- @page_number_align = DEFAULT_PAGE_NUMBER_ALIGN
29
-
30
+ @page_number_align = DEFAULT_PAGE_NUMBER_ALIGN
31
+ @page_number_label = nil
32
+ @page_number_label_size = nil
33
+ @page_number_number_size = nil
34
+ @page_number_show = DEFAULT_PAGE_NUMBER_SHOW
35
+
30
36
  super options, &block
31
37
  end
32
-
33
-
38
+
39
+
34
40
  #-------------------------------------------------------------
35
41
  # Public Methods
36
42
  #-------------------------------------------------------------
37
-
43
+
38
44
  #=============== SETTERS ==============================
39
-
45
+
40
46
  def align(value)
41
47
  @page_number_align = value.to_s.to_sym
42
48
  end
43
-
49
+
50
+ def label(value)
51
+ @page_number_label = value.to_s.strip # renderer will enforce trailing space
52
+ end
53
+
54
+ def label_size(value)
55
+ v = value.to_i
56
+ @page_number_label_size = (v == 0) ? nil : v
57
+ end
58
+
59
+ def number_size(value)
60
+ v = value.to_i
61
+ @page_number_number_size = (v == 0) ? nil : v
62
+ end
63
+
44
64
  def show(value)
45
65
  @page_number_show = !!value
46
66
  end
47
-
48
-
67
+
68
+ def size(value)
69
+ v = value.to_i
70
+ @page_number_label_size = (v == 0) ? nil : v
71
+ @page_number_number_size = (v == 0) ? nil : v
72
+ end
73
+
74
+
49
75
  #=============== VALIDATION ===========================
50
-
76
+
51
77
  def valid?
52
78
  (!page_number_show || [:left, :center, :right].include?(page_number_align))
53
79
  end
54
-
55
-
80
+
81
+
56
82
  #-------------------------------------------------------------
57
83
  # Private Instance Methods
58
84
  #-------------------------------------------------------------
59
85
  private
60
-
86
+
61
87
  def option_keys
62
- [:align, :show]
88
+ [:align, :label, :label_size, :number_size, :show]
63
89
  end
64
-
90
+
65
91
  end
66
-
92
+
67
93
  end
68
94
  end
69
- end
95
+ end
@@ -4,67 +4,76 @@ require 'caracal/core/models/base_model'
4
4
  module Caracal
5
5
  module Core
6
6
  module Models
7
-
7
+
8
8
  # This class handles block options passed to the page size
9
9
  # method.
10
10
  #
11
11
  class PageSizeModel < BaseModel
12
-
12
+
13
13
  #-------------------------------------------------------------
14
14
  # Configuration
15
15
  #-------------------------------------------------------------
16
-
16
+
17
17
  # constants
18
- const_set(:DEFAULT_PAGE_WIDTH, 12240) # 8.5in in twips
19
- const_set(:DEFAULT_PAGE_HEIGHT, 15840) # 11.0in in twips
20
-
18
+ const_set(:DEFAULT_PAGE_WIDTH, 12240) # 8.5in in twips
19
+ const_set(:DEFAULT_PAGE_HEIGHT, 15840) # 11.0in in twips
20
+ const_set(:DEFAULT_PAGE_ORIENTATION, 'portrait')
21
+
21
22
  # accessors
22
23
  attr_reader :page_width
23
24
  attr_reader :page_height
24
-
25
+ attr_reader :page_orientation
26
+
25
27
  # initialization
26
28
  def initialize(options={}, &block)
27
- @page_width = DEFAULT_PAGE_WIDTH
28
- @page_height = DEFAULT_PAGE_HEIGHT
29
-
29
+ @page_width = DEFAULT_PAGE_WIDTH
30
+ @page_height = DEFAULT_PAGE_HEIGHT
31
+ @page_orientation = DEFAULT_PAGE_ORIENTATION
32
+
30
33
  super options, &block
31
34
  end
32
-
33
-
35
+
36
+
34
37
  #-------------------------------------------------------------
35
38
  # Public Methods
36
39
  #-------------------------------------------------------------
37
-
40
+
38
41
  #=============== SETTERS ==============================
39
-
42
+
40
43
  def height(value)
41
44
  @page_height = value.to_i
42
45
  end
43
-
46
+
47
+ def orientation(value)
48
+ allowed = ['landscape','portrait']
49
+ given = value.to_s.downcase
50
+ @page_orientation = allowed.include?(given) ? given : 'portrait'
51
+ end
52
+
44
53
  def width(value)
45
54
  @page_width = value.to_i
46
55
  end
47
-
48
-
56
+
57
+
49
58
  #=============== VALIDATION ==============================
50
-
59
+
51
60
  def valid?
52
61
  dims = [page_width, page_height]
53
62
  dims.all? { |d| d > 0 }
54
63
  end
55
-
56
-
64
+
65
+
57
66
  #-------------------------------------------------------------
58
67
  # Private Instance Methods
59
68
  #-------------------------------------------------------------
60
69
  private
61
-
70
+
62
71
  def option_keys
63
- [:width, :height]
72
+ [:width, :height, :orientation]
64
73
  end
65
-
74
+
66
75
  end
67
-
76
+
68
77
  end
69
78
  end
70
- end
79
+ end
@@ -30,7 +30,7 @@ module Caracal
30
30
  # initialization
31
31
  def initialize(options={}, &block)
32
32
  content = options.delete(:content) { "" }
33
- text content, options.dup
33
+ text content, options.dup, &block
34
34
  super options, &block
35
35
  end
36
36
 
@@ -92,6 +92,13 @@ module Caracal
92
92
 
93
93
  #=============== SUB-METHODS ===========================
94
94
 
95
+ # .br
96
+ def br
97
+ model = Caracal::Core::Models::LineBreakModel.new()
98
+ runs << model
99
+ model
100
+ end
101
+
95
102
  # .link
96
103
  def link(*args, &block)
97
104
  options = Caracal::Utilities.extract_options!(args)
@@ -107,9 +114,9 @@ module Caracal
107
114
  model
108
115
  end
109
116
 
110
- # .br
111
- def br
112
- model = Caracal::Core::Models::LineBreakModel.new()
117
+ # .page
118
+ def page
119
+ model = Caracal::Core::Models::PageBreakModel.new({ wrap: false })
113
120
  runs << model
114
121
  model
115
122
  end