Sha256: d9d5985da8bbd0c02e3d5c1bb008a9bc1fbabc578ee37676d222a71090fd5e41

Contents?: true

Size: 1.56 KB

Versions: 37

Compression:

Stored size: 1.56 KB

Contents

# ~*~ encoding: utf-8 ~*~
require 'net/http'
require 'uri'
require 'open-uri'

# Web Sequence Diagrams
#
# Render an inline web sequence diagram by sending the WSD code through the
# online renderer available from www.websequencediagrams.com.
#
class Gollum::Filter::WSD < Gollum::Filter
  WSD_URL = "http://www.websequencediagrams.com/index.php"

  # Extract all sequence diagram blocks into the map and replace with
  # placeholders.
  def extract(data)
    return data if @markup.format == :txt
    data.gsub(/^\{\{\{\{\{\{ ?(.+?)\r?\n(.+?)\r?\n\}\}\}\}\}\}\r?$/m) do
      id       = Digest::SHA1.hexdigest(Regexp.last_match[2])
      @map[id] = { :style => Regexp.last_match[1], :code => Regexp.last_match[2] }
      id
    end
  end

  # Process all diagrams from the map and replace the placeholders with
  # the final HTML.
  #
  # data - The String data (with placeholders).
  #
  # Returns the marked up String data.
  def process(data)
    @map.each do |id, spec|
      data.gsub!(id) do
        render_wsd(spec[:code], spec[:style])
      end
    end
    data
  end

  private
  # Render the sequence diagram on the remote server.
  #
  # Returns an <img> tag to the rendered image, or an HTML error.
  def render_wsd(code, style)
    response = Net::HTTP.post_form(URI.parse(WSD_URL), 'style' => style, 'message' => code)
    if response.body =~ /img: "(.+)"/
      url = "//www.websequencediagrams.com/#{Regexp.last_match[1]}"
      "<img src=\"#{url}\" />"
    else
      puts response.body
      html_error("Sorry, unable to render sequence diagram at this time.")
    end
  end
end

Version data entries

37 entries across 37 versions & 2 rubygems

Version Path
gitlab-gollum-lib-4.2.7.10.gitlab.2 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7.10.gitlab.1 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7.9 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7.8 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7.7 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7.6 lib/gollum-lib/filter/wsd.rb
gollum-lib-4.2.10-java lib/gollum-lib/filter/wsd.rb
gollum-lib-4.2.10 lib/gollum-lib/filter/wsd.rb
gollum-lib-4.2.9-java lib/gollum-lib/filter/wsd.rb
gollum-lib-4.2.9 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7.5 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7.4 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7.3 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7.2 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7.1 lib/gollum-lib/filter/wsd.rb
gitlab-gollum-lib-4.2.7 lib/gollum-lib/filter/wsd.rb
gollum-lib-4.2.7-java lib/gollum-lib/filter/wsd.rb
gollum-lib-4.2.7 lib/gollum-lib/filter/wsd.rb
gollum-lib-5.0.a.3-java lib/gollum-lib/filter/wsd.rb
gollum-lib-5.0.a.3 lib/gollum-lib/filter/wsd.rb