summaryrefslogtreecommitdiff
path: root/lib/banzai
diff options
context:
space:
mode:
authorDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-01-12 02:05:18 -0200
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-01-14 12:09:31 -0200
commita6a5990ee5f504107944c3bba5c18dbdea9f5207 (patch)
treee359ff72148867eece11916af0d5ed8bcfacf577 /lib/banzai
parentaac6598482036e12a20b4c75f2a508bd6a017245 (diff)
downloadgitlab-ce-a6a5990ee5f504107944c3bba5c18dbdea9f5207.tar.gz
Add Banzai::Filter::GollumTagsFilter for parsing Gollum's tags in HTML
Diffstat (limited to 'lib/banzai')
-rw-r--r--lib/banzai/filter/gollum_tags_filter.rb106
1 files changed, 106 insertions, 0 deletions
diff --git a/lib/banzai/filter/gollum_tags_filter.rb b/lib/banzai/filter/gollum_tags_filter.rb
new file mode 100644
index 00000000000..f78b30bd4e9
--- /dev/null
+++ b/lib/banzai/filter/gollum_tags_filter.rb
@@ -0,0 +1,106 @@
+require 'banzai'
+require 'html/pipeline/filter'
+
+module Banzai
+ module Filter
+ # HTML Filter for parsing Gollum's tags in HTML.
+ #
+ # Based on Gollum::Filter::Tags
+ #
+ # Context options:
+ # :project_wiki (required) - Current project wiki.
+ #
+ class GollumTagsFilter < HTML::Pipeline::Filter
+ include ActionView::Helpers::TagHelper
+
+ # Pattern to match tag contents.
+ TAGS_PATTERN = %r{(.?)\[\[(.+?)\]\]([^\[]?)}
+
+ def call
+ search_text_nodes(doc).each do |node|
+ content = node.content
+
+ next unless content.match(TAGS_PATTERN)
+
+ html = process_tag($2)
+
+ node.replace(html) if html != node.content
+ end
+
+ doc
+ end
+
+ private
+
+ # Process a single tag into its final HTML form.
+ #
+ # tag - The String tag contents (the stuff inside the double brackets).
+ #
+ # 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
+ end
+
+ # Attempt to process the tag as an image tag.
+ #
+ # tag - The String tag contents (the stuff inside the double brackets).
+ #
+ # 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?
+
+ name = parts[0].strip
+
+ if file = project_wiki.find_file(name)
+ path = ::File.join project_wiki_base_path, file.path
+ elsif name =~ /^https?:\/\/.+(jpg|png|gif|svg|bmp)$/i
+ path = name
+ end
+
+ if path
+ content_tag(:img, nil, src: path)
+ end
+ 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?
+
+ if parts.size == 1
+ url = parts[0].strip
+ else
+ name, url = *parts.compact.map(&:strip)
+ end
+
+ content_tag(:a, name || url, href: url)
+ end
+
+ def project_wiki
+ context[:project_wiki]
+ end
+
+ def project_wiki_base_path
+ project_wiki && project_wiki.wiki_base_path
+ end
+
+ # Ensure that a :project_wiki key exists in context
+ #
+ # Note that while the key might exist, its value could be nil!
+ def validate
+ needs :project_wiki
+ end
+ end
+ end
+end