documenter 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,56 @@
1
1
  = documenter
2
2
 
3
- Description goes here.
3
+ Documenter is small collection of scripts to work with different format of documents
4
+ It includes:
5
+ * Odf and Odt classes to fill forms, tables and e.t.c
6
+ * File format convertor based on JodConvertor. To work with it, you need to have open office istalled localy
7
+ * Pdf worker based on pdftk. You should install it too
8
+ * Csv class to work with csv more easely
4
9
 
5
- == Note on Patches/Pull Requests
10
+ == Installation
6
11
 
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
- * Send me a pull request. Bonus points for topic branches.
12
+ % gem install documenter
13
+
14
+ == Example
15
+
16
+ === Csv
17
+
18
+ Load data from file into two demetions array
19
+ data = File.open('test.csv').csv(:cell_split => ';')
20
+
21
+ Export data to csv file
22
+ data.to_csv(:file => 'myout.csv', :header => ['a','a+100'], :cell_split => ';')
23
+
24
+
25
+ === Convertor
26
+
27
+ Convertor.convert 'test.xls', 'test.pdf'
28
+
29
+ === Pdf
30
+
31
+ Join files together
32
+
33
+ Pdf.cat ['a.pdf','b.pdf'], 'output.pdf'
34
+
35
+ Split pdf to pages
36
+ second parameter - string that would be used as a pattern to save each page
37
+
38
+ Pdf.burst 'hello.pdf', 'doc/page_%.6d.pdf'
39
+
40
+
41
+ #opening existing document
42
+ doc = OdtFile.new 'custom_register.odt'
43
+ #set value of form field
44
+ doc.text.fill_form('number' => 'test')
45
+ #search for table and yield first row copy for each data row
46
+ doc.text.edit_table(:name => 'posts', :data =>posts) do |row, content|
47
+ #replace form fields
48
+ row.replace_form(content)
49
+ #we shoul return row back
50
+ row
51
+ end
52
+ doc.save 'out.odt'
53
+ #you should use close to delete temporary files
54
+ doc.close
14
55
 
15
- == Copyright
16
56
 
17
- Copyright (c) 2009 A N. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{documenter}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["A N"]
12
- s.date = %q{2009-11-30}
12
+ s.date = %q{2009-12-11}
13
13
  s.description = %q{This gem uses OpenOffice and pdftk to convert, split, join, fill and copy files. It may be usefull if you have many documents to work with. It is not realy fast solution and it depend on istalled software}
14
14
  s.email = %q{goremika@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -25,7 +25,25 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "documenter.gemspec",
27
27
  "lib/documenter.rb",
28
+ "lib/documenter/csv.rb",
29
+ "lib/documenter/ooo.rb",
30
+ "lib/documenter/ooo/converter.rb",
31
+ "lib/documenter/ooo/jod/lib/DEPENDENCIES.txt",
32
+ "lib/documenter/ooo/jod/lib/commons-cli-1.2.jar",
33
+ "lib/documenter/ooo/jod/lib/commons-io-1.4.jar",
34
+ "lib/documenter/ooo/jod/lib/jodconverter-2.2.2.jar",
35
+ "lib/documenter/ooo/jod/lib/jodconverter-cli-2.2.2.jar",
36
+ "lib/documenter/ooo/jod/lib/juh-3.0.1.jar",
37
+ "lib/documenter/ooo/jod/lib/jurt-3.0.1.jar",
38
+ "lib/documenter/ooo/jod/lib/ridl-3.0.1.jar",
39
+ "lib/documenter/ooo/jod/lib/slf4j-api-1.5.6.jar",
40
+ "lib/documenter/ooo/jod/lib/slf4j-jdk14-1.5.6.jar",
41
+ "lib/documenter/ooo/jod/lib/unoil-3.0.1.jar",
42
+ "lib/documenter/ooo/jod/lib/xstream-1.3.1.jar",
43
+ "lib/documenter/ooo/odf.rb",
44
+ "lib/documenter/ooo/odt.rb",
28
45
  "lib/documenter/pdf.rb",
46
+ "lib/documenter/pdftk/pdftk.exe",
29
47
  "test/helper.rb",
30
48
  "test/test_documenter.rb"
31
49
  ]
@@ -1,4 +1,8 @@
1
1
  module Documenter
2
- require 'documenter/pdf'
2
+ require File.dirname(__FILE__)+'/documenter/pdf'
3
+ require File.dirname(__FILE__)+'/documenter/ooo'
4
+ require File.dirname(__FILE__)+'/documenter/csv'
3
5
  end
4
6
 
7
+
8
+
@@ -0,0 +1,123 @@
1
+
2
+ class Csv
3
+ #defaults params
4
+ @@defaults = {:bracket => '"', :line_break => "\n", :cell_split => ',', :file => 'out.csv'}
5
+
6
+ #save given params[:data] two demetions array to file
7
+ def self.save params={}
8
+ params = @@defaults.merge(params)
9
+ File.open(params[:file],'w') do |f|
10
+ f.write(params[:title]+params[:line_break]) if params[:title]
11
+ f.write array_to_str(params[:header],params)+params[:line_break] if params[:header]
12
+ params[:data].each do |row|
13
+ #you can edit row in given block if you need so
14
+ if block_given?
15
+ ar = yield row
16
+ else
17
+ ar = row
18
+ end
19
+ f.write array_to_str(ar,params)+params[:line_break]
20
+ end
21
+ end
22
+ end
23
+
24
+
25
+ #converts one demention array to single csv string
26
+ def self.array_to_str array, params
27
+ params = @@defaults.merge params
28
+ array.map{|x| params[:bracket]+x.to_s.gsub(/#{params[:bracket]}/,params[:bracket]+params[:bracket])+params[:bracket]}*params[:cell_split]
29
+ end
30
+
31
+ #load data from file into table
32
+ def self.load_table file, params
33
+ params = @@defaults.merge params
34
+ bracket = params[:bracket]
35
+ line_break = params[:line_break]
36
+ cell_split = params[:cell_split]
37
+
38
+ rows = []
39
+ cells = []
40
+ buffer = ''
41
+ #bracket mode show if we between brackets. If so, we should not react on several things
42
+ bracket_mode = false
43
+ prev_bracket = false
44
+
45
+ # we will look at one char at time
46
+ file.each_byte do |byte|
47
+ char = byte.chr
48
+ #if you see a bracket char
49
+ if char == bracket
50
+ #and previus was bracket to
51
+ if prev_bracket==true
52
+ #we just save one bracket
53
+ buffer += char
54
+ prev_bracket = false
55
+ else
56
+ prev_bracket = true
57
+ end
58
+ #after the bracket we change mode
59
+ bracket_mode = !bracket_mode
60
+ else
61
+ prev_bracket = false
62
+ if !bracket_mode
63
+ if char == cell_split || char == line_break
64
+ #we met splitter
65
+ #in sny way we finishing cell
66
+ cells << buffer
67
+ buffer = ''
68
+ #but if it was a line break
69
+ if char == line_break
70
+ #we finish a line to
71
+ rows << cells
72
+ cells = []
73
+ end
74
+ else
75
+ buffer += char
76
+ end
77
+ else
78
+ #in brackets mode we just copy data
79
+ buffer += char
80
+ end
81
+ end
82
+ end
83
+ #wen all data finished, we just save what we got in buffers
84
+ cells << buffer if buffer.size>0
85
+ rows << cells if cells.size>0
86
+ rows
87
+ end
88
+ end
89
+
90
+
91
+ class File
92
+ #Easy loading data from file
93
+ def csv params
94
+ Csv.load_table(self, params)
95
+ end
96
+ end
97
+
98
+ class String
99
+ #Easy loading data from string
100
+ def csv params
101
+ Csv.load_table(self, params)
102
+ end
103
+ end
104
+
105
+ class Array
106
+ #saving data from array
107
+ def to_csv params={}
108
+ params[:data] = self
109
+ #we can you block t change data defore saving
110
+ if block_given?
111
+ Csv.save(params) do |row|
112
+ yield row
113
+ end
114
+ else
115
+ Csv.save(params)
116
+ end
117
+ end
118
+
119
+ end
120
+
121
+ #data = File.open('test.csv').csv(:cell_split => ';')
122
+ #data.to_csv(:file => 'myout.csv', :header => ['a','a+100'], :cell_split => ';')
123
+
@@ -0,0 +1,4 @@
1
+ module Ooo
2
+ require File.dirname(__FILE__)+'/ooo/converter'
3
+ require File.dirname(__FILE__)+'/ooo/odt'
4
+ end
@@ -0,0 +1,28 @@
1
+
2
+ class Converter
3
+ @@initialized = false
4
+ def self.office_path
5
+ 'c:\Program Files\OpenOffice.org 3\program\soffice.exe'
6
+ #todo - search for installed
7
+ #todo - check for small portable version
8
+ end
9
+
10
+
11
+
12
+ def self.init
13
+ ooo = office_path
14
+ system('"'+ooo+' " -headless -accept="socket,port=8100;urp;"')
15
+ @@initialized = true
16
+ #todo - check if already running
17
+ end
18
+
19
+
20
+ def self.convert from, to
21
+ init if !@@initialized
22
+ system("java -jar #{File.dirname(__FILE__)}/jod/lib/jodconverter-cli-2.2.2.jar #{from} #{to}")
23
+ end
24
+ end
25
+
26
+ #Converter.convert 'test.csv', 'text.xls'
27
+
28
+
@@ -0,0 +1,17 @@
1
+ To use the library in your own Java app you need
2
+
3
+ * commons-io
4
+ * jodconverter
5
+ * juh
6
+ * jurt
7
+ * ridl
8
+ * slf4j-api
9
+ * slf4j-jdk14 or another slf4j implementation - see http://slf4j.org
10
+ * unoil
11
+ * xstream - only if you use XmlDocumentFormatRegistry
12
+
13
+ The command line interface additionally requires
14
+
15
+ * commons-cli
16
+ * jodconverter-cli
17
+
@@ -0,0 +1,42 @@
1
+ require 'zip/zip'
2
+
3
+ class OdfFile
4
+
5
+ def random_name
6
+ Time.now.strftime('%H%M%S')+rand(100000).to_s
7
+ end
8
+
9
+ def initialize filename
10
+ #working with copy
11
+ rn = random_name
12
+ @file = random_name+'tmp.odf'
13
+ @content = random_name+'tmp.xml'
14
+
15
+ File.copy filename, @file
16
+ zip = Zip::ZipFile.open(@file)
17
+ zip.extract('content.xml',@content)
18
+ end
19
+
20
+ def load_xml
21
+ File.read(@content)
22
+ end
23
+
24
+ def save_xml xml
25
+ File.open(@content,'w') do |f|
26
+ f.write xml
27
+ end
28
+ end
29
+
30
+ def close
31
+ File.delete @content
32
+ File.delete @file
33
+ end
34
+
35
+ def save filename
36
+ zip = Zip::ZipFile.open(@file)
37
+ puts "just before saving"
38
+ zip.replace('content.xml',@content)
39
+ zip.close
40
+ File.copy @file, filename
41
+ end
42
+ end
@@ -0,0 +1,133 @@
1
+ require File.dirname(__FILE__)+'/odf.rb'
2
+ require 'hpricot'
3
+
4
+ class OdtText
5
+ attr_accessor :xml
6
+
7
+ def initialize xml
8
+ @xml = Hpricot.parse(xml)
9
+ end
10
+
11
+ def fill_form data
12
+ (@xml/'text:user-field-decl').each do |decl|
13
+ name = decl['text:name']
14
+ decl['office:string-value'] = data[name] if data[name]
15
+ end
16
+ @xml
17
+ end
18
+
19
+ def replace_form data
20
+ (@xml/'text:user-field-get').each do |decl|
21
+ name = decl['text:name']
22
+ if data && data[name]
23
+ parent_html = decl.parent.inner_html
24
+ decl.parent.inner_html = parent_html.gsub(/#{decl.to_html}/,"#{data[name].to_s}")
25
+ end
26
+ end
27
+ puts @xml
28
+ @xml
29
+ end
30
+
31
+
32
+ def find_table name
33
+ end
34
+
35
+
36
+ def rename_table oldname, newname
37
+ end
38
+
39
+ def to_html
40
+ @xml.to_html
41
+ end
42
+
43
+ def to_xml
44
+ @xml.to_html
45
+ end
46
+
47
+
48
+ def edit_table params
49
+ @name = params[:name]
50
+ @rows = params[:data]
51
+ raise 'cannot edit table rows without datarows' if !@rows || @rows.size==0
52
+ @start_row = (params[:no_header]) ? 0 : 1
53
+
54
+ table = nil
55
+ @xml.search("table:table").each {|t| table = t if t['table:name'] == @name }
56
+ raise "no such table '#{@name.to_s}'" if !table
57
+
58
+ table['table:name'] = params[:new_name] if params[:new_name]
59
+ puts table
60
+ row = (table/'table:table-row')[@start_row]
61
+ template = row.to_html
62
+ row.inner_html=''
63
+
64
+ @rows.each do |row|
65
+ puts 'inside'
66
+ table.inner_html += (yield OdtText.new(template), row).to_html
67
+ end
68
+
69
+ #puts @xml
70
+ end
71
+
72
+ def fill_table params
73
+ edit_table_rows(params) do |row, row_date|
74
+ row.replace_form(row_data)
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ class OdtFile < OdfFile
81
+ attr_accessor :text
82
+
83
+ def initialize filename
84
+ super
85
+ load
86
+ end
87
+
88
+
89
+ def load
90
+ @text = OdtText.new(load_xml)
91
+ end
92
+
93
+ def save_changes
94
+ save_xml @text.to_xml
95
+ end
96
+
97
+
98
+ def save filename
99
+ save_changes
100
+ name, extention = filename.split('.')
101
+ extention = 'odt' if !extention
102
+ raise "Wrong extention #{extention} for odt file" if extention != 'odt'
103
+ filename = "#{name}.#{extention}"
104
+ super filename
105
+ end
106
+
107
+ end
108
+
109
+
110
+ =begin
111
+
112
+
113
+ posts = [{'posts' => 'asdasdasd'}, {'posts' => 'adfasdfasdf'}, {'posts' => 'asdfasf'}]
114
+ packets = [{'t' => 'qweerty'}, {'t' => 'qwerty'}]
115
+
116
+ i =0
117
+ doc = OdtFile.new 'custom_register.odt'
118
+ doc.text.fill_form('number' => 'test')
119
+ doc.text.edit_table(:name => 'posts', :data =>posts) do |row, content|
120
+ row.replace_form(content)
121
+ row.edit_table(:name => 'packets', :new_name => 'packets'+i.to_s, :data => packets) do |row2,content|
122
+ row2.replace_form(content)
123
+ i += 1
124
+ row2
125
+ end
126
+ row
127
+ end
128
+
129
+ doc.save 'out.odt'
130
+ doc.close
131
+
132
+
133
+ =end
@@ -1,16 +1,24 @@
1
+ require 'ftools'
2
+
3
+
1
4
  class Pdf
5
+ # pass to localy installed pdftk
6
+ @@exe = File.dirname(__FILE__)+'/pdftk/pdftk.exe'
2
7
 
3
- def self.is_windows?
4
- processor, platform, *rest = RUBY_PLATFORM.split("-")
5
- platform == 'mswin32'
8
+ #join array of pdfs filenames to one output file
9
+ def self.cat pdfs, output
10
+ system("#{@@exe} #{pdfs*' '} cat output #{output} compress dont_ask ")
6
11
  end
7
-
8
12
 
9
- def self.join array_of_pdfs, output, extras = 'compress'
10
- if is_windows?
11
- system("pdftk/pdftk.exe #{array_of_pdfs*' '} cat output #{output} #{extras} dont_ask ")
12
- else
13
- raise "Currently only windows supported"
13
+ #cut file to single pages
14
+ def self.burst file, pattern
15
+ system("#{@@exe} #{file} burst")
16
+ Dir.new(".").each do |file|
17
+ if (file =~ /pg_[0-9]*\.pdf/)
18
+ number = file.match(/[0-9]{4}/)[0].to_i
19
+ new_file = pattern % number
20
+ File.move file, new_file
21
+ end
14
22
  end
15
23
  end
16
- end
24
+ end
@@ -1,7 +1,13 @@
1
1
  require 'helper'
2
2
 
3
+ #some integration style tests :)
3
4
  class TestDocumenter < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
5
+ should "get to diff files and join then to one" do
6
+ data = [['a0','b0'],['a1','b1']]
7
+ data.to_csv(:file => 'myout.csv', :header => ['a','b'])
8
+ Converter.convert 'myout.csv', 'test1.pdf'
9
+
6
10
  end
11
+
12
+
7
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: documenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - A N
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-30 00:00:00 +03:00
12
+ date: 2009-12-11 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -60,7 +60,25 @@ files:
60
60
  - VERSION
61
61
  - documenter.gemspec
62
62
  - lib/documenter.rb
63
+ - lib/documenter/csv.rb
64
+ - lib/documenter/ooo.rb
65
+ - lib/documenter/ooo/converter.rb
66
+ - lib/documenter/ooo/jod/lib/DEPENDENCIES.txt
67
+ - lib/documenter/ooo/jod/lib/commons-cli-1.2.jar
68
+ - lib/documenter/ooo/jod/lib/commons-io-1.4.jar
69
+ - lib/documenter/ooo/jod/lib/jodconverter-2.2.2.jar
70
+ - lib/documenter/ooo/jod/lib/jodconverter-cli-2.2.2.jar
71
+ - lib/documenter/ooo/jod/lib/juh-3.0.1.jar
72
+ - lib/documenter/ooo/jod/lib/jurt-3.0.1.jar
73
+ - lib/documenter/ooo/jod/lib/ridl-3.0.1.jar
74
+ - lib/documenter/ooo/jod/lib/slf4j-api-1.5.6.jar
75
+ - lib/documenter/ooo/jod/lib/slf4j-jdk14-1.5.6.jar
76
+ - lib/documenter/ooo/jod/lib/unoil-3.0.1.jar
77
+ - lib/documenter/ooo/jod/lib/xstream-1.3.1.jar
78
+ - lib/documenter/ooo/odf.rb
79
+ - lib/documenter/ooo/odt.rb
63
80
  - lib/documenter/pdf.rb
81
+ - lib/documenter/pdftk/pdftk.exe
64
82
  - test/helper.rb
65
83
  - test/test_documenter.rb
66
84
  has_rdoc: true