Skip to content

Commit

Permalink
Merge pull request #109 from publify/move-text-filter-classes
Browse files Browse the repository at this point in the history
Move text filter classes together into the PublifyCore::TextFilter namespace
  • Loading branch information
mvz authored Sep 29, 2023
2 parents 4a1334c + a8cccb6 commit 38a7741
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 181 deletions.
18 changes: 8 additions & 10 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config --no-offense-counts --no-auto-gen-timestamp`
# using RuboCop version 1.50.1.
# using RuboCop version 1.56.4.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -23,7 +23,7 @@ Metrics/AbcSize:

# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 221
Max: 220

# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Expand Down Expand Up @@ -64,7 +64,6 @@ Naming/PredicateName:
Naming/VariableNumber:
Exclude:
- 'spec/controllers/authors_controller_spec.rb'
- 'spec/models/comment_spec.rb'

# Configuration parameters: MinSize.
Performance/CollectionLiteralInLoop:
Expand All @@ -77,7 +76,7 @@ RSpec/AnyInstance:
- 'spec/models/article_spec.rb'
- 'spec/models/note_spec.rb'

# Configuration parameters: Prefixes.
# Configuration parameters: Prefixes, AllowedPatterns.
# Prefixes: when, with, without
RSpec/ContextWording:
Exclude:
Expand Down Expand Up @@ -116,10 +115,6 @@ RSpec/ExpectInHook:
RSpec/FilePath:
Exclude:
- 'spec/helpers/author_helper_spec.rb'
- 'spec/lib/publify_textfilter_markdown_smartquotes_spec.rb'
- 'spec/lib/publify_textfilter_markdown_spec.rb'
- 'spec/lib/publify_textfilter_smartypants_spec.rb'
- 'spec/lib/publify_textfilter_twitterfilter_spec.rb'
- 'spec/lib/transforms_spec.rb'
- 'spec/models/article/factory_spec.rb'

Expand Down Expand Up @@ -153,14 +148,16 @@ RSpec/MultipleExpectations:
RSpec/MultipleMemoizedHelpers:
Max: 9

# Configuration parameters: IgnoreSharedExamples.
# Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
# SupportedStyles: always, named_only
RSpec/NamedSubject:
Exclude:
- 'spec/helpers/base_helper_spec.rb'
- 'spec/models/article_spec.rb'
- 'spec/models/content_spec.rb'
- 'spec/models/page_spec.rb'

# Configuration parameters: AllowedGroups.
RSpec/NestedGroups:
Max: 5

Expand All @@ -172,6 +169,7 @@ RSpec/RepeatedExample:
Exclude:
- 'spec/models/configuration_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
RSpec/ScatteredSetup:
Exclude:
- 'spec/controllers/admin/articles_controller_spec.rb'
Expand All @@ -191,7 +189,7 @@ RSpec/VerifiedDoubles:
- 'spec/models/note_spec.rb'

# Configuration parameters: Include.
# Include: db/migrate/*.rb
# Include: db/**/*.rb
Rails/CreateTableWithTimestamps:
Exclude:
- 'db/migrate/113_initial_schema.rb'
Expand Down
10 changes: 5 additions & 5 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,14 @@ lib/publify_core/testing_support/fixtures/testfile.jpg
lib/publify_core/testing_support/fixtures/testfile.png
lib/publify_core/testing_support/fixtures/testfile.txt
lib/publify_core/testing_support/upload_fixtures.rb
lib/publify_core/text_filter/markdown.rb
lib/publify_core/text_filter/markdown_smartquotes.rb
lib/publify_core/text_filter/none.rb
lib/publify_core/text_filter/smartypants.rb
lib/publify_core/text_filter/twitterfilter.rb
lib/publify_core/version.rb
lib/publify_guid.rb
lib/publify_plugins.rb
lib/publify_textfilter_markdown.rb
lib/publify_textfilter_markdown_smartquotes.rb
lib/publify_textfilter_none.rb
lib/publify_textfilter_smartypants.rb
lib/publify_textfilter_twitterfilter.rb
lib/publify_time.rb
lib/sidebar_field.rb
lib/sidebar_registry.rb
Expand Down
2 changes: 1 addition & 1 deletion app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def tags
end

def html_postprocess(field, html)
super(field, PublifyApp::Textfilter::Twitterfilter.filtertext(html))
super(field, PublifyCore::TextFilter::Twitterfilter.filtertext(html))
end

def truncate(message, length)
Expand Down
2 changes: 1 addition & 1 deletion app/views/notes/show_in_reply.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= image_tag(@reply['user']['profile_image_url'], class: 'alignleft', alt: @reply['user']['name']) %>
<% end %>
<strong><%= get_reply_context_url(@reply) %></strong>
<p><%= nofollowify_links(PublifyApp::Textfilter::Twitterfilter.filtertext(@reply['text'])) %></p>
<p><%= nofollowify_links(PublifyCore::TextFilter::Twitterfilter.filtertext(@reply['text'])) %></p>
<p>
<small>
<%= get_reply_context_twitter_link(@reply) %>
Expand Down
10 changes: 5 additions & 5 deletions lib/publify_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
require "publify_core/engine"
require "publify_core/lang"
require "publify_core/content_text_helpers"
require "publify_core/text_filter/none"
require "publify_core/text_filter/markdown"
require "publify_core/text_filter/markdown_smartquotes"
require "publify_core/text_filter/smartypants"
require "publify_core/text_filter/twitterfilter"

require "carrierwave"
require "jquery-rails"
Expand All @@ -20,11 +25,6 @@

require "email_notify"
require "publify_guid"
require "publify_textfilter_none"
require "publify_textfilter_markdown"
require "publify_textfilter_markdown_smartquotes"
require "publify_textfilter_smartypants"
require "publify_textfilter_twitterfilter"
require "publify_time"
require "sidebar_registry"
require "spam_protection"
Expand Down
53 changes: 53 additions & 0 deletions lib/publify_core/text_filter/markdown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require "text_filter_plugin"
require "commonmarker"

module PublifyCore::TextFilter
class Markdown < TextFilterPlugin::Markup
plugin_display_name "Markdown"
plugin_description "Markdown markup language from" \
' <a href="http://daringfireball.com/">Daring Fireball</a>'

def self.help_text
<<~TXT
[Markdown](http://daringfireball.net/projects/markdown/) is a simple
text-to-HTML converter that turns common text idioms into HTML. The
[full syntax](http://daringfireball.net/projects/markdown/syntax) is
available from the author's site, but here's a short summary:
* **Paragraphs**: Start a new paragraph by skipping a line.
* **Italics**: Put text in *italics* by enclosing it in either * or
_: `*italics*` turns into *italics*.
* **Bold**: Put text in **bold** by enclosing it in two *s:
`**bold**` turns into **bold**.
* **Pre-formatted text**: Enclosing a short block of text in
backquotes (&#96;) displays it in a monospaced font and converts HTML
metacharacters so they display correctly. Example:
&#96;`<img src="foo"/>`&#96; displays as `<img src="foo"/>`. Also,
any paragraph indented 4 or more spaces is treated as pre-formatted
text.
* **Block quotes**: Any paragraph (or line) that starts with a `>` is
treated as a blockquote.
* **Hyperlinks**: You can create links like this:
`[amazon's web site](http://www.amazon.com)`. That produces
"[amazon's web site](http://www.amazon.com)".
* **Lists**: You can create numbered or bulleted lists by ending a
paragraph with a colon (:), skipping a line, and then using asterisks
(*, for bullets) or numbers (for numbered lists). See the
[Markdown syntax page](http://daringfireball.net/projects/markdown/syntax)
for examples.
* **Raw HTML**: Markdown will pass raw HTML through unchanged, so you
can use HTML's syntax whenever Markdown doesn't provide a reasonable
alternative.
TXT
end

def self.filtertext(text)
# FIXME: Workaround for <publify:foo> not being interpreted as an HTML tag.
escaped_macros = text.gsub(%r{(</?publify):}, '\1X')
html = CommonMarker.render_html(escaped_macros, :UNSAFE)
html.gsub(%r{(</?publify)X}, '\1:').strip
end
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require "text_filter_plugin"
require "publify_textfilter_markdown"
require "publify_core/text_filter/markdown"

module PublifyTextfilter
class MarkdownSmartquotes < PublifyApp::Textfilter::Markdown
module PublifyCore::TextFilter
class MarkdownSmartquotes < PublifyCore::TextFilter::Markdown
plugin_display_name "Markdown with smart quotes"
plugin_description "Markdown markup language from" \
' <a href="http://daringfireball.com/">Daring Fireball</a>' \
Expand Down
14 changes: 14 additions & 0 deletions lib/publify_core/text_filter/none.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require "text_filter_plugin"

module PublifyCore::TextFilter
class None < TextFilterPlugin::Markup
plugin_display_name "None"
plugin_description "Raw HTML only"

def self.filtertext(text)
text
end
end
end
14 changes: 14 additions & 0 deletions lib/publify_core/text_filter/smartypants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require "rubypants"

module PublifyCore::TextFilter
class Smartypants < TextFilterPlugin::PostProcess
plugin_display_name "Smartypants"
plugin_description "Converts HTML to use typographically correct quotes and dashes"

def self.filtertext(text)
RubyPants.new(text).to_html
end
end
end
55 changes: 55 additions & 0 deletions lib/publify_core/text_filter/twitterfilter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require "text_filter_plugin"
require "html/pipeline"
require "html/pipeline/hashtag/hashtag_filter"

module PublifyCore::TextFilter
class Twitterfilter < TextFilterPlugin::PostProcess
plugin_display_name "HTML Filter"
plugin_description "Strip HTML tags"

class TwitterHashtagFilter < HTML::Pipeline::HashtagFilter
def initialize(text)
super(text,
tag_url: "https://twitter.com/search?q=%%23%<tag>s&src=tren&mode=realtime",
tag_link_attr: "")
end
end

class TwitterMentionFilter < HTML::Pipeline::MentionFilter
def initialize(text)
super(text, base_url: "https://twitter.com")
end

# Override base mentions finder, treating @mention just like any other @foo.
def self.mentioned_logins_in(text, username_pattern = UsernamePattern)
text.gsub MentionPatterns[username_pattern] do |match|
login = Regexp.last_match(1)
yield match, login, false
end
end

# Override base link creator, removing the class
def link_to_mentioned_user(login)
result[:mentioned_usernames] |= [login]

url = base_url.dup
url << "/" unless %r{[/~]\z}.match?(url)

"<a href='#{url << login}'>" \
"@#{login}" \
"</a>"
end
end

def self.filtertext(text)
# First, autolink
helper = PublifyCore::ContentTextHelpers.new
text = helper.auto_link(text)

text = TwitterHashtagFilter.new(text).call
TwitterMentionFilter.new(text).call.to_s
end
end
end
56 changes: 0 additions & 56 deletions lib/publify_textfilter_markdown.rb

This file was deleted.

16 changes: 0 additions & 16 deletions lib/publify_textfilter_none.rb

This file was deleted.

16 changes: 0 additions & 16 deletions lib/publify_textfilter_smartypants.rb

This file was deleted.

Loading

0 comments on commit 38a7741

Please sign in to comment.