diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b5b95c4..5b3f6756 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +### CI/CD + +### Dependencies + + +## [1.9.0] - 2022-10-25 + +### Added +* Option to include dates only in the lastmod fields of XML sitemaps. Default includes full date-time. + ### CI/CD * Bump Python to 3.11 in CI/CD workflows. diff --git a/README.md b/README.md index 20473a61..3ef804d4 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,13 @@ for pages where the filename has the `.html` extension. If you prefer to exclude `.html` extension from the URLs in your sitemap, then pass `drop-html-extension: true` to the action in your workflow. Note that you should also ensure that any canonical links that you list within -the html files corresponds to your choice here. +the html files corresponds to your choice here. + +### `date-only` + +The `date-only` input controls whether XML sitemaps include the full date and time in lastmod, +or only the date. The default is `date-only: false`, which includes the full date and time +in the lastmod fields. If you only want the date in the lastmod, then use `date-only: true`. ## Outputs @@ -203,7 +209,7 @@ you can also use a specific version such as with: ```yml - name: Generate the sitemap - uses: cicirello/generate-sitemap@v1.8.5 + uses: cicirello/generate-sitemap@v1.9.0 with: base-url-path: https://THE.URL.TO.YOUR.PAGE/ ``` diff --git a/action.yml b/action.yml index 650aa3f5..3e5886c6 100644 --- a/action.yml +++ b/action.yml @@ -57,6 +57,10 @@ inputs: description: 'Enables dropping .html from urls in sitemap.' required: false default: false + date-only: + description: 'Pass true to include only the date without the time in XML sitemaps; and false to include full date and time.' + required: false + default: false outputs: sitemap-path: description: 'The path to the generated sitemap file.' @@ -75,3 +79,4 @@ runs: - ${{ inputs.sitemap-format }} - ${{ inputs.additional-extensions }} - ${{ inputs.drop-html-extension }} + - ${{ inputs.date-only }} diff --git a/generatesitemap.py b/generatesitemap.py index 9318dcf7..a971bf2c 100755 --- a/generatesitemap.py +++ b/generatesitemap.py @@ -247,8 +247,16 @@ def urlstring(f, baseUrl, dropExtension=False) : {0} {1} """ - -def xmlSitemapEntry(f, baseUrl, dateString, dropExtension=False) : + +def removeTime(dateString) : + """Removes the time from a date-time. + + Keyword arguments: + dateString - The date-time. + """ + return dateString[:10] + +def xmlSitemapEntry(f, baseUrl, dateString, dropExtension=False, dateOnly=False) : """Forms a string with an entry formatted for an xml sitemap including lastmod date. @@ -258,7 +266,10 @@ def xmlSitemapEntry(f, baseUrl, dateString, dropExtension=False) : dateString - lastmod date correctly formatted dropExtension - true to drop extensions of .html from the filename in urls """ - return xmlSitemapEntryTemplate.format(urlstring(f, baseUrl, dropExtension), dateString) + return xmlSitemapEntryTemplate.format( + urlstring(f, baseUrl, dropExtension), + removeTime(dateString) if dateOnly else dateString + ) def writeTextSitemap(files, baseUrl, dropExtension=False) : """Writes a plain text sitemap to the file sitemap.txt. @@ -273,7 +284,7 @@ def writeTextSitemap(files, baseUrl, dropExtension=False) : sitemap.write(urlstring(f, baseUrl, dropExtension)) sitemap.write("\n") -def writeXmlSitemap(files, baseUrl, dropExtension=False) : +def writeXmlSitemap(files, baseUrl, dropExtension=False, dateOnly=False) : """Writes an xml sitemap to the file sitemap.xml. Keyword Arguments: @@ -285,7 +296,7 @@ def writeXmlSitemap(files, baseUrl, dropExtension=False) : sitemap.write('\n') sitemap.write('\n') for f in files : - sitemap.write(xmlSitemapEntry(f, baseUrl, lastmod(f), dropExtension)) + sitemap.write(xmlSitemapEntry(f, baseUrl, lastmod(f), dropExtension, dateOnly)) sitemap.write("\n") sitemap.write('\n') @@ -310,7 +321,8 @@ def main( includePDF, sitemapFormat, additionalExt, - dropExtension + dropExtension, + dateOnly ) : """The main function of the generate-sitemap GitHub Action. @@ -340,7 +352,7 @@ def main( if pathToSitemap[-1] != "/" : pathToSitemap += "/" if sitemapFormat == "xml" : - writeXmlSitemap(files, baseUrl, dropExtension) + writeXmlSitemap(files, baseUrl, dropExtension, dateOnly) pathToSitemap += "sitemap.xml" else : writeTextSitemap(files, baseUrl, dropExtension) @@ -360,7 +372,8 @@ def main( includePDF = sys.argv[4].lower() == "true", sitemapFormat = sys.argv[5], additionalExt = set(sys.argv[6].lower().replace(",", " ").replace(".", " ").split()), - dropExtension = sys.argv[7].lower() == "true" + dropExtension = sys.argv[7].lower() == "true", + dateOnly = sys.argv[8].lower() == "true" ) diff --git a/tests/tests.py b/tests/tests.py index 43e96a15..70517937 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -570,6 +570,11 @@ def test_urlstring_drop_html(self) : self.assertEqual(expected[i%len(expected)], gs.urlstring(f, base1, True)) self.assertEqual(expected[i%len(expected)], gs.urlstring(f, base2, True)) + def test_removeTime(self) : + date = "2020-09-11T13:35:00-04:00" + expected = "2020-09-11" + self.assertEqual(expected, gs.removeTime(date)) + def test_xmlSitemapEntry(self) : base = "https://TESTING.FAKE.WEB.ADDRESS.TESTING/" f = "./a.html" @@ -581,6 +586,17 @@ def test_xmlSitemapEntry(self) : expected = "\nhttps://TESTING.FAKE.WEB.ADDRESS.TESTING/a\n2020-09-11T13:35:00-04:00\n" self.assertEqual(actual, expected) + def test_xmlSitemapEntryDateOnly(self) : + base = "https://TESTING.FAKE.WEB.ADDRESS.TESTING/" + f = "./a.html" + date = "2020-09-11T13:35:00-04:00" + actual = gs.xmlSitemapEntry(f, base, date, False, True) + expected = "\nhttps://TESTING.FAKE.WEB.ADDRESS.TESTING/a.html\n2020-09-11\n" + self.assertEqual(actual, expected) + actual = gs.xmlSitemapEntry(f, base, date, True, True) + expected = "\nhttps://TESTING.FAKE.WEB.ADDRESS.TESTING/a\n2020-09-11\n" + self.assertEqual(actual, expected) + def test_robotsTxtParser(self) : expected = [ [], ["/"],