rrtf 1.0.0 → 1.3.1

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 (87) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +17 -0
  3. data/README.md +29 -0
  4. data/documentation/RRTF.html +5 -5
  5. data/documentation/RRTF/AnonymousStyle.html +2 -2
  6. data/documentation/RRTF/BorderFormatting.html +48 -48
  7. data/documentation/RRTF/BorderStyle.html +1 -1
  8. data/documentation/RRTF/CharacterFormatting.html +1 -1
  9. data/documentation/RRTF/CharacterStyle.html +1 -1
  10. data/documentation/RRTF/Colour.html +1 -1
  11. data/documentation/RRTF/ColourTable.html +1 -1
  12. data/documentation/RRTF/CommandNode.html +333 -93
  13. data/documentation/RRTF/ContainerNode.html +1 -1
  14. data/documentation/RRTF/Converters.html +1 -1
  15. data/documentation/RRTF/Converters/HTML.html +1 -1
  16. data/documentation/RRTF/Converters/HTML/Helpers.html +1 -1
  17. data/documentation/RRTF/Converters/HTML/Node.html +1 -1
  18. data/documentation/RRTF/Converters/HTML/NodeSet.html +1 -1
  19. data/documentation/RRTF/Document.html +2 -2
  20. data/documentation/RRTF/DocumentFormatting.html +48 -48
  21. data/documentation/RRTF/DocumentProperties.html +8 -3
  22. data/documentation/RRTF/Font.html +1 -1
  23. data/documentation/RRTF/FontTable.html +1 -1
  24. data/documentation/RRTF/FooterNode.html +2 -2
  25. data/documentation/RRTF/GeometryNode.html +2 -2
  26. data/documentation/RRTF/GeometryProperties.html +9 -9
  27. data/documentation/RRTF/HeaderNode.html +2 -2
  28. data/documentation/RRTF/ImageNode.html +64 -61
  29. data/documentation/RRTF/Information.html +1 -1
  30. data/documentation/RRTF/LinkNode.html +2 -2
  31. data/documentation/RRTF/ListLevel.html +1 -1
  32. data/documentation/RRTF/ListLevelNode.html +2 -2
  33. data/documentation/RRTF/ListMarker.html +1 -1
  34. data/documentation/RRTF/ListNode.html +2 -2
  35. data/documentation/RRTF/ListTable.html +1 -1
  36. data/documentation/RRTF/ListTemplate.html +1 -1
  37. data/documentation/RRTF/ListTextNode.html +2 -2
  38. data/documentation/RRTF/Node.html +1 -1
  39. data/documentation/RRTF/Page.html +1 -1
  40. data/documentation/RRTF/Page/Margin.html +1 -1
  41. data/documentation/RRTF/Page/Size.html +1 -1
  42. data/documentation/RRTF/PageFormatting.html +184 -195
  43. data/documentation/RRTF/ParagraphFormatting.html +75 -48
  44. data/documentation/RRTF/ParagraphNode.html +2 -2
  45. data/documentation/RRTF/ParagraphStyle.html +1 -1
  46. data/documentation/RRTF/PositionFormatting.html +48 -48
  47. data/documentation/RRTF/PositionStyle.html +1 -1
  48. data/documentation/RRTF/Properties.html +1 -1
  49. data/documentation/RRTF/RTFError.html +1 -1
  50. data/documentation/RRTF/SectionFormatting.html +750 -0
  51. data/documentation/RRTF/SectionNode.html +383 -0
  52. data/documentation/RRTF/SectionStyle.html +830 -0
  53. data/documentation/RRTF/ShadingFormatting.html +48 -48
  54. data/documentation/RRTF/ShadingStyle.html +1 -1
  55. data/documentation/RRTF/Style.html +2 -2
  56. data/documentation/RRTF/Stylesheet.html +1 -1
  57. data/documentation/RRTF/TabStyle.html +698 -0
  58. data/documentation/RRTF/TableCellNode.html +2 -2
  59. data/documentation/RRTF/TableNode.html +1 -1
  60. data/documentation/RRTF/TableRowNode.html +1 -1
  61. data/documentation/RRTF/TextNode.html +1 -1
  62. data/documentation/RRTF/Utilities.html +1 -1
  63. data/documentation/_index.html +33 -5
  64. data/documentation/class_list.html +1 -1
  65. data/documentation/file.README.html +32 -1
  66. data/documentation/index.html +32 -1
  67. data/documentation/method_list.html +499 -355
  68. data/documentation/top-level-namespace.html +1 -1
  69. data/examples/08.rtf +2 -2
  70. data/examples/09.rtf +9 -13
  71. data/examples/09_shapes.rb +2 -1
  72. data/examples/11.rtf +19 -0
  73. data/examples/11_sections.rb +11 -0
  74. data/examples/12.rtf +19 -0
  75. data/examples/12_tabs.rb +21 -0
  76. data/lib/rrtf/formatting.rb +155 -22
  77. data/lib/rrtf/node.rb +1 -0
  78. data/lib/rrtf/node/command_node.rb +38 -4
  79. data/lib/rrtf/node/image_node.rb +26 -32
  80. data/lib/rrtf/node/section_node.rb +21 -0
  81. data/lib/rrtf/properties/geometry_properties.rb +4 -4
  82. data/lib/rrtf/style.rb +2 -0
  83. data/lib/rrtf/style/section_style.rb +66 -0
  84. data/lib/rrtf/style/tab_style.rb +55 -0
  85. data/lib/rrtf/version.rb +1 -1
  86. data/rrtf.gemspec +4 -4
  87. metadata +30 -20
@@ -1,4 +1,5 @@
1
1
  require 'fastimage'
2
+ require 'open-uri'
2
3
 
3
4
  module RRTF
4
5
  # This class represents an image within a RTF document. Currently only the
@@ -60,7 +61,7 @@ module RRTF
60
61
  # This is the constructor for the ImageNode class.
61
62
  #
62
63
  # @param parent [Node] a reference to the node that owns the new image node.
63
- # @param source [String, File] a reference to the image source; this must be a String or a File.
64
+ # @param source [String, File] a reference to the image source; this must be a String, String URL, or a File.
64
65
  # @param id [Integer] a unique 32-bit identifier for the image.
65
66
  # @param options [Hash] a hash of options.
66
67
  # @option options [String] "width" (nil) the display width of the image in twips (can be a string, see {Utilities.value2twips}).
@@ -72,7 +73,7 @@ module RRTF
72
73
  # specified source does not exist or cannot be accessed.
73
74
  def initialize(parent, source, id, options = {})
74
75
  super(parent)
75
- @source = nil
76
+ @source = source
76
77
  @id = id
77
78
 
78
79
  # load default options
@@ -95,16 +96,19 @@ module RRTF
95
96
  end
96
97
 
97
98
  # Store path to image.
98
- @source = source if source.instance_of?(String) || source.instance_of?(Tempfile)
99
- @source = source.path if source.instance_of?(File)
99
+ if @source.is_a?(String)
100
+ begin
101
+ @source = open(@source)
102
+ rescue OpenURI::HTTPError => error
103
+ response = error.io
104
+ RTFError.fire("Could not open '#{@source}'. Server responded with #{response.status.join(',')}.")
105
+ rescue Exception => error
106
+ RTFError.fire("Could not open '#{@source}'. #{error.message}.")
107
+ end # rescue block
108
+ elsif !@source.respond_to?(:each_byte)
109
+ RTFError.fire("A string or object that responds to :each_byte must be supplied - '#{@source}' given.")
110
+ end # unless
100
111
 
101
- # Check the file's existence and accessibility.
102
- if !File.exist?(@source)
103
- RTFError.fire("Unable to find the #{File.basename(@source)} file.")
104
- end
105
- if !File.readable?(@source)
106
- RTFError.fire("Access to the #{File.basename(@source)} file denied.")
107
- end
108
112
  # Attempt to determine image type and dimensions.
109
113
  @type, @width, @height = self.class.inspect(@source)
110
114
  if @type.nil?
@@ -128,18 +132,16 @@ module RRTF
128
132
  text << "\\picw#{@width}\\pich#{@height}\\bliptag#{@id}"
129
133
  text << "\\#{@type}\n"
130
134
 
131
- open_file do |file|
132
- file.each_byte do |byte|
133
- hex_str = byte.to_s(16)
134
- hex_str.insert(0,'0') if hex_str.length == 1
135
- text << hex_str
136
- count += 1
137
- if count == 40
138
- text << "\n"
139
- count = 0
140
- end # if
141
- end # each_byte
142
- end # open_file
135
+ @source.each_byte do |byte|
136
+ hex_str = byte.to_s(16)
137
+ hex_str.insert(0,'0') if hex_str.length == 1
138
+ text << hex_str
139
+ count += 1
140
+ if count == 40
141
+ text << "\n"
142
+ count = 0
143
+ end # if
144
+ end # each_byte
143
145
  text << "\n}"
144
146
 
145
147
  text.string
@@ -162,14 +164,6 @@ module RRTF
162
164
  [(@width*scale_factor).to_i, (@height*scale_factor).to_i]
163
165
  end
164
166
  end # case
165
- end
166
-
167
- def open_file(&block)
168
- if block
169
- File.open(@source, 'rb', &block)
170
- else
171
- File.open(@source, 'rb')
172
- end # if
173
- end # open_file()
167
+ end # size_image()
174
168
  end # End of the ImageNode class.
175
169
  end # module RRTF
@@ -0,0 +1,21 @@
1
+ module RRTF
2
+ # This class represents a section within an RTF document. Section nodes
3
+ # do not contain other nodes; instead, they mark the start of a new section.
4
+ # @author Wesley Hileman
5
+ class SectionNode < CommandNode
6
+ def initialize(parent, style=nil)
7
+ prefix = '\sect\sectd'
8
+ prefix << style.prefix(parent.root) unless style.nil?
9
+
10
+ super(parent, prefix, '', true, false)
11
+ end
12
+
13
+ # Overrides {ContainerNode#store} to prevent child nodes from being
14
+ # added to sections.
15
+ #
16
+ # @raise [RTFError] whenever called.
17
+ def store(node)
18
+ RTFError.fire("Cannot add child nodes to section nodes: tried to add #{node}.")
19
+ end
20
+ end
21
+ end
@@ -320,10 +320,10 @@ module RRTF
320
320
  next if margin.nil?
321
321
 
322
322
  margin = Page::Margin.new(margin)
323
- left = Utilities.value2emu(margin.left+'twip')
324
- right = Utilities.value2emu(margin.right+'twip')
325
- top = Utilities.value2emu(margin.top+'twip')
326
- bottom = Utilities.value2emu(margin.bottom+'twip')
323
+ left = Utilities.value2emu("#{margin.left}twip")
324
+ right = Utilities.value2emu("#{margin.right}twip")
325
+ top = Utilities.value2emu("#{margin.top}twip")
326
+ bottom = Utilities.value2emu("#{margin.bottom}twip")
327
327
 
328
328
  instance_variable_set("@#{margin_attr}", margin)
329
329
  instance_variable_set("@#{margin_attr}_left", left)
@@ -2,6 +2,8 @@ require 'rrtf/style/anonymous_style'
2
2
  require 'rrtf/style/border_style'
3
3
  require 'rrtf/style/position_style'
4
4
  require 'rrtf/style/shading_style'
5
+ require 'rrtf/style/tab_style'
5
6
  require 'rrtf/style/style'
6
7
  require 'rrtf/style/paragraph_style'
7
8
  require 'rrtf/style/character_style'
9
+ require 'rrtf/style/section_style'
@@ -0,0 +1,66 @@
1
+ require 'stringio'
2
+
3
+ module RRTF
4
+ # This class represents a section style for an RTF document.
5
+ class SectionStyle < Style
6
+ include SectionFormatting
7
+ include PageFormatting
8
+
9
+ # This is the constructor for the SectionStyle class.
10
+ #
11
+ # @param [Hash] options the section style options.
12
+ # @option options (see Style#initialize)
13
+ # @option options (see SectionFormatting#initialize_section_formatting)
14
+ # @option options (see PageFormatting#initialize_page_formatting)
15
+ def initialize(options = {})
16
+ super(options)
17
+ initialize_section_formatting(options)
18
+ initialize_page_formatting(options)
19
+ end
20
+
21
+ # Converts the stylesheet character style into its RTF representation
22
+ # (for stylesheet)
23
+ def to_rtf(document, options = {})
24
+ # load default options
25
+ options = {
26
+ "uglify" => false,
27
+ "base_indent" => 0
28
+ }.merge(options)
29
+ # build formatting helpers
30
+ base_prefix = options["uglify"] ? '' : ' '*options["base_indent"]
31
+ name_prefix = options["uglify"] ? ' ' : ''
32
+ suffix = options["uglify"] ? '' : ' '
33
+
34
+ rtf = StringIO.new
35
+
36
+ rtf << base_prefix
37
+ rtf << "{\\*\\ds#{handle}#{suffix}"
38
+ rtf << "#{rtf_formatting(document)}#{suffix}"
39
+ rtf << "\\additive#{suffix}" if @additive
40
+ rtf << "\\sbasedon#{@based_on_style_handle}#{suffix}" unless @based_on_style_handle.nil?
41
+ rtf << "\\sautoupd#{suffix}" if @auto_update
42
+ rtf << "\\snext#{@next_style_handle}#{suffix}" unless @next_style_handle.nil?
43
+ rtf << "\\sqformat#{suffix}" if @primary
44
+ rtf << "\\spriority#{@priority}#{suffix}" unless @priority.nil?
45
+ rtf << "\\shidden#{suffix}" if @hidden
46
+ rtf << "#{name_prefix}#{name};}"
47
+
48
+ rtf.string
49
+ end
50
+
51
+ # This method generates a string containing the prefix associated with the
52
+ # style object.
53
+ def prefix(document)
54
+ text = StringIO.new
55
+
56
+ text << "\\ds#{handle} " unless handle.nil?
57
+ text << rtf_formatting
58
+
59
+ text.string
60
+ end
61
+
62
+ def rtf_formatting
63
+ "#{section_formatting_to_rtf} #{page_formatting_to_rtf}"
64
+ end
65
+ end # End of the CharacterStyle class.
66
+ end # module RRTF
@@ -0,0 +1,55 @@
1
+ require 'stringio'
2
+
3
+ module RRTF
4
+ # Represents a set of formatting that can be applied to paragraph and table
5
+ # borders
6
+ class TabStyle < AnonymousStyle
7
+
8
+ TYPE_DICTIONARY = {
9
+ "FLUSH_RIGHT" => 'rqr',
10
+ "CENTERED" => 'tqc',
11
+ "DECIMAL" => 'tqdec'
12
+ }.freeze
13
+
14
+ LEADER_DICTIONARY = {
15
+ "DOT" => 'tldot',
16
+ "MIDDLE_DOT" => 'tlmdot',
17
+ "HYPHEN" => 'tlhyph',
18
+ "UNDERLINE" => 'tlul',
19
+ "THICK_LINE" => 'tlth',
20
+ "EQUAL" => 'tleq'
21
+ }.freeze
22
+
23
+ attr_accessor :type, :leader, :position
24
+
25
+ # This is the constructor for the BorderStyle class.
26
+ #
27
+ # @param [Hash] options the tab style options.
28
+ # @option options (see AnonymousStyle#initialize)
29
+ # @option options [String] "type" (nil) the tab type ("FLUSH_RIGHT", "CENTERED", or "DECIMAL").
30
+ # @option options [String] "leader" (nil) the leader type ("DOT", "MIDDLE_DOT","HYPHEN", "UNDERLINE", "THICK_LINE", or "EQUAL").
31
+ # @option options [String, Integer] "position" (720) the position of the tab stop from the left margin in twips (can be a string, see {Utilities.value2twips}).
32
+ def initialize(options = {})
33
+ super(options)
34
+ @type = TYPE_DICTIONARY[options.delete("type")]
35
+ @leader = LEADER_DICTIONARY[options.delete("leader")]
36
+ @position = Utilities.value2twips(options.delete("position")) || 720
37
+ end
38
+
39
+ # This method generates a string containing the prefix associated with the
40
+ # style object. Equivalent to {#rtf_formatting} for the TabStyle class.
41
+ def prefix(document)
42
+ rtf_formatting
43
+ end
44
+
45
+ def rtf_formatting
46
+ rtf = StringIO.new
47
+
48
+ rtf << "\\#{@type}" unless @type.nil?
49
+ rtf << "\\#{@leader}" unless @leader.nil?
50
+ rtf << "\\tx#{@position}"
51
+
52
+ rtf.string
53
+ end
54
+ end # class TabStyle
55
+ end # module RRTF
@@ -1,3 +1,3 @@
1
1
  module RRTF
2
- VERSION = "1.0.0"
2
+ VERSION = "1.3.1"
3
3
  end
@@ -20,10 +20,10 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.13"
24
- spec.add_development_dependency "rake", "~> 10.0"
25
- spec.add_development_dependency "rspec", "~> 3.0"
26
- spec.add_development_dependency "yard", "~> 0.9"
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "yard"
27
27
  spec.add_development_dependency "byebug"
28
28
  # Required for HTML converter functionality
29
29
  spec.add_dependency "nokogiri"
metadata CHANGED
@@ -1,71 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rrtf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wesley Hileman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-01 00:00:00.000000000 Z
11
+ date: 2020-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.13'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.13'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: yard
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0.9'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0.9'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: byebug
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -188,10 +188,14 @@ files:
188
188
  - documentation/RRTF/PositionStyle.html
189
189
  - documentation/RRTF/Properties.html
190
190
  - documentation/RRTF/RTFError.html
191
+ - documentation/RRTF/SectionFormatting.html
192
+ - documentation/RRTF/SectionNode.html
193
+ - documentation/RRTF/SectionStyle.html
191
194
  - documentation/RRTF/ShadingFormatting.html
192
195
  - documentation/RRTF/ShadingStyle.html
193
196
  - documentation/RRTF/Style.html
194
197
  - documentation/RRTF/Stylesheet.html
198
+ - documentation/RRTF/TabStyle.html
195
199
  - documentation/RRTF/TableCellNode.html
196
200
  - documentation/RRTF/TableNode.html
197
201
  - documentation/RRTF/TableRowNode.html
@@ -231,6 +235,10 @@ files:
231
235
  - examples/09_shapes.rb
232
236
  - examples/10.rtf
233
237
  - examples/10_stylesheet.rb
238
+ - examples/11.rtf
239
+ - examples/11_sections.rb
240
+ - examples/12.rtf
241
+ - examples/12_tabs.rb
234
242
  - examples/resources/images/redshirt.png
235
243
  - examples/resources/images/redshirts.jpg
236
244
  - examples/resources/json/redshirt_styles.json
@@ -257,6 +265,7 @@ files:
257
265
  - lib/rrtf/node/list_text_node.rb
258
266
  - lib/rrtf/node/node.rb
259
267
  - lib/rrtf/node/paragraph_node.rb
268
+ - lib/rrtf/node/section_node.rb
260
269
  - lib/rrtf/node/table_cell_node.rb
261
270
  - lib/rrtf/node/table_node.rb
262
271
  - lib/rrtf/node/table_row_node.rb
@@ -274,8 +283,10 @@ files:
274
283
  - lib/rrtf/style/character_style.rb
275
284
  - lib/rrtf/style/paragraph_style.rb
276
285
  - lib/rrtf/style/position_style.rb
286
+ - lib/rrtf/style/section_style.rb
277
287
  - lib/rrtf/style/shading_style.rb
278
288
  - lib/rrtf/style/style.rb
289
+ - lib/rrtf/style/tab_style.rb
279
290
  - lib/rrtf/stylesheet.rb
280
291
  - lib/rrtf/utilities.rb
281
292
  - lib/rrtf/version.rb
@@ -299,8 +310,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
299
310
  - !ruby/object:Gem::Version
300
311
  version: '0'
301
312
  requirements: []
302
- rubyforge_project:
303
- rubygems_version: 2.5.2
313
+ rubygems_version: 3.0.3
304
314
  signing_key:
305
315
  specification_version: 4
306
316
  summary: A ruby-based rich text format (RTF) document generator.