From ce8e09963903fcad725002b2d42b54b4af5d0930 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Fri, 12 Jul 2019 21:09:57 +0200 Subject: [PATCH] FEATURE: Use configured quotation marks in fancy topic title --- app/models/site_setting.rb | 18 ++++++++++++++++++ app/models/topic.rb | 2 +- lib/html_prettify.rb | 4 ++-- lib/tasks/topics.rake | 19 +++++++++++++++++++ spec/models/topic_spec.rb | 10 ++++++++++ 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index f0be63c6688..160d19e84e6 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -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 @@ -122,6 +123,7 @@ class SiteSetting < ActiveRecord::Base @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 @@ -137,6 +139,22 @@ class SiteSetting < ActiveRecord::Base ? 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 diff --git a/app/models/topic.rb b/app/models/topic.rb index 648da51a741..4fd4c67e6fd 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -336,7 +336,7 @@ class Topic < ActiveRecord::Base 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 diff --git a/lib/html_prettify.rb b/lib/html_prettify.rb index db3bd14e08f..c339091d586 100644 --- a/lib/html_prettify.rb +++ b/lib/html_prettify.rb @@ -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+. diff --git a/lib/tasks/topics.rake b/lib/tasks/topics.rake index f9c9264d035..e91c4a6f319 100644 --- a/lib/tasks/topics.rake +++ b/lib/tasks/topics.rake @@ -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 diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 26dda3fb468..510146a0bb8 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -390,6 +390,16 @@ describe Topic do 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('„Weißt du“, sagte er, „was ‚Discourse‘ ist?“') + + SiteSetting.markdown_typographer_quotation_marks = "«\u00A0|\u00A0»|‹\u00A0|\u00A0›" + topic.title = '"Qui vivra verra"' + expect(topic.fancy_title).to eq('« Qui vivra verra »') + end + context 'readonly mode' do before do Discourse.enable_readonly_mode