Skip to content

Commit

Permalink
Merge pull request #115 from publify/move-string-extensions
Browse files Browse the repository at this point in the history
Move String monkey-patches into a module under PublifyCore
  • Loading branch information
mvz committed Oct 18, 2023
2 parents 5872d09 + 583e39f commit dccebc2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 62 deletions.
12 changes: 0 additions & 12 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,6 @@ RSpec/ExpectInHook:
- 'spec/models/note_spec.rb'
- 'spec/models/trigger_spec.rb'

# Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
# Include: **/*_spec*rb*, **/spec/**/*
RSpec/FilePath:
Exclude:
- 'spec/lib/transforms_spec.rb'

# Configuration parameters: AssignmentOnly.
RSpec/InstanceVariable:
Enabled: false
Expand Down Expand Up @@ -172,12 +166,6 @@ RSpec/ScatteredSetup:
Exclude:
- 'spec/controllers/admin/articles_controller_spec.rb'

# Configuration parameters: Include, CustomTransform, IgnoreMethods, IgnoreMetadata.
# Include: **/*_spec.rb
RSpec/SpecFilePathFormat:
Exclude:
- 'spec/lib/transforms_spec.rb'

RSpec/StubbedMock:
Exclude:
- 'spec/controllers/admin/notes_controller_spec.rb'
Expand Down
2 changes: 1 addition & 1 deletion Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ lib/publify_core.rb
lib/publify_core/content_text_helpers.rb
lib/publify_core/engine.rb
lib/publify_core/lang.rb
lib/publify_core/string_ext.rb
lib/publify_core/testing_support/dns_mock.rb
lib/publify_core/testing_support/factories/articles.rb
lib/publify_core/testing_support/factories/blogs.rb
Expand Down Expand Up @@ -461,7 +462,6 @@ lib/tasks/manifest.rake
lib/tasks/publify_core_tasks.rake
lib/text_filter_plugin.rb
lib/theme.rb
lib/transforms.rb
themes/plain/about.markdown
themes/plain/javascripts/theme.js
themes/plain/preview.png
Expand Down
2 changes: 1 addition & 1 deletion lib/publify_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require "publify_core/text_filter/markdown_smartquotes"
require "publify_core/text_filter/smartypants"
require "publify_core/text_filter/twitterfilter"
require "publify_core/string_ext"

require "carrierwave"
require "jquery-rails"
Expand All @@ -30,7 +31,6 @@
require "spam_protection"
require "text_filter_plugin"
require "theme"
require "transforms"

module PublifyCore
Theme.register_themes File.join(Engine.root, "themes")
Expand Down
52 changes: 52 additions & 0 deletions lib/publify_core/string_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

# FIXME: Replace with helpers and/or methods provided by Rails
module PublifyCore
module StringExt
ACCENTS = { %w(á à â ä ã Ã Ä Â À) => "a",
%w(é è ê ë Ë É È Ê) => "e",
%w(í ì î ï I Î Ì) => "i",
%w(ó ò ô ö õ Õ Ö Ô Ò) => "o",
["œ"] => "oe",
["ß"] => "ss",
%w(ú ù û ü U Û Ù) => "u",
%w(ç Ç) => "c" }.freeze

def to_permalink
string = self
ACCENTS.each do |key, value|
string = string.tr(key.join, value)
end
string = string.tr("'", "-")
string.gsub(/<[^>]*>/, "").to_url
end

# Returns a-string-with-dashes when passed 'a string with dashes'.
# All special chars are stripped in the process
def to_url
return if nil?

s = downcase.tr("\"'", "")
s = s.gsub(/\P{Word}/, " ")
s.strip.tr_s(" ", "-").tr(" ", "-").sub(/^$/, "-")
end

def to_title(item, settings, params)
TitleBuilder.new(self).build(item, settings, params)
end

# Strips any html markup from a string
TYPO_TAG_KEY = TYPO_ATTRIBUTE_KEY = /[\w:_-]+/.freeze
TYPO_ATTRIBUTE_VALUE = /(?:[A-Za-z0-9]+|(?:'[^']*?'|"[^"]*?"))/.freeze
TYPO_ATTRIBUTE = /(?:#{TYPO_ATTRIBUTE_KEY}(?:\s*=\s*#{TYPO_ATTRIBUTE_VALUE})?)/.freeze
TYPO_ATTRIBUTES = /(?:#{TYPO_ATTRIBUTE}(?:\s+#{TYPO_ATTRIBUTE})*)/.freeze
TAG =
%r{<[!/?\[]?(?:#{TYPO_TAG_KEY}|--)(?:\s+#{TYPO_ATTRIBUTES})?\s*(?:[!/?\]]+|--)?>}
.freeze
def strip_html
gsub(TAG, "").gsub(/\s+/, " ").strip
end
end
end

String.include PublifyCore::StringExt
47 changes: 0 additions & 47 deletions lib/transforms.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

RSpec.describe String do
RSpec.describe PublifyCore::StringExt do
describe "#to_permalink" do
it "builds a nice permalink from an accentuated string" do
expect("L'été s'ra chaud, l'été s'ra chaud".to_permalink)
Expand Down

0 comments on commit dccebc2

Please sign in to comment.