diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-05-25 16:55:47 +0300 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-05-25 17:39:18 +0300 |
commit | 41e4072393f0088c75f27d61293318844c62125e (patch) | |
tree | b7cdb33f408afa022a3d37bd6ccab0fe74df5892 /lib | |
parent | 4ba547809fd8fd228ea463e4f107242d1966c892 (diff) |
Add regexp_for_value helper method
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/dependency_linker/base_linker.rb | 13 | ||||
-rw-r--r-- | lib/gitlab/dependency_linker/json_linker.rb | 27 | ||||
-rw-r--r-- | lib/gitlab/dependency_linker/method_linker.rb | 25 |
3 files changed, 25 insertions, 40 deletions
diff --git a/lib/gitlab/dependency_linker/base_linker.rb b/lib/gitlab/dependency_linker/base_linker.rb index 15fa9f40116..7ba0b3c39b7 100644 --- a/lib/gitlab/dependency_linker/base_linker.rb +++ b/lib/gitlab/dependency_linker/base_linker.rb @@ -68,6 +68,19 @@ module Gitlab def highlighted_lines @highlighted_lines ||= highlighted_text.lines end + + def regexp_for_value(value, default: /[^'"]+/) + case value + when Array + Regexp.union(value.map { |v| regexp_for_value(v, default: default) }) + when String + Regexp.escape(value) + when Regexp + value + else + default + end + end end end end diff --git a/lib/gitlab/dependency_linker/json_linker.rb b/lib/gitlab/dependency_linker/json_linker.rb index 021347b6429..1b1ca000977 100644 --- a/lib/gitlab/dependency_linker/json_linker.rb +++ b/lib/gitlab/dependency_linker/json_linker.rb @@ -24,32 +24,13 @@ module Gitlab # link_json('specific_package', '1.0.1', link: :key) # # Will link `specific_package` in `"specific_package": "1.0.1"` def link_json(key, value = nil, link: :value, &url_proc) - key = - case key - when Array - Regexp.union(key.map { |name| Regexp.escape(name) }) - when String - Regexp.escape(key) - when nil - '[^"]+' - else - key - end - - value = - case value - when String - Regexp.escape(value) - when nil - '[^"]+' - else - value - end + key = regexp_for_value(key, default: /[^"]+/) + value = regexp_for_value(value, default: /[^"]+/) if link == :value - value = "(?<name>#{value})" + value = /(?<name>#{value})/ else - key = "(?<name>#{key})" + key = /(?<name>#{key})/ end link_regex(/"#{key}":\s*"#{value}"/, &url_proc) diff --git a/lib/gitlab/dependency_linker/method_linker.rb b/lib/gitlab/dependency_linker/method_linker.rb index eeb7c1587c0..0ffa2a83c93 100644 --- a/lib/gitlab/dependency_linker/method_linker.rb +++ b/lib/gitlab/dependency_linker/method_linker.rb @@ -20,25 +20,16 @@ module Gitlab # # link_method_call('name') # # Will link `package` in `self.name = "package"` - def link_method_call(method_names, value = nil, &url_proc) - method_names = Array(method_names).map { |name| Regexp.escape(name) } - - value = - case value - when String - Regexp.escape(value) - when nil - /[^'"]+/ - else - value - end + def link_method_call(method_name, value = nil, &url_proc) + method_name = regexp_for_value(method_name) + value = regexp_for_value(value) regex = %r{ - #{Regexp.union(method_names)} # Method name - \s* # Whitespace - [(=]? # Opening brace or equals sign - \s* # Whitespace - ['"](?<name>#{value})['"] # Package name in quotes + #{method_name} # Method name + \s* # Whitespace + [(=]? # Opening brace or equals sign + \s* # Whitespace + ['"](?<name>#{value})['"] # Package name in quotes }x link_regex(regex, &url_proc) |