Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added page column option #141

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ tmp
mkmf.log
gemfiles/*.gemfile.lock
.DS_Store
.idea
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion caracal.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
70 changes: 70 additions & 0 deletions lib/caracal/core/models/page_cols_model.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions lib/caracal/core/page_settings.rb
Original file line number Diff line number Diff line change
@@ -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'


Expand All @@ -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


#-------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/caracal/renderers/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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