@@ -29,22 +29,53 @@ def generate(site)
29
29
Jekyll . logger . info ( 'Skipping social image generation' )
30
30
return
31
31
end
32
- guides = Dir . glob ( File . join ( site . source , '_guides' , '*.adoc' ) )
32
+ generate_images ( Dir . glob ( File . join ( site . source , '_guides' , '*.adoc' ) ) , site )
33
+ generate_images ( Dir . glob ( File . join ( site . source , '_posts' , '*.adoc' ) ) , site )
34
+ end
35
+
36
+ def split_text_into_lines ( text )
37
+ lines = [ ]
38
+ words = text . split ( ' ' )
39
+ current_line = ''
40
+
41
+ words . each do |word |
42
+ if current_line . length + word . length <= 32
43
+ current_line += ( current_line == '' ? '' : ' ' ) + word
44
+ else
45
+ lines . push ( current_line )
46
+ current_line = word
47
+ end
48
+ end
49
+
50
+ lines . push ( current_line ) unless current_line . empty?
51
+
52
+ lines
53
+ end
54
+
55
+ private
56
+
57
+ def generate_images ( files , site )
33
58
output_dir = 'assets/images/social'
34
59
FileUtils . mkdir_p ( File . join ( site . source , output_dir ) )
35
60
36
- guides . each do |guide_file |
61
+ files . each do |guide_file |
37
62
basename = File . basename ( guide_file , '.adoc' )
38
63
if basename . start_with? ( '_' )
39
64
next
40
65
end
41
66
title = extract_title ( guide_file )
67
+ # Skip if title is empty
68
+ if ( title . nil? || title . empty? )
69
+ next
70
+ end
42
71
output_file = File . join ( site . source , output_dir , "#{ basename } .png" )
43
72
# Skip if the file already exists
44
73
if File . exist? ( output_file )
45
74
next
46
75
end
47
76
77
+ Jekyll . logger . info ( "Generating social image for '#{ title } ' in #{ output_file } " )
78
+
48
79
# Generate the SVG image
49
80
svg_image_str = generate_svg_string ( title )
50
81
@@ -65,38 +96,16 @@ def generate(site)
65
96
# Change the last parameters to change the position of the generated image
66
97
png_image . compose! ( ChunkyPNG ::Image . from_blob ( b . string ) , 0 , 80 )
67
98
68
- Jekyll . logger . info ( "Generating social image to #{ output_file } " )
69
99
# Save the composed image to the output file
70
100
png_image . save ( output_file )
71
101
end
72
102
end
73
103
74
- def split_text_into_lines ( text )
75
- lines = [ ]
76
- words = text . split ( ' ' )
77
- current_line = ''
78
-
79
- words . each do |word |
80
- if current_line . length + word . length <= 32
81
- current_line += ( current_line == '' ? '' : ' ' ) + word
82
- else
83
- lines . push ( current_line )
84
- current_line = word
85
- end
86
- end
87
-
88
- lines . push ( current_line ) unless current_line . empty?
89
-
90
- lines
91
- end
92
-
93
- private
94
-
95
104
def generate_svg_string ( title )
96
105
idx = 90
97
106
font_size = 30
98
107
tspan_elements = ''
99
- split_text_into_lines ( title ) . each_with_index do |line , index |
108
+ split_text_into_lines ( title . gsub ( '&' , '&' ) ) . each_with_index do |line , index |
100
109
tspan_elements += "<tspan x='50%' y='#{ idx } '>#{ line } </tspan>"
101
110
idx += font_size + 10
102
111
end
0 commit comments