asciidoctor-plantuml-0.0.7/ 0000755 0001750 0001750 00000000000 13124404241 017134 5 ustar balasankarc balasankarc asciidoctor-plantuml-0.0.7/test/ 0000755 0001750 0001750 00000000000 13124404241 020113 5 ustar balasankarc balasankarc asciidoctor-plantuml-0.0.7/test/test_plantuml.rb 0000644 0001750 0001750 00000012400 13124404241 023330 0 ustar balasankarc balasankarc require "test/unit" require "asciidoctor" require "stringio" require "nokogiri" require "asciidoctor-plantuml" DOC_BASIC = <<-eos = Hello PlantUML! [plantuml, format="png"] -- User -> (Start) User --> (Use the application) : Label -- eos DOC_BASIC2 = <<-eos = Hello PlantUML! [plantuml, format="png"] @startuml User -> (Start) User --> (Use the application) : Label @enduml eos DOC_ID = <<-eos = Hello PlantUML! [plantuml, format="png", id="myId"] User -> (Start) User --> (Use the application) : Label eos DOC_DIM = <<-eos = Hello PlantUML! [plantuml, format="png", width="100px", height="50px"] User -> (Start) User --> (Use the application) : Label eos DOC_ALT = <<-eos = Hello PlantUML! [plantuml, format="png", alt="alt"] User -> (Start) User --> (Use the application) : Label eos DOC_BAD_FORMAT = <<-eos = Hello PlantUML! [plantuml, format="jpg"] User -> (Start) User --> (Use the application) : Label eos DOC_MULTI = <<-eos = Hello PlantUML! [plantuml, format="png"] User -> (Start) User --> (Use the application) : Label [plantuml, format="png"] User -> (Start) User --> (Use the application) : Label [plantuml, format="txt"] User -> (Start) User --> (Use the application) : Label eos DOC_TXT = <<-eos = Hello PlantUML! [plantuml, format="txt"] -- User -> (Start) User --> (Use the application) : Label -- eos class PlantUmlTest < Test::Unit::TestCase GENURL = "http://localhost:8080/plantuml/png/U9npA2v9B2efpStX2YrEBLBGjLFG20Q9Q4Bv804WIw4a8rKXiQ0W9pCviIGpFqzJmKh19p4fDOVB8JKl1QWT05kd5wq0" def setup Asciidoctor::PlantUml.configure do |c| c.url = "http://localhost:8080/plantuml" c.txt_enable = true end end def test_plantuml_block_processor html = ::Asciidoctor.convert(StringIO.new(DOC_BASIC), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('img.plantuml') assert_equal elements.size, 1 element = elements.first assert_equal GENURL, element["src"] end def test_plantuml_block_processor2 html = ::Asciidoctor.convert(StringIO.new(DOC_BASIC2), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('img.plantuml') assert_equal elements.size, 1 element = elements.first assert_equal GENURL, element["src"] end def test_plantuml_id_attribute html = ::Asciidoctor.convert(StringIO.new(DOC_ID), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('img.plantuml') assert_equal elements.size, 1 element = elements.first assert_equal "myId", element["id"] end def test_plantuml_dimension_attribute html = ::Asciidoctor.convert(StringIO.new(DOC_DIM), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('img.plantuml') assert_equal elements.size, 1 element = elements.first assert_equal "100px", element["width"] assert_equal "50px", element["height"] end def test_plantuml_alt_attribute html = ::Asciidoctor.convert(StringIO.new(DOC_ALT), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('img.plantuml') assert_equal elements.size, 1 element = elements.first assert_equal "alt", element["alt"] end def test_should_show_bad_format html = ::Asciidoctor.convert(StringIO.new(DOC_BAD_FORMAT), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('pre.plantuml-error') assert_equal elements.size, 1 end def test_plantuml_multiple html = ::Asciidoctor.convert(StringIO.new(DOC_MULTI), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('img.plantuml') assert elements.size >= 2 elements = page.css('.plantuml-error') assert_equal elements.size, 0 end def test_plantuml_bad_server Asciidoctor::PlantUml.configure do |c| c.url = "http://nonexistent.com/plantuml" end html = ::Asciidoctor.convert(StringIO.new(DOC_MULTI), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('img.plantuml') assert_equal elements.size, 3 elements = page.css('.plantuml-error') assert_equal elements.size, 0 end def test_plantuml_invalid_uri Asciidoctor::PlantUml.configure do |c| c.url = "ftp://test.com" end html = ::Asciidoctor.convert(StringIO.new(DOC_BASIC), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('pre.plantuml-error') assert_equal elements.size, 1 end def test_plantuml_nil_uri Asciidoctor::PlantUml.configure do |c| c.url = nil end html = ::Asciidoctor.convert(StringIO.new(DOC_BASIC), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('pre.plantuml-error') assert_equal elements.size, 1 end def test_plantuml_empty_uri Asciidoctor::PlantUml.configure do |c| c.url = "" end html = ::Asciidoctor.convert(StringIO.new(DOC_BASIC), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('pre.plantuml-error') assert_equal elements.size, 1 end def test_disable_txt Asciidoctor::PlantUml.configure do |c| c.url = "http://localhost:8080/plantuml" c.txt_enable = false end html = ::Asciidoctor.convert(StringIO.new(DOC_TXT), backend: "html5") page = Nokogiri::HTML(html) elements = page.css('pre.plantuml-error') assert_equal elements.size, 1 end end asciidoctor-plantuml-0.0.7/lib/ 0000755 0001750 0001750 00000000000 13124404241 017702 5 ustar balasankarc balasankarc asciidoctor-plantuml-0.0.7/lib/asciidoctor-plantuml.rb 0000644 0001750 0001750 00000000312 13124404241 024360 0 ustar balasankarc balasankarc require 'asciidoctor' require 'asciidoctor/extensions' require_relative 'asciidoctor-plantuml/plantuml' Asciidoctor::Extensions.register do block Asciidoctor::PlantUml::BlockProcessor, :plantuml end asciidoctor-plantuml-0.0.7/lib/asciidoctor-plantuml/ 0000755 0001750 0001750 00000000000 13124404241 024037 5 ustar balasankarc balasankarc asciidoctor-plantuml-0.0.7/lib/asciidoctor-plantuml/plantuml.rb 0000644 0001750 0001750 00000017421 13124404241 026225 0 ustar balasankarc balasankarc require 'uri' require 'zlib' require 'open-uri' require 'net/http' module Asciidoctor module PlantUml class Configuration DEFAULT_URL = ENV["PLANTUML_URL"] || "" attr_accessor :url, :txt_enable, :svg_enable, :png_enable def initialize @url = DEFAULT_URL @txt_enable = true @svg_enable = true @png_enable = true end end class << self attr_writer :configuration end def self.configuration @configuration ||= Configuration.new end def self.configure yield(configuration) end class Processor FORMATS = ["png", "svg", "txt"] DEFAULT_FORMAT = FORMATS[0] class << self def valid_format?(format) FORMATS.include?(format) end def server_url PlantUml::configuration.url end def txt_enabled? PlantUml::configuration.txt_enable end def png_enabled? PlantUml::configuration.png_enable end def svg_enabled? PlantUml::configuration.svg_enable end def enabled? txt_enabled? || png_enabled? || svg_enabled? end def plantuml_content(code, attrs = {}) format = attrs["format"] || DEFAULT_FORMAT if !enabled? return plantuml_disabled_content(code, attrs) end if !valid_uri?(server_url) return plantuml_server_unavailable_content(server_url, attrs) end case format when "png" plantuml_img_content(code, format, attrs) when "txt" if txt_enabled? plantuml_txt_content(code, format, attrs) else plantuml_invalid_content(format, attrs) end when "svg" plantuml_img_content(code, format, attrs) else plantuml_invalid_content(format, attrs) end end # Compression code used to generate PlantUML URLs. Taken directly from the # Transcoder class in the PlantUML java code. def gen_url(text, format) result = "" compressedData = Zlib::Deflate.deflate(text) compressedData.chars.each_slice(3) do |bytes| #print bytes[0], ' ' , bytes[1] , ' ' , bytes[2] b1 = bytes[0].nil? ? 0 : (bytes[0].ord & 0xFF) b2 = bytes[1].nil? ? 0 : (bytes[1].ord & 0xFF) b3 = bytes[2].nil? ? 0 : (bytes[2].ord & 0xFF) result += append3bytes(b1, b2, b3) end join_paths(server_url, "/#{format}/", result).to_s end private def plantuml_txt_content(code, format, attrs = {}) begin url = gen_url(code, format) open(url) do |f| plantuml_ascii_content(f.read, format, attrs) end rescue plantuml_img_content(code, format, attrs) end end def plantuml_ascii_content(code, format, attrs = {}) content = "
\n" content += code content +="" content += "
" content += "PlantUML Error: Invalid format \"#{format}\"" content +="" content += "
" content += "PlantUML Error: cannot connect to PlantUML server at \"#{url}\"" content +="" content += "
\n" content += code content +="" content += "