From c9178231d00fd9dc87ba446e261773cc69653fca Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 09:52:49 +0100 Subject: [PATCH 01/23] Update caracal.gemspec --- caracal.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caracal.gemspec b/caracal.gemspec index 82438e06..247be278 100644 --- a/caracal.gemspec +++ b/caracal.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.add_dependency 'nokogiri', '~> 1.6' - spec.add_dependency 'rubyzip', '~> 1.1' + spec.add_dependency 'rubyzip', ['>= 1.1.0', '< 3.0'] spec.add_dependency 'tilt', '>= 1.4' spec.add_development_dependency 'bundler', '~> 1.3' From f19d1a85515b5210640f8b0b556f761d0f319fdf Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 09:59:44 +0100 Subject: [PATCH 02/23] Create publish_gem.yml --- github/workflows/publish_gem.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 github/workflows/publish_gem.yml diff --git a/github/workflows/publish_gem.yml b/github/workflows/publish_gem.yml new file mode 100644 index 00000000..998aa646 --- /dev/null +++ b/github/workflows/publish_gem.yml @@ -0,0 +1,27 @@ +name: Publish RubyGem + +on: + release: + types: + - created + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Set up Ruby + uses: actions/setup-ruby@v1 + with: + ruby-version: 2.1 # Set your desired Ruby version here + + - name: Install Bundler + run: gem install bundler + + - name: Build and publish gem + run: | + bundle install + gem build gem_name.gemspec # Replace 'gem_name' with your gem's name + gem push gem_name-*.gem # Replace 'gem_name' with your gem's name + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From bfae1238069a2d25952bee7e91f478b593460712 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:02:44 +0100 Subject: [PATCH 03/23] Update publish_gem.yml --- github/workflows/publish_gem.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/workflows/publish_gem.yml b/github/workflows/publish_gem.yml index 998aa646..5b5ec3a1 100644 --- a/github/workflows/publish_gem.yml +++ b/github/workflows/publish_gem.yml @@ -3,7 +3,7 @@ name: Publish RubyGem on: release: types: - - created + - released jobs: build: From e99eefc839bba1a0c1080fbab3aab02f75ca5789 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:22:41 +0100 Subject: [PATCH 04/23] test --- .github/workflows/ruby.yml | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/ruby.yml diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml new file mode 100644 index 00000000..3d479c64 --- /dev/null +++ b/.github/workflows/ruby.yml @@ -0,0 +1,38 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake +# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby + +name: Ruby + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +permissions: + contents: read + +jobs: + test: + + runs-on: ubuntu-latest + strategy: + matrix: + ruby-version: ['2.6', '2.7', '3.0'] + + steps: + - uses: actions/checkout@v3 + - name: Set up Ruby + # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby, + # change this to (see https://github.com/ruby/setup-ruby#versioning): + # uses: ruby/setup-ruby@v1 + uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + - name: Run tests + run: bundle exec rake From 1790b43256699b19aff5506c214671bc9b0ca379 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:23:27 +0100 Subject: [PATCH 05/23] Create publish_gem.yml --- .github/workflows/publish_gem.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .github/workflows/publish_gem.yml diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml new file mode 100644 index 00000000..5b5ec3a1 --- /dev/null +++ b/.github/workflows/publish_gem.yml @@ -0,0 +1,27 @@ +name: Publish RubyGem + +on: + release: + types: + - released + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Set up Ruby + uses: actions/setup-ruby@v1 + with: + ruby-version: 2.1 # Set your desired Ruby version here + + - name: Install Bundler + run: gem install bundler + + - name: Build and publish gem + run: | + bundle install + gem build gem_name.gemspec # Replace 'gem_name' with your gem's name + gem push gem_name-*.gem # Replace 'gem_name' with your gem's name + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 37692ee0f37391485adcf1bedd8cbd1624b6d8b3 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:23:36 +0100 Subject: [PATCH 06/23] Delete ruby.yml --- .github/workflows/ruby.yml | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 .github/workflows/ruby.yml diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml deleted file mode 100644 index 3d479c64..00000000 --- a/.github/workflows/ruby.yml +++ /dev/null @@ -1,38 +0,0 @@ -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. -# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake -# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby - -name: Ruby - -on: - push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] - -permissions: - contents: read - -jobs: - test: - - runs-on: ubuntu-latest - strategy: - matrix: - ruby-version: ['2.6', '2.7', '3.0'] - - steps: - - uses: actions/checkout@v3 - - name: Set up Ruby - # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby, - # change this to (see https://github.com/ruby/setup-ruby#versioning): - # uses: ruby/setup-ruby@v1 - uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0 - with: - ruby-version: ${{ matrix.ruby-version }} - bundler-cache: true # runs 'bundle install' and caches installed gems automatically - - name: Run tests - run: bundle exec rake From 8d9ec76762a8d09df486d9bd95eda74ceab46ec0 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:23:48 +0100 Subject: [PATCH 07/23] Delete github directory --- github/workflows/publish_gem.yml | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 github/workflows/publish_gem.yml diff --git a/github/workflows/publish_gem.yml b/github/workflows/publish_gem.yml deleted file mode 100644 index 5b5ec3a1..00000000 --- a/github/workflows/publish_gem.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Publish RubyGem - -on: - release: - types: - - released - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Set up Ruby - uses: actions/setup-ruby@v1 - with: - ruby-version: 2.1 # Set your desired Ruby version here - - - name: Install Bundler - run: gem install bundler - - - name: Build and publish gem - run: | - bundle install - gem build gem_name.gemspec # Replace 'gem_name' with your gem's name - gem push gem_name-*.gem # Replace 'gem_name' with your gem's name - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 0cf2ebefd55cf8a72d8e1e06802375b19b2407ec Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:25:26 +0100 Subject: [PATCH 08/23] ruby version update --- .github/workflows/publish_gem.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index 5b5ec3a1..0d617c52 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -13,7 +13,7 @@ jobs: - name: Set up Ruby uses: actions/setup-ruby@v1 with: - ruby-version: 2.1 # Set your desired Ruby version here + ruby-version: 3.2 - name: Install Bundler run: gem install bundler From 8101b523af4deb66f63d5362b18f92fec4c48a79 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:29:25 +0100 Subject: [PATCH 09/23] Correcting actions --- .github/workflows/main.yml | 18 ++++++++++++++++++ .github/workflows/publish_gem.yml | 11 +++++------ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..5a5d2222 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,18 @@ +name: Publish RubyGem + +on: + push: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Ruby + uses: actions/setup-ruby@v1 + with: + ruby-version: '3.2' + bundler-cache: true diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index 0d617c52..60aedf28 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -10,18 +10,17 @@ jobs: runs-on: ubuntu-latest steps: + - uses: actions/checkout@v3 - name: Set up Ruby uses: actions/setup-ruby@v1 with: - ruby-version: 3.2 - - - name: Install Bundler - run: gem install bundler + ruby-version: '3.2' + bundler-cache: true - name: Build and publish gem run: | bundle install - gem build gem_name.gemspec # Replace 'gem_name' with your gem's name - gem push gem_name-*.gem # Replace 'gem_name' with your gem's name + gem build caracal.gemspec + gem push caracal-*.gem env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 332dcab7c1af96d43e726547543eaba608fcfa7c Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:30:18 +0100 Subject: [PATCH 10/23] Correcting actions --- .github/workflows/main.yml | 2 +- .github/workflows/publish_gem.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5a5d2222..3dd11997 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,7 +12,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Set up Ruby - uses: actions/setup-ruby@v1 + uses: ruby/setup-ruby@v1 with: ruby-version: '3.2' bundler-cache: true diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index 60aedf28..afc260d9 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -12,7 +12,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Set up Ruby - uses: actions/setup-ruby@v1 + uses: ruby/setup-ruby@v1 with: ruby-version: '3.2' bundler-cache: true From e42ebcd2f92c0b1d3532ad88b55d489a40289bae Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:32:36 +0100 Subject: [PATCH 11/23] Bundler update and tests --- .github/workflows/main.yml | 4 ++++ .github/workflows/publish_gem.yml | 5 ++++- caracal.gemspec | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3dd11997..7f999d65 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,3 +16,7 @@ jobs: with: ruby-version: '3.2' bundler-cache: true + + - name: Tests + run: | + rspec diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index afc260d9..1e7c0d5f 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -17,9 +17,12 @@ jobs: ruby-version: '3.2' bundler-cache: true + - name: Tests + run: | + rspec + - name: Build and publish gem run: | - bundle install gem build caracal.gemspec gem push caracal-*.gem env: diff --git a/caracal.gemspec b/caracal.gemspec index 247be278..1e071f37 100644 --- a/caracal.gemspec +++ b/caracal.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'rubyzip', ['>= 1.1.0', '< 3.0'] spec.add_dependency 'tilt', '>= 1.4' - spec.add_development_dependency 'bundler', '~> 1.3' + spec.add_development_dependency 'bundler', '~> 2.4' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec', '~> 3.0' end From b6f3bc4f6738368205c120d523289cfc245fd463 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:33:29 +0100 Subject: [PATCH 12/23] Bundler update and tests --- .github/workflows/main.yml | 2 +- .github/workflows/publish_gem.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7f999d65..5b5c2085 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,4 +19,4 @@ jobs: - name: Tests run: | - rspec + bundle exec rspec diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index 1e7c0d5f..fe9d07e2 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -19,7 +19,7 @@ jobs: - name: Tests run: | - rspec + bundle exec rspec - name: Build and publish gem run: | From 177968d69b51f049d24dfa8c526b2119c36ad298 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:36:57 +0100 Subject: [PATCH 13/23] Publish tweak --- .github/workflows/publish_gem.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index fe9d07e2..132e349e 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -24,6 +24,6 @@ jobs: - name: Build and publish gem run: | gem build caracal.gemspec - gem push caracal-*.gem + gem push --key github --host https://rubygems.pkg.github.com/the-curve-consulting caracal-*.gem env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 85e800a808a01c5129416cf4db5c8cdaf1792608 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:40:47 +0100 Subject: [PATCH 14/23] Publish tweak --- .github/workflows/publish_gem.yml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index 132e349e..ba4ad332 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -23,7 +23,16 @@ jobs: - name: Build and publish gem run: | - gem build caracal.gemspec - gem push --key github --host https://rubygems.pkg.github.com/the-curve-consulting caracal-*.gem + echo "Setting up access to GitHub Package Registry" + mkdir -p ~/.gem + touch ~/.gem/credentials + chmod 600 ~/.gem/credentials + echo ":github: Bearer ${GITHUB_TOKEN}" >> ~/.gem/credentials + + echo "Building the gem" + find . -name '*.gemspec' -maxdepth 1 -exec gem build {} \; + echo "Pushing the built gem to GitHub Package Registry" + find . -name '*.gem' -maxdepth 1 -print0 | xargs -0 gem push --key github --host "https://rubygems.pkg.github.com/${OWNER}" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + OWNER: the-curve-consulting From 9521ecb9747d3cf0ec699038e267757839264a1c Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:50:29 +0100 Subject: [PATCH 15/23] Publush to rubygems --- .github/workflows/publish_gem.yml | 13 ++----------- caracal.gemspec | 2 +- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index ba4ad332..fe9d07e2 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -23,16 +23,7 @@ jobs: - name: Build and publish gem run: | - echo "Setting up access to GitHub Package Registry" - mkdir -p ~/.gem - touch ~/.gem/credentials - chmod 600 ~/.gem/credentials - echo ":github: Bearer ${GITHUB_TOKEN}" >> ~/.gem/credentials - - echo "Building the gem" - find . -name '*.gemspec' -maxdepth 1 -exec gem build {} \; - echo "Pushing the built gem to GitHub Package Registry" - find . -name '*.gem' -maxdepth 1 -print0 | xargs -0 gem push --key github --host "https://rubygems.pkg.github.com/${OWNER}" + gem build caracal.gemspec + gem push caracal-*.gem env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - OWNER: the-curve-consulting diff --git a/caracal.gemspec b/caracal.gemspec index 1e071f37..29b7095f 100644 --- a/caracal.gemspec +++ b/caracal.gemspec @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'caracal/version' Gem::Specification.new do |spec| - spec.name = 'caracal' + spec.name = 'caracal_the_curve' spec.version = Caracal::VERSION spec.authors = ['Trade Infomatics', 'John Dugan'] spec.email = ['jpdugan@gmail.com'] From d889764be054ab90b3b2d4b53a2c7fe594b25d1e Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:51:23 +0100 Subject: [PATCH 16/23] Publush to rubygems --- .github/workflows/publish_gem.yml | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index fe9d07e2..870b5341 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -23,7 +23,18 @@ jobs: - name: Build and publish gem run: | - gem build caracal.gemspec - gem push caracal-*.gem + cat << EOF > ~/.gem/credentials + --- + :github: Bearer ${GITHUB_TOKEN} + :rubygems_api_key: ${RUBYGEMS_API_KEY} + EOF + + chmod 0600 ~/.gem/credentials + + echo "Running gem release task..." + release_command="${RELEASE_COMMAND:-rake release}" + $release_command env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}} + RELEASE_COMMAND: rake release From 60bc85155bc52fcce1cc7aa4fe65feb0c526f26f Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:52:58 +0100 Subject: [PATCH 17/23] Publush to rubygems --- .github/workflows/publish_gem.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index 870b5341..065ca2a4 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -23,6 +23,7 @@ jobs: - name: Build and publish gem run: | + mkdir -p ~/.gem cat << EOF > ~/.gem/credentials --- :github: Bearer ${GITHUB_TOKEN} From 3509e07ef405a131ba1853868135481edb949db7 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Aug 2023 10:55:11 +0100 Subject: [PATCH 18/23] Publush to rubygems --- .github/workflows/publish_gem.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish_gem.yml b/.github/workflows/publish_gem.yml index 065ca2a4..a0e57c30 100644 --- a/.github/workflows/publish_gem.yml +++ b/.github/workflows/publish_gem.yml @@ -32,9 +32,8 @@ jobs: chmod 0600 ~/.gem/credentials - echo "Running gem release task..." - release_command="${RELEASE_COMMAND:-rake release}" - $release_command + find . -name '*.gemspec' -maxdepth 1 -exec gem build {} \; + find . -name '*.gem' -maxdepth 1 -print0 | xargs -0 gem push env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}} From c48f27607416f79e81720ce6c810eeae5bd0f1b2 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sun, 21 Jan 2024 20:42:29 +0000 Subject: [PATCH 19/23] Attempting to add header and footer support --- lib/caracal.rb | 16 ++ lib/caracal/core/footer.rb | 33 +++++ lib/caracal/core/header.rb | 32 ++++ lib/caracal/core/models/field_model.rb | 137 ++++++++++++++++++ lib/caracal/core/models/footer_model.rb | 42 ++++++ lib/caracal/core/models/header_model.rb | 42 ++++++ lib/caracal/core/models/paragraph_model.rb | 15 ++ lib/caracal/core/models/relationship_model.rb | 1 + lib/caracal/core/relationships.rb | 1 + lib/caracal/document.rb | 20 +++ .../renderers/content_types_renderer.rb | 1 + lib/caracal/renderers/document_renderer.rb | 25 +++- lib/caracal/renderers/footer_renderer.rb | 72 +++++---- lib/caracal/renderers/header_renderer.rb | 62 ++++++++ spec/lib/caracal/core/footer_spec.rb | 31 ++++ spec/lib/caracal/core/header_spec.rb | 31 ++++ .../caracal/core/models/footer_model_spec.rb | 40 +++++ .../caracal/core/models/header_model_spec.rb | 40 +++++ .../core/models/paragraph_model_spec.rb | 19 ++- spec/lib/caracal/core/relationships_spec.rb | 4 +- 20 files changed, 626 insertions(+), 38 deletions(-) create mode 100644 lib/caracal/core/footer.rb create mode 100644 lib/caracal/core/header.rb create mode 100644 lib/caracal/core/models/field_model.rb create mode 100644 lib/caracal/core/models/footer_model.rb create mode 100644 lib/caracal/core/models/header_model.rb create mode 100644 lib/caracal/renderers/header_renderer.rb create mode 100644 spec/lib/caracal/core/footer_spec.rb create mode 100644 spec/lib/caracal/core/header_spec.rb create mode 100644 spec/lib/caracal/core/models/footer_model_spec.rb create mode 100644 spec/lib/caracal/core/models/header_model_spec.rb diff --git a/lib/caracal.rb b/lib/caracal.rb index 2c5fdb75..25effd98 100644 --- a/lib/caracal.rb +++ b/lib/caracal.rb @@ -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 diff --git a/lib/caracal/core/footer.rb b/lib/caracal/core/footer.rb new file mode 100644 index 00000000..fb430435 --- /dev/null +++ b/lib/caracal/core/footer.rb @@ -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 diff --git a/lib/caracal/core/header.rb b/lib/caracal/core/header.rb new file mode 100644 index 00000000..c37d5434 --- /dev/null +++ b/lib/caracal/core/header.rb @@ -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 diff --git a/lib/caracal/core/models/field_model.rb b/lib/caracal/core/models/field_model.rb new file mode 100644 index 00000000..ea724013 --- /dev/null +++ b/lib/caracal/core/models/field_model.rb @@ -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 diff --git a/lib/caracal/core/models/footer_model.rb b/lib/caracal/core/models/footer_model.rb new file mode 100644 index 00000000..161ea563 --- /dev/null +++ b/lib/caracal/core/models/footer_model.rb @@ -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 diff --git a/lib/caracal/core/models/header_model.rb b/lib/caracal/core/models/header_model.rb new file mode 100644 index 00000000..c432bb3d --- /dev/null +++ b/lib/caracal/core/models/header_model.rb @@ -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 diff --git a/lib/caracal/core/models/paragraph_model.rb b/lib/caracal/core/models/paragraph_model.rb index 5eef1254..7b61bc6b 100644 --- a/lib/caracal/core/models/paragraph_model.rb +++ b/lib/caracal/core/models/paragraph_model.rb @@ -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' @@ -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) diff --git a/lib/caracal/core/models/relationship_model.rb b/lib/caracal/core/models/relationship_model.rb index c20fe2cb..0780062d 100644 --- a/lib/caracal/core/models/relationship_model.rb +++ b/lib/caracal/core/models/relationship_model.rb @@ -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', diff --git a/lib/caracal/core/relationships.rb b/lib/caracal/core/relationships.rb index e4b9316f..26853d7f 100644 --- a/lib/caracal/core/relationships.rb +++ b/lib/caracal/core/relationships.rb @@ -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 } diff --git a/lib/caracal/document.rb b/lib/caracal/document.rb index 66fdc368..b50e3fb5 100644 --- a/lib/caracal/document.rb +++ b/lib/caracal/document.rb @@ -5,6 +5,8 @@ require 'caracal/core/custom_properties' require 'caracal/core/file_name' require 'caracal/core/fonts' +require 'caracal/core/footer' +require 'caracal/core/header' require 'caracal/core/iframes' require 'caracal/core/ignorables' require 'caracal/core/images' @@ -27,6 +29,7 @@ require 'caracal/renderers/document_renderer' require 'caracal/renderers/fonts_renderer' require 'caracal/renderers/footer_renderer' +require 'caracal/renderers/header_renderer' require 'caracal/renderers/numbering_renderer' require 'caracal/renderers/package_relationships_renderer' require 'caracal/renderers/relationships_renderer' @@ -63,6 +66,8 @@ class Document include Caracal::Core::Tables include Caracal::Core::Text + include Caracal::Core::Footer + include Caracal::Core::Header #------------------------------------------------------ # Public Class Methods @@ -129,6 +134,13 @@ def contents @contents ||= [] end + def footer_content + @footer_content + end + + def header_content + @header_content + end #============ RENDERING =============================== @@ -144,6 +156,7 @@ def render render_custom(zip) render_fonts(zip) render_footer(zip) + render_header(zip) render_settings(zip) render_styles(zip) render_document(zip) @@ -219,6 +232,13 @@ def render_footer(zip) zip.write(content) end + def render_header(zip) + content = ::Caracal::Renderers::HeaderRenderer.render(self) + + zip.put_next_entry('word/header1.xml') + zip.write(content) + end + def render_media(zip) images = relationships.select { |r| r.relationship_type == :image } images.each do |rel| diff --git a/lib/caracal/renderers/content_types_renderer.rb b/lib/caracal/renderers/content_types_renderer.rb index 0054159d..99597d9b 100644 --- a/lib/caracal/renderers/content_types_renderer.rb +++ b/lib/caracal/renderers/content_types_renderer.rb @@ -28,6 +28,7 @@ def to_xml xml.send 'Override', { 'PartName' => '/docProps/custom.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.custom-properties+xml' } xml.send 'Override', { 'PartName' => '/word/document.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml' } xml.send 'Override', { 'PartName' => '/word/footer1.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml' } + xml.send 'Override', { 'PartName' => '/word/header1.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml' } xml.send 'Override', { 'PartName' => '/word/fontTable.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml' } xml.send 'Override', { 'PartName' => '/word/numbering.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml' } xml.send 'Override', { 'PartName' => '/word/settings.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml' } diff --git a/lib/caracal/renderers/document_renderer.rb b/lib/caracal/renderers/document_renderer.rb index 5b14b3fb..d2519952 100644 --- a/lib/caracal/renderers/document_renderer.rb +++ b/lib/caracal/renderers/document_renderer.rb @@ -31,10 +31,11 @@ def to_xml #============= PAGE SETTINGS ============================== xml['w'].sectPr do - if document.page_number_show - if rel = document.find_relationship('footer1.xml') - xml['w'].footerReference({ 'r:id' => rel.formatted_id, 'w:type' => 'default' }) - end + if rel = document.find_relationship('footer1.xml') + xml['w'].footerReference({ 'r:id' => rel.formatted_id, 'w:type' => 'default' }) + end + if rel = document.find_relationship('header1.xml') + xml['w'].headerReference({ 'r:id' => rel.formatted_id, 'w:type' => 'default' }) end xml['w'].pgSz page_size_options xml['w'].pgMar page_margin_options @@ -219,6 +220,20 @@ def render_link(xml, model) end end + def render_field(xml, model) + xml['w'].fldChar({ 'w:fldCharType' => 'begin' }) + xml['w'].r do + xml['w'].rPr do + render_run_attributes(xml, model, false) + end + xml['w'].instrText({ 'xml:space' => 'preserve' }) do + xml.text model.formatted_type + end + end + xml['w'].fldChar({ 'w:fldCharType' => 'separate' }) + xml['w'].fldChar({ 'w:fldCharType' => 'end' }) + end + def render_list(xml, model) if model.list_level == 0 document.toplevel_lists << model @@ -424,4 +439,4 @@ def page_size_options end end -end +end \ No newline at end of file diff --git a/lib/caracal/renderers/footer_renderer.rb b/lib/caracal/renderers/footer_renderer.rb index 3e6a78e3..cd491917 100644 --- a/lib/caracal/renderers/footer_renderer.rb +++ b/lib/caracal/renderers/footer_renderer.rb @@ -5,53 +5,65 @@ module Caracal module Renderers - class FooterRenderer < XmlRenderer + class FooterRenderer < DocumentRenderer #------------------------------------------------------------- # Public Methods #------------------------------------------------------------- - # This method produces the xml required for the `word/settings.xml` + # This method produces the xml required for the `word/footer1.xml` # sub-document. # def to_xml builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml| - xml['w'].ftr root_options do - xml['w'].p paragraph_options do - xml['w'].pPr do - xml['w'].contextualSpacing({ 'w:val' => '0' }) - xml['w'].jc({ 'w:val' => "#{ document.page_number_align }" }) - end - unless document.page_number_label.nil? + if document.page_number_show + xml['w'].ftr root_options do + xml['w'].p paragraph_options do + xml['w'].pPr do + xml['w'].contextualSpacing({ 'w:val' => '0' }) + xml['w'].jc({ 'w:val' => "#{ document.page_number_align }" }) + end + unless document.page_number_label.nil? + xml['w'].r run_options do + xml['w'].rPr do + xml['w'].rStyle({ 'w:val' => 'PageNumber' }) + unless document.page_number_label_size.nil? + xml['w'].sz({ 'w:val' => document.page_number_label_size }) + end + end + xml['w'].t({ 'xml:space' => 'preserve' }) do + xml.text "#{ document.page_number_label } " + end + end + end xml['w'].r run_options do xml['w'].rPr do - xml['w'].rStyle({ 'w:val' => 'PageNumber' }) - unless document.page_number_label_size.nil? - xml['w'].sz({ 'w:val' => document.page_number_label_size }) + unless document.page_number_number_size.nil? + xml['w'].sz({ 'w:val' => document.page_number_number_size }) + xml['w'].szCs({ 'w:val' => document.page_number_number_size }) end end - xml['w'].t({ 'xml:space' => 'preserve' }) do - xml.text "#{ document.page_number_label } " + xml['w'].fldChar({ 'w:fldCharType' => 'begin' }) + xml['w'].instrText({ 'xml:space' => 'preserve' }) do + xml.text 'PAGE' end + xml['w'].fldChar({ 'w:fldCharType' => 'end' }) end - end - xml['w'].r run_options do - xml['w'].rPr do - unless document.page_number_number_size.nil? - xml['w'].sz({ 'w:val' => document.page_number_number_size }) - xml['w'].szCs({ 'w:val' => document.page_number_number_size }) + xml['w'].r run_options do + xml['w'].rPr do + xml['w'].rtl({ 'w:val' => '0' }) end end - xml['w'].fldChar({ 'w:fldCharType' => 'begin' }) - xml['w'].instrText({ 'xml:space' => 'preserve' }) do - xml.text 'PAGE' - end - xml['w'].fldChar({ 'w:fldCharType' => 'end' }) end - xml['w'].r run_options do - xml['w'].rPr do - xml['w'].rtl({ 'w:val' => '0' }) - end + end + elsif document.footer_content.present? && document.footer_content.valid? + xml['w'].ftr root_options do + + #============= CONTENTS =================================== + + document.footer_content.contents.each do |model| + method = render_method_for_model(model) + send(method, xml, model) end end end @@ -87,4 +99,4 @@ def root_options end end -end +end \ No newline at end of file diff --git a/lib/caracal/renderers/header_renderer.rb b/lib/caracal/renderers/header_renderer.rb new file mode 100644 index 00000000..fda9d067 --- /dev/null +++ b/lib/caracal/renderers/header_renderer.rb @@ -0,0 +1,62 @@ +require 'nokogiri' + +require 'caracal/renderers/xml_renderer' + + +module Caracal + module Renderers + class HeaderRenderer < DocumentRenderer + + #------------------------------------------------------------- + # Public Methods + #------------------------------------------------------------- + + # This method produces the xml required for the `word/header1.xml` + # sub-document. + # + def to_xml + builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml| + if document.header_content.present? && document.header_content.valid? + xml['w'].hdr root_options do + + #============= CONTENTS =================================== + + document.header_content.contents.each do |model| + method = render_method_for_model(model) + send(method, xml, model) + end + end + end + end + builder.to_xml(save_options) + end + + + #------------------------------------------------------------- + # Private Methods + #------------------------------------------------------------- + private + + def root_options + { + 'xmlns:mc' => 'http://schemas.openxmlformats.org/markup-compatibility/2006', + 'xmlns:o' => 'urn:schemas-microsoft-com:office:office', + 'xmlns:r' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships', + 'xmlns:m' => 'http://schemas.openxmlformats.org/officeDocument/2006/math', + 'xmlns:v' => 'urn:schemas-microsoft-com:vml', + 'xmlns:wp' => 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing', + 'xmlns:w10' => 'urn:schemas-microsoft-com:office:word', + 'xmlns:w' => 'http://schemas.openxmlformats.org/wordprocessingml/2006/main', + 'xmlns:wne' => 'http://schemas.microsoft.com/office/word/2006/wordml', + 'xmlns:sl' => 'http://schemas.openxmlformats.org/schemaLibrary/2006/main', + 'xmlns:a' => 'http://schemas.openxmlformats.org/drawingml/2006/main', + 'xmlns:pic' => 'http://schemas.openxmlformats.org/drawingml/2006/picture', + 'xmlns:c' => 'http://schemas.openxmlformats.org/drawingml/2006/chart', + 'xmlns:lc' => 'http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas', + 'xmlns:dgm' => 'http://schemas.openxmlformats.org/drawingml/2006/diagram' + } + end + + end + end +end diff --git a/spec/lib/caracal/core/footer_spec.rb b/spec/lib/caracal/core/footer_spec.rb new file mode 100644 index 00000000..8592605e --- /dev/null +++ b/spec/lib/caracal/core/footer_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe Caracal::Core::Text do + subject { Caracal::Document.new } + + #------------------------------------------------------------- + # Configuration + #------------------------------------------------------------- + + describe 'configuration tests' do + + # accessors + describe 'accessors' do + it { expect(subject.footer_content).to be_nil } + end + end + + #------------------------------------------------------------- + # Public Methods + #------------------------------------------------------------- + + describe 'public method tests' do + + describe '.footer' do + before { subject.footer } + + it { expect(subject.footer_content).not_to be_nil } + it { expect(subject.footer_content).to be_a(Caracal::Core::Models::FooterModel) } + end + end +end diff --git a/spec/lib/caracal/core/header_spec.rb b/spec/lib/caracal/core/header_spec.rb new file mode 100644 index 00000000..2648e631 --- /dev/null +++ b/spec/lib/caracal/core/header_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe Caracal::Core::Text do + subject { Caracal::Document.new } + + #------------------------------------------------------------- + # Configuration + #------------------------------------------------------------- + + describe 'configuration tests' do + + # accessors + describe 'accessors' do + it { expect(subject.header_content).to be_nil } + end + end + + #------------------------------------------------------------- + # Public Methods + #------------------------------------------------------------- + + describe 'public method tests' do + + describe '.header' do + before { subject.header } + + it { expect(subject.header_content).not_to be_nil } + it { expect(subject.header_content).to be_a(Caracal::Core::Models::HeaderModel) } + end + end +end diff --git a/spec/lib/caracal/core/models/footer_model_spec.rb b/spec/lib/caracal/core/models/footer_model_spec.rb new file mode 100644 index 00000000..6354a07e --- /dev/null +++ b/spec/lib/caracal/core/models/footer_model_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe Caracal::Core::Models::FooterModel do + subject do + described_class.new + end + + #------------------------------------------------------------- + # Public Methods + #------------------------------------------------------------- + + describe 'public method tests' do + + #=============== DATA ACCESSORS ==================== + + describe 'data tests' do + + # .contents + describe '.contents' do + it { expect(subject.contents).to be_a(Array) } + end + end + + #=============== VALIDATION ======================== + + describe '.valid?' do + describe 'when content provided' do + before { allow(subject).to receive(:contents).and_return(['a']) } + + it { expect(subject.valid?).to eq true } + end + + describe 'when content not provided' do + before { allow(subject).to receive(:contents).and_return([]) } + + it { expect(subject.valid?).to eq false } + end + end + end +end diff --git a/spec/lib/caracal/core/models/header_model_spec.rb b/spec/lib/caracal/core/models/header_model_spec.rb new file mode 100644 index 00000000..cca490af --- /dev/null +++ b/spec/lib/caracal/core/models/header_model_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe Caracal::Core::Models::HeaderModel do + subject do + described_class.new + end + + #------------------------------------------------------------- + # Public Methods + #------------------------------------------------------------- + + describe 'public method tests' do + + #=============== DATA ACCESSORS ==================== + + describe 'data tests' do + + # .contents + describe '.contents' do + it { expect(subject.contents).to be_a(Array) } + end + end + + #=============== VALIDATION ======================== + + describe '.valid?' do + describe 'when content provided' do + before { allow(subject).to receive(:contents).and_return(['a']) } + + it { expect(subject.valid?).to eq true } + end + + describe 'when content not provided' do + before { allow(subject).to receive(:contents).and_return([]) } + + it { expect(subject.valid?).to eq false } + end + end + end +end diff --git a/spec/lib/caracal/core/models/paragraph_model_spec.rb b/spec/lib/caracal/core/models/paragraph_model_spec.rb index c70f803e..ab29ecac 100644 --- a/spec/lib/caracal/core/models/paragraph_model_spec.rb +++ b/spec/lib/caracal/core/models/paragraph_model_spec.rb @@ -137,6 +137,23 @@ it { expect(subject.runs.size).to eq length + 1 } end + # .field + describe '.field' do + let!(:length) { subject.runs.length } + + context ':page' do + before { subject.field :page } + + it { expect(subject.runs.size).to eq length + 1 } + end + + context ':numpages' do + before { subject.field :numpages } + + it { expect(subject.runs.size).to eq length + 1 } + end + end + # .bookmark describe '.bookmark_start' do let!(:length) { subject.runs.length } @@ -149,7 +166,7 @@ describe '.bookmark_end' do let!(:length) { subject.runs.length } - before { subject.bookmark_end {id:'1'}} + before { subject.bookmark_end(id:'1')} it { expect(subject.runs.size).to eq length + 1 } end diff --git a/spec/lib/caracal/core/relationships_spec.rb b/spec/lib/caracal/core/relationships_spec.rb index 40ad3bbb..547ab707 100644 --- a/spec/lib/caracal/core/relationships_spec.rb +++ b/spec/lib/caracal/core/relationships_spec.rb @@ -15,7 +15,7 @@ # .default_relationships describe '.default_relationships' do - let(:expected) { [:font, :footer, :numbering, :setting, :style] } + let(:expected) { [:font, :footer, :header, :numbering, :setting, :style] } let(:actual) { subject.class.default_relationships.map { |r| r[:type] } } it { expect(actual).to eq expected } @@ -105,7 +105,7 @@ it { expect(subject.relationships.size).to eq default_length } end describe 'when not registered' do - before do + before do subject.register_relationship(m1) subject.unregister_relationship(m2.relationship_target) end From 16c49a52b1fab55fdf84b51f95c374ecb2223c74 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sun, 21 Jan 2024 20:46:11 +0000 Subject: [PATCH 20/23] Nil fix --- lib/caracal/renderers/footer_renderer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/caracal/renderers/footer_renderer.rb b/lib/caracal/renderers/footer_renderer.rb index cd491917..ba380199 100644 --- a/lib/caracal/renderers/footer_renderer.rb +++ b/lib/caracal/renderers/footer_renderer.rb @@ -56,7 +56,7 @@ def to_xml end end end - elsif document.footer_content.present? && document.footer_content.valid? + elsif document.footer_content&.present? && document.footer_content&.valid? xml['w'].ftr root_options do #============= CONTENTS =================================== From b40aabfa6b2675f27c39eb7e7f18320ae6e41380 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sun, 21 Jan 2024 21:09:06 +0000 Subject: [PATCH 21/23] Merge field fix? --- lib/caracal/renderers/footer_renderer.rb | 6 +++--- lib/caracal/renderers/header_renderer.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/caracal/renderers/footer_renderer.rb b/lib/caracal/renderers/footer_renderer.rb index ba380199..96c19a52 100644 --- a/lib/caracal/renderers/footer_renderer.rb +++ b/lib/caracal/renderers/footer_renderer.rb @@ -32,7 +32,7 @@ def to_xml end end xml['w'].t({ 'xml:space' => 'preserve' }) do - xml.text "#{ document.page_number_label } " + xml.text "#{ document.page_number_label } \\* MERGEFORMAT" end end end @@ -45,7 +45,7 @@ def to_xml end xml['w'].fldChar({ 'w:fldCharType' => 'begin' }) xml['w'].instrText({ 'xml:space' => 'preserve' }) do - xml.text 'PAGE' + xml.text 'PAGE \* MERGEFORMAT' end xml['w'].fldChar({ 'w:fldCharType' => 'end' }) end @@ -56,7 +56,7 @@ def to_xml end end end - elsif document.footer_content&.present? && document.footer_content&.valid? + elsif document.footer_content&.valid? xml['w'].ftr root_options do #============= CONTENTS =================================== diff --git a/lib/caracal/renderers/header_renderer.rb b/lib/caracal/renderers/header_renderer.rb index fda9d067..ae299c58 100644 --- a/lib/caracal/renderers/header_renderer.rb +++ b/lib/caracal/renderers/header_renderer.rb @@ -16,7 +16,7 @@ class HeaderRenderer < DocumentRenderer # def to_xml builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml| - if document.header_content.present? && document.header_content.valid? + if document.header_content&.valid? xml['w'].hdr root_options do #============= CONTENTS =================================== From 1ee54c01d9d0d6ce9ffaaf3fd64d0f99a302446c Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sun, 21 Jan 2024 21:52:17 +0000 Subject: [PATCH 22/23] Fixing page numbers! --- lib/caracal/renderers/document_renderer.rb | 14 ++++++++++---- lib/caracal/renderers/footer_renderer.rb | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/caracal/renderers/document_renderer.rb b/lib/caracal/renderers/document_renderer.rb index d2519952..6cbd6aa1 100644 --- a/lib/caracal/renderers/document_renderer.rb +++ b/lib/caracal/renderers/document_renderer.rb @@ -221,17 +221,23 @@ def render_link(xml, model) end def render_field(xml, model) - xml['w'].fldChar({ 'w:fldCharType' => 'begin' }) + xml['w'].r do + xml['w'].fldChar({ 'w:fldCharType' => 'begin' }) + end xml['w'].r do xml['w'].rPr do render_run_attributes(xml, model, false) end xml['w'].instrText({ 'xml:space' => 'preserve' }) do - xml.text model.formatted_type + xml.text " #{model.formatted_type} " end end - xml['w'].fldChar({ 'w:fldCharType' => 'separate' }) - xml['w'].fldChar({ 'w:fldCharType' => 'end' }) + xml['w'].r do + xml['w'].fldChar({ 'w:fldCharType' => 'separate' }) + end + xml['w'].r do + xml['w'].fldChar({ 'w:fldCharType' => 'end' }) + end end def render_list(xml, model) diff --git a/lib/caracal/renderers/footer_renderer.rb b/lib/caracal/renderers/footer_renderer.rb index 96c19a52..f20dd692 100644 --- a/lib/caracal/renderers/footer_renderer.rb +++ b/lib/caracal/renderers/footer_renderer.rb @@ -32,7 +32,7 @@ def to_xml end end xml['w'].t({ 'xml:space' => 'preserve' }) do - xml.text "#{ document.page_number_label } \\* MERGEFORMAT" + xml.text "#{ document.page_number_label } " end end end @@ -45,7 +45,7 @@ def to_xml end xml['w'].fldChar({ 'w:fldCharType' => 'begin' }) xml['w'].instrText({ 'xml:space' => 'preserve' }) do - xml.text 'PAGE \* MERGEFORMAT' + xml.text 'PAGE' end xml['w'].fldChar({ 'w:fldCharType' => 'end' }) end From 8ab688fca8d328181ec10e4926761b3c69371a9b Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sun, 21 Jan 2024 22:01:08 +0000 Subject: [PATCH 23/23] Adds support for non-existant header/footer --- lib/caracal/renderers/footer_renderer.rb | 11 ++++++----- lib/caracal/renderers/header_renderer.rb | 5 ++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/caracal/renderers/footer_renderer.rb b/lib/caracal/renderers/footer_renderer.rb index f20dd692..fc8a48b9 100644 --- a/lib/caracal/renderers/footer_renderer.rb +++ b/lib/caracal/renderers/footer_renderer.rb @@ -56,14 +56,15 @@ def to_xml end end end - elsif document.footer_content&.valid? + else xml['w'].ftr root_options do #============= CONTENTS =================================== - - document.footer_content.contents.each do |model| - method = render_method_for_model(model) - send(method, xml, model) + if document.footer_content&.valid? + document.footer_content.contents.each do |model| + method = render_method_for_model(model) + send(method, xml, model) + end end end end diff --git a/lib/caracal/renderers/header_renderer.rb b/lib/caracal/renderers/header_renderer.rb index ae299c58..0861c6c5 100644 --- a/lib/caracal/renderers/header_renderer.rb +++ b/lib/caracal/renderers/header_renderer.rb @@ -16,11 +16,10 @@ class HeaderRenderer < DocumentRenderer # def to_xml builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml| - if document.header_content&.valid? - xml['w'].hdr root_options do + xml['w'].hdr root_options do #============= CONTENTS =================================== - + if document.header_content&.valid? document.header_content.contents.each do |model| method = render_method_for_model(model) send(method, xml, model)