Skip to content

Commit

Permalink
FEATURE: Use configured quotation marks in fancy topic title
Browse files Browse the repository at this point in the history
  • Loading branch information
gschlager committed Jul 12, 2019
1 parent 8f89254 commit ce8e099
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
18 changes: 18 additions & 0 deletions app/models/site_setting.rb
Expand Up @@ -3,6 +3,7 @@
require 'site_setting_extension'
require_dependency 'global_path'
require_dependency 'site_settings/yaml_loader'
require 'htmlentities'

class SiteSetting < ActiveRecord::Base
extend GlobalPath
Expand Down Expand Up @@ -122,6 +123,7 @@ def self.reset_cached_settings!
@attachment_content_type_blacklist_regex = nil
@attachment_filename_blacklist_regex = nil
@unicode_username_whitelist_regex = nil
@pretty_quote_entities = nil
end

def self.attachment_content_type_blacklist_regex
Expand All @@ -137,6 +139,22 @@ def self.unicode_username_character_whitelist_regex
? Regexp.new(SiteSetting.unicode_username_character_whitelist) : nil
end

def self.pretty_quote_entities
@pretty_quote_entities ||= begin
htmlentities = HTMLEntities.new
quotation_marks = SiteSetting.markdown_typographer_quotation_marks
.split("|")
.map { |quote| htmlentities.encode(quote, :basic, :named, :decimal) }

{
double_left_quote: quotation_marks[0],
double_right_quote: quotation_marks[1],
single_left_quote: quotation_marks[2],
single_right_quote: quotation_marks[3]
}
end
end

# helpers for getting s3 settings that fallback to global
class Upload
def self.s3_cdn_url
Expand Down
2 changes: 1 addition & 1 deletion app/models/topic.rb
Expand Up @@ -336,7 +336,7 @@ def limit_private_messages_per_day

def self.fancy_title(title)
return unless escaped = ERB::Util.html_escape(title)
fancy_title = Emoji.unicode_unescape(HtmlPrettify.render(escaped))
fancy_title = Emoji.unicode_unescape(HtmlPrettify.render(escaped, SiteSetting.pretty_quote_entities))
fancy_title.length > Topic.max_fancy_title_length ? escaped : fancy_title
end

Expand Down
4 changes: 2 additions & 2 deletions lib/html_prettify.rb
Expand Up @@ -10,8 +10,8 @@
#

class HtmlPrettify < String
def self.render(html)
new(html).to_html
def self.render(html, entities = {})
new(html, [2], entities).to_html
end

# Create a new RubyPants instance with the text in +string+.
Expand Down
19 changes: 19 additions & 0 deletions lib/tasks/topics.rake
Expand Up @@ -83,6 +83,25 @@ task "topics:watch_all_replied_topics" => :environment do
puts "", "Done"
end

task "topics:update_fancy_titles" => :environment do
if !SiteSetting.title_fancy_entities?
puts "fancy topic titles are disabled"
return
end

DB.exec("UPDATE topics SET fancy_title = NULL")

total = Topic.count
count = 0

Topic.find_each do |topic|
topic.fancy_title
print_status(count += 1, total)
end

puts "", "Done"
end

def print_status(current, max)
print "\r%9d / %d (%5.1f%%)" % [current, max, ((current.to_f / max.to_f) * 100).round(1)]
end
10 changes: 10 additions & 0 deletions spec/models/topic_spec.rb
Expand Up @@ -390,6 +390,16 @@ def build_topic_with_title(title)
expect(topic.fancy_title).to eq(long_title)
end

it "uses the configured quote entities" do
SiteSetting.markdown_typographer_quotation_marks = "„|“|‚|‘"
topic.title = %q|"Weißt du", sagte er, "was 'Discourse' ist?"|
expect(topic.fancy_title).to eq('&bdquo;Weißt du&ldquo;, sagte er, &bdquo;was &sbquo;Discourse&lsquo; ist?&ldquo;')

SiteSetting.markdown_typographer_quotation_marks = \u00A0|\u00A0»|‹\u00A0|\u00A0›"
topic.title = '"Qui vivra verra"'
expect(topic.fancy_title).to eq('&laquo;&nbsp;Qui vivra verra&nbsp;&raquo;')
end

context 'readonly mode' do
before do
Discourse.enable_readonly_mode
Expand Down

2 comments on commit ce8e099

@discoursebot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/german-quotation-marks/121944/10

@discoursereviewbot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.