ooodt 0.0.1.alpha

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f670a5de3fb4a1c1ae35f5951538491662a5b355
4
+ data.tar.gz: 998fc889961ce58f63d7a8cd396a56fd0817f0ac
5
+ SHA512:
6
+ metadata.gz: 845030abc2b1364d2fc0aba913c47f0496586db9d07825e5f3b5bbae3d875ab6819689bf05b7da0c5b459474c9295368fd63659d9f385ccea7a45f5d126f1038
7
+ data.tar.gz: 05bdf6cf0ac0501f8f9cf715c2bc575b12d35f863b26e82e862ed7baa4a56969524e0fe666b0a25b93fe6c59a02c5ec3fc6e88c955248438c02586e45f14f3ec
@@ -0,0 +1 @@
1
+ pkg/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ooodt (0.0.1.alpha)
5
+ builder (~> 3.2.2)
6
+ rubyzip (~> 1.1.7)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ builder (3.2.2)
12
+ rake (10.4.2)
13
+ rubyzip (1.1.7)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ ooodt!
20
+ rake (~> 10.4.2)
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Andor Chen
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # ooodt
2
+
3
+ ooodt is a Ruby .odt file packager.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ odt = Ooodt.make do
9
+ title 'Sample Title'
10
+ creator 'Andor Chen'
11
+ keyword 'key1, key2'
12
+ creation_date '2010-05-06'
13
+
14
+ content_file '/path/to/content.xml'
15
+ styles_file '/path/to/styles.xml'
16
+
17
+ pictures ['/path/to/foo.jpg', '/path/to/bar.png']
18
+ end
19
+ odt.save('sample.odt')
20
+ ```
21
+
22
+ ## Installation
23
+
24
+ ```sh
25
+ $ gem install ooodt
26
+ ```
27
+
28
+ or, in `Gemfile`:
29
+
30
+ ```ruby
31
+ gem 'ooodt'
32
+ ```
33
+
34
+ ## Requirements
35
+
36
+ * `content.xml`: the `<office:document-content>` ([ref](http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#__RefHeading__1415004_253892949))
37
+ * `styles.xml`: the `<office:document-styles>` ([ref](http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#__RefHeading__1415006_253892949))
38
+ * LibreOffice or OpenOffice to open `.odt` file
39
+
40
+ ## Resources
41
+
42
+ - [OASIS Open Document Format for Office Applications](https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=office)
43
+
44
+ ## Thanks to
45
+
46
+ - [eeepub](https://github.com/jugyo/eeepub/)
47
+
48
+ ## Copyright
49
+
50
+ Copyright (c) 2015 Andor Chen. See LICENSE for details.
@@ -0,0 +1,52 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), *%w[lib]))
2
+
3
+ require_relative 'lib/ooodt/version'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ Ooodt::VERSION
17
+ end
18
+
19
+ def gemspec_file
20
+ "#{name}.gemspec"
21
+ end
22
+
23
+ def gem_file
24
+ "#{name}-#{version}.gem"
25
+ end
26
+
27
+ #############################################################################
28
+ #
29
+ # Packaging tasks
30
+ #
31
+ #############################################################################
32
+
33
+ desc 'Release the gem, push to github and rubygems.org'
34
+ task :release => :build do
35
+ unless `git branch` =~ /^\* master$/
36
+ puts "You must be on the master branch to release!"
37
+ exit!
38
+ end
39
+ sh "git commit --allow-empty -m 'Release #{version}'"
40
+ sh "git tag v#{version}"
41
+ sh "git push origin master"
42
+ sh "git push origin v#{version}"
43
+ sh "gem push pkg/#{name}-#{version}.gem"
44
+ end
45
+
46
+ desc 'Build the gem and then move to /pkg'
47
+ task :build do
48
+ sh "mkdir -p pkg"
49
+ sh "gem build #{gemspec_file}"
50
+ sh "mv #{gem_file} pkg"
51
+ end
52
+
@@ -0,0 +1,10 @@
1
+ require_relative 'ooodt/maker'
2
+
3
+ module Ooodt
4
+ # Make .odt file
5
+ #
6
+ # @param [Proc] block the block for initialize Ooodt::Maker
7
+ def self.make(&block)
8
+ Ooodt::Maker.new(&block)
9
+ end
10
+ end
@@ -0,0 +1,101 @@
1
+ require 'builder'
2
+
3
+ module Ooodt
4
+ # Abstract base class for container item of .oddt file.
5
+ # Provides some helper methods.
6
+ #
7
+ # @abstract
8
+ class ContainerItem
9
+ class << self
10
+
11
+ private
12
+
13
+ # Set default value to attribute
14
+ #
15
+ # @param [Symbol] name the attribute name
16
+ # @param [Object] default the default value
17
+ def default_value(name, default)
18
+ instance_variable_name = "@#{name}"
19
+ define_method(name) do
20
+ self.instance_variable_get(instance_variable_name) ||
21
+ self.instance_variable_set(instance_variable_name, default)
22
+ end
23
+ end
24
+
25
+ # Define alias of attribute accessor
26
+ #
27
+ # @param [Symbol] name the attribute name as alias
28
+ # @param [Symbol] name the attribute name as source
29
+ def attr_alias(name, src)
30
+ alias_method name, src
31
+ alias_method :"#{name}=", :"#{src}="
32
+ end
33
+ end
34
+
35
+ # @param [Hash<Symbol, Object>] values the hash of symbols and objects for attributes
36
+ def initialize(values)
37
+ set_values(values)
38
+ end
39
+
40
+ # Set values for attributes
41
+ #
42
+ # @param [Hash<Symbol, Object>] values the hash of symbols and objects for attributes
43
+ def set_values(values)
44
+ values.each do |k, v|
45
+ self.send(:"#{k}=", v)
46
+ end
47
+ end
48
+
49
+ # Convert to xml of container item
50
+ #
51
+ # @return [String] the xml of container item
52
+ def to_xml
53
+ out = ""
54
+ builder = Builder::XmlMarkup.new(:target => out, :indent => 0)
55
+ builder.instruct!
56
+ build_xml(builder)
57
+ out
58
+ end
59
+
60
+ # Save as container item
61
+ #
62
+ # @param [String] filepath the file path for container item
63
+ def save(filepath)
64
+ File.open(filepath, 'w') do |file|
65
+ file << self.to_xml
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ # Guess media type from file name
72
+ #
73
+ # @param [String] filename the file name
74
+ # @return [String] the media-type
75
+ def guess_media_type(filename)
76
+ case filename
77
+ when /.*\.(jpeg|jpg)$/
78
+ 'image/jpeg'
79
+ when /.*\.png$/i
80
+ 'image/png'
81
+ when /.*\.gif$/i
82
+ 'image/gif'
83
+ when /.*\.svg$/i
84
+ 'image/svg+xml'
85
+ end
86
+ end
87
+
88
+ # Convert options for xml attributes
89
+ #
90
+ # @param [Hash<Symbol, Object>] hash the hash of symbols and objects for xml attributes
91
+ # @return [Hash<String, Object>] the options for xml attributes
92
+ def convert_to_xml_attributes(hash)
93
+ result = {}
94
+ hash.each do |k, v|
95
+ key = k.to_s.gsub('_', '-').to_sym
96
+ result[key] = v
97
+ end
98
+ result
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,87 @@
1
+ require_relative 'container_item'
2
+ require_relative 'manifest'
3
+ require_relative 'meta'
4
+ require_relative 'odf'
5
+
6
+ require 'tmpdir'
7
+ require 'fileutils'
8
+
9
+ module Ooodt
10
+ # The class to make .odt file easily
11
+ #
12
+ # @example
13
+ # odt = Ooodt.make do
14
+ # title 'sample'
15
+ # creator 'Andor Chen'
16
+ # keyword 'key1, key2'
17
+ # creation_date '2010-05-06'
18
+ #
19
+ # pictures ['/path/to/foo.jpg', '/path/to/bar.png']
20
+ # end
21
+ # odt.save('sample.odt')
22
+ class Maker
23
+
24
+ # @param [Proc] block the block for initialize
25
+ def initialize(&block)
26
+ @pictures ||= []
27
+ @content_file ||= 'content.xml'
28
+ @meta_file ||= 'meta.xml'
29
+ @styles_file ||= 'styles.xml'
30
+
31
+ instance_eval(&block) if block_given?
32
+ end
33
+
34
+ [
35
+ :title,
36
+ :creator,
37
+ :creation_date,
38
+ :keyword,
39
+ :pictures,
40
+ :content_file,
41
+ :meta_file,
42
+ :styles_file
43
+ ].each do |name|
44
+ define_method(name) do |arg|
45
+ instance_variable_set("@#{name}", arg)
46
+ end
47
+ end
48
+
49
+ # Save as .odt file
50
+ #
51
+ # @param [String] filename the .odt file name to save
52
+ def save(filename)
53
+ create_odt.save(filename)
54
+ end
55
+
56
+ private
57
+
58
+ def create_odt
59
+ dir = Dir.mktmpdir
60
+
61
+ FileUtils.cp(@content_file, File.join(dir, 'content.xml'))
62
+ FileUtils.cp(@styles_file, File.join(dir, 'styles.xml'))
63
+
64
+ @pictures.each do |pic|
65
+ dest_dir = File.join(dir, 'Pictures')
66
+ FileUtils.mkdir_p(dest_dir)
67
+ FileUtils.cp(pic, dest_dir)
68
+ end
69
+
70
+ Meta.new(
71
+ :creator => @creator,
72
+ :title => @title,
73
+ :creation_date => @creation_date,
74
+ :keyword => @keyword
75
+ ).save(File.join(dir, @meta_file))
76
+
77
+ manifest = Manifest.new(
78
+ :pictures => @pictures
79
+ )
80
+
81
+ ODF.new(
82
+ :dir => dir,
83
+ :manifest => manifest
84
+ )
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,26 @@
1
+ module Ooodt
2
+ class Manifest < ContainerItem
3
+ attr_accessor :pictures
4
+
5
+ def build_xml(builder)
6
+ builder.tag!('manifest:manifest',
7
+ 'xmlns:manifest' => 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0',
8
+ 'manifest:version' => '1.2') do
9
+ builder.tag!('manifest:file-entry', 'manifest:full-path' => '/',
10
+ 'manifest:version' => '1.2',
11
+ 'manifest:media-type' => 'application/vnd.oasis.opendocument.text')
12
+
13
+ ['content.xml', 'meta.xml', 'styles.xml'].each do |file|
14
+ builder.tag!('manifest:file-entry', 'manifest:full-path' => file,
15
+ 'manifest:media-type' => 'text/xml')
16
+ end
17
+
18
+ pictures.each do |pic|
19
+ pic = File.basename(pic)
20
+ builder.tag!('manifest:file-entry', 'manifest:full-path' => "Pictures/#{pic}",
21
+ 'manifest:media-type' => guess_media_type(pic))
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'version'
2
+
3
+ module Ooodt
4
+ class Meta < ContainerItem
5
+ attr_accessor :creator, :title, :creation_date, :keyword
6
+
7
+ default_value :title, 'Untitled'
8
+ default_value :creation_date, Time.now # FIXME: time format
9
+ default_value :keyword, ''
10
+
11
+ def build_xml(builder)
12
+ builder.tag!('office:document-meta', 'xmlns:office' => "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
13
+ 'xmlns:xlink' => "http://www.w3.org/1999/xlink",
14
+ 'xmlns:dc' => "http://purl.org/dc/elements/1.1/",
15
+ 'xmlns:meta' => "urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
16
+ 'xmlns:ooo' => "http://openoffice.org/2004/office",
17
+ 'xmlns:grddl' => "http://www.w3.org/2003/g/data-view#",
18
+ 'office:version' => "1.2") do
19
+ builder.tag!('office:meta') do
20
+ builder.tag!('dc:creator') { builder.text! creator }
21
+ builder.tag!('dc:date') { builder.text! Time.now.strftime('%Y-%m-%dT%H:%M:%S.%N') }
22
+ builder.tag!('dc:title') { builder.text! title }
23
+ builder.tag!('meta:creation-date') { builder.text! creation_date }
24
+ builder.tag!('meta:keyword') { builder.text! keyword }
25
+ builder.tag!('meta:generator') { builder.text! "ooodt/#{Ooodt::VERSION}" }
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,56 @@
1
+ require 'zip'
2
+
3
+ module Ooodt
4
+ # Class to create ODF
5
+ class ODF
6
+
7
+ attr_accessor :dir, :manifest
8
+
9
+ # @param [Hash<Symbol, Object>] values the values of symbols and objects for ODF
10
+ #
11
+ # @example
12
+ # EeePub::ODF.new(
13
+ # :dir => '/path/to/dir',
14
+ # :manifest => 'manifest.odf'
15
+ # )
16
+ def initialize(values)
17
+ values.each do |k, v|
18
+ self.send(:"#{k}=", v)
19
+ end
20
+ end
21
+
22
+ # Save as ODF
23
+ #
24
+ # @param [String] output_path the output file path of .odt file
25
+ def save(output_path)
26
+ output_path = File.expand_path(output_path)
27
+
28
+ create_odt do
29
+ mimetype = Zip::OutputStream::open(output_path) do |os|
30
+ os.put_next_entry('mimetype', nil, nil, Zip::Entry::STORED, Zlib::NO_COMPRESSION)
31
+ os << 'application/vnd.oasis.opendocument.text'
32
+ end
33
+ zipfile = Zip::File.open(output_path)
34
+ Dir.glob('**/*').each do |path|
35
+ zipfile.add(path, path)
36
+ end
37
+ zipfile.commit
38
+ end
39
+ FileUtils.remove_entry_secure dir
40
+ end
41
+
42
+ private
43
+
44
+ def create_odt
45
+ FileUtils.chdir(dir) do
46
+ meta_inf = 'META-INF'
47
+ FileUtils.mkdir_p(meta_inf)
48
+
49
+ manifest.save(File.join(meta_inf, 'manifest.xml'))
50
+
51
+ yield
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Ooodt
2
+ VERSION = '0.0.1.alpha'
3
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/ooodt/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.specification_version = 2 if s.respond_to? :specification_version=
5
+ s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
6
+ s.rubygems_version = '2.2.2'
7
+ s.required_ruby_version = '>= 1.9.3'
8
+
9
+ s.name = 'ooodt'
10
+ s.version = Ooodt::VERSION
11
+ s.license = 'MIT'
12
+
13
+ s.authors = ['Andor Chen']
14
+ s.email = ['andor.chen.27@gmail.com']
15
+ s.homepage = 'http://github.com/andorchen/ooodt'
16
+ s.summary = %q{.odt file packager}
17
+ s.description = %q{ooodt is a Ruby .odt file packager.}
18
+
19
+ s.files = `git ls-files`.split($/)
20
+ s.require_paths = ['lib']
21
+
22
+ s.add_runtime_dependency('builder', '~> 3.2.2')
23
+ s.add_runtime_dependency('rubyzip', '~> 1.1.7')
24
+
25
+ s.add_development_dependency('rake', '~> 10.4.2')
26
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ooodt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Andor Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: builder
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubyzip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.1.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 10.4.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 10.4.2
55
+ description: ooodt is a Ruby .odt file packager.
56
+ email:
57
+ - andor.chen.27@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - lib/ooodt.rb
69
+ - lib/ooodt/container_item.rb
70
+ - lib/ooodt/maker.rb
71
+ - lib/ooodt/manifest.rb
72
+ - lib/ooodt/meta.rb
73
+ - lib/ooodt/odf.rb
74
+ - lib/ooodt/version.rb
75
+ - ooodt.gemspec
76
+ homepage: http://github.com/andorchen/ooodt
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 1.9.3
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">"
92
+ - !ruby/object:Gem::Version
93
+ version: 1.3.1
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5
97
+ signing_key:
98
+ specification_version: 2
99
+ summary: ".odt file packager"
100
+ test_files: []