aboutsummaryrefslogtreecommitdiff
path: root/app/helpers/title_helper.rb
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2025-01-27 22:01:42 +0100
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-01-27 22:01:42 +0100
commitffb794a7255b8cbb89d2a4f5490feb59911b8014 (patch)
treeb3bb1b612196ea59ead4ab4e14eac54c7486563f /app/helpers/title_helper.rb
parentac58c1ba27a68c69d185ee402247a92915fe1f22 (diff)
downloadoperum-ffb794a7255b8cbb89d2a4f5490feb59911b8014.tar.gz
operum-ffb794a7255b8cbb89d2a4f5490feb59911b8014.zip
Replace underscores with the proper HTML tags
This was already handled correctly in the case of TeX and CSV, but the HTML rendering was still to be handled. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'app/helpers/title_helper.rb')
-rw-r--r--app/helpers/title_helper.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/app/helpers/title_helper.rb b/app/helpers/title_helper.rb
new file mode 100644
index 0000000..4de2779
--- /dev/null
+++ b/app/helpers/title_helper.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+# Helper functions for dealing with titles of Things.
+module TitleHelper
+ # Returns the given string with underscores to <em></em>.
+ def title_to_html(title:)
+ replace_underscores_with(string: title, open_char: '<em>', close_char: '</em>').html_safe # rubocop:disable Rails/OutputSafety
+ end
+
+ # Replace underscores from a string with the given open/close characters.
+ def replace_underscores_with(string:, open_char:, close_char:)
+ string.gsub(/_([^_]+)_/, "#{open_char}\\1#{close_char}")
+ end
+end