wiz_rtf 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/wiz_rtf/cell.rb +45 -0
- data/lib/wiz_rtf/cmd.rb +18 -0
- data/lib/wiz_rtf/color.rb +24 -0
- data/lib/wiz_rtf/document.rb +84 -0
- data/lib/wiz_rtf/font.rb +31 -0
- data/lib/wiz_rtf/image.rb +89 -0
- data/lib/wiz_rtf/row.rb +75 -0
- data/lib/wiz_rtf/rtf_io.rb +42 -0
- data/lib/wiz_rtf/table.rb +33 -0
- data/lib/wiz_rtf/text.rb +28 -0
- data/lib/wiz_rtf/version.rb +3 -0
- data/lib/wiz_rtf.rb +15 -0
- data/wiz_rtf.gemspec +32 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 59f873180f1945187a00d6270f8cf970040d2bbd
|
4
|
+
data.tar.gz: aee4737b8f3339ed9cebdb4f5adaad9e77de7dca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4391c7456a972ada2a975514a10c4fe93ca86f39e34b405418e7c0c70db0c8d1b9f000a5a5b662bdd23cfde33783cae0c94895c7751430c593e4005148f38589
|
7
|
+
data.tar.gz: baaf60c8f954792771d4e46e3b95f15736a5cf725d46b29ca3efda65221d72866e4ef3e0c2b558121e4b9af92255ec19370f734a2b0e6e1c9f6daf4b78b5ceee
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 songgz
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# WizRtf
|
2
|
+
|
3
|
+
A gem for exporting Word Documents in ruby using the Microsoft Rich Text Format (RTF) Specification.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'wiz_rtf'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install wiz_rtf
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
```ruby
|
23
|
+
doc = WizRtf::Document.new do
|
24
|
+
text "学生综合素质报告", :align => :center, 'font-size' => 48
|
25
|
+
image('h:\eahey.png')
|
26
|
+
page_break
|
27
|
+
text "A Table Demo"
|
28
|
+
table [[{content:'e',rowspan:4},{content:'4',rowspan:4},1,{content:'1',colspan:2}],
|
29
|
+
[{content:'4',rowspan:3,colspan:2},8],[11]], column_widths:{1=>100,2 => 100,3 => 50,4 => 50,5 => 50} do
|
30
|
+
add_row [1]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
doc.save('c:\text.rtf')
|
34
|
+
```
|
35
|
+
## Development
|
36
|
+
|
37
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
38
|
+
|
39
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it ( https://github.com/[my-github-username]/wiz_rtf/fork )
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "wiz_rtf"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/wiz_rtf/cell.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
class Cell
|
9
|
+
attr_accessor :colspan, :rowspan, :content, :v_merge, :right_width
|
10
|
+
|
11
|
+
def initialize(cell)
|
12
|
+
unless cell.is_a?(Hash)
|
13
|
+
@colspan = 1
|
14
|
+
@rowspan = 1
|
15
|
+
@content = cell
|
16
|
+
else
|
17
|
+
@colspan = cell[:colspan] || 1
|
18
|
+
@rowspan = cell[:rowspan] || 1
|
19
|
+
@content = cell[:content] || ''
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def render(io)
|
24
|
+
io.cmd :celld
|
25
|
+
io.cmd :clbrdrt
|
26
|
+
io.cmd :brdrs
|
27
|
+
io.cmd :brdrw10
|
28
|
+
io.cmd :clbrdrl
|
29
|
+
io.cmd :brdrs
|
30
|
+
io.cmd :brdrw10
|
31
|
+
io.cmd :clbrdrb
|
32
|
+
io.cmd :brdrs
|
33
|
+
io.cmd :brdrw10
|
34
|
+
io.cmd :clbrdrr
|
35
|
+
io.cmd :brdrs
|
36
|
+
io.cmd :brdrw10
|
37
|
+
io.cmd v_merge if v_merge
|
38
|
+
io.cmd :cellx, right_width
|
39
|
+
io.txt content
|
40
|
+
io.cmd :cell
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
data/lib/wiz_rtf/cmd.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
class Cmd
|
9
|
+
def initialize(name, value = nil)
|
10
|
+
@name = name
|
11
|
+
@value = value
|
12
|
+
end
|
13
|
+
|
14
|
+
def render(io)
|
15
|
+
io.cmd @name, @value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
class Color
|
9
|
+
|
10
|
+
def initialize(red, green, blue)
|
11
|
+
@red = red
|
12
|
+
@green = green
|
13
|
+
@blue = blue
|
14
|
+
end
|
15
|
+
|
16
|
+
def render(io)
|
17
|
+
io.delimit do
|
18
|
+
io.cmd :red, @red
|
19
|
+
io.cmd :green, @green
|
20
|
+
io.cmd :blue, @blue
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
class Document
|
9
|
+
def initialize(&block)
|
10
|
+
@fonts = []
|
11
|
+
@colors = []
|
12
|
+
@parts = []
|
13
|
+
font 0, 'fswiss', 'Arial', 0, 2
|
14
|
+
font 1, 'fmodern', 'Courier New', 0, 1
|
15
|
+
font 2, 'fnil', '宋体', 2, 2
|
16
|
+
color 0, 0, 0
|
17
|
+
color 255, 0, 0
|
18
|
+
block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
|
19
|
+
end
|
20
|
+
|
21
|
+
def head
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def font(num, family, name, character_set = 0, prq = 2)
|
26
|
+
@fonts << WizRtf::Font.new(num, family, name, character_set, prq)
|
27
|
+
end
|
28
|
+
|
29
|
+
def color(red, green, blue)
|
30
|
+
@colors << WizRtf::Color.new(red, green, blue)
|
31
|
+
end
|
32
|
+
|
33
|
+
def text(str, styles = {:align => :left})
|
34
|
+
@parts << WizRtf::Text.new(str, styles)
|
35
|
+
end
|
36
|
+
|
37
|
+
def image(file)
|
38
|
+
@parts << WizRtf::Image.new(file)
|
39
|
+
end
|
40
|
+
|
41
|
+
def table(rows = [],options = {}, &block)
|
42
|
+
@parts << WizRtf::Table.new(rows, options, &block)
|
43
|
+
end
|
44
|
+
|
45
|
+
def line_break
|
46
|
+
@parts << WizRtf::Cmd.new(:par)
|
47
|
+
end
|
48
|
+
|
49
|
+
#writes a page interruption (new page)
|
50
|
+
def page_break
|
51
|
+
@parts << WizRtf::Cmd.new(:page)
|
52
|
+
end
|
53
|
+
|
54
|
+
def render(io)
|
55
|
+
io.group do
|
56
|
+
io.cmd :rtf, 1
|
57
|
+
io.cmd :ansi
|
58
|
+
io.cmd :ansicpg, 2052
|
59
|
+
io.cmd :deff, 0
|
60
|
+
io.group do
|
61
|
+
io.cmd :fonttbl
|
62
|
+
@fonts.each do |font|
|
63
|
+
font.render(io)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
io.group do
|
67
|
+
io.cmd :colortbl
|
68
|
+
io.delimit
|
69
|
+
@colors.each do |color|
|
70
|
+
color.render(io)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
@parts.each do |part|
|
74
|
+
part.render(io)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def save(file)
|
80
|
+
File.open(file, 'w') { |file| render(WizRtf::RtfIO.new(file)) }
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
data/lib/wiz_rtf/font.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
class Font
|
9
|
+
def initialize(num, family, name, character_set = 0, prq = 2)
|
10
|
+
@num = num
|
11
|
+
@family = family
|
12
|
+
@name = name
|
13
|
+
@character_set = character_set
|
14
|
+
@prq = prq
|
15
|
+
end
|
16
|
+
|
17
|
+
def render(io)
|
18
|
+
io.group do
|
19
|
+
io.delimit do
|
20
|
+
io.cmd :f, @num
|
21
|
+
io.cmd @family
|
22
|
+
io.cmd :fprq, @prq
|
23
|
+
io.cmd :fcharset, @character_set
|
24
|
+
io.write ' '
|
25
|
+
io.write @name
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
class Image
|
9
|
+
JPEG_SOF_BLOCKS = [0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF]
|
10
|
+
PIC_TYPE = {png: :pngblip, jpg: :jpegblip, bmp: :pngblip, gif: :pngblip}
|
11
|
+
|
12
|
+
def initialize(file)
|
13
|
+
begin
|
14
|
+
@img = IO.binread(file)
|
15
|
+
@width, @height = self.size
|
16
|
+
rescue Exception => e
|
17
|
+
STDERR.puts "** error parsing #{file}: #{e.inspect}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def type
|
22
|
+
png = Regexp.new("\x89PNG".force_encoding("binary"))
|
23
|
+
jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
|
24
|
+
jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
|
25
|
+
|
26
|
+
@type = case @img
|
27
|
+
when /^GIF8/
|
28
|
+
:gif
|
29
|
+
when /^#{png}/
|
30
|
+
:png
|
31
|
+
when /^#{jpg}/
|
32
|
+
:jpg
|
33
|
+
when /^#{jpg2}/
|
34
|
+
:jpg
|
35
|
+
when /^BM/
|
36
|
+
:bmp
|
37
|
+
else
|
38
|
+
:unknown
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def size
|
43
|
+
case self.type
|
44
|
+
when :gif
|
45
|
+
@img[6..10].unpack('SS')
|
46
|
+
when :png
|
47
|
+
@img[16..24].unpack('NN')
|
48
|
+
when :bmp
|
49
|
+
d = @img[14..28]
|
50
|
+
d.unpack('C')[0] == 40 ? d[4..-1].unpack('LL') : d[4..8].unpack('SS')
|
51
|
+
when :jpg
|
52
|
+
d = StringIO.new(@img)
|
53
|
+
section_marker = 0xff # Section marker.
|
54
|
+
d.seek(2) # Skip the first two bytes of JPEG identifier.
|
55
|
+
loop do
|
56
|
+
marker, code, length = d.read(4).unpack('CCn')
|
57
|
+
fail "JPEG marker not found!" if marker != section_marker
|
58
|
+
if JPEG_SOF_BLOCKS.include?(code)
|
59
|
+
#@bits, @height, @width, @channels = d.read(6).unpack("CnnC")
|
60
|
+
return d.read(6).unpack('CnnC')[1..2].reverse
|
61
|
+
end
|
62
|
+
d.seek(length - 2, IO::SEEK_CUR)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def render(io)
|
68
|
+
io.group do
|
69
|
+
io.cmd '*'
|
70
|
+
io.cmd 'shppict'
|
71
|
+
io.group do
|
72
|
+
io.cmd :pict
|
73
|
+
io.cmd PIC_TYPE[@type]
|
74
|
+
io.cmd :picscalex, 99
|
75
|
+
io.cmd :picscaley, 99
|
76
|
+
io.cmd :picw, @width
|
77
|
+
io.cmd :pich, @height
|
78
|
+
io.write "\n"
|
79
|
+
@img.each_byte do |b|
|
80
|
+
hex_str = b.to_s(16)
|
81
|
+
hex_str.insert(0,'0') if hex_str.length == 1
|
82
|
+
io.write hex_str
|
83
|
+
end
|
84
|
+
io.write "\n"
|
85
|
+
end
|
86
|
+
end if @img
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/lib/wiz_rtf/row.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
class Row
|
9
|
+
def initialize(table, cells = [])
|
10
|
+
@table = table
|
11
|
+
@cells = []
|
12
|
+
@col_offset = 1
|
13
|
+
@right_width = 0
|
14
|
+
cells.each do |cell|
|
15
|
+
add_cell cell
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_cell(cell, merge = false)
|
20
|
+
add_cell('', true) if !merge && row_spanned?(@col_offset)
|
21
|
+
|
22
|
+
c = WizRtf::Cell.new(cell)
|
23
|
+
@table.row_spans[@col_offset] = c if c.rowspan > 1
|
24
|
+
|
25
|
+
if c.rowspan > 1
|
26
|
+
c.v_merge = :clvmgf
|
27
|
+
elsif row_spanned? @col_offset
|
28
|
+
c.v_merge = :clvmrg
|
29
|
+
@table.row_spans[@col_offset].rowspan -= 1
|
30
|
+
c.colspan = @table.row_spans[@col_offset].colspan || c.colspan
|
31
|
+
end
|
32
|
+
c.colspan.times do
|
33
|
+
@right_width += column_width(@col_offset)
|
34
|
+
@col_offset += 1
|
35
|
+
end
|
36
|
+
c.right_width = @right_width
|
37
|
+
|
38
|
+
@cells << c
|
39
|
+
|
40
|
+
add_cell('', true) if row_spanned?(@col_offset)
|
41
|
+
end
|
42
|
+
|
43
|
+
def row_spanned?(offset)
|
44
|
+
@table.row_spans[offset] && @table.row_spans[offset].rowspan > 1
|
45
|
+
end
|
46
|
+
|
47
|
+
def column_width(offset)
|
48
|
+
return 20 * (@table.column_widths[offset] || WizRtf::Table::DEFAULT_COLUMN_WIDTH) if @table.column_widths.is_a?(Hash)
|
49
|
+
@table.column_widths * 20
|
50
|
+
end
|
51
|
+
|
52
|
+
def render(io)
|
53
|
+
io.cmd :trowd
|
54
|
+
io.cmd :trbrdrt
|
55
|
+
io.cmd :brdrs
|
56
|
+
io.cmd :brdrw10
|
57
|
+
io.cmd :trbrdrl
|
58
|
+
io.cmd :brdrs
|
59
|
+
io.cmd :brdrw10
|
60
|
+
io.cmd :trbrdrb
|
61
|
+
io.cmd :brdrs
|
62
|
+
io.cmd :brdrw10
|
63
|
+
io.cmd :trbrdrr
|
64
|
+
io.cmd :brdrs
|
65
|
+
io.cmd :brdrw10
|
66
|
+
io.cmd :trautofit1
|
67
|
+
io.cmd :intbl
|
68
|
+
@cells.each do |c|
|
69
|
+
c.render(io)
|
70
|
+
end
|
71
|
+
io.cmd :row
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
class RtfIO
|
9
|
+
def initialize(io = nil)
|
10
|
+
@io = io || StringIO.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def write(txt)
|
14
|
+
@io.write txt
|
15
|
+
end
|
16
|
+
|
17
|
+
def cmd(name, value = nil)
|
18
|
+
@io.write '\\'
|
19
|
+
@io.write name
|
20
|
+
@io.write value if value
|
21
|
+
end
|
22
|
+
|
23
|
+
def txt(str)
|
24
|
+
str = str.to_s
|
25
|
+
str = str.gsub("{", "\\{").gsub("}", "\\}").gsub("\\", "\\\\")
|
26
|
+
str = str.encode("UTF-16LE", :undef=>:replace).each_codepoint.map {|n| n < 128 ? n.chr : "\\u#{n}\\'3f"}.join('')
|
27
|
+
@io.write ' '
|
28
|
+
@io.write str
|
29
|
+
end
|
30
|
+
|
31
|
+
def group
|
32
|
+
@io.write '{'
|
33
|
+
yield if block_given?
|
34
|
+
@io.write '}'
|
35
|
+
end
|
36
|
+
|
37
|
+
def delimit
|
38
|
+
yield if block_given?
|
39
|
+
@io.write ';'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
class Table
|
9
|
+
DEFAULT_COLUMN_WIDTH = 40
|
10
|
+
attr_accessor :row_spans, :column_widths
|
11
|
+
|
12
|
+
def initialize(rows = [], options = {}, &block)
|
13
|
+
@rows = []
|
14
|
+
@row_spans = {}
|
15
|
+
@column_widths = options[:column_widths] || DEFAULT_COLUMN_WIDTH
|
16
|
+
rows.each_index do |index|
|
17
|
+
add_row rows[index]
|
18
|
+
end
|
19
|
+
block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_row(cells = [])
|
23
|
+
@rows << WizRtf::Row.new(self, cells)
|
24
|
+
end
|
25
|
+
|
26
|
+
def render(io)
|
27
|
+
@rows.each do |row|
|
28
|
+
row.render(io)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/lib/wiz_rtf/text.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
class Text
|
9
|
+
ALIGN_MAP = {left:'ql',center:'qc',right:'qr'}
|
10
|
+
FONT_MAP = {'font-size' => :fs}
|
11
|
+
|
12
|
+
def initialize(str = '', styles = {})
|
13
|
+
@str = str
|
14
|
+
@styles = {:align => :left,'font-size' => 24}.merge(styles)
|
15
|
+
end
|
16
|
+
|
17
|
+
def render(io)
|
18
|
+
io.group do
|
19
|
+
io.cmd :pard
|
20
|
+
io.cmd ALIGN_MAP[@styles[:align]]
|
21
|
+
io.cmd FONT_MAP[@styles['font-size']]
|
22
|
+
io.txt @str
|
23
|
+
io.cmd :par
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
data/lib/wiz_rtf.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "wiz_rtf/version"
|
2
|
+
require "wiz_rtf/font"
|
3
|
+
require "wiz_rtf/color"
|
4
|
+
require "wiz_rtf/cmd"
|
5
|
+
require "wiz_rtf/text"
|
6
|
+
require "wiz_rtf/image"
|
7
|
+
require "wiz_rtf/cell"
|
8
|
+
require "wiz_rtf/row"
|
9
|
+
require "wiz_rtf/table"
|
10
|
+
require "wiz_rtf/rtf_io"
|
11
|
+
require "wiz_rtf/document"
|
12
|
+
|
13
|
+
module WizRtf
|
14
|
+
# Your code goes here...
|
15
|
+
end
|
data/wiz_rtf.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'wiz_rtf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "wiz_rtf"
|
8
|
+
spec.version = WizRtf::VERSION
|
9
|
+
spec.authors = ["songgz"]
|
10
|
+
spec.email = ["sgzhe@163.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A gem for exporting Word Documents in ruby using the Microsoft Rich Text Format (RTF) Specification.}
|
13
|
+
spec.description = %q{A gem for rtf.}
|
14
|
+
spec.homepage = "https://github.com/songgz/wiz_rtf"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wiz_rtf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- songgz
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: A gem for rtf.
|
42
|
+
email:
|
43
|
+
- sgzhe@163.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".travis.yml"
|
51
|
+
- CODE_OF_CONDUCT.md
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- bin/console
|
57
|
+
- bin/setup
|
58
|
+
- lib/wiz_rtf.rb
|
59
|
+
- lib/wiz_rtf/cell.rb
|
60
|
+
- lib/wiz_rtf/cmd.rb
|
61
|
+
- lib/wiz_rtf/color.rb
|
62
|
+
- lib/wiz_rtf/document.rb
|
63
|
+
- lib/wiz_rtf/font.rb
|
64
|
+
- lib/wiz_rtf/image.rb
|
65
|
+
- lib/wiz_rtf/row.rb
|
66
|
+
- lib/wiz_rtf/rtf_io.rb
|
67
|
+
- lib/wiz_rtf/table.rb
|
68
|
+
- lib/wiz_rtf/text.rb
|
69
|
+
- lib/wiz_rtf/version.rb
|
70
|
+
- wiz_rtf.gemspec
|
71
|
+
homepage: https://github.com/songgz/wiz_rtf
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata:
|
75
|
+
allowed_push_host: https://rubygems.org
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: A gem for exporting Word Documents in ruby using the Microsoft Rich Text
|
96
|
+
Format (RTF) Specification.
|
97
|
+
test_files: []
|