about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorFire Demon <firedemon@creature.cafe>2020-07-26 03:54:13 -0500
committerFire Demon <firedemon@creature.cafe>2020-08-30 05:45:16 -0500
commita827f14c383949535f7fa01ddfa5a87c85fac41d (patch)
tree23cecc11412e34f78961d3941816c1aaa77539a9 /app/lib
parente2bd72f3beb534795fbe06c0307097987cf5486a (diff)
[Command Tags] Handle representing code blocks for all content types
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/command_tag/commands/text_tools.rb14
-rw-r--r--app/lib/command_tag/processor.rb4
2 files changed, 13 insertions, 5 deletions
diff --git a/app/lib/command_tag/commands/text_tools.rb b/app/lib/command_tag/commands/text_tools.rb
index acb0a5985..441706265 100644
--- a/app/lib/command_tag/commands/text_tools.rb
+++ b/app/lib/command_tag/commands/text_tools.rb
@@ -4,10 +4,14 @@ module CommandTag::Commands::TextTools
   def handle_code_at_start(args)
     name = args.count > 1 ? args[0] : 'code'
 
-    if @status.content_type == 'text/plain'
-      @vars[name] = ["----------\n#{args.present? ? args.last : nil}\n----------"]
-    else
-      @vars[name] = ["<pre><code>#{args.present? ? args.last : nil}</code></pre>"]
-    end
+    value = args.last.presence || ''
+    @vars[name] = case @status.content_type
+                  when 'text/markdown'
+                    ["```\n#{value}\n```"]
+                  when 'text/html'
+                    ["<pre><code>#{html_encode(value).gsub("\n", '<br/>')}</code></pre>"]
+                  else
+                    ["----------\n#{value}\n----------"]
+                  end
   end
 end
diff --git a/app/lib/command_tag/processor.rb b/app/lib/command_tag/processor.rb
index 9e9812bb6..6e740ba98 100644
--- a/app/lib/command_tag/processor.rb
+++ b/app/lib/command_tag/processor.rb
@@ -187,6 +187,10 @@ class CommandTag::Processor
     text
   end
 
+  def html_encode(text)
+    (@html_entities ||= HTMLEntities.new).encode(text)
+  end
+
   def var(name)
     @vars[name].presence || []
   end