Class: SlackTransformer::Slack::Code

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_transformer/slack/code.rb

Constant Summary collapse

PATTERN =
/
  (?<=^|\W|_)

  # preceded by start of line, non-word character, or _

  (?<!`)

  # but not `

  `([^`]+?)(`+)

  # one or more of not ` preceded by ` followed by one or more `
/x

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Code

Returns a new instance of Code.



20
21
22
# File 'lib/slack_transformer/slack/code.rb', line 20

def initialize(input)
  @input = input
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



4
5
6
# File 'lib/slack_transformer/slack/code.rb', line 4

def input
  @input
end

Instance Method Details

#to_htmlObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/slack_transformer/slack/code.rb', line 24

def to_html
  input.gsub(PATTERN) do |match|
    closing_backticks = Regexp.last_match(2)
    closing_backticks_length = closing_backticks.length
    closing_backticks_remainder = closing_backticks_length % 3

    if closing_backticks_remainder == 0
      match
    else
      inner_text = Regexp.last_match(1)
      inner_trailing_backticks = '`' * (closing_backticks_length / 3 * 3)
      outer_trailing_backticks = '`' * (closing_backticks_remainder - 1)

      "<code>#{inner_text}#{inner_trailing_backticks}</code>#{outer_trailing_backticks}"
    end
  end
end