Class: Coradoc::Input::Html::Converters::Img
- Inherits:
-
Base
- Object
- Base
- Coradoc::Input::Html::Converters::Img
show all
- Defined in:
- lib/coradoc/html/input/converters/img.rb
Constant Summary
collapse
- INSTANCE =
new
Instance Method Summary
collapse
Methods inherited from Base
#extract_leading_trailing_whitespace, #extract_text_from_content, #extract_title, #node_has_ancestor?, #textnode_after_start_with?, #textnode_before_end_with?, #treat_children_coradoc, #treat_coradoc, #unconstrained_after?, #unconstrained_before?
Instance Method Details
#copy_temp_file(imgdata) ⇒ Object
62
63
64
65
66
67
68
69
70
|
# File 'lib/coradoc/html/input/converters/img.rb', line 62
def copy_temp_file(imgdata)
f = Tempfile.open(['radoc', '.jpg'])
f.binmode
f.write(Base64.strict_decode64(imgdata))
f.rewind
ext = Marcel::MimeType.for(f).sub(%r{^[^/]+/}, '')
ext = 'svg' if ext == 'svg+xml'
[ext, f.path, f]
end
|
#datauri2file(src) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/coradoc/html/input/converters/img.rb', line 27
def datauri2file(src)
return unless src
%r{^data:image/(?:[^;]+);base64,(?<imgdata>.+)$} =~ src
dest_dir = Pathname.new(Coradoc::Html::Input.config.destination).dirname
images_dir = dest_dir.join('images')
FileUtils.mkdir_p(images_dir)
ext, image_src_path, tempfile = determine_image_src_path(
src,
imgdata
)
image_dest_path = images_dir + "#{image_number}.#{ext}"
if File.exist?(image_src_path)
FileUtils.cp(image_src_path, image_dest_path)
else
Kernel.warn "Image #{image_src_path} does not exist"
end
image_number_increment
image_dest_path.relative_path_from(dest_dir)
ensure
tempfile&.close!
end
|
#determine_image_src_path(src, imgdata) ⇒ Object
55
56
57
58
59
60
|
# File 'lib/coradoc/html/input/converters/img.rb', line 55
def determine_image_src_path(src, imgdata)
return copy_temp_file(imgdata) if imgdata
ext = File.extname(src).strip.downcase[1..]
[ext, Pathname.new(Coradoc::Html::Input.config.sourcedir).join(src)]
end
|
#image_number ⇒ Object
16
17
18
19
20
21
|
# File 'lib/coradoc/html/input/converters/img.rb', line 16
def image_number
format(
Coradoc::Html::Input.config.image_counter_pattern,
Coradoc::Html::Input.config.image_counter
)
end
|
#image_number_increment ⇒ Object
23
24
25
|
# File 'lib/coradoc/html/input/converters/img.rb', line 23
def image_number_increment
Coradoc::Html::Input.config.image_counter += 1
end
|
#to_coradoc(node, _state = {}) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/coradoc/html/input/converters/img.rb', line 72
def to_coradoc(node, _state = {})
id = node['id']
alt = node['alt']
src = node['src']
width = node['width']
height = node['height']
width = width.to_i if width&.match?(/\A\d+\z/)
height = height.to_i if height&.match?(/\A\d+\z/)
title = (node)
src = datauri2file(src) if Coradoc::Html::Input.config.external_images
return unless src
Coradoc::CoreModel::Image.new(
src: src,
alt: alt,
caption: title,
width: width&.to_s,
height: height&.to_s,
id: id
)
end
|