wizport 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/wizport/document/Rtf/document.rb +21 -40
- data/lib/wizport/document/Rtf/element.rb +23 -1
- data/lib/wizport/document/Rtf/table.rb +58 -20
- data/lib/wizport/document/Rtf/text.rb +10 -25
- data/lib/wizport/version.rb +1 -1
- data/spec/wizport/document/rtf/document_spec.rb +8 -6
- metadata +2 -8
- data/lib/wizport/document/Rtf/cell.rb +0 -64
- data/lib/wizport/document/Rtf/command.rb +0 -22
- data/lib/wizport/document/Rtf/group.rb +0 -17
- data/lib/wizport/document/Rtf/plaintext.rb +0 -19
- data/lib/wizport/document/Rtf/row.rb +0 -74
- data/lib/wizport/document/Rtf/rtf_builder.rb +0 -40
@@ -3,51 +3,40 @@
|
|
3
3
|
# Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
|
4
4
|
# Copyright 2012 by sgzhe@163.com
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
require "stringio"
|
8
7
|
module Wizport
|
9
8
|
module Rtf
|
10
|
-
class Document <
|
9
|
+
class Document < Element
|
11
10
|
def initialize(file = nil, &block)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
@rtf = StringIO.new
|
12
|
+
group do
|
13
|
+
cmd :rtf, 1
|
14
|
+
cmd :ansi
|
15
|
+
cmd :ansicpg, 2052
|
16
|
+
block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
|
17
|
+
end
|
17
18
|
save file if file
|
18
19
|
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
elements << Text.new(txt,styles)
|
23
|
-
end
|
24
|
-
|
25
|
-
def table(rows = [],&block)
|
26
|
-
elements << Table.new(rows, &block)
|
21
|
+
def write(txt)
|
22
|
+
@rtf << txt
|
27
23
|
end
|
28
24
|
|
29
|
-
def
|
30
|
-
|
31
|
-
|
25
|
+
def text(str, styles = {:align => :left})
|
26
|
+
Wizport::Rtf::Text.new(self, str, styles)
|
32
27
|
end
|
33
28
|
|
34
|
-
def
|
35
|
-
|
29
|
+
def table(rows = [], &block)
|
30
|
+
Wizport::Rtf::Table.new(self, rows, &block)
|
36
31
|
end
|
37
32
|
|
38
33
|
|
39
|
-
def method_missing(id, *args, &block)
|
40
|
-
cmd id, *args, &block
|
41
|
-
end
|
42
|
-
|
43
34
|
def to_s
|
44
35
|
|
45
36
|
end
|
46
37
|
|
47
38
|
def to_rtf
|
48
|
-
|
49
|
-
accept(builder)
|
50
|
-
builder.to_rtf
|
39
|
+
@rtf.string
|
51
40
|
end
|
52
41
|
|
53
42
|
def save(file)
|
@@ -59,18 +48,10 @@ module Wizport
|
|
59
48
|
end
|
60
49
|
|
61
50
|
if __FILE__ == $0
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
row ['f','s','d','d']
|
68
|
-
end
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
#p d.to_rtf
|
73
|
-
|
74
|
-
|
51
|
+
#rtf = Wizport::Rtf::Document.new do
|
52
|
+
# text "学生综合素质评价"
|
53
|
+
# table [[1, {content :'1', colspan: 2}], [3, {content :'4', rowspan: 2, colspan: 2}], [1]]
|
54
|
+
#end
|
55
|
+
#rtf.save('c:/r.rtf')
|
75
56
|
end
|
76
57
|
|
@@ -6,7 +6,29 @@
|
|
6
6
|
module Wizport
|
7
7
|
module Rtf
|
8
8
|
class Element
|
9
|
-
|
9
|
+
def initialize(rtf)
|
10
|
+
@rtf = rtf
|
11
|
+
end
|
12
|
+
|
13
|
+
def cmd(name, value = nil)
|
14
|
+
@rtf.write '\\'
|
15
|
+
@rtf.write name
|
16
|
+
@rtf.write value if value
|
17
|
+
end
|
18
|
+
|
19
|
+
def txt(str)
|
20
|
+
str = str.to_s
|
21
|
+
str = str.gsub("{", "\\{").gsub("}", "\\}").gsub("\\", "\\\\")
|
22
|
+
str = str.encode("UTF-16LE", :undef=>:replace).each_codepoint.map {|n| n < 128 ? n.chr : "\\u#{n}\\'3f"}.join('')
|
23
|
+
@rtf.write ' '
|
24
|
+
@rtf.write str
|
25
|
+
end
|
26
|
+
|
27
|
+
def group
|
28
|
+
@rtf.write '{'
|
29
|
+
yield if block_given?
|
30
|
+
@rtf.write '}'
|
31
|
+
end
|
10
32
|
end
|
11
33
|
end
|
12
34
|
end
|
@@ -5,33 +5,71 @@
|
|
5
5
|
|
6
6
|
module Wizport
|
7
7
|
module Rtf
|
8
|
-
class Table <
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
#@cells = {}
|
16
|
-
rows.each do |cells|
|
17
|
-
row cells
|
8
|
+
class Table < Element
|
9
|
+
|
10
|
+
def initialize(rtf, rows = [], options = {}, &block)
|
11
|
+
super(rtf)
|
12
|
+
@row_spans = {}
|
13
|
+
rows.each_index do |index|
|
14
|
+
add_row rows[index]
|
18
15
|
end
|
19
16
|
block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
|
20
17
|
end
|
21
18
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
19
|
+
def add_row(cells = [])
|
20
|
+
cmd :trowd
|
21
|
+
cmd :trautofit1
|
22
|
+
cmd :intbl
|
23
|
+
@col_offset = 1
|
24
|
+
cells.each do |cell|
|
25
|
+
if row_spanned? @col_offset
|
26
|
+
add_cell ""
|
27
|
+
add_cell cell
|
28
|
+
elsif row_spanned? @col_offset + 1
|
29
|
+
add_cell cell
|
30
|
+
add_cell ""
|
31
|
+
else
|
32
|
+
add_cell cell
|
33
|
+
end
|
34
|
+
end
|
35
|
+
cmd :row
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_cell(cell)
|
39
|
+
if cell.is_a?(Hash)
|
40
|
+
rowspan = cell[:rowspan]
|
41
|
+
colspan = cell[:colspan]
|
42
|
+
content = cell[:content]
|
43
|
+
@row_spans[@col_offset] = cell if cell[:rowspan]
|
44
|
+
end
|
45
|
+
colspan = colspan || 1
|
46
|
+
rowspan = rowspan || 1
|
47
|
+
content = content || cell
|
48
|
+
|
49
|
+
cmd :celld
|
50
|
+
if rowspan > 1
|
51
|
+
cmd :clvmgf
|
52
|
+
@col_offset += colspan
|
53
|
+
elsif row_spanned? @col_offset
|
54
|
+
cmd :clvmrg
|
55
|
+
@row_spans[@col_offset][:rowspan] -= 1
|
56
|
+
@col_offset += row_spans_with_colspan
|
57
|
+
else
|
58
|
+
@col_offset += colspan
|
59
|
+
end
|
60
|
+
cmd :cellx, (@col_offset - 1)* 2000
|
61
|
+
txt content
|
62
|
+
cmd :cell
|
63
|
+
end
|
64
|
+
|
65
|
+
def row_spanned?(offset)
|
66
|
+
@row_spans[offset] && @row_spans[offset][:rowspan].to_i > 1
|
67
|
+
end
|
25
68
|
|
69
|
+
def row_spans_with_colspan
|
70
|
+
return @row_spans[@col_offset][:colspan] || 1
|
26
71
|
end
|
27
72
|
|
28
|
-
#def [](r,c)
|
29
|
-
# @cells["#{r},#{c}"]
|
30
|
-
#end
|
31
|
-
#
|
32
|
-
#def [](r,c,v)
|
33
|
-
# @cells["#{r},#{c}"] = v
|
34
|
-
#end
|
35
73
|
end
|
36
74
|
end
|
37
75
|
end
|
@@ -5,35 +5,20 @@
|
|
5
5
|
|
6
6
|
module Wizport
|
7
7
|
module Rtf
|
8
|
-
class Text <
|
8
|
+
class Text < Element
|
9
9
|
ALIGN_MAP = {left:'ql',center:'qc'}
|
10
10
|
|
11
|
-
def initialize(
|
12
|
-
super()
|
11
|
+
def initialize(rtf, str = '', styles = {})
|
12
|
+
super(rtf)
|
13
13
|
styles = {:align => :left,:size => 24}.merge(styles)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
group do
|
15
|
+
cmd :part
|
16
|
+
cmd ALIGN_MAP[styles[:align]]
|
17
|
+
cmd :fs,styles[:size]
|
18
|
+
txt str
|
19
|
+
cmd :par
|
20
|
+
end
|
19
21
|
end
|
20
|
-
|
21
|
-
def size=(value)
|
22
|
-
@size.value = value
|
23
|
-
end
|
24
|
-
|
25
|
-
def size
|
26
|
-
@size.value
|
27
|
-
end
|
28
|
-
|
29
|
-
def align=(value)
|
30
|
-
@align.name = ALIGN_MAP[styles[value]]
|
31
|
-
end
|
32
|
-
|
33
|
-
def align
|
34
|
-
ALIGN_MAP.invert[@align.name]
|
35
|
-
end
|
36
|
-
|
37
22
|
end
|
38
23
|
end
|
39
24
|
end
|
data/lib/wizport/version.rb
CHANGED
@@ -5,12 +5,14 @@ require "../../../../spec/spec_helper"
|
|
5
5
|
describe Wizport::Rtf::Document do
|
6
6
|
|
7
7
|
it "a simple example of Rtf" do
|
8
|
-
rtf = Wizport::Rtf::Document.new
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
rtf = Wizport::Rtf::Document.new do
|
9
|
+
text "学生综合素质评价"
|
10
|
+
table [[1,{content:'1',colspan:2}],[3,{content:'4',rowspan:2,colspan:2}],[11]] do
|
11
|
+
add_row [1,1,1]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
14
15
|
rtf.save('c:/r.rtf')
|
16
|
+
|
15
17
|
end
|
16
18
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wizport
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -56,14 +56,8 @@ files:
|
|
56
56
|
- lib/wizport.rb
|
57
57
|
- lib/wizport/builder/pdf.rb
|
58
58
|
- lib/wizport/builder/rtf.rb
|
59
|
-
- lib/wizport/document/Rtf/cell.rb
|
60
|
-
- lib/wizport/document/Rtf/command.rb
|
61
59
|
- lib/wizport/document/Rtf/document.rb
|
62
60
|
- lib/wizport/document/Rtf/element.rb
|
63
|
-
- lib/wizport/document/Rtf/group.rb
|
64
|
-
- lib/wizport/document/Rtf/plaintext.rb
|
65
|
-
- lib/wizport/document/Rtf/row.rb
|
66
|
-
- lib/wizport/document/Rtf/rtf_builder.rb
|
67
61
|
- lib/wizport/document/Rtf/spec.rb
|
68
62
|
- lib/wizport/document/Rtf/table.rb
|
69
63
|
- lib/wizport/document/Rtf/text.rb
|
@@ -1,64 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
|
4
|
-
# Copyright 2012 by sgzhe@163.com
|
5
|
-
|
6
|
-
module Wizport
|
7
|
-
module Rtf
|
8
|
-
class Cell
|
9
|
-
attr_accessor :colspan, :rowspan
|
10
|
-
|
11
|
-
def initialize(data, row = nil)
|
12
|
-
@row = row
|
13
|
-
@index = @row.cells.size
|
14
|
-
@colspan = 1
|
15
|
-
@rowspan = 1
|
16
|
-
@content = data
|
17
|
-
#:content => "2x1", :colspan
|
18
|
-
if data.is_a?(Hash)
|
19
|
-
@colspan = data[:colspan] if data[:colspan]
|
20
|
-
@rowspan = data[:rowspan] if data[:rowspan]
|
21
|
-
@content = data[:content]
|
22
|
-
end
|
23
|
-
|
24
|
-
#unless col_spanned?
|
25
|
-
@row.table.elements << Command.new(:celld)
|
26
|
-
@row.table.elements << Command.new(:clvmgf) if @rowspan > 1
|
27
|
-
@row.table.elements << Command.new(:clvmrg) if row_spanned?
|
28
|
-
@row.table.elements << Command.new(:cellx, (col_spanned)*1000)
|
29
|
-
@row.table.elements << Plaintext.new(@content.to_s)
|
30
|
-
@row.table.elements << Command.new(:cell)
|
31
|
-
#end
|
32
|
-
|
33
|
-
#@row.cellx_command Command.new(:clvmgf) if @row_span > 1
|
34
|
-
#@row.cellx_command Command.new(:clvmrg) if row_spanned?
|
35
|
-
#@row.cellx_command Command.new(:cellx, (@index+@col_span)*1000)
|
36
|
-
#@row.cellt_command Plaintext.new(@text)
|
37
|
-
#@row.cellt_command Command.new(:cell)
|
38
|
-
end
|
39
|
-
|
40
|
-
def col_spanned?
|
41
|
-
col_spanned >= (@index + 1) && colspan == 1
|
42
|
-
end
|
43
|
-
|
44
|
-
def col_spanned
|
45
|
-
cols = 0
|
46
|
-
(@index).times do |c|
|
47
|
-
cols += @row.cells[c].colspan
|
48
|
-
end
|
49
|
-
cols += colspan
|
50
|
-
end
|
51
|
-
|
52
|
-
|
53
|
-
def row_spanned?
|
54
|
-
@row.row_spanned?(col_spanned - colspan)
|
55
|
-
#@row.index.times do |r|
|
56
|
-
# return true if @row.table.rows[r].cells[@index] && @row.table.rows[r].cells[@index].rowspan + r > @row.index
|
57
|
-
#end
|
58
|
-
#return false
|
59
|
-
end
|
60
|
-
|
61
|
-
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
|
4
|
-
# Copyright 2012 by sgzhe@163.com
|
5
|
-
|
6
|
-
module Wizport
|
7
|
-
module Rtf
|
8
|
-
class Command < Element
|
9
|
-
attr_accessor :name,:value
|
10
|
-
|
11
|
-
def initialize(name,value = nil)
|
12
|
-
@name = name
|
13
|
-
@value = value
|
14
|
-
end
|
15
|
-
|
16
|
-
def ctrl
|
17
|
-
@name.to_s + @value.to_s
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
|
4
|
-
# Copyright 2012 by sgzhe@163.com
|
5
|
-
|
6
|
-
|
7
|
-
module Wizport
|
8
|
-
module Rtf
|
9
|
-
class Group < Element
|
10
|
-
attr_accessor :elements
|
11
|
-
def initialize()
|
12
|
-
@elements = []
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
|
4
|
-
# Copyright 2012 by sgzhe@163.com
|
5
|
-
|
6
|
-
module Wizport
|
7
|
-
module Rtf
|
8
|
-
class Plaintext < Element
|
9
|
-
attr_accessor :txt
|
10
|
-
|
11
|
-
def initialize(txt)
|
12
|
-
txt = txt.gsub("{", "\\{").gsub("}", "\\}").gsub("\\", "\\\\")
|
13
|
-
txt = txt.encode("UTF-16LE", :undef=>:replace).each_codepoint.map {|n| n < 128 ? n.chr : "\\u#{n}\\'3f"}.join('')
|
14
|
-
@txt = txt
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,74 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
|
4
|
-
# Copyright 2012 by sgzhe@163.com
|
5
|
-
|
6
|
-
module Wizport
|
7
|
-
module Rtf
|
8
|
-
class Row
|
9
|
-
attr_accessor :table, :cells, :index
|
10
|
-
def initialize(columns,tbl = nil)
|
11
|
-
@table = tbl
|
12
|
-
@cells = []
|
13
|
-
@index = @table.rows.size
|
14
|
-
|
15
|
-
@table.elements << Command.new(:trowd)
|
16
|
-
@table.elements << Command.new(:trautofit1)
|
17
|
-
@table.elements << Command.new(:intbl)
|
18
|
-
columns.each_index do |i|
|
19
|
-
@cellx_index = nil
|
20
|
-
@cellt_index = nil
|
21
|
-
temp = 0
|
22
|
-
if columns[i].is_a?(Hash)
|
23
|
-
temp += columns[i][:colspan].to_i
|
24
|
-
else
|
25
|
-
temp += 1
|
26
|
-
end
|
27
|
-
|
28
|
-
if row_spanned?(temp)
|
29
|
-
@cells << Cell.new("",self)
|
30
|
-
end
|
31
|
-
@cells << Cell.new(columns[i],self)
|
32
|
-
#if columns[i].is_a?(Hash)
|
33
|
-
# if columns[i][:colspan] && columns[i][:colspan] > 1
|
34
|
-
# (columns[i][:colspan]-1).times do
|
35
|
-
# @cells << Cell.new("",self)
|
36
|
-
# end
|
37
|
-
# end
|
38
|
-
#end
|
39
|
-
end
|
40
|
-
@table.elements << Command.new(:row)
|
41
|
-
end
|
42
|
-
|
43
|
-
def row_spanned?(c_i)
|
44
|
-
rs = 0
|
45
|
-
|
46
|
-
(@index).times do |r|
|
47
|
-
t = 0
|
48
|
-
@table.rows[r].cells.each do |c|
|
49
|
-
t += c.colspan
|
50
|
-
if t == c_i + 1
|
51
|
-
rs += c.rowspan
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
#rs += @table.rows[r].cells[c_i].rowspan if @table.rows[r].cells[c_i] && @table.rows[r].cells[c_i].rowspan > 1
|
56
|
-
end
|
57
|
-
rs >= (@index + 1)
|
58
|
-
end
|
59
|
-
|
60
|
-
def cellx_command(cmd)
|
61
|
-
@cellx_index = @cellx_index || @table.elements.size
|
62
|
-
@table.elements.insert @cellx_index, cmd
|
63
|
-
@cellx_index += 1
|
64
|
-
end
|
65
|
-
|
66
|
-
def cellt_command(cmd)
|
67
|
-
@cellt_index = @cellt_index || @table.elements.size
|
68
|
-
@table.elements.insert @cellt_index, cmd
|
69
|
-
@cellt_index += 1
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
|
4
|
-
# Copyright 2012 by sgzhe@163.com
|
5
|
-
|
6
|
-
require "stringio"
|
7
|
-
module Wizport
|
8
|
-
module Rtf
|
9
|
-
class RtfBuilder
|
10
|
-
include Wizport::Visitor
|
11
|
-
|
12
|
-
def initialize
|
13
|
-
@rtf = StringIO.new
|
14
|
-
end
|
15
|
-
|
16
|
-
visit Group do |element|
|
17
|
-
@rtf << '{'
|
18
|
-
element.elements.each do |el|
|
19
|
-
el.accept(self)
|
20
|
-
end if element.is_a?(Wizport::Rtf::Group)
|
21
|
-
@rtf << '}'
|
22
|
-
end
|
23
|
-
|
24
|
-
visit Command do |element|
|
25
|
-
@rtf << '\\'
|
26
|
-
@rtf << element.ctrl
|
27
|
-
end
|
28
|
-
|
29
|
-
visit Plaintext do |element|
|
30
|
-
@rtf << ' '
|
31
|
-
@rtf << element.txt
|
32
|
-
end
|
33
|
-
|
34
|
-
def to_rtf
|
35
|
-
@rtf.string
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|