diff options
author | neodarz <neodarz@neodarz.net> | 2019-05-26 00:15:15 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-05-26 00:15:15 +0200 |
commit | 54dc5304b69a91ea7a48d311b394fba99a185b77 (patch) | |
tree | 68d6c2f2f194dc6842334fb4c88c6558ccfe8074 /pyblog | |
parent | ae28b77e0569fc847df48f7461660df52fe925cf (diff) | |
download | my_new_personal_website-54dc5304b69a91ea7a48d311b394fba99a185b77.tar.xz my_new_personal_website-54dc5304b69a91ea7a48d311b394fba99a185b77.zip |
Move make_sitemap_url_element function to external file
Diffstat (limited to '')
-rwxr-xr-x | pyblog | 43 |
1 files changed, 3 insertions, 40 deletions
@@ -436,48 +436,11 @@ def generate_index(feed): os.remove(tmppath) -def make_sitemap_url_element(link, updated=None, changefreq=None, priority=None): - """Make a sitemap <url> element. - - Parameters - ---------- - link : str or xml.etree.ElementTree.Element - If using an xml.etree.ElementTree.Element element, then it shall - be an atom:link element, e.g., <link href="http://zmwangx.github.io/"/>. - updated : datetime or xml.etree.ElementTree.Element, optional - If using an xml.etree.ElementTree.Element element, then it shall - be an atom:updated element, e.g., - <updated>2015-05-05T22:38:42-07:00</updated>. - changefreq : {"always", "hourly", "daily", "weekly", "monthly", "yearly", "never"}, optional - priority : {1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1}, optional - - """ - - urlelem = ET.Element("url") - loc = ET.Element("loc") - loc.text = link.attrib["href"] if isinstance(link, ET._Element) else link - urlelem.append(loc) - if updated is not None: - lastmod = ET.Element("lastmod") - lastmod.text = (updated.text if isinstance(updated, ET._Element) - else updated.isoformat()) - urlelem.append(lastmod) - if changefreq is not None: - changefreq_elem = ET.Element("changefreq") - changefreq_elem.text = changefreq - urlelem.append(changefreq_elem) - if priority is not None: - priority_elem = ET.Element("priority") - priority_elem.text = "%.1f" % priority - urlelem.append(priority_elem) - return urlelem - - def generate_sitemap(feed): """Generate sitemap.xml.""" sitemap = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9") # index - sitemap.append(make_sitemap_url_element(BLOG_HOME, feed.updated, "daily", 1.0)) + sitemap.append(utils.make_sitemap_url_element(BLOG_HOME, feed.updated, "daily", 1.0)) # other top level pages for name in os.listdir(BUILDDIR): if (not name.endswith(".html") or name == "index.html" or @@ -493,11 +456,11 @@ def generate_sitemap(feed): updated_tag = soup.footer.find(attrs={"class": "updated"}) if updated_tag is not None: updated = dateutil.parser.parse(updated_tag.text) - sitemap.append(make_sitemap_url_element(link, updated, "monthly", 0.9)) + sitemap.append(utils.make_sitemap_url_element(link, updated, "monthly", 0.9)) # blog entries for entry in feed.entries: - sitemap.append(make_sitemap_url_element(entry.link, entry.updated, "monthly", 0.9)) + sitemap.append(utils.make_sitemap_url_element(entry.link, entry.updated, "monthly", 0.9)) sitemappath = os.path.join(BUILDDIR, "sitemap.xml") with open(sitemappath, "w", encoding="utf-8") as sitemapfile: sitemapfile.write('<?xml version="1.0" encoding="UTF-8"?>\n%s\n' % |