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

Adding support for page numbers #1

Merged
merged 5 commits into from
Feb 2, 2024
Merged
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
16 changes: 16 additions & 0 deletions lib/caracal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ def self.root
include Caracal::Core::Tables
include Caracal::Core::Text
end

Caracal::Core::Models::FooterModel.class_eval do
include Caracal::Core::Images
include Caracal::Core::Lists
include Caracal::Core::Rules
include Caracal::Core::Tables
include Caracal::Core::Text
end

Caracal::Core::Models::HeaderModel.class_eval do
include Caracal::Core::Images
include Caracal::Core::Lists
include Caracal::Core::Rules
include Caracal::Core::Tables
include Caracal::Core::Text
end
33 changes: 33 additions & 0 deletions lib/caracal/core/footer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'caracal/core/models/footer_model'
require 'caracal/errors'


module Caracal
module Core

# This module encapsulates all the functionality related to adding a
# footer on every page of the document.
#
module Footer
def self.included(base)
base.class_eval do

#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

def footer(*args, &block)
options = Caracal::Utilities.extract_options!(args)

model = Caracal::Core::Models::FooterModel.new(options, &block)

@footer_content = model

model
end

end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/caracal/core/header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'caracal/core/models/header_model'
require 'caracal/errors'


module Caracal
module Core

# This module encapsulates all the functionality related to adding a header
# to every page of the document.
#
module Header
def self.included(base)
base.class_eval do

#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

def header(*args, &block)
options = Caracal::Utilities.extract_options!(args)

model = Caracal::Core::Models::HeaderModel.new(options, &block)

@header_content = model

model
end
end
end
end
end
end
137 changes: 137 additions & 0 deletions lib/caracal/core/models/field_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
require 'caracal/core/models/base_model'


module Caracal
module Core
module Models

# This class encapsulates the logic needed to store and manipulate
# text data.
#
class FieldModel < BaseModel

#--------------------------------------------------
# Configuration
#--------------------------------------------------

# constants
const_set(:TYPE_MAP, { page: 'PAGE', numpages: 'NUMPAGES' })

# accessors
attr_reader :field_dirty
attr_reader :field_type
attr_reader :field_style
attr_reader :field_font
attr_reader :field_color
attr_reader :field_size
attr_reader :field_bold
attr_reader :field_italic
attr_reader :field_underline
attr_reader :field_bgcolor
attr_reader :field_highlight_color
attr_reader :field_vertical_align

#-------------------------------------------------------------
# Public Class Methods
#-------------------------------------------------------------

def self.formatted_type(type)
TYPE_MAP.fetch(type.to_s.to_sym)
end


#-------------------------------------------------------------
# Public Instance Methods
#-------------------------------------------------------------

#=============== GETTERS ==============================

def formatted_type
self.class.formatted_type(field_type)
end

#========== GETTERS ===============================

# .run_attributes
def run_attributes
{
style: field_style,
font: field_font,
color: field_color,
size: field_size,
bold: field_bold,
italic: field_italic,
underline: field_underline,
bgcolor: field_bgcolor,
highlight_color: field_highlight_color,
vertical_align: field_vertical_align
}
end


#========== SETTERS ===============================

# booleans
[:bold, :italic, :underline].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@field_#{ m }", !!value)
end
end

# integers
[:size].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@field_#{ m }", value.to_i)
end
end

# strings
[:bgcolor, :color, :dirty, :font, :highlight_color, :style, :type,].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@field_#{ m }", value.to_s)
end
end

# symbols
[:vertical_align].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@field_#{ m }", value.to_s.to_sym)
end
end


#========== VALIDATION ============================

def valid?
a = [:type]
a.map { |m| send("field_#{ m }") }.compact.size == a.size
end


#--------------------------------------------------
# Private Methods
#--------------------------------------------------
private

def option_keys
[:type, :style, :font, :color, :size, :bold, :italic, :underline, :bgcolor, :highlight_color, :vertical_align]
end

def method_missing(method, *args, &block)
# TODO: Better field centric description

# I'm on the fence with respect to this implementation. We're ignoring
# :method_missing errors to allow syntax flexibility for paragraph-type
# models. The issue is the syntax format of those models--the way we pass
# the content value as a special argument--coupled with the model's
# ability to accept nested instructions.
#
# By ignoring method missing errors here, we can pass the entire paragraph
# block in the initial, built-in call to :text.
end

end

end
end
end
42 changes: 42 additions & 0 deletions lib/caracal/core/models/footer_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'caracal/core/models/base_model'
require 'caracal/core/models/margin_model'
require 'caracal/core/models/paragraph_model'


module Caracal
module Core
module Models

# This class handles block options passed to tables via their data
# collections.
#
class FooterModel < BaseModel

#-------------------------------------------------------------
# Configuration
#-------------------------------------------------------------

# initialization
def initialize(options={}, &block)
super options, &block
end

#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

#=============== DATA ACCESSORS =======================

def contents
@contents ||= []
end

#=============== VALIDATION ===========================

def valid?
contents.size > 0
end
end
end
end
end
42 changes: 42 additions & 0 deletions lib/caracal/core/models/header_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'caracal/core/models/base_model'
require 'caracal/core/models/margin_model'
require 'caracal/core/models/paragraph_model'


module Caracal
module Core
module Models

# This class handles block options passed to tables via their data
# collections.
#
class HeaderModel < BaseModel

#-------------------------------------------------------------
# Configuration
#-------------------------------------------------------------

# initialization
def initialize(options={}, &block)
super options, &block
end

#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

#=============== DATA ACCESSORS =======================

def contents
@contents ||= []
end

#=============== VALIDATION ===========================

def valid?
contents.size > 0
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/caracal/core/models/paragraph_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'caracal/core/models/bookmark_model'
require 'caracal/core/models/link_model'
require 'caracal/core/models/text_model'
require 'caracal/core/models/field_model'
require 'caracal/errors'


Expand Down Expand Up @@ -126,6 +127,20 @@ def br
model
end

# .page_num
def field(*args, &block)
options = Caracal::Utilities.extract_options!(args)
options = options.merge({ type: args[0] })

model = Caracal::Core::Models::FieldModel.new(options, &block)
if model.valid?
runs << model
else
raise Caracal::Errors::InvalidModelError, ':page_num method must receive a string for the display text.'
end
model
end

# .link
def link(*args, &block)
options = Caracal::Utilities.extract_options!(args)
Expand Down
1 change: 1 addition & 0 deletions lib/caracal/core/models/relationship_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RelationshipModel < BaseModel
TYPE_MAP = {
font: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable',
footer: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer',
header: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/header',
image: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
link: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
numbering: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering',
Expand Down
1 change: 1 addition & 0 deletions lib/caracal/core/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def self.default_relationships
[
{ target: 'fontTable.xml', type: :font },
{ target: 'footer1.xml', type: :footer },
{ target: 'header1.xml', type: :header },
{ target: 'numbering.xml', type: :numbering },
{ target: 'settings.xml', type: :setting },
{ target: 'styles.xml', type: :style }
Expand Down
Loading