_sitemap: Simplify sitemap genration.

This commit is contained in:
Stig-Ørjan Smelror 2024-02-19 19:47:23 +01:00
parent 6a138075fb
commit e5c4b9ae3e

66
qsgen2
View File

@ -942,16 +942,14 @@ function _sitemap() {
# Find all HTML files and store them in an array # Find all HTML files and store them in an array
# local -a html_files=("${(@f)$(find "${www_root}" -type f -name "*.html")}") # local -a html_files=("${(@f)$(find "${www_root}" -type f -name "*.html")}")
builtin cd ${config[site_root]} builtin cd ${config[site_root]}
local -a html_files=(**/[a-z]*.html(.)) local -a blog_files=()
local -a blog_files=(${html_files[@]:#*blog*})
local -a page_files=() local -a page_files=()
for file in "${html_files[@]}"; do for file in "${html_files[@]}"; do
[[ $file != *blog* ]] && page_files+=("$file") if [[ $file == *blog* ]]; then
done blog_files+=("$file")
else
local -a xml_files=([a-z]*.xml(.)) page_files+=("$file")
for file in "${xml_files[@]}"; do fi
[[ $file != *sitemap.* ]] && xml_files+=("$file")
done done
# Start of the XML file for BLOGS # Start of the XML file for BLOGS
@ -1012,38 +1010,32 @@ function _sitemap() {
echo '</urlset>' >> "${sitemap_page}" echo '</urlset>' >> "${sitemap_page}"
_msg std " - ${p_file}" _msg std " - ${p_file}"
# Start of the XML file
echo '<?xml version="1.0" encoding="UTF-8"?>' > ${sitemap_file}
echo "<!-- Sitemap generated by ${QSGEN} ${VERSION} - https://github.com/kekePower/qsgen2 -->" >> ${sitemap_file}
echo "<?xml-stylesheet type=\"text/xsl\" href=\"${site_url}/css/default-sitemap.xsl?sitemap=page\"?>" >> ${sitemap_file}
echo '<urlset' >> ${sitemap_file}
echo ' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' >> ${sitemap_file}
echo ' xmlns:xhtml="http://www.w3.org/1999/xhtml"' >> ${sitemap_file}
echo ' xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"' >> ${sitemap_file}
echo '>' >> ${sitemap_file}
# Add each URL to the sitemap
for file in "${xml_files[@]}"
do
# Remove www_root from the path and prepend site_url
local url="${site_url}/${file#$www_root}"
local lastmod=$(stat -c %y "${file}" 2>/dev/null | cut -d' ' -f1,2 | sed 's/ /T/' | sed 's/\..*$//')
echo " <url>" >> ${sitemap_file}
echo " <loc>${url}</loc>" >> ${sitemap_file}
echo " <lastmod><![CDATA[${lastmod}+01:00]]></lastmod>" >> "${sitemap_file}"
echo " <changefreq><![CDATA[always]]></changefreq>" >> ${sitemap_file}
echo " <priority><![CDATA[1]]></priority>" >> ${sitemap_file}
echo " </url>" >> ${sitemap_file}
done
# End of the XML file
echo '</urlset>' >> "${sitemap_file}"
_msg std " - ${sm_file}"
if (${debug}); then _msg debug "${0:t}_msg_2" " ${sitemap_file}"; fi if (${debug}); then _msg debug "${0:t}_msg_2" " ${sitemap_file}"; fi
fi fi
# Start of the XML file for the main sitemap
echo '<?xml version="1.0" encoding="UTF-8"?>' > "${sitemap_file}"
echo "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">" >> "${sitemap_file}"
# Add sitemap-blogs.xml to the sitemap
echo " <sitemap>" >> "${sitemap_file}"
echo " <loc>${site_url}/${b_file}</loc>" >> "${sitemap_file}"
local lastmod_b=$(stat -c %y "${b_file}" 2>/dev/null | cut -d' ' -f1,2 | sed 's/ /T/' | sed 's/\..*$//')
echo " <lastmod>${lastmod_b}</lastmod>" >> "${sitemap_file}"
echo " </sitemap>" >> "${sitemap_file}"
# Add sitemap-pages.xml to the sitemap
echo " <sitemap>" >> "${sitemap_file}"
echo " <loc>${site_url}/${p_file}</loc>" >> "${sitemap_file}"
local lastmod_p=$(stat -c %y "${p_file}" 2>/dev/null | cut -d' ' -f1,2 | sed 's/ /T/' | sed 's/\..*$//')
echo " <lastmod>${lastmod_p}</lastmod>" >> "${sitemap_file}"
echo " </sitemap>" >> "${sitemap_file}"
# End of the XML file
echo "</sitemapindex>" >> "${sitemap_file}"
_msg std " - ${sm_file}"
builtin cd ${config[project_root]} builtin cd ${config[project_root]}
} }