Luke Ross

Colorado

4 releases git clone https://lukeross.name/projects/colorado.git/

Web-based git repository viewer.

Commit 0674bf9f1626c4b144c6c9a60d6d9863f4caf3b0

Hook in feed to template

Committed 23 Oct 2018 by Luke Ross

colorado/templates/base.xml

@@ -6,6 +6,7 @@
 <meta name="viewport" content="width=device-width" />
 <title meld:id="html-title">Git Viewer</title>
 <link href="/static/colorado.css" rel="stylesheet" meld:id="static-css" type="text/css" />
+<link rel="alternate" type="application/atom+xml" title="Feed" href="/feed" meld:id="html-feed" />
 </head>
 
 <body>


colorado/views.py

@@ -50,19 +50,25 @@ def run_on_meld(tpl, id, fn):
 		return fn(node)
 
 
-def configure_template(tpl, repo=None):
-	run_on_meld(
-		tpl,
-		"static-css",
-		lambda n: n.set(
-			"href",
-			current_app.config.get('COLORADO_CSS', None) or
-			url_for(".static", filename="colorado.css")
-		)
-	)
+def configure_template(tpl, repo=None, feed=None):
+	css = [
+		c for c in (
+			url_for(".static", filename="colorado.css"),
+			current_app.config.get('COLORADO_CSS', None)
+		) if c
+	]
+	for ele, fn in tpl.findmeld("static-css").repeat(css):
+		ele.set("href", fn)
+
 	run_on_meld(tpl, "static-js", lambda n: n.set("src", url_for(
 		".static", filename="colorado.js"
 	)))
+
+	if feed:
+		run_on_meld(tpl, "html-feed", lambda n: n.set("href", feed))
+	else:
+		run_on_meld(tpl, "html-feed", lambda n: n.deparent())
+
 	if repo:
 		run_on_meld(tpl, "html-title", lambda n: n.content(
 			repo.name
@@ -228,7 +234,8 @@ def look_up_thing(slug, type, id):
 @bp.route("/<slug>/", methods=["GET"])
 def repo_home(slug):
 	repo = get_repo(slug)
-	return top_level_view(slug, "branch", repo.get_master().name)
+	feed = url_for(".feed_view", slug=slug)
+	return top_level_view(slug, "branch", repo.get_master().name, feed)
 
 
 def get_parent_diff(git, commit, **kwargs):
@@ -347,7 +354,10 @@ def history_view(slug, type, id):
 	configure_template(tpl, repo)
 	all_commits = chain([commit], commit.iter_parents())
 	for ele, c in tpl.findmeld("rev").repeat(all_commits):
-		for inner, (thing, type) in ele.findmeld("rev-tag-repeat").repeat(tag_things.get(c.hexsha, [])):
+		repeats = ele.findmeld("rev-tag-repeat").repeat(
+			tag_things.get(c.hexsha, [])
+		)
+		for inner, (thing, type) in repeats:
 			inner.findmeld("rev-tag-link").content(thing.name)
 			inner.findmeld("rev-tag-link").set("href", url_for(
 				".top_level_view", slug=slug, type=type, id=thing.name
@@ -477,14 +487,14 @@ def tree_view(slug, type, id, path):
 
 
 @bp.route("/<slug>/<type>/<id>/tree", methods=["GET"])
-def top_level_view(slug, type, id):
+def top_level_view(slug, type, id, feed=None):
 	repo, point, commit = look_up_thing(slug, type, id)
 	show_download = type in ("branch", "release")
 	return tree_base_view(
-		slug, type, id, repo, commit, commit.tree, show_download)
+		slug, type, id, repo, commit, commit.tree, show_download, feed)
 
 
-def tree_base_view(slug, type, id, repo, commit, tree, show_download=False):
+def tree_base_view(slug, type, id, repo, commit, tree, show_download=False, feed=None):
 	by_file = {}
 	commit_count = 0
 	for current in chain([commit], commit.iter_parents()):
@@ -499,7 +509,7 @@ def tree_base_view(slug, type, id, repo, commit, tree, show_download=False):
 				by_file[diff.b_path] = current
 
 	tpl = parse_xml_for_template("repo-home")
-	configure_template(tpl, repo)
+	configure_template(tpl, repo, feed)
 	tpl.findmeld("repo-name").content(repo.name)
 	tpl.findmeld("repo-desc").content(repo.description)
 	tpl.findmeld("repo-commits")[0].content(str(commit_count))
@@ -672,4 +682,7 @@ def feed_view(slug):
 	from colorado.feed import make_feed
 	from lxml import etree
 	repo = get_repo(slug)
-	return etree.tostring(make_feed(repo), pretty_print=True)
+	return make_response(
+		etree.tostring(make_feed(repo), pretty_print=True),
+		{"Content-Type": "application/atom+xml"}
+	)