docxtor2 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +21 -0
  4. data/.rspec +2 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +6 -0
  7. data/.wrong +1 -0
  8. data/Gemfile +18 -0
  9. data/LICENSE.txt +22 -0
  10. data/README.md +71 -0
  11. data/Rakefile +7 -0
  12. data/TODO.md +2 -0
  13. data/docxtor2.gemspec +32 -0
  14. data/lib/docxtor2.rb +39 -0
  15. data/lib/docxtor2/block_evaluator.rb +13 -0
  16. data/lib/docxtor2/constants.rb +4 -0
  17. data/lib/docxtor2/content_builder.rb +29 -0
  18. data/lib/docxtor2/document_builder.rb +19 -0
  19. data/lib/docxtor2/element_list.rb +26 -0
  20. data/lib/docxtor2/generator.rb +12 -0
  21. data/lib/docxtor2/known/aliases.rb +11 -0
  22. data/lib/docxtor2/known/mappings.rb +31 -0
  23. data/lib/docxtor2/known/parts.rb +9 -0
  24. data/lib/docxtor2/known/path.rb +11 -0
  25. data/lib/docxtor2/known/styles.rb +10 -0
  26. data/lib/docxtor2/known/templates.rb +7 -0
  27. data/lib/docxtor2/object_utils.rb +7 -0
  28. data/lib/docxtor2/package.rb +33 -0
  29. data/lib/docxtor2/package/document.rb +28 -0
  30. data/lib/docxtor2/package/document/element.rb +89 -0
  31. data/lib/docxtor2/package/document/heading.rb +11 -0
  32. data/lib/docxtor2/package/document/page_break.rb +16 -0
  33. data/lib/docxtor2/package/document/paragraph.rb +90 -0
  34. data/lib/docxtor2/package/document/table_of_contents.rb +90 -0
  35. data/lib/docxtor2/package/part.rb +10 -0
  36. data/lib/docxtor2/template_parser.rb +42 -0
  37. data/lib/docxtor2/version.rb +3 -0
  38. data/spec/docxtor2/content_builder_spec.rb +24 -0
  39. data/spec/docxtor2/docxtor2_spec.rb +13 -0
  40. data/spec/docxtor2/package/document/element_spec.rb +20 -0
  41. data/spec/docxtor2/package/document/heading_spec.rb +9 -0
  42. data/spec/docxtor2/package/document/paragraph_spec.rb +56 -0
  43. data/spec/docxtor2/package/document/table_of_contents_spec.rb +18 -0
  44. data/spec/docxtor2/package/document_spec.rb +13 -0
  45. data/spec/docxtor2/package/part_spec.rb +16 -0
  46. data/spec/docxtor2/package_spec.rb +37 -0
  47. data/spec/docxtor2/prerequisites_spec.rb +9 -0
  48. data/spec/docxtor2/support/contexts/integration_context.rb +6 -0
  49. data/spec/docxtor2/support/contexts/xml_builder_context.rb +11 -0
  50. data/spec/docxtor2/support/examples/.gitkeep +0 -0
  51. data/spec/docxtor2/support/matchers/wordprocessingml_matchers.rb +42 -0
  52. data/spec/docxtor2/support/matchers/xpath_matchers.rb +46 -0
  53. data/spec/docxtor2/template_parser_spec.rb +18 -0
  54. data/spec/spec_helper.rb +35 -0
  55. data/templates/.gitkeep +0 -0
  56. data/templates/default/[Content_Types].xml +16 -0
  57. data/templates/default/_rels/.rels +4 -0
  58. data/templates/default/word/_rels/document.xml.rels +13 -0
  59. data/templates/default/word/endnotes.xml +17 -0
  60. data/templates/default/word/fontTable.xml +31 -0
  61. data/templates/default/word/footer1.xml +35 -0
  62. data/templates/default/word/footer2.xml +48 -0
  63. data/templates/default/word/footnotes.xml +17 -0
  64. data/templates/default/word/numbering.xml +212 -0
  65. data/templates/default/word/settings.xml +107 -0
  66. data/templates/default/word/styles.xml +368 -0
  67. data/templates/default/word/theme/theme1.xml +2 -0
  68. data/templates/default/word/websettings.xml +4 -0
  69. metadata +212 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA512:
3
+ metadata.gz: a2f820ac5638b4a075cde953d7c125c9cc75cae56dcbdc86fc2888b133717b8929437b0b12ef440dcc5e5a9177af64c23b95cf7915c4db50c85ad62269e7367e
4
+ data.tar.gz: b2dadaa7fd4bcb899ca1f9315ab366a557ff33bc1be78f9b2f9790a09b257c0dab90619db156026d50c823e1716cdeb679adeb4b7b5d7ab822cfc7f439a15ddb
5
+ SHA1:
6
+ metadata.gz: a802b6b1cfca4ae877cbe6e136edeb4bcfe4cf0e
7
+ data.tar.gz: 3ec90943064d9914b0de8563e629ce25452aab0d
@@ -0,0 +1 @@
1
+ repo_token: 4wdVJp9RHu5gRyn96RakitmhAHQri6OFe
@@ -0,0 +1,21 @@
1
+ ~*
2
+ .DS_Store
3
+ *.swp
4
+
5
+ *.gem
6
+ *.rbc
7
+ .bundle
8
+ .config
9
+ .yardoc
10
+ Gemfile.lock
11
+ InstalledFiles
12
+ _yardoc
13
+ coverage
14
+ doc/
15
+ lib/bundler/man
16
+ pkg
17
+ rdoc
18
+ spec/reports
19
+ test/tmp
20
+ test/version_tmp
21
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ ree-1.8.7-2012.02
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - ree-1.8.7-2012.02
4
+ - 1.8.7
5
+ - 1.9.2
6
+ - 1.9.3
data/.wrong ADDED
@@ -0,0 +1 @@
1
+ color
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test, :development do
6
+ gem 'pre-commit'
7
+ end
8
+
9
+ group :test do
10
+ gem 'coveralls', :require => false
11
+ gem 'simplecov', :require => false
12
+ gem 'tconsole'
13
+ gem 'turn'
14
+ end
15
+
16
+ group :development do
17
+
18
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vasiliy Yorkin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ docxtor2
2
+ ========
3
+
4
+ [![Build Status](https://travis-ci.org/vyorkin/docxtor2.png?branch=master)](https://travis-ci.org/vyorkin/docxtor2)
5
+ [![Coverage Status](https://coveralls.io/repos/vyorkin/docxtor2/badge.png)](https://coveralls.io/r/vyorkin/docxtor2)
6
+ [![Dependency Status](https://gemnasium.com/vyorkin/docxtor2.png)](https://gemnasium.com/vyorkin/docxtor2)
7
+ [![Gem Version](http://stillmaintained.com/vyorkin/docxtor2.png)](http://stillmaintained.com/vyorkin/docxtor2)
8
+
9
+ ## Summary
10
+
11
+ Ruby docx generator
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'docxtor2'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install 'docxtor2'
26
+
27
+ ## Usage
28
+
29
+ ```
30
+ stream = Docxtor2.generate do
31
+ table_of_contents "Contents"
32
+ h 1, "heading1"
33
+
34
+ p "text1", :b => true do
35
+ style 'p1'
36
+ spacing :before => 80, :after => 240
37
+ italic; u
38
+
39
+ w "text2"
40
+ br
41
+ write "text3"
42
+ end
43
+
44
+ h 2 do
45
+ w "heading2"
46
+ line_break
47
+ write "some text"
48
+ br
49
+ write "another text"
50
+ end
51
+
52
+ p "content", :style => 'p2', :i => true, :align => 'center'
53
+ end
54
+
55
+ file = File.new('test.docx', 'w')
56
+ file.write(stream.string)
57
+ file.close
58
+ ```
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
67
+
68
+ run tests:
69
+
70
+ turn -Itest test/lib
71
+
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/TODO.md ADDED
@@ -0,0 +1,2 @@
1
+ 1) Paragraph spacing support
2
+ 2) Dynamic style generations
@@ -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 'docxtor2/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "docxtor2"
8
+ spec.version = Docxtor2::VERSION
9
+ spec.authors = ["Vasiliy Yorkin"]
10
+ spec.email = ["vasiliy.yorin@gmail.com"]
11
+ spec.description = %q{Ruby docx generator, provides simple DSL for building word documents}
12
+ spec.summary = %q{Ruby docx generator}
13
+ spec.homepage = "http://github.com/vyorkin/docxtor2"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ # HINT: Version lock is required for ruby ree-1.8.7
24
+ spec.add_development_dependency "nokogiri", "~> 1.4.0"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "rspec-xml"
27
+ spec.add_development_dependency "wrong"
28
+
29
+ spec.add_runtime_dependency "builder"
30
+ spec.add_runtime_dependency "rubytree"
31
+ spec.add_runtime_dependency "rubyzip", "0.9.9"
32
+ end
@@ -0,0 +1,39 @@
1
+ require "builder"
2
+ require "zip/zip"
3
+
4
+ require "docxtor2/constants"
5
+ require "docxtor2/known/parts"
6
+ require "docxtor2/known/path"
7
+ require "docxtor2/known/templates"
8
+ require "docxtor2/known/styles"
9
+ require "docxtor2/known/mappings"
10
+ require "docxtor2/known/aliases"
11
+
12
+ module Docxtor2
13
+ autoload :Generator, 'docxtor2/generator'
14
+ autoload :DocumentBuilder, 'docxtor2/document_builder'
15
+ autoload :ContentBuilder, 'docxtor2/content_builder'
16
+ autoload :TemplateParser, 'docxtor2/template_parser'
17
+ autoload :Package, 'docxtor2/package'
18
+ autoload :BlockEvaluator, 'docxtor2/block_evaluator'
19
+ autoload :ObjectUtils, 'docxtor2/object_utils'
20
+ autoload :ElementList, 'docxtor2/element_list'
21
+
22
+ class Package
23
+ autoload :Part, 'docxtor2/package/part'
24
+ autoload :Document, 'docxtor2/package/document'
25
+ class Document
26
+ autoload :Element, 'docxtor2/package/document/element'
27
+ autoload :Run, 'docxtor2/package/document/run'
28
+ autoload :Text, 'docxtor2/package/document/text'
29
+ autoload :TableOfContents, 'docxtor2/package/document/table_of_contents'
30
+ autoload :Paragraph, 'docxtor2/package/document/paragraph'
31
+ autoload :Heading, 'docxtor2/package/document/heading'
32
+ autoload :PageBreak, 'docxtor2/package/document/page_break'
33
+ end
34
+ end
35
+
36
+ def self.generate(template = Known::Templates::DEFAULT, &block)
37
+ Generator.generate(template, &block)
38
+ end
39
+ end
@@ -0,0 +1,13 @@
1
+ module Docxtor2
2
+ module BlockEvaluator
3
+ def evaluate(&block)
4
+ if block_given?
5
+ if block.arity == 1
6
+ yield self
7
+ else
8
+ instance_eval &block
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ module Docxtor2
2
+ SEARCH_PATTERN = File.join('**', '{*,.}{xml,rels}')
3
+ DOCUMENT_XML_PATH = 'word/document.xml'
4
+ end
@@ -0,0 +1,29 @@
1
+ module Docxtor2
2
+ class ContentBuilder < ElementList
3
+ include BlockEvaluator
4
+
5
+ class << self
6
+ def build(&block)
7
+ instance = new(block)
8
+ instance.build
9
+ end
10
+ end
11
+
12
+ map({ :table_of_contents => Package::Document::TableOfContents,
13
+ :p => Package::Document::Paragraph,
14
+ :h => Package::Document::Heading,
15
+ :page_break => Package::Document::PageBreak
16
+ })
17
+
18
+ def initialize(block)
19
+ super()
20
+ evaluate &block
21
+ end
22
+
23
+ def build
24
+ xml = Builder::XmlMarkup.new
25
+ write_elements(xml)
26
+ xml.target!
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ module Docxtor2
2
+ class DocumentBuilder
3
+ class << self
4
+ def build(&block)
5
+ instance = new(block)
6
+ instance.document
7
+ end
8
+ end
9
+
10
+ def initialize(block)
11
+ content = ContentBuilder.build(&block)
12
+ @document = Package::Document.new(content)
13
+ end
14
+
15
+ def document
16
+ @document
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ module Docxtor2
2
+ class ElementList
3
+ def initialize
4
+ @elements = []
5
+ end
6
+
7
+ def self.map(mappings)
8
+ mappings.each do |name, klass|
9
+ define_method(name) do |*args, &block|
10
+ self << klass.new(*args, &block)
11
+ end
12
+ end
13
+ end
14
+
15
+ protected
16
+
17
+ def write_elements(xml)
18
+ @elements.each { |el| el.render(xml) }
19
+ end
20
+
21
+ def <<(el)
22
+ @elements << el
23
+ el
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ module Docxtor2
2
+ class Generator
3
+ class << self
4
+ def generate(template, &block)
5
+ parts = TemplateParser.parse(template)
6
+ document = DocumentBuilder.build(&block);
7
+ package = Package.new(parts, document)
8
+ package.to_stream
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Docxtor2
2
+ module Known
3
+ module Aliases
4
+ PARAGRAPH = {
5
+ :b => :bold,
6
+ :i => :italic,
7
+ :u => :underline
8
+ }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ module Docxtor2
2
+ module Known
3
+ module Mappings
4
+ PARAGRAPH = {
5
+ :style => 'pStyle',
6
+ :align => 'jc',
7
+ :font_size => 'sz',
8
+ :font_size_complex => 'szCs',
9
+ :spacing => {
10
+ :name => 'spacing',
11
+ :before => 'before',
12
+ :after => 'after'
13
+ },
14
+ :indent => {
15
+ :name => 'ind',
16
+ :start => 'start',
17
+ :end => 'end',
18
+ :hanging => 'hanging'
19
+ }
20
+ }
21
+ PARAGRAPH_SIMPLE = PARAGRAPH.reject { |k, v| v.is_a? Hash }
22
+ PARAGRAPH_COMPLEX = PARAGRAPH.reject { |k, v| !v.is_a? Hash }
23
+
24
+ RUN = {
25
+ :bold => 'b',
26
+ :italic => 'i',
27
+ :underline => 'u'
28
+ }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ module Docxtor2
2
+ module Known
3
+ module Parts
4
+ DOCUMENT = 'document'
5
+ SETTINGS = 'settings'
6
+ STYLES = 'styles'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module Docxtor2
2
+ module Known
3
+ module Path
4
+ ROOT = File.expand_path('../../../..', __FILE__)
5
+ TESTS = File.join(ROOT, 'spec/docxtor2')
6
+ SUPPORT = File.join(TESTS, 'support')
7
+ TEMPLATES = File.join(ROOT, 'templates')
8
+ TMP = File.join(TESTS, 'tmp')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module Docxtor2
2
+ module Known
3
+ module Styles
4
+ TOC = 'defprnRUSSelStyle'
5
+ TOC_PARAGRAPH = '10'
6
+
7
+ PARAGRAPH = 'a5'
8
+ end
9
+ end
10
+ end