diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-01-12 18:59:49 -0200 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-01-14 12:09:31 -0200 |
commit | 765a2c73271cf311311c391e7e64f83e141c79ae (patch) | |
tree | 9a9ea4b50427fb7deec318b64f45aec1b0bd33b8 /lib/banzai/filter/gollum_tags_filter.rb | |
parent | 89e8b82b638e440bc487c4135cdfa4402fdffde5 (diff) | |
download | gitlab-ce-765a2c73271cf311311c391e7e64f83e141c79ae.tar.gz |
Refactoring Banzai::Filter::GollumTagsFilter
Diffstat (limited to 'lib/banzai/filter/gollum_tags_filter.rb')
-rw-r--r-- | lib/banzai/filter/gollum_tags_filter.rb | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/lib/banzai/filter/gollum_tags_filter.rb b/lib/banzai/filter/gollum_tags_filter.rb index f78b30bd4e9..e4a73030597 100644 --- a/lib/banzai/filter/gollum_tags_filter.rb +++ b/lib/banzai/filter/gollum_tags_filter.rb @@ -14,7 +14,10 @@ module Banzai include ActionView::Helpers::TagHelper # Pattern to match tag contents. - TAGS_PATTERN = %r{(.?)\[\[(.+?)\]\]([^\[]?)} + TAGS_PATTERN = %r{\[\[(.+?)\]\]} + + # Pattern to match allowed image extensions + ALLOWED_IMAGE_EXTENSIONS = %r{.+(jpg|png|gif|svg|bmp)\z}i def call search_text_nodes(doc).each do |node| @@ -22,9 +25,11 @@ module Banzai next unless content.match(TAGS_PATTERN) - html = process_tag($2) + html = process_tag($1) - node.replace(html) if html != node.content + if html && html != node.content + node.replace(html) + end end doc @@ -38,11 +43,11 @@ module Banzai # # Returns the String HTML version of the tag. def process_tag(tag) - if html = process_image_tag(tag) - html - else - process_page_link_tag(tag) - end + parts = tag.split('|') + + return if parts.size.zero? + + process_image_tag(parts) || process_page_link_tag(parts) end # Attempt to process the tag as an image tag. @@ -51,16 +56,15 @@ module Banzai # # Returns the String HTML if the tag is a valid image tag or nil # if it is not. - def process_image_tag(tag) - parts = tag.split('|') - return if parts.size.zero? + def process_image_tag(parts) + content = parts[0].strip - name = parts[0].strip + return unless image?(content) - if file = project_wiki.find_file(name) + if url?(content) + path = content + elsif file = project_wiki.find_file(content) path = ::File.join project_wiki_base_path, file.path - elsif name =~ /^https?:\/\/.+(jpg|png|gif|svg|bmp)$/i - path = name end if path @@ -68,16 +72,21 @@ module Banzai end end + def image?(path) + path =~ ALLOWED_IMAGE_EXTENSIONS + end + + def url?(path) + path.start_with?(*%w(http https)) + end + # Attempt to process the tag as a page link tag. # # tag - The String tag contents (the stuff inside the double brackets). # # Returns the String HTML if the tag is a valid page link tag or nil # if it is not. - def process_page_link_tag(tag) - parts = tag.split('|') - return if parts.size.zero? - + def process_page_link_tag(parts) if parts.size == 1 url = parts[0].strip else |