|
| 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 File.exist?("./assets/images/social/#{File.basename(page_path, '.adoc')}.png") |
| 12 | + return "/assets/images/social/#{File.basename(page_path, '.adoc')}.png" |
| 13 | + else |
| 14 | + return "/assets/images/quarkus_card.png" |
| 15 | + end |
| 16 | + else |
| 17 | + text |
| 18 | + end |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + class GenerateSocialImagesGenerator < Generator |
| 23 | + def generate(site) |
| 24 | + # Check if skip_social_images is set to true |
| 25 | + # If so, skip generating social images |
| 26 | + # This is useful when running the site locally |
| 27 | + if site.config['skip_social_images'] |
| 28 | + Jekyll.logger.info('Skipping social image generation') |
| 29 | + return |
| 30 | + end |
| 31 | + generate_images(Dir.glob(File.join(site.source, '_posts', '*.adoc')), site) |
| 32 | + generate_images(Dir.glob(File.join(site.source, '_guides', '*.adoc')), site) |
| 33 | + end |
| 34 | + |
| 35 | + def split_text_into_lines(text) |
| 36 | + lines = [] |
| 37 | + words = text.split(' ') |
| 38 | + current_line = '' |
| 39 | + |
| 40 | + words.each do |word| |
| 41 | + if current_line.length + word.length <= 32 |
| 42 | + current_line += (current_line == '' ? '' : ' ') + word |
| 43 | + else |
| 44 | + lines.push(current_line) |
| 45 | + current_line = word |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + lines.push(current_line) unless current_line.empty? |
| 50 | + |
| 51 | + lines |
| 52 | + end |
| 53 | + |
| 54 | + private |
| 55 | + |
| 56 | + def generate_images(files, site) |
| 57 | + output_dir = 'assets/images/social' |
| 58 | + FileUtils.mkdir_p(File.join(site.source, output_dir)) |
| 59 | + |
| 60 | + files.each do |guide_file| |
| 61 | + basename = File.basename(guide_file, '.adoc') |
| 62 | + if basename.start_with?('_') |
| 63 | + next |
| 64 | + end |
| 65 | + title = extract_title(guide_file) |
| 66 | + # Skip if title is empty |
| 67 | + if (title.nil? || title.empty?) |
| 68 | + next |
| 69 | + end |
| 70 | + output_file = File.join(site.source, output_dir, "#{basename}.png") |
| 71 | + # Skip if the file already exists |
| 72 | + if File.exist?(output_file) |
| 73 | + next |
| 74 | + end |
| 75 | + |
| 76 | + Jekyll.logger.info("Generating social image for '#{title}' in #{output_file}") |
| 77 | + |
| 78 | + # Generate the SVG image |
| 79 | + svg_image_str = generate_svg_string(title) |
| 80 | + |
| 81 | + # Create a Cairo surface and context for the PNG image (must be smaller than 600x330) |
| 82 | + surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, 600, 250) |
| 83 | + context = Cairo::Context.new(surface) |
| 84 | + |
| 85 | + # Load and render the SVG onto the Cairo context |
| 86 | + svg = RSVG::Handle.new_from_data(svg_image_str) |
| 87 | + context.render_rsvg_handle(svg) |
| 88 | + |
| 89 | + # Save the Cairo surface to a PNG file |
| 90 | + b = StringIO.new |
| 91 | + surface.write_to_png(b) |
| 92 | + |
| 93 | + # Compose the generated image with the template image |
| 94 | + png_image = ChunkyPNG::Image.from_file('_plugins/assets/quarkus_card_blank.png') |
| 95 | + # Change the last parameters to change the position of the generated image |
| 96 | + png_image.compose!(ChunkyPNG::Image.from_blob(b.string), 0, 80) |
| 97 | + |
| 98 | + # Save the composed image to the output file |
| 99 | + png_image.save(output_file) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + def generate_svg_string(title) |
| 104 | + idx = 90 |
| 105 | + font_size = 30 |
| 106 | + tspan_elements = '' |
| 107 | + # Sanitize title |
| 108 | + title = title.gsub(/&/, '&') |
| 109 | + title = title.gsub(/</, '<') |
| 110 | + title = title.gsub(/>/, '>') |
| 111 | + |
| 112 | + split_text_into_lines(title).each_with_index do |line, index| |
| 113 | + tspan_elements += "<tspan x='50%' y='#{idx}'>#{line}</tspan>" |
| 114 | + idx += font_size + 10 |
| 115 | + end |
| 116 | + " |
| 117 | + <svg width=\"600\" height=\"330\"> |
| 118 | + <style> |
| 119 | + .title { fill: white; font-size: #{font_size}px; font-weight: bold; font-family:'Open Sans'} |
| 120 | + </style> |
| 121 | + <text x=\"50%\" y=\"50%\" text-anchor=\"middle\" class=\"title\" > |
| 122 | + #{tspan_elements} |
| 123 | + </text> |
| 124 | + </svg> |
| 125 | + " |
| 126 | + end |
| 127 | + |
| 128 | + def extract_title(adoc_file) |
| 129 | + line_nr = 0 |
| 130 | + File.readlines(adoc_file).each do |line| |
| 131 | + if line_nr == 0 |
| 132 | + # If line does not start with --- break |
| 133 | + unless line.strip.start_with?('---') |
| 134 | + break |
| 135 | + end |
| 136 | + end |
| 137 | + if line_nr > 0 && line.strip.start_with?('---') |
| 138 | + break; |
| 139 | + end |
| 140 | + if line.strip.start_with?('title:') |
| 141 | + title = line.strip.sub('title:', '').strip |
| 142 | + # Remove quotes |
| 143 | + title = title.gsub(/\A[\"']|[\"']\z/, '') |
| 144 | + return title |
| 145 | + end |
| 146 | + line_nr += 1 |
| 147 | + end |
| 148 | + doc = Asciidoctor.load_file(adoc_file, header_only: true, logger: NullLogger.new) |
| 149 | + doc.doctitle |
| 150 | + end |
| 151 | + end |
| 152 | +end |
| 153 | + |
| 154 | +Liquid::Template.register_filter(Jekyll::SocialImages) |
0 commit comments