Skip to content

Commit 3887332

Browse files
authored
Merge pull request #24 from comphy-lab/coderabbitai/docstrings/477e2da
📝 Add docstrings to `SEO-and-paper-update`
2 parents 477e2da + 9be2eef commit 3887332

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

scripts/generate_seo_tags.rb

+35-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,18 @@
2727
# Track URL normalizations
2828
normalized_urls = {}
2929

30-
# Helper method to normalize URLs
30+
##
31+
# Normalizes a URL into a file path by removing fragments,
32+
# ensuring a leading slash, appending "index.html" for directory URLs or URLs missing an extension,
33+
# and then stripping the leading slash for file operations.
34+
#
35+
# @param url [String] the URL to be normalized.
36+
# @return [String] the normalized relative file path.
37+
#
38+
# @example
39+
# normalize_url("about") #=> "about/index.html"
40+
# normalize_url("/contact#team") #=> "contact/index.html"
41+
# normalize_url("/index.html") #=> "index.html"
3142
def normalize_url(url)
3243
# Remove anchor
3344
url = url.split('#').first
@@ -119,7 +130,20 @@ def normalize_url(url)
119130
end
120131
end
121132

122-
# Function to update HTML files with metadata
133+
##
134+
# Updates an HTML file with SEO metadata by modifying its keywords and description meta tags.
135+
#
136+
# This method reads the HTML file at the specified path and uses Nokogiri to parse and update—or add—meta tags for keywords and description based on the provided collections. It concatenates unique keywords and uses the first description when appropriate, updating the meta description only if it is missing or too short.
137+
#
138+
# @param file_path [String] Path to the HTML file to update.
139+
# @param keywords [#to_a] A collection of keywords to include in the meta keywords tag.
140+
# @param description [#to_a] A collection where the first element is used for the meta description tag.
141+
#
142+
# @return [Boolean, nil] Returns true if the update was successful, false if an error occurred, or nil if the file
143+
# does not exist or its HTML lacks a head element.
144+
#
145+
# @example
146+
# update_successful = update_html_with_metadata("public/index.html", Set.new(["ruby", "seo"]), Set.new(["Enhance page visibility with Ruby"]))
123147
def update_html_with_metadata(file_path, keywords, description)
124148
return unless File.exist?(file_path)
125149

@@ -196,7 +220,15 @@ def update_html_with_metadata(file_path, keywords, description)
196220

197221
puts "Updated #{files_updated} HTML files with SEO metadata"
198222

199-
# Generate sitemapindex.xml
223+
##
224+
# Generates a sitemap index XML file from an existing sitemap.
225+
#
226+
# This method checks for a sitemap.xml file in the specified directory. If found, it retrieves the file's last modified timestamp,
227+
# creates a sitemapindex.xml file linking to the sitemap with the last modification date, writes the file to disk, and returns true.
228+
# If sitemap.xml is not present, the method logs a message and returns false.
229+
#
230+
# @param site_dir [String] The directory containing the sitemap.xml file.
231+
# @return [Boolean] True if sitemapindex.xml is generated successfully; false otherwise.
200232
def generate_sitemapindex(site_dir)
201233
sitemap_path = File.join(site_dir, 'sitemap.xml')
202234
sitemapindex_path = File.join(site_dir, 'sitemapindex.xml')

0 commit comments

Comments
 (0)