From 43299be5b0e8bd16d8b4b46f626995224b4cedb6 Mon Sep 17 00:00:00 2001 From: Sebastian Kawelke Date: Wed, 5 Aug 2020 09:36:04 +0200 Subject: [PATCH 1/7] Changed bundler version --- .gitignore | 1 + caracal.gemspec | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4f25038f..d827a3fb 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ tmp mkmf.log gemfiles/*.gemfile.lock .DS_Store +.idea \ No newline at end of file diff --git a/caracal.gemspec b/caracal.gemspec index 82438e06..8845470a 100644 --- a/caracal.gemspec +++ b/caracal.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'rubyzip', '~> 1.1' spec.add_dependency 'tilt', '>= 1.4' - spec.add_development_dependency 'bundler', '~> 1.3' + spec.add_development_dependency 'bundler', '~> 2.1.4' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec', '~> 3.0' end From dd25b4e6fa4b8dec5e5a46cfa52317fbb19025c8 Mon Sep 17 00:00:00 2001 From: Sebastian Kawelke Date: Wed, 5 Aug 2020 09:40:17 +0200 Subject: [PATCH 2/7] Added page coloumns option --- lib/caracal/core/page_settings.rb | 2 ++ lib/caracal/renderers/document_renderer.rb | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/lib/caracal/core/page_settings.rb b/lib/caracal/core/page_settings.rb index 81ed8672..52a95256 100644 --- a/lib/caracal/core/page_settings.rb +++ b/lib/caracal/core/page_settings.rb @@ -25,6 +25,8 @@ def self.included(base) attr_reader :page_margin_bottom attr_reader :page_margin_left attr_reader :page_margin_right + attr_reader :page_cols_num + attr_reader :page_cols_space #------------------------------------------------------------- diff --git a/lib/caracal/renderers/document_renderer.rb b/lib/caracal/renderers/document_renderer.rb index 5b14b3fb..d624c961 100644 --- a/lib/caracal/renderers/document_renderer.rb +++ b/lib/caracal/renderers/document_renderer.rb @@ -422,6 +422,14 @@ def page_size_options } end + def page_cols_options + { + 'w:num' => document.page_cols_num, + 'w:space' => document.page_cols_space, + 'w:equalWidth' => "on", + } + end + end end end From 07101ca2a9921dbb44e52af80768601c2a1ca387 Mon Sep 17 00:00:00 2001 From: Sebastian Kawelke Date: Wed, 5 Aug 2020 10:20:19 +0200 Subject: [PATCH 3/7] Added Model for Page Coloumns --- lib/caracal/core/models/page_cols_model.rb | 70 ++++++++++++++++++++++ lib/caracal/core/page_settings.rb | 11 ++++ 2 files changed, 81 insertions(+) create mode 100644 lib/caracal/core/models/page_cols_model.rb diff --git a/lib/caracal/core/models/page_cols_model.rb b/lib/caracal/core/models/page_cols_model.rb new file mode 100644 index 00000000..ba73406c --- /dev/null +++ b/lib/caracal/core/models/page_cols_model.rb @@ -0,0 +1,70 @@ +require 'caracal/core/models/base_model' + + +module Caracal + module Core + module Models + + # This class handles block options passed to the page size + # method. + # + class PageColsModel < BaseModel + + #------------------------------------------------------------- + # Configuration + #------------------------------------------------------------- + + # constants + const_set(:DEFAULT_PAGE_COLS_NUM, 1) + const_set(:DEFAULT_PAGE_COLS_SPACE, 15840) + + # accessors + attr_reader :page_cols_num + attr_reader :page_cols_space + + # initialization + def initialize(options={}, &block) + @page_cols_num = DEFAULT_PAGE_COLS_NUM + @page_cols_space = DEFAULT_PAGE_COLS_SPACE + + super options, &block + end + + + #------------------------------------------------------------- + # Public Methods + #------------------------------------------------------------- + + #=============== SETTERS ============================== + + def cols(value) + @page_cols_num = value.to_i + end + + def space(value) + @page_cols_space = value.to_i + end + + + #=============== VALIDATION ============================== + + def valid? + dims = [page_width, page_height] + dims.all? { |d| d > 0 } + end + + + #------------------------------------------------------------- + # Private Instance Methods + #------------------------------------------------------------- + private + + def option_keys + [:num, :space] + end + + end + + end + end +end diff --git a/lib/caracal/core/page_settings.rb b/lib/caracal/core/page_settings.rb index 52a95256..1860fe92 100644 --- a/lib/caracal/core/page_settings.rb +++ b/lib/caracal/core/page_settings.rb @@ -1,5 +1,6 @@ require 'caracal/core/models/margin_model' require 'caracal/core/models/page_size_model' +require 'caracal/core/models/page_cols_model' require 'caracal/errors' @@ -68,6 +69,16 @@ def page_size(options={}, &block) end end + # This method controls the column structure of the page. + def page_cols(options={}, &block) + model = Caracal::Core::Models::PageColsModel.new(options, &block) + + if model.valid? + @page_cols_num = model.page_cols_num + @page_cols_space = model.page_cols_space + end + end + end end end From 63a5dc0c842e72b8e815db126426b39d07c04b5b Mon Sep 17 00:00:00 2001 From: Sebastian Kawelke Date: Wed, 5 Aug 2020 11:23:22 +0200 Subject: [PATCH 4/7] Changed false variable names --- lib/caracal/core/models/page_cols_model.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/caracal/core/models/page_cols_model.rb b/lib/caracal/core/models/page_cols_model.rb index ba73406c..f7765929 100644 --- a/lib/caracal/core/models/page_cols_model.rb +++ b/lib/caracal/core/models/page_cols_model.rb @@ -49,7 +49,7 @@ def space(value) #=============== VALIDATION ============================== def valid? - dims = [page_width, page_height] + dims = [page_cols_num, page_cols_space] dims.all? { |d| d > 0 } end From 889e28b3ae637af40125d3dd451cba5cd54be8c0 Mon Sep 17 00:00:00 2001 From: Sebastian Kawelke Date: Wed, 5 Aug 2020 11:40:20 +0200 Subject: [PATCH 5/7] Added missing else case for page_cols validation --- lib/caracal/core/page_settings.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/caracal/core/page_settings.rb b/lib/caracal/core/page_settings.rb index 1860fe92..e5d82a8b 100644 --- a/lib/caracal/core/page_settings.rb +++ b/lib/caracal/core/page_settings.rb @@ -76,6 +76,8 @@ def page_cols(options={}, &block) if model.valid? @page_cols_num = model.page_cols_num @page_cols_space = model.page_cols_space + else + raise Caracal::Errors::InvalidModelError, 'page_cols method requires non-zero :cols_num and :cols_space options.' end end From 11d9962a24d7843227de59c77a2e7f4b44c5d725 Mon Sep 17 00:00:00 2001 From: Sebastian Kawelke Date: Wed, 5 Aug 2020 11:43:30 +0200 Subject: [PATCH 6/7] Added missing statement for renderer --- lib/caracal/renderers/document_renderer.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/caracal/renderers/document_renderer.rb b/lib/caracal/renderers/document_renderer.rb index d624c961..125c4f56 100644 --- a/lib/caracal/renderers/document_renderer.rb +++ b/lib/caracal/renderers/document_renderer.rb @@ -38,6 +38,7 @@ def to_xml end xml['w'].pgSz page_size_options xml['w'].pgMar page_margin_options + xml['w'].cols page_cols_options end end From 88bc81d9bbb756712b8fe5260294965a9dd40f03 Mon Sep 17 00:00:00 2001 From: seb-kw <66557440+seb-kw@users.noreply.github.com> Date: Wed, 5 Aug 2020 11:54:54 +0200 Subject: [PATCH 7/7] Updated README.md added section for page coloumns --- README.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 56f19204..ffde96a7 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,8 @@ docx.name # => 'example_document.docx' *The default file name is caracal.docx.* -### Page Size +### Page Settings +##### Page Size Page dimensions can be set using the `page_size` method. The method accepts two parameters for controlling the width and height of the document. It also accepts a third parameter for setting the page orientation. If you want landscape orientation, you need to change both the page @@ -271,7 +272,7 @@ end Both the `width` and `height` attributes require positive integer values. -### Page Margins +##### Page Margins Page margins can be set using the `page_margins` method. The method accepts four parameters for controlling the margins of the document. @@ -289,7 +290,7 @@ end All attributes require positive integer values. Additionally, the combined size of the margins on either axis cannot exceed the page size on that axis (e.g., the sum of the `left` and `right` values must be less than the `page_width`). -### Page Breaks +##### Page Breaks Page breaks can be added via the `page` method. The method accepts no parameters. @@ -298,7 +299,7 @@ docx.page # starts a new page. ``` -### Page Numbers +##### Page Numbers Page numbers can be added to the footer via the `page_numbers` method. The method accepts optional parameters for controlling the alignment, label and size of the text. @@ -316,6 +317,16 @@ end The `size` option and the `label_size` and `number_size` options are mutually exclusive. +##### Page Coloumns + +Page Coloumns can be set via the `page_cols` method. The method accepts paramters for the number of coloumns and the space for the coloumns. + +```ruby +docx.page_cols do + cols 2 # sets the number of coloumns. Default is 1. + space 708 # sets the space each coloumn needs. Default is 15840 (see Page Size). +end +``` ### Fonts