diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index 0ed17079..fa7794ae 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -1,3 +1,5 @@ +require 'digest/sha1' + module Gollum class Markup # Initialize a new Markup object. @@ -17,14 +19,83 @@ module Gollum # # Returns the formatted String content. def render - base_markup = GitHub::Markup.render(@name, @data) rescue nil + data = extract_tags(@data) + data = GitHub::Markup.render(@name, data) rescue '' + data = process_tags(data) end # Extract all tags into the tagmap and replace with placeholders. # - # Returns nothing. - def extract_tags + # data - The raw String data. + # + # Returns the placeholder'd String data. + def extract_tags(data) + data.gsub(/\[\[(.+?)\]\]/) do + id = Digest::SHA1.hexdigest($1) + @tagmap[id] = $1 + id + end + end + # Process all tags from the tagmap and replace the placeholders with the + # final markup. + # + # data - The String data (with placeholders). + # + # Returns the marked up String data. + def process_tags(data) + @tagmap.each do |id, tag| + data.sub!(id, process_tag(tag)) + end + data + end + + # 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) + return html + elsif html = process_file_link_tag(tag) + return html + else + return 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) + nil + end + + # Attempt to process the tag as a file link tag. + # + # tag - The String tag contents (the stuff inside the double brackets). + # + # Returns the String HTML if the tag is a valid file link tag or nil + # if it is not. + def process_file_link_tag(tag) + nil + 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('|') + name = parts[0] + cname = Gollum::canonical_name(parts[1] || parts[0]) + %{#{name}} end end end \ No newline at end of file diff --git a/test/test_markup.rb b/test/test_markup.rb new file mode 100644 index 00000000..7ee12d9c --- /dev/null +++ b/test/test_markup.rb @@ -0,0 +1,9 @@ +require File.join(File.dirname(__FILE__), *%w[helper]) + +context "Markup" do + test "page link" do + data = "a [[Bilbo Baggins]] b" + output = Gollum::Markup.new("x.md", data).render + assert_equal %{

a Bilbo Baggins b

\n}, output + end +end