Skip to content

Instantly share code, notes, and snippets.

@tamoot
Last active August 21, 2017 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamoot/ebc8876b0d0e98b4fd198ceb1e514ab1 to your computer and use it in GitHub Desktop.
Save tamoot/ebc8876b0d0e98b4fd198ceb1e514ab1 to your computer and use it in GitHub Desktop.
#tDiary 向け PlantUML プラグイン
# -*- coding: utf-8 -*-
# create image by PlantUML http://plantuml.com/
#
# Copyright (c) tamoot <tamoot+tdiary@gmail.com>
# Distributed under the GPL
#
require 'uri'
require 'zlib'
require 'digest/md5'
module ::PlantUML
module Deflate
CHARS ||= ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a + ['-', '_']
def self.compress(text)
compressed = Zlib::Deflate.deflate(text, Zlib::BEST_COMPRESSION)
compressed.chars.each_slice(3).map do |chars|
append3bytes(chars[0].ord, chars[1]&.ord.to_i, chars[2]&.ord.to_i)
end.join
end
private
def self.append3bytes(b1, b2, b3)
[
b1 >> 2,
((b1 & 0x3) << 4) | (b2 >> 4),
((b2 & 0xF) << 2) | (b3 >> 6),
b3 & 0x3F,
].map { |c| CHARS[c & 0x3F] || '?' }.join
end
end
end
def plantuml(text)
html = %Q|<div class="plantuml">|
begin
uri = URI::parse( @conf['plantuml.server'] )
uri.path.gsub!(/\/+$/, "")
uri.path << '/png/' << PlantUML::Deflate::compress(text)
html << %Q|<img src=#{uri}></img>|
rescue
html << $!.message
end
html << %Q|</div>|
end
add_conf_proc('plantuml_server', 'PlantUMLサーバ') do
if @mode == 'saveconf'
@conf['plantuml.server'] = @cgi.params['plantuml.server'][0]
end
r = <<-_HTML
<h3>Summary</h3>
<p>The image is generated by using specified PlantUML server.</p>
<h3>URL of PlantUML server</h3>
<p><input type="text" name="plantuml.server" size="100" value="#{@conf['plantuml.server']}"></p>
_HTML
r
end
# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End:
# vim: ts=3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment