Class: Vectory::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/vectory/utils.rb

Class Method Summary collapse

Class Method Details

.absolute_path?(uri) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/vectory/utils.rb', line 55

def absolute_path?(uri)
  %r{^/}.match?(uri) || %r{^[A-Z]:/}.match?(uri)
end

.datauri(uri, local_dir = ".") ⇒ Object

Extracted from github.com/metanorma/metanorma-utils/blob/v1.5.2/lib/utils/image.rb

sources/plantuml/plantuml20200524-90467-1iqek5i.png already includes localdir Check whether just the local path or the other specified relative path works.



16
17
18
19
20
21
22
23
24
# File 'lib/vectory/utils.rb', line 16

def datauri(uri, local_dir = ".")
  (datauri?(uri) || url?(uri)) and return uri

  path = path_which_exist(uri, local_dir)
  path and return encode_datauri(path)

  Vectory.ui.warn "Image specified at `#{uri}` does not exist."
  uri # Return original provided location
end

.datauri2mime(uri) ⇒ Object

FIXME: This method should ONLY return 1 type, remove Array wrapper



79
80
81
82
83
84
# File 'lib/vectory/utils.rb', line 79

def datauri2mime(uri)
  output = decode_datauri(uri)
  return nil unless output && output[:type_detected]

  [output[:type_detected]]
end

.datauri?(uri) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/vectory/utils.rb', line 47

def datauri?(uri)
  /^data:/.match?(uri)
end

.decode_datauri(uri) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vectory/utils.rb', line 86

def decode_datauri(uri)
  %r{^data:(?<mimetype>[^;]+);base64,(?<mimedata>.+)$} =~ uri
  return nil unless mimetype && mimedata

  data = Base64.strict_decode64(mimedata)
  {
    type_declared: mimetype,
    type_detected: Marcel::MimeType.for(data, declared_type: mimetype),
    data: data,
  }
end

.encode_datauri(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vectory/utils.rb', line 33

def encode_datauri(path)
  return nil unless File.exist?(path)

  type = Marcel::MimeType.for(Pathname.new(path)) ||
    'text/plain; charset="utf-8"'

  bin = File.binread(path)
  data = Base64.strict_encode64(bin)
  "data:#{type};base64,#{data}"
  # rescue StandardError
  #   warn "Data-URI encoding of `#{path}` failed."
  #   nil
end

.path_which_exist(uri, local_dir) ⇒ Object



26
27
28
29
30
31
# File 'lib/vectory/utils.rb', line 26

def path_which_exist(uri, local_dir)
  options = absolute_path?(uri) ? [uri] : [uri, File.join(local_dir, uri)]
  options.detect do |p|
    File.file?(p)
  end
end

.save_dataimage(uri) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/vectory/utils.rb', line 67

def save_dataimage(uri)
  %r{^data:(?:image|application)/(?<imgtype>[^;]+);(?:charset=[^;]+;)?base64,(?<imgdata>.+)$} =~ uri # rubocop:disable Layout/LineLength
  imgtype.sub!(/\+[a-z0-9]+$/, "") # svg+xml
  imgtype = "png" unless /^[a-z0-9]+$/.match? imgtype
  Tempfile.open(["image", ".#{imgtype}"]) do |f|
    f.binmode
    f.write(Base64.strict_decode64(imgdata))
    f.path
  end
end

.svgmap_rewrite0_path(src, localdirectory) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/vectory/utils.rb', line 59

def svgmap_rewrite0_path(src, localdirectory)
  if /^data:/.match?(src)
    save_dataimage(src)
  else
    File.file?(src) ? src : localdirectory + src
  end
end

.url?(url) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/vectory/utils.rb', line 51

def url?(url)
  %r{^[A-Z]{2,}://}i.match?(url)
end