Skip to content

Commit

Permalink
Adds .keep_next paragraph property.
Browse files Browse the repository at this point in the history
> Specifies that the paragraph (or at least part of it) should be
rendered on the same page as the next paragraph when possible. It is an
empty element: <w:keepNext/>. If multiple paragraphs are to be kept
together but they exceed a page, then the set of paragraphs begin on a
new page and page breaks are used thereafter as needed.

http://officeopenxml.com/WPparagraphProperties.php

Usage

```
context.p 'hi' do
  keep_next true
end
```
  • Loading branch information
Ben Ketron committed Jan 28, 2020
1 parent 562bdc4 commit 5a312dc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ docx.p 'Sample text.' do
bgcolor 'cccccc' # sets the background color.
highlight_color 'yellow' # sets the highlight color. only accepts OOXML enumerations. see http://www.datypic.com/sc/ooxml/t-w_ST_HighlightColor.html.
vertical_align 'superscript' # sets the vertical alignment.
keep_next false # sets whether or not to keep this paragraph with the next paragraph when separated by page break.
end
```

Expand Down
3 changes: 2 additions & 1 deletion lib/caracal/core/models/paragraph_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ParagraphModel < BaseModel
attr_reader :paragraph_italic
attr_reader :paragraph_underline
attr_reader :paragraph_bgcolor
attr_reader :paragraph_keep_next

# initialization
def initialize(options={}, &block)
Expand Down Expand Up @@ -63,7 +64,7 @@ def run_attributes
#========== SETTERS ===============================

# booleans
[:bold, :italic, :underline].each do |m|
[:bold, :italic, :keep_next, :underline].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@paragraph_#{ m }", !!value)
end
Expand Down
1 change: 1 addition & 0 deletions lib/caracal/renderers/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def render_paragraph(xml, model)
xml['w'].pStyle({ 'w:val' => model.paragraph_style }) unless model.paragraph_style.nil?
xml['w'].contextualSpacing({ 'w:val' => '0' })
xml['w'].jc({ 'w:val' => model.paragraph_align }) unless model.paragraph_align.nil?
xml['w'].keepNext if model.paragraph_keep_next == true
render_run_attributes(xml, model, true)
end
model.runs.each do |run|
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/caracal/core/models/paragraph_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
italic false
underline true
bgcolor 'cccccc'
keep_next true
end
end

Expand All @@ -31,6 +32,7 @@
it { expect(subject.paragraph_italic).to eq false }
it { expect(subject.paragraph_underline).to eq true }
it { expect(subject.paragraph_bgcolor).to eq 'cccccc' }
it { expect(subject.paragraph_keep_next).to eq true }
end

end
Expand Down Expand Up @@ -70,6 +72,11 @@

it { expect(subject.paragraph_italic).to eq true }
end
describe '.keep_next' do
before { subject.keep_next(true) }

it { expect(subject.paragraph_keep_next).to eq true }
end
describe '.underline' do
before { subject.underline(true) }

Expand Down

0 comments on commit 5a312dc

Please sign in to comment.