Automatically group files using the series meta

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2024-05-22 22:41:32 -07:00
parent 00f0f13b93
commit 5c19fc4208

30
agda.rb
View File

@ -280,7 +280,7 @@ class FileGroup
end end
end end
def cross_link_files(file_documents) def cross_link_files
# Now, we have a complete list of all the IDs visible in scope. # Now, we have a complete list of all the IDs visible in scope.
# Redirect relevant links to these IDs. This achieves within-post # Redirect relevant links to these IDs. This achieves within-post
# links. # links.
@ -301,7 +301,6 @@ class FileGroup
id = target[:id] id = target[:id]
relpath = Pathname.new(other_file).dirname.relative_path_from(Pathname.new(file).dirname) relpath = Pathname.new(other_file).dirname.relative_path_from(Pathname.new(file).dirname)
puts relpath
node['href'] = "#{relpath}##{id}" node['href'] = "#{relpath}##{id}"
else else
# No definitions in any blog page. For now, just delete the anchor. # No definitions in any blog page. For now, just delete the anchor.
@ -313,15 +312,32 @@ class FileGroup
end end
agda_context = AgdaContext.new agda_context = AgdaContext.new
file_documents = {} file_documents = {}
files.each do |file| series_groups = files.group_by do |file|
file_documents[file] = document = Nokogiri::HTML.parse(File.open(file)) file_documents[file] = document = Nokogiri::HTML.parse(File.open(file))
document.css("meta[name=blog-series]")&.attribute("content")&.to_s
end end
handler = FileGroup.new agda_context
file_documents.each do |file, document| # For the 'nil' group, process individually.
handler.process_source_file file, document if files_with_no_series = series_groups.delete(nil)
files_with_no_series.each do |file|
file_group = FileGroup.new agda_context
file_group.process_source_file file, file_documents[file]
file_group.cross_link_files
end
end end
handler.cross_link_files(file_documents)
# For groups, process them together to allow cross-linking
series_groups.each do |_, files_in_series|
file_group = FileGroup.new agda_context
files_in_series.each do |file|
file_group.process_source_file file, file_documents[file]
end
file_group.cross_link_files
end
# Having modified all the HTML files, save them.
file_documents.each do |file, document| file_documents.each do |file, document|
File.write(file, document.to_html(encoding: 'UTF-8')) File.write(file, document.to_html(encoding: 'UTF-8'))
end end