From b9a510c70742dd902dc93d0a4110c168b18ca272 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Thu, 7 Jan 2021 21:02:51 +0000 Subject: [PATCH 01/64] Switch to GitHub Actions --- .github/workflows/ci.yml | 53 ++++++++++++++++++++++++++++++++++++++ .github/workflows/lint.yml | 21 +++++++++++++++ .travis.yml => travis.yml | 0 3 files changed, 74 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/lint.yml rename .travis.yml => travis.yml (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..43a70494 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,53 @@ +name: Ruby CI + +on: + pull_request: + types: [opened, synchronize, reopened] + push: + branches: + - main + +jobs: + test: + env: + NOKOGIRI_USE_SYSTEM_LIBRARIES: true + runs-on: ubuntu-latest + + strategy: + fail-fast: true + matrix: + gemfile: + - gemfiles/rails_6.gemfile + - gemfiles/rails_5.gemfile + - gemfiles/rails_4.gemfile + - gemfiles/rails_3.gemfile + ruby-version: + - 3.0.0 + - 2.7.2 + - 2.6.6 + - 2.5.8 + experimental: [false] + include: + - ruby-version: head + experimental: true + - ruby-version: truffleruby-head + experimental: true + + continue-on-error: ${{ matrix.experimental }} + + env: + BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile + + steps: + - uses: actions/checkout@v2 + - name: Install libxslt and libxml2 + run: sudo apt-get install libxslt-dev libxml2-dev + - name: Set up Ruby ${{ matrix.ruby-version }} + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true + - name: Install dependencies + run: bundle install + - name: Run tests + run: bundle exec rake diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..fa9e7c11 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,21 @@ +name: Linting + +on: + pull_request: + types: [opened, synchronize, reopened] + push: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.0 + bundler-cache: true + - run: bundle install + - name: Rubocop + run: bundle exec rake rubocop diff --git a/.travis.yml b/travis.yml similarity index 100% rename from .travis.yml rename to travis.yml From 553eecea36c17067eeab4ddd971ca607d97e2c73 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Thu, 7 Jan 2021 21:09:26 +0000 Subject: [PATCH 02/64] Drop double env --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 43a70494..da9be7ea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,8 +9,6 @@ on: jobs: test: - env: - NOKOGIRI_USE_SYSTEM_LIBRARIES: true runs-on: ubuntu-latest strategy: From 30b49fdac356e4c18574da4dfde5d4826126d3aa Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Thu, 7 Jan 2021 21:11:32 +0000 Subject: [PATCH 03/64] Let it fail --- .github/workflows/lint.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index fa9e7c11..b8a5c0ed 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -10,6 +10,7 @@ on: jobs: test: runs-on: ubuntu-latest + continue-on-error: true steps: - uses: actions/checkout@v2 - uses: ruby/setup-ruby@v1 From 1d31f832030fd95e7f9cbd036722be7e3e9a0391 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Thu, 7 Jan 2021 21:11:38 +0000 Subject: [PATCH 04/64] Drop extension --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index da9be7ea..a33c6db4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,10 +15,10 @@ jobs: fail-fast: true matrix: gemfile: - - gemfiles/rails_6.gemfile - - gemfiles/rails_5.gemfile - - gemfiles/rails_4.gemfile - - gemfiles/rails_3.gemfile + - rails_6 + - rails_5 + - rails_4 + - rails_3 ruby-version: - 3.0.0 - 2.7.2 From 10415172dd16e982da9683cd46dc8fa5afa19bf6 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Thu, 7 Jan 2021 21:33:59 +0000 Subject: [PATCH 05/64] Add bundler args --- .github/workflows/ci.yml | 2 +- html-pipeline.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a33c6db4..be95382c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,6 +46,6 @@ jobs: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true - name: Install dependencies - run: bundle install + run: bundle install --path ../../vendor/bundle - name: Run tests run: bundle exec rake diff --git a/html-pipeline.gemspec b/html-pipeline.gemspec index ec88cfe2..e87e5263 100644 --- a/html-pipeline.gemspec +++ b/html-pipeline.gemspec @@ -8,7 +8,7 @@ Gem::Specification.new do |gem| gem.license = 'MIT' gem.authors = ['Ryan Tomayko', 'Jerry Cheung', 'Garen J. Torikian'] gem.email = ['ryan@github.com', 'jerry@github.com', 'gjtorikian@gmail.com'] - gem.description = 'GitHub HTML processing filters and utilities' + gem.description = 'HTML processing filters and utilities' gem.summary = 'Helpers for processing content through a chain of filters' gem.homepage = 'https://github.com/jch/html-pipeline' From f49a5573ccf1b0999c8e08186b922bac9694bfbd Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Thu, 7 Jan 2021 21:35:13 +0000 Subject: [PATCH 06/64] Remove copy-paste --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be95382c..8b8bae85 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,8 +38,6 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Install libxslt and libxml2 - run: sudo apt-get install libxslt-dev libxml2-dev - name: Set up Ruby ${{ matrix.ruby-version }} uses: ruby/setup-ruby@v1 with: From 818af6bc72c9bb2cbd87ea392e2fddcf0207fc51 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Fri, 8 Jan 2021 09:53:59 +0000 Subject: [PATCH 07/64] Drop old Rails and unnecessary active_support --- .github/workflows/ci.yml | 8 ------ Appraisals | 19 -------------- Gemfile | 7 +++-- gemfiles/rails_3.gemfile | 27 ------------------- gemfiles/rails_4.gemfile | 27 ------------------- gemfiles/rails_5.gemfile | 26 ------------------- gemfiles/rails_6.gemfile | 26 ------------------- html-pipeline.gemspec | 6 +++-- lib/html/pipeline.rb | 1 - lib/html/pipeline/emoji_filter.rb | 13 ++++++---- test/test_helper.rb | 6 ++--- travis.yml | 43 ------------------------------- 12 files changed, 19 insertions(+), 190 deletions(-) delete mode 100644 Appraisals delete mode 100644 gemfiles/rails_3.gemfile delete mode 100644 gemfiles/rails_4.gemfile delete mode 100644 gemfiles/rails_5.gemfile delete mode 100644 gemfiles/rails_6.gemfile delete mode 100644 travis.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8b8bae85..9658b3dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,11 +14,6 @@ jobs: strategy: fail-fast: true matrix: - gemfile: - - rails_6 - - rails_5 - - rails_4 - - rails_3 ruby-version: - 3.0.0 - 2.7.2 @@ -33,9 +28,6 @@ jobs: continue-on-error: ${{ matrix.experimental }} - env: - BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile - steps: - uses: actions/checkout@v2 - name: Set up Ruby ${{ matrix.ruby-version }} diff --git a/Appraisals b/Appraisals deleted file mode 100644 index 6037fd68..00000000 --- a/Appraisals +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -appraise 'rails-3' do - gem 'rack', '< 2' - gem 'rails', '3.2.22.2' -end - -appraise 'rails-4' do - gem 'rack', '< 2' - gem 'rails', '~> 4.2.6' -end - -appraise 'rails-5' do - gem 'rails', '~> 5.0.0' -end - -appraise 'rails-6' do - gem 'rails', '~> 6.0.0' -end diff --git a/Gemfile b/Gemfile index adc923bd..89e7c6fa 100644 --- a/Gemfile +++ b/Gemfile @@ -5,8 +5,11 @@ source 'https://rubygems.org' # Specify your gem's dependencies in html-pipeline.gemspec gemspec +group :development, :test do + gem 'awesome_print' +end + group :development do - gem 'appraisal' gem 'bundler' gem 'rake' end @@ -21,6 +24,6 @@ group :test do gem 'sanitize', '~> 4.6', require: false gem 'escape_utils', '~> 1.0', require: false - gem 'rouge', '~> 3.1', require: false gem 'minitest-focus', '~> 1.1' + gem 'rouge', '~> 3.1', require: false end diff --git a/gemfiles/rails_3.gemfile b/gemfiles/rails_3.gemfile deleted file mode 100644 index d3497431..00000000 --- a/gemfiles/rails_3.gemfile +++ /dev/null @@ -1,27 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "rack", "< 2" -gem "rails", "3.2.22.2" - -group :development do - gem "appraisal" - gem "bundler" - gem "rake" -end - -group :test do - gem "commonmarker", "~> 0.16", require: false - gem "email_reply_parser", "~> 0.5", require: false - gem "gemoji", "~> 2.0", require: false - gem "minitest" - gem "RedCloth", "~> 4.2.9", require: false - gem "rinku", "~> 1.7", require: false - gem "sanitize", "~> 4.6", require: false - gem "escape_utils", "~> 1.0", require: false - gem "rouge", "~> 3.1", require: false - gem "minitest-focus", "~> 1.1" -end - -gemspec path: "../" diff --git a/gemfiles/rails_4.gemfile b/gemfiles/rails_4.gemfile deleted file mode 100644 index a00d20ad..00000000 --- a/gemfiles/rails_4.gemfile +++ /dev/null @@ -1,27 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "rack", "< 2" -gem "rails", "~> 4.2.6" - -group :development do - gem "appraisal" - gem "bundler" - gem "rake" -end - -group :test do - gem "commonmarker", "~> 0.16", require: false - gem "email_reply_parser", "~> 0.5", require: false - gem "gemoji", "~> 2.0", require: false - gem "minitest" - gem "RedCloth", "~> 4.2.9", require: false - gem "rinku", "~> 1.7", require: false - gem "sanitize", "~> 4.6", require: false - gem "escape_utils", "~> 1.0", require: false - gem "rouge", "~> 3.1", require: false - gem "minitest-focus", "~> 1.1" -end - -gemspec path: "../" diff --git a/gemfiles/rails_5.gemfile b/gemfiles/rails_5.gemfile deleted file mode 100644 index 1f64a4f9..00000000 --- a/gemfiles/rails_5.gemfile +++ /dev/null @@ -1,26 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "rails", "~> 5.0.0" - -group :development do - gem "appraisal" - gem "bundler" - gem "rake" -end - -group :test do - gem "commonmarker", "~> 0.16", require: false - gem "email_reply_parser", "~> 0.5", require: false - gem "gemoji", "~> 2.0", require: false - gem "minitest" - gem "RedCloth", "~> 4.2.9", require: false - gem "rinku", "~> 1.7", require: false - gem "sanitize", "~> 4.6", require: false - gem "escape_utils", "~> 1.0", require: false - gem "rouge", "~> 3.1", require: false - gem "minitest-focus", "~> 1.1" -end - -gemspec path: "../" diff --git a/gemfiles/rails_6.gemfile b/gemfiles/rails_6.gemfile deleted file mode 100644 index 134e8425..00000000 --- a/gemfiles/rails_6.gemfile +++ /dev/null @@ -1,26 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "rails", "~> 6.0.0" - -group :development do - gem "appraisal" - gem "bundler" - gem "rake" -end - -group :test do - gem "commonmarker", "~> 0.16", require: false - gem "email_reply_parser", "~> 0.5", require: false - gem "gemoji", "~> 2.0", require: false - gem "minitest" - gem "RedCloth", "~> 4.2.9", require: false - gem "rinku", "~> 1.7", require: false - gem "sanitize", "~> 4.6", require: false - gem "escape_utils", "~> 1.0", require: false - gem "rouge", "~> 3.1", require: false - gem "minitest-focus", "~> 1.1" -end - -gemspec path: "../" diff --git a/html-pipeline.gemspec b/html-pipeline.gemspec index e87e5263..5a1c7552 100644 --- a/html-pipeline.gemspec +++ b/html-pipeline.gemspec @@ -15,8 +15,10 @@ Gem::Specification.new do |gem| gem.files = `git ls-files -z`.split("\x0").reject { |f| f =~ %r{^(test|gemfiles|script)/} } gem.require_paths = ['lib'] - gem.add_dependency 'activesupport', '>= 2' - gem.add_dependency 'nokogiri', '>= 1.4' + gem.required_ruby_version = ['>= 2.5.8', '< 4.0'] + + gem.add_dependency 'activesupport', '~> 4' + gem.add_dependency 'nokogiri', '~> 1.11' gem.post_install_message = <" end diff --git a/test/test_helper.rb b/test/test_helper.rb index ddb78e80..530e272d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,14 +6,12 @@ require 'minitest/pride' require 'minitest/focus' -require 'active_support/core_ext/string' - module TestHelpers # Asserts that two html fragments are equivalent. Attribute order # will be ignored. def assert_equal_html(expected, actual) - assert_equal Nokogiri::HTML::DocumentFragment.parse(expected).to_hash, - Nokogiri::HTML::DocumentFragment.parse(actual).to_hash + assert_equal Nokogiri::HTML::DocumentFragment.parse(expected).to_h, + Nokogiri::HTML::DocumentFragment.parse(actual).to_h end end diff --git a/travis.yml b/travis.yml deleted file mode 100644 index ff321ccd..00000000 --- a/travis.yml +++ /dev/null @@ -1,43 +0,0 @@ -language: ruby -cache: bundler -bundler_args: --path ../../vendor/bundle - -addons: - apt: - sources: - - libicu-dev - - kalakris-cmake - packages: - - cmake - -script: bundle exec rake - -gemfile: - - gemfiles/rails_6.gemfile - - gemfiles/rails_5.gemfile - - gemfiles/rails_4.gemfile - - gemfiles/rails_3.gemfile - -rvm: - - 2.4.6 - - 2.3.8 - - 2.5.7 - - ruby-head - -matrix: - fast_finish: true - allow_failures: - - rvm: ruby-head - exclude: - - gemfile: gemfiles/rails_6.gemfile - rvm: 2.4.6 - - gemfile: gemfiles/rails_6.gemfile - rvm: 2.3.8 - - gemfile: gemfiles/rails_4.gemfile - rvm: 2.5.7 - - gemfile: gemfiles/rails_4.gemfile - rvm: 2.4.6 - - gemfile: gemfiles/rails_3.gemfile - rvm: 2.5.7 - - gemfile: gemfiles/rails_3.gemfile - rvm: 2.4.6 From 428ec51652b2f2390b4ab8d82b9405d02a2f1cfa Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Fri, 8 Jan 2021 10:04:59 +0000 Subject: [PATCH 08/64] Start linting --- .rubocop.yml | 4 ++++ Gemfile | 4 ++++ Rakefile | 4 ++++ html-pipeline.gemspec | 18 +++++++++--------- 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..81748ee8 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,4 @@ +inherit_gem: + rubocop-standard: + - config/default.yml + - config/minitest.yml diff --git a/Gemfile b/Gemfile index 89e7c6fa..0fd8711c 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,10 @@ gemspec group :development, :test do gem 'awesome_print' + + gem 'rubocop' + gem 'rubocop-performance' + gem 'rubocop-standard' end group :development do diff --git a/Rakefile b/Rakefile index f26f9adc..cc4508a3 100755 --- a/Rakefile +++ b/Rakefile @@ -15,3 +15,7 @@ Rake::TestTask.new do |t| end task default: :test + +require 'rubocop/rake_task' + +RuboCop::RakeTask.new(:rubocop) diff --git a/html-pipeline.gemspec b/html-pipeline.gemspec index 5a1c7552..a8cd1d09 100644 --- a/html-pipeline.gemspec +++ b/html-pipeline.gemspec @@ -1,6 +1,6 @@ # frozen_string_literal: true -require File.expand_path('../lib/html/pipeline/version', __FILE__) +require File.expand_path('lib/html/pipeline/version', __dir__) Gem::Specification.new do |gem| gem.name = 'html-pipeline' @@ -20,12 +20,12 @@ Gem::Specification.new do |gem| gem.add_dependency 'activesupport', '~> 4' gem.add_dependency 'nokogiri', '~> 1.11' - gem.post_install_message = < Date: Fri, 8 Jan 2021 10:05:31 +0000 Subject: [PATCH 09/64] Drop truffle-ruby --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9658b3dc..e13625e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,8 +23,6 @@ jobs: include: - ruby-version: head experimental: true - - ruby-version: truffleruby-head - experimental: true continue-on-error: ${{ matrix.experimental }} From f042ca9041443a16eeabb686f6ffbd70b2f5ab9a Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Fri, 8 Jan 2021 10:07:41 +0000 Subject: [PATCH 10/64] Obey the bundler --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e13625e7..a8d3b334 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,8 @@ jobs: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true - name: Install dependencies - run: bundle install --path ../../vendor/bundle + run: | + bundle config set --local path '../../vendor/bundle' + bundle install - name: Run tests run: bundle exec rake From db3d990fc92538f7abec3a8349e7a8accd5e06f1 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Fri, 8 Jan 2021 10:10:14 +0000 Subject: [PATCH 11/64] Update deps --- Gemfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 0fd8711c..d4890b59 100644 --- a/Gemfile +++ b/Gemfile @@ -23,9 +23,9 @@ group :test do gem 'email_reply_parser', '~> 0.5', require: false gem 'gemoji', '~> 2.0', require: false gem 'minitest' - gem 'RedCloth', '~> 4.2.9', require: false - gem 'rinku', '~> 1.7', require: false - gem 'sanitize', '~> 4.6', require: false + gem 'RedCloth', '~> 4.3', require: false + gem 'rinku', '~> 1.7', require: false + gem 'sanitize', '~> 5.2', require: false gem 'escape_utils', '~> 1.0', require: false gem 'minitest-focus', '~> 1.1' From c904e2b2df427c1d3fb43c13f66b9172c79abc6f Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Fri, 8 Jan 2021 10:12:22 +0000 Subject: [PATCH 12/64] Drop activesupport dependency --- html-pipeline.gemspec | 1 - 1 file changed, 1 deletion(-) diff --git a/html-pipeline.gemspec b/html-pipeline.gemspec index a8cd1d09..cf8dccb6 100644 --- a/html-pipeline.gemspec +++ b/html-pipeline.gemspec @@ -17,7 +17,6 @@ Gem::Specification.new do |gem| gem.required_ruby_version = ['>= 2.5.8', '< 4.0'] - gem.add_dependency 'activesupport', '~> 4' gem.add_dependency 'nokogiri', '~> 1.11' gem.post_install_message = <<~MSG From 18ace65286b7d5c3b638eee927732778d96749d3 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Fri, 8 Jan 2021 10:17:46 +0000 Subject: [PATCH 13/64] Lint first pass --- lib/html/pipeline.rb | 15 ++-- lib/html/pipeline/@mention_filter.rb | 15 ++-- lib/html/pipeline/@team_mention_filter.rb | 16 ++-- lib/html/pipeline/absolute_source_filter.rb | 2 + lib/html/pipeline/camo_filter.rb | 2 +- lib/html/pipeline/email_reply_filter.rb | 14 ++-- lib/html/pipeline/emoji_filter.rb | 16 ++-- lib/html/pipeline/filter.rb | 1 + lib/html/pipeline/image_filter.rb | 2 +- lib/html/pipeline/image_max_width_filter.rb | 2 +- lib/html/pipeline/markdown_filter.rb | 31 ++++---- lib/html/pipeline/sanitization_filter.rb | 37 ++++----- lib/html/pipeline/syntax_highlight_filter.rb | 6 +- lib/html/pipeline/text_filter.rb | 1 + lib/html/pipeline/toc_filter.rb | 4 +- .../helpers/mocked_instrumentation_service.rb | 1 + .../pipeline/absolute_source_filter_test.rb | 4 +- test/html/pipeline/camo_filter_test.rb | 8 +- test/html/pipeline/email_reply_filter_test.rb | 78 +++++++++---------- test/html/pipeline/emoji_filter_test.rb | 2 +- test/html/pipeline/https_filter_test.rb | 2 +- test/html/pipeline/image_filter_test.rb | 2 +- .../pipeline/image_max_width_filter_test.rb | 2 +- test/html/pipeline/markdown_filter_test.rb | 22 +++--- test/html/pipeline/mention_filter_test.rb | 4 +- .../html/pipeline/sanitization_filter_test.rb | 18 ++--- .../pipeline/syntax_highlight_filter_test.rb | 14 ++-- .../html/pipeline/team_mention_filter_test.rb | 8 +- test/html/pipeline/toc_filter_test.rb | 2 +- test/html/pipeline_test.rb | 2 +- test/test_helper.rb | 2 +- 31 files changed, 172 insertions(+), 163 deletions(-) diff --git a/lib/html/pipeline.rb b/lib/html/pipeline.rb index f1277b46..f68fbeac 100644 --- a/lib/html/pipeline.rb +++ b/lib/html/pipeline.rb @@ -46,6 +46,7 @@ class Pipeline autoload :TextFilter, 'html/pipeline/text_filter' class MissingDependencyError < RuntimeError; end + def self.require_dependency(name, requirer) require name rescue LoadError => e @@ -76,8 +77,10 @@ def self.parse(document_or_html) # Public: String name for this Pipeline. Defaults to Class name. attr_writer :instrumentation_name + def instrumentation_name return @instrumentation_name if defined?(@instrumentation_name) + @instrumentation_name = self.class.name end @@ -88,6 +91,7 @@ class << self def initialize(filters, default_context = {}, result_class = nil) raise ArgumentError, 'default_context cannot be nil' if default_context.nil? + @filters = filters.flatten.freeze @default_context = default_context.freeze @result_class = result_class || Hash @@ -166,12 +170,11 @@ def setup_instrumentation(name = nil, service = nil) # block, otherwise the block is ran without instrumentation. # # Returns the result of the provided block. - def instrument(event, payload = nil) + def instrument(event, payload = nil, &block) payload ||= default_payload return yield(payload) unless instrumentation_service - instrumentation_service.instrument event, payload do |payload| - yield payload - end + + instrumentation_service.instrument event, payload, &block end # Internal: Default payload for instrumentation. @@ -192,9 +195,7 @@ class Nokogiri::XML::Node # ... some other shit when replacing text nodes. See 'utf-8 output 2' in # user_content_test.rb for details. def replace_with_encoding_fix(replacement) - if replacement.respond_to?(:to_str) - replacement = document.fragment("
#{replacement}
").children.first.children - end + replacement = document.fragment("
#{replacement}
").children.first.children if replacement.respond_to?(:to_str) replace_without_encoding_fix(replacement) end diff --git a/lib/html/pipeline/@mention_filter.rb b/lib/html/pipeline/@mention_filter.rb index 415a78c7..dcbc8144 100644 --- a/lib/html/pipeline/@mention_filter.rb +++ b/lib/html/pipeline/@mention_filter.rb @@ -40,22 +40,22 @@ def self.mentioned_logins_in(text, username_pattern = UsernamePattern) # Hash that contains all of the mention patterns used by the pipeline MentionPatterns = Hash.new do |hash, key| - hash[key] = / + hash[key] = %r{ (?:^|\W) # beginning of string or non-word char @((?>#{key})) # @username - (?!\/) # without a trailing slash + (?!/) # without a trailing slash (?= \.+[ \t\W]| # dots followed by space or non-word character \.+$| # dots at end of line [^0-9a-zA-Z_.]| # non-word character except dot $ # end of line ) - /ix + }ix end # Default pattern used to extract usernames from text. The value can be # overriden by providing the username_pattern variable in the context. - UsernamePattern = /[a-z0-9][a-z0-9-]*/ + UsernamePattern = /[a-z0-9][a-z0-9-]*/.freeze # List of username logins that, when mentioned, link to the blog post # about @mentions instead of triggering a real mention. @@ -67,7 +67,7 @@ def self.mentioned_logins_in(text, username_pattern = UsernamePattern) ].freeze # Don't look for mentions in text nodes that are children of these elements - IGNORE_PARENTS = %w(pre code a style script).to_set + IGNORE_PARENTS = %w[pre code a style script].to_set def call result[:mentioned_usernames] ||= [] @@ -76,8 +76,10 @@ def call content = node.to_html next unless content.include?('@') next if has_ancestor?(node, IGNORE_PARENTS) + html = mention_link_filter(content, base_url, info_url, username_pattern) next if html == content + node.replace(html) end doc @@ -120,6 +122,7 @@ def mention_link_filter(text, _base_url = '/', info_url = nil, username_pattern def link_to_mention_info(text, info_url = nil) return "@#{text}" if info_url.nil? + "" \ "@#{text}" \ '' @@ -129,7 +132,7 @@ def link_to_mentioned_user(login) result[:mentioned_usernames] |= [login] url = base_url.dup - url << '/' unless url =~ /[\/~]\z/ + url << '/' unless %r{[/~]\z}.match?(url) "" \ "@#{login}" \ diff --git a/lib/html/pipeline/@team_mention_filter.rb b/lib/html/pipeline/@team_mention_filter.rb index 562ef344..0529ea82 100644 --- a/lib/html/pipeline/@team_mention_filter.rb +++ b/lib/html/pipeline/@team_mention_filter.rb @@ -29,8 +29,8 @@ class TeamMentionFilter < Filter # Returns a String replaced with the return of the block. def self.mentioned_teams_in(text, team_pattern = TeamPattern) text.gsub team_pattern do |match| - org = $1 - team = $2 + org = Regexp.last_match(1) + team = Regexp.last_match(2) yield match, org, team end end @@ -38,13 +38,13 @@ def self.mentioned_teams_in(text, team_pattern = TeamPattern) # Default pattern used to extract team names from text. The value can be # overridden by providing the team_pattern variable in the context. To # properly link the mention, should be in the format of /@(1)\/(2)/. - TeamPattern = / + TeamPattern = %r{ (?<=^|\W) # beginning of string or non-word char @([a-z0-9][a-z0-9-]*) # @organization - \/ # dividing slash + / # dividing slash ([a-z0-9][a-z0-9\-_]*) # team \b - /ix + }ix.freeze # Don't look for mentions in text nodes that are children of these elements IGNORE_PARENTS = %w[pre code a style script].to_set @@ -56,8 +56,10 @@ def call content = node.to_html next unless content.include?('@') next if has_ancestor?(node, IGNORE_PARENTS) + html = mention_link_filter(content, base_url, team_pattern) next if html == content + node.replace(html) end doc @@ -88,8 +90,8 @@ def link_to_mentioned_team(org, team) result[:mentioned_teams] |= [team] url = base_url.dup - url << '/' unless url =~ /[\/~]\z/ - + url << '/' unless %r{[/~]\z}.match?(url) + "" \ "@#{org}/#{team}" \ '' diff --git a/lib/html/pipeline/absolute_source_filter.rb b/lib/html/pipeline/absolute_source_filter.rb index 533491ac..7385d03a 100644 --- a/lib/html/pipeline/absolute_source_filter.rb +++ b/lib/html/pipeline/absolute_source_filter.rb @@ -21,8 +21,10 @@ class AbsoluteSourceFilter < Filter def call doc.search('img').each do |element| next if element['src'].nil? || element['src'].empty? + src = element['src'].strip next if src.start_with? 'http' + base = if src.start_with? '/' image_base_url else diff --git a/lib/html/pipeline/camo_filter.rb b/lib/html/pipeline/camo_filter.rb index 065b6ebc..6a2fda5a 100644 --- a/lib/html/pipeline/camo_filter.rb +++ b/lib/html/pipeline/camo_filter.rb @@ -98,7 +98,7 @@ def asset_host_allowed?(host) # Private: helper to hexencode a string. Each byte ends up encoded into # two characters, zero padded value in the range [0-9a-f]. def hexencode(str) - str.unpack('H*').first + str.unpack1('H*') end end end diff --git a/lib/html/pipeline/email_reply_filter.rb b/lib/html/pipeline/email_reply_filter.rb index fef61949..3af74d41 100644 --- a/lib/html/pipeline/email_reply_filter.rb +++ b/lib/html/pipeline/email_reply_filter.rb @@ -15,13 +15,13 @@ class Pipeline class EmailReplyFilter < TextFilter include EscapeUtils - EMAIL_HIDDEN_HEADER = %(