Repository: carpentries/amy

Issue:
Check that workshop URL is to repo not to site
We should at least warn people when they enter a something.github.io/yyyy-mm-dd-site URL instead of the github.com/something/yyyy-mm-dd-site URL that we want.



Discussion:
On Sat, May 30, 2015 at 12:22:40PM -0700, Greg Wilson wrote:

> We should at least warn people when they enter a
> something.github.io/yyyy-mm-dd-site URL instead of the
> github.com/something/yyyy-mm-dd-site URL that we want.

Hmm.  Why do we care about this distinction?  For building lists of
workshops, links to the rendered website (something.github.io/…) seem
to make more sense.  Are we cloning or performing other Git-based
activity on the event repositories?  I don't think we are, in which
case I don't mind if the event website is versioned in Mercurial (for
example) or not versioned at all.  If we're relying on the presense of
specific files (e.g. the YAML header of index.html), we should be able
to scrape what we need from the meta tags:

  $ curl http://swcarpentry.github.io/2015-05-30-hastac-swc/ | grep 'meta name'
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="venue" content="HASTAC Conference (Software Carpentry)" />
    <meta name="date" content="May 30-31, 2015" />
    <meta name="startdate" content="2015-05-30" />
    <meta name="enddate" content="2015-05-31" />

Not all headers are returned as `<meta>` tags. I think Greg's right about this one.

On Fri, Jun 05, 2015 at 09:05:48AM -0700, Piotr Banaszkiewicz wrote:

> Not all headers are returned as `<meta>` tags.

So just add (and document) the ones we need [1](https://github.com/swcarpentry/workshop-template/blob/6c3511a1638f98ae9d7860cadd0845675665286c/_layouts/workshop.html#L12)?

Alternatively, we could have folks enter the URL to a YAML file with
the required metadata in the first YAML document, so folks using
workshop-template could point us at their [2](https://github.com/swcarpentry/workshop-template/blob/gh-pages/index.html), and folks using other
websites (or no website at all) could throw up some YAML and point us
there.

I would actually far prefer having a link to the website, not the repo. The repo is basically useless to me as an admin, and the website is fantastically useful to confirm dates, location, instructors, registration etc. 

If we don't store the website URL, at least can we generate a link to the website on the "event" display page?

> If we don't store the website URL, at least can we generate a link to the website on the "event" display page?

I'll add it tomorrow.

Ooops, forgot to add this to the #373… I'll create a new PR for this.

@amyrbrown would you rather have that link to the website displayed on All Events page, or on event details page?

Right now I have something like this:
![screenshot from 2015-06-19 14 13 40](https://cloud.githubusercontent.com/assets/72821/8253172/7091fc94-168d-11e5-8806-9a395e096f8c.png)

so you have both links on the event details page.


Resolving patch:
diff --git a/workshops/templatetags/links.py b/workshops/templatetags/links.py
new file mode 100644
--- /dev/null
+++ b/workshops/templatetags/links.py
@@ -0,0 +1,49 @@
+import re
+
+from django import template
+
+register = template.Library()
+
+REPO_REGEXP = re.compile(r'https://github\.com/(?P<name>\w+)/(?P<repo>\S+)/?')
+REPO_SCHEMA = 'https://github.com/{name}/{repo}/'
+WEBSITE_REGEXP = re.compile(
+    r'http://(?P<name>\w+)\.github\.(io|com)/(?P<repo>\S+)/?'
+)
+WEBSITE_SCHEMA = 'http://{name}.github.io/{repo}/'
+
+
+@register.assignment_tag(takes_context=True)
+def format_link_to_repository(context, link):
+    """
+    Use: {% format_link_to_repository event.url as repo_link %}
+    Then: {{ repo_link|urlize|default_if_none:"—" }}
+    """
+    print(context['request'])
+    if not link:
+        return link
+    elif REPO_REGEXP.match(link):
+        return link
+    else:
+        try:
+            repository = WEBSITE_REGEXP.findall(link)[0]
+            return REPO_SCHEMA.format(name=repository[0], repo=repository[-1])
+        except IndexError:
+            return link
+
+
+@register.assignment_tag
+def format_link_to_website(link):
+    """
+    Use: {% format_link_to_website event.url as website_link %}
+    Then: {{ website_link|urlize|default_if_none:"—" }}
+    """
+    if not link:
+        return link
+    elif WEBSITE_REGEXP.match(link):
+        return link
+    else:
+        try:
+            site = REPO_REGEXP.findall(link)[0]
+            return WEBSITE_SCHEMA.format(name=site[0], repo=site[-1])
+        except IndexError:
+            return link


Test patch: