|
| 1 | +require 'chunky_png' |
| 2 | +require 'cairo' |
| 3 | +require 'rsvg2' |
| 4 | + |
| 5 | +module Jekyll |
| 6 | + # Generates social images for blog posts and guides |
| 7 | + module SocialImages |
| 8 | + def social_image(text, page_path) |
| 9 | + # If text is not empty, return it |
| 10 | + if text.nil? || text.empty? |
| 11 | + # If page_path contains "guides/", return the social image path |
| 12 | + if page_path.include?('guides/') |
| 13 | + return "/assets/images/social/#{File.basename(page_path, '.adoc')}.png" |
| 14 | + else |
| 15 | + return "/assets/images/quarkus_card.png" |
| 16 | + end |
| 17 | + else |
| 18 | + text |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + class GenerateSocialImagesGenerator < Generator |
| 24 | + def generate(site) |
| 25 | + guides = Dir.glob(File.join(site.source, '_guides', '*.adoc')) |
| 26 | + output_dir = 'assets/images/social' |
| 27 | + FileUtils.mkdir_p(File.join(site.dest, output_dir)) |
| 28 | + |
| 29 | + guides.each do |guide_file| |
| 30 | + basename = File.basename(guide_file, '.adoc') |
| 31 | + if basename.start_with?('_') |
| 32 | + next |
| 33 | + end |
| 34 | + title = extract_title(guide_file) |
| 35 | + output_file = File.join(site.dest, output_dir, "#{basename}.png") |
| 36 | + # Skip if the file already exists |
| 37 | + if File.exist?(output_file) |
| 38 | + next |
| 39 | + end |
| 40 | + |
| 41 | + # Generate the SVG image |
| 42 | + svg_image_str = generate_svg_string(title) |
| 43 | + |
| 44 | + # Create a Cairo surface and context for the PNG image (must be smaller than 600x330) |
| 45 | + surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, 600, 250) |
| 46 | + context = Cairo::Context.new(surface) |
| 47 | + |
| 48 | + # Load and render the SVG onto the Cairo context |
| 49 | + svg = RSVG::Handle.new_from_data(svg_image_str) |
| 50 | + context.render_rsvg_handle(svg) |
| 51 | + |
| 52 | + # Save the Cairo surface to a PNG file |
| 53 | + b = StringIO.new |
| 54 | + surface.write_to_png(b) |
| 55 | + |
| 56 | + # Compose the generated image with the template image |
| 57 | + png_image = ChunkyPNG::Image.from_file('_plugins/assets/quarkus_card_blank.png') |
| 58 | + # Change the last parameters to change the position of the generated image |
| 59 | + png_image.compose!(ChunkyPNG::Image.from_blob(b.string), 0, 80) |
| 60 | + |
| 61 | + Jekyll.logger.info("Generating social image to #{output_file}") |
| 62 | + # Save the composed image to the output file |
| 63 | + png_image.save(output_file) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def split_text_into_lines(text) |
| 68 | + lines = [] |
| 69 | + words = text.split(' ') |
| 70 | + current_line = '' |
| 71 | + |
| 72 | + words.each do |word| |
| 73 | + if current_line.length + word.length <= 32 |
| 74 | + current_line += (current_line == '' ? '' : ' ') + word |
| 75 | + else |
| 76 | + lines.push(current_line) |
| 77 | + current_line = word |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + lines.push(current_line) unless current_line.empty? |
| 82 | + |
| 83 | + lines |
| 84 | + end |
| 85 | + |
| 86 | + private |
| 87 | + |
| 88 | + def generate_svg_string(title) |
| 89 | + idx = 90 |
| 90 | + font_size = 30 |
| 91 | + tspan_elements = '' |
| 92 | + split_text_into_lines(title).each_with_index do |line, index| |
| 93 | + tspan_elements += "<tspan x='50%' y='#{idx}'>#{line}</tspan>" |
| 94 | + idx += font_size + 10 |
| 95 | + end |
| 96 | + " |
| 97 | + <svg width=\"600\" height=\"330\"> |
| 98 | + <style> |
| 99 | + .title { fill: white; font-size: #{font_size}px; font-weight: bold; font-family:'Open Sans'} |
| 100 | + </style> |
| 101 | + <text x=\"50%\" y=\"50%\" text-anchor=\"middle\" class=\"title\" > |
| 102 | + #{tspan_elements} |
| 103 | + </text> |
| 104 | + </svg> |
| 105 | + " |
| 106 | + end |
| 107 | + |
| 108 | + def extract_title(adoc_file) |
| 109 | + title = '' |
| 110 | + File.open(adoc_file, 'r') do |file| |
| 111 | + file.each_line do |line| |
| 112 | + if line.start_with? '=' |
| 113 | + title = line[2..].strip |
| 114 | + break |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + title |
| 119 | + end |
| 120 | + end |
| 121 | +end |
| 122 | + |
| 123 | +Liquid::Template.register_filter(Jekyll::SocialImages) |
0 commit comments