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/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 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 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..f7765929 --- /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_cols_num, page_cols_space] + 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 81ed8672..e5d82a8b 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' @@ -25,6 +26,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 #------------------------------------------------------------- @@ -66,6 +69,18 @@ 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 + else + raise Caracal::Errors::InvalidModelError, 'page_cols method requires non-zero :cols_num and :cols_space options.' + end + end + end end end diff --git a/lib/caracal/renderers/document_renderer.rb b/lib/caracal/renderers/document_renderer.rb index 5b14b3fb..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 @@ -422,6 +423,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