Skip to content

Commit

Permalink
Fix title extraction algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Nov 1, 2023
1 parent f49dcef commit 05ee114
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions _plugins/social_images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def generate(site)
Jekyll.logger.info('Skipping social image generation')
return
end
generate_images(Dir.glob(File.join(site.source, '_guides', '*.adoc')), site)
generate_images(Dir.glob(File.join(site.source, '_posts', '*.adoc')), site)
generate_images(Dir.glob(File.join(site.source, '_guides', '*.adoc')), site)
end

def split_text_into_lines(text)
Expand Down Expand Up @@ -105,7 +105,12 @@ def generate_svg_string(title)
idx = 90
font_size = 30
tspan_elements = ''
split_text_into_lines(title.gsub('&', '&')).each_with_index do |line, index|
# Sanitize title
title = title.gsub(/&/, '&')
title = title.gsub(/</, '&lt;')
title = title.gsub(/>/, '&gt;')

split_text_into_lines(title).each_with_index do |line, index|
tspan_elements += "<tspan x='50%' y='#{idx}'>#{line}</tspan>"
idx += font_size + 10
end
Expand All @@ -122,16 +127,27 @@ def generate_svg_string(title)
end

def extract_title(adoc_file)
title = ''
File.open(adoc_file, 'r') do |file|
file.each_line do |line|
if line.start_with? '='
title = line[2..].strip
line_nr = 0
File.readlines(adoc_file).each do |line|
if line_nr == 0
# If line does not start with --- break
unless line.strip.start_with?('---')
break
end
end
if line_nr > 0 && line.strip.start_with?('---')
break;
end
if line.strip.start_with?('title:')
title = line.strip.sub('title:', '').strip
# Remove quotes
title = title.gsub(/\A[\"']|[\"']\z/, '')
return title
end
line_nr += 1
end
title
doc = Asciidoctor.load_file(adoc_file, header_only: true, logger: NullLogger.new)
doc.doctitle
end
end
end
Expand Down

0 comments on commit 05ee114

Please sign in to comment.