Skip to content

Commit

Permalink
Replace page.root (LibraryCarpentry#310)
Browse files Browse the repository at this point in the history
* replace page.root with 'base' variable that knows path level

* deal with case of trailing / in URL

* use link to figure out path for logos

* simplify path building for favicons

* baseurl isn't used in our template so it can removed
* use 'base' variable for path root instead of unecessary http request to
  site.url

* replace page.root with 'base' variable that knows path level

* deal with case of trailing / in URL

* use link to figure out path for logos

* simplify path building for favicons

* baseurl isn't used in our template so it can removed
* use 'base' variable for path root instead of unecessary http request to
  site.url

* rename variable, consolidate comments

* rename variable in template files
  • Loading branch information
fmichonneau authored Aug 23, 2018
1 parent c9fe2d8 commit 39ed62b
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 41 deletions.
2 changes: 1 addition & 1 deletion _includes/all_keypoints.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h2>Key Points</h2>
{% unless episode.break %}
<tr>
<td class="col-md-3">
<a href="{{ page.root }}{{ episode.url }}">{{ episode.title }}</a>
<a href="{{ relative_root_path }}{{ episode.url }}">{{ episode.title }}</a>
</td>
<td class="col-md-9">
<ul>
Expand Down
27 changes: 27 additions & 0 deletions _includes/base_path.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% comment %}
This is adapated from: https://ricostacruz.com/til/relative-paths-in-jekyll

`page.url` gives the URL of the current page with a leading /:

- when the URL ends with the extension (e.g., /foo/bar.html) then we can get
the depth by counting the number of / and remove - 1
- when the URL ends with a / (e.g. /foo/bar/) then the number / gives the depth
directly
{% endcomment %}

{% assign relative_root_path = '' %}

{% assign last_char = page.url | slice: -1 %}

{% if last_char == "/"}
{% assign offset = 0 %}
{% else %}
{% assign offset = 1 %}
{% endif %}

{% assign depth = page.url | split: '/' | size | minus: offset %}
{% if depth <= 1 %}{% assign relative_root_path = '.' %}
{% elsif depth == 2 %}{% assign relative_root_path = '..' %}
{% elsif depth == 3 %}{% assign relative_root_path = '../..' %}
{% elsif depth == 4 %}{% assign relative_root_path = '../../..' %}
{% endif %}
8 changes: 4 additions & 4 deletions _includes/carpentries.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% endcomment %}
<div class="row">
<div class="col-md-2" align="center">
<a href="{{ site.carpentries_site }}"><img src="{{ page.root }}/assets/img/cp-logo-blue.svg" alt="The Carpentries logo" /></a>
<a href="{{ site.carpentries_site }}"><img src="{{ relative_root_path }}/assets/img/cp-logo-blue.svg" alt="The Carpentries logo" /></a>
</div>
<div class="col-md-8">
<p><a href="{{ site.carpentries_site }}">The Carpentries</a> comprises
Expand All @@ -23,7 +23,7 @@
</div>
<div class="row">
<div class="col-md-2" align="center">
<a href="{{ site.swc_site }}"><img src="{{ page.root }}/assets/img/swc-icon-blue.svg" alt="Software Carpentry logo" /></a>
<a href="{{ site.swc_site }}"><img src="{{ relative_root_path }}/assets/img/swc-icon-blue.svg" alt="Software Carpentry logo" /></a>
</div>
<div class="col-md-8">
<p>Since 1998, <a href="{{ site.swc_site }}">Software Carpentry</a> has
Expand All @@ -38,7 +38,7 @@
<br/>
<div class="row">
<div class="col-md-2" align="center">
<a href="{{ site.dc_site }}"><img src="{{ page.root }}/assets/img/dc-icon-black.svg" alt="Data Carpentry logo" /></a>
<a href="{{ site.dc_site }}"><img src="{{ relative_root_path }}/assets/img/dc-icon-black.svg" alt="Data Carpentry logo" /></a>
</div>
<div class="col-md-8">
<p><a href="{{ site.dc_site }}">Data Carpentry</a> develops and teaches
Expand All @@ -53,7 +53,7 @@
<br/>
<div class="row">
<div class="col-md-2" align="center">
<a href="{{ site.lc_site }}"><img src="{{ page.root }}/assets/img/lc-icon-black.png" alt="Library Carpentry logo" /></a>
<a href="{{ site.lc_site }}"><img src="{{ relative_root_path }}/assets/img/lc-icon-black.png" alt="Library Carpentry logo" /></a>
</div>
<div class="col-md-8">
<p><a href="{{ site.lc_site }}">Library Carpentry</a> is in discussions with
Expand Down
10 changes: 5 additions & 5 deletions _includes/episode_navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
<div class="col-xs-1">
<h3 class="text-left">
{% if page.previous.url %}
<a href="{{ page.root }}{{ page.previous.url }}"><span class="glyphicon glyphicon-menu-left" aria-hidden="true"></span><span class="sr-only">previous episode</span></a>
<a href="{{ relative_root_path }}{{ page.previous.url }}"><span class="glyphicon glyphicon-menu-left" aria-hidden="true"></span><span class="sr-only">previous episode</span></a>
{% else %}
<a href="{{ page.root }}/"><span class="glyphicon glyphicon-menu-up" aria-hidden="true"></span><span class="sr-only">lesson home</span></a>
<a href="{{ relative_root_path }}/"><span class="glyphicon glyphicon-menu-up" aria-hidden="true"></span><span class="sr-only">lesson home</span></a>
{% endif %}
</h3>
</div>
<div class="col-xs-10">
{% if include.episode_navbar_title %}
<h3 class="maintitle"><a href="{{ page.root }}/">{{ site.title }}</a></h3>
<h3 class="maintitle"><a href="{{ relative_root_path }}/">{{ site.title }}</a></h3>
{% endif %}
</div>
<div class="col-xs-1">
<h3 class="text-right">
{% if page.next.url %}
<a href="{{ page.root }}{{ page.next.url }}"><span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span><span class="sr-only">next episode</span></a>
<a href="{{ relative_root_path }}{{ page.next.url }}"><span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span><span class="sr-only">next episode</span></a>
{% else %}
<a href="{{ page.root }}/"><span class="glyphicon glyphicon-menu-up" aria-hidden="true"></span><span class="sr-only">lesson home</span></a>
<a href="{{ relative_root_path }}/"><span class="glyphicon glyphicon-menu-up" aria-hidden="true"></span><span class="sr-only">lesson home</span></a>
{% endif %}
</h3>
</div>
Expand Down
2 changes: 1 addition & 1 deletion _includes/favicons.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% assign favicon_url = site.baseurl | append: '/assets/favicons/' | append: site.carpentry | prepend: site.url %}
{% assign favicon_url = relative_root_path | append: '/assets/favicons/' | append: site.carpentry %}

{% if site.carpentry == 'swc' %}
{% assign carpentry = 'Software Carpentry' %}
Expand Down
6 changes: 3 additions & 3 deletions _includes/javascript.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{% comment %}
Javascript used in lesson and workshop pages.
{% endcomment %}
<script src="{{ page.root }}/assets/js/jquery.min.js"></script>
<script src="{{ page.root }}/assets/js/bootstrap.min.js"></script>
<script src="{{ page.root }}/assets/js/lesson.js"></script>
<script src="{{ relative_root_path }}/assets/js/jquery.min.js"></script>
<script src="{{ relative_root_path }}/assets/js/bootstrap.min.js"></script>
<script src="{{ relative_root_path }}/assets/js/lesson.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
Expand Down
2 changes: 1 addition & 1 deletion _includes/main_title.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% comment %}
Main title for lesson pages.
{% endcomment %}
<h1 class="maintitle"><a href="{{ page.root }}{% link index.md %}">{{ site.title }}</a>{% if page.title %}: {{ page.title }}{% endif %}</h1>
<h1 class="maintitle"><a href="{{ relative_root_path }}{% link index.md %}">{{ site.title }}</a>{% if page.title %}: {{ page.title }}{% endif %}</h1>
28 changes: 14 additions & 14 deletions _includes/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,64 @@
{% comment %} Select what logo to display. {% endcomment %}
{% if site.carpentry == "swc" %}
<a href="{{ site.swc_site }}" class="pull-left">
<img class="navbar-logo" src="{{ page.root }}/assets/img/swc-icon-blue.svg" alt="Software Carpentry logo" />
<img class="navbar-logo" src="{{ relative_root_path }}{% link /assets/img/swc-icon-blue.svg %}" alt="Software Carpentry logo" />
</a>
{% elsif site.carpentry == "dc" %}
<a href="{{ site.dc_site }}" class="pull-left">
<img class="navbar-logo" src="{{ page.root }}/assets/img/dc-icon-black.svg" alt="Data Carpentry logo" />
<img class="navbar-logo" src="{{ relative_root_path }}{% link /assets/img/dc-icon-black.svg %}" alt="Data Carpentry logo" />
</a>
{% elsif site.carpentry == "lc" %}
<a href="{{ site.lc_site }}" class="pull-left">
<img class="navbar-logo" src="{{ page.root }}/assets/img/lc-icon-black.svg" alt="Library Carpentry logo" />
<img class="navbar-logo" src="{{ relative_root_path }}{% link /assets/img/lc-icon-black.svg %}" alt="Library Carpentry logo" />
</a>
{% elsif site.carpentry == "cp" %}
<a href="{{ site.carpentries_site }}" class="pull-left">
<img class="navbar-logo" src="{{ page.root }}/assets/img/cp-logo-blue.svg" alt="The Carpentries logo" />
<img class="navbar-logo" src="{{ relative_root_path }}{% link /assets/img/cp-logo-blue.svg %}" alt="The Carpentries logo" />
</a>
{% endif %}

{% comment %} Always show link to home page. {% endcomment %}
<a class="navbar-brand" href="{{ page.root }}{% link index.md %}">Home</a>
<a class="navbar-brand" href="{{ relative_root_path }}{% link index.md %}">Home</a>

</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">

{% comment %} Always show code of conduct. {% endcomment %}
<li><a href="{{ page.root }}{% link CODE_OF_CONDUCT.md %}">Code of Conduct</a></li>
<li><a href="{{ relative_root_path }}{% link CODE_OF_CONDUCT.md %}">Code of Conduct</a></li>

{% if site.kind == "lesson" %}
{% comment %} Show setup instructions. {% endcomment %}
<li><a href="{{ page.root }}{% link setup.md %}">Setup</a></li>
<li><a href="{{ relative_root_path }}{% link setup.md %}">Setup</a></li>

{% comment %} Show lesson episodes for lessons. {% endcomment %}
<li class="dropdown">
<a href="{{ page.root }}/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Episodes <span class="caret"></span></a>
<a href="{{ relative_root_path }}/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Episodes <span class="caret"></span></a>
<ul class="dropdown-menu">
{% for episode in site.episodes %}
<li><a href="{{ page.root }}{{ episode.url }}">{{ episode.title }}</a></li>
<li><a href="{{ relative_root_path }}{{ episode.url }}">{{ episode.title }}</a></li>
{% endfor %}
<li role="separator" class="divider"></li>
<li><a href="{{ page.root }}{% link aio.md %}">All in one page (Beta)</a></li>
<li><a href="{{ relative_root_path }}{% link aio.md %}">All in one page (Beta)</a></li>
</ul>
</li>
{% endif %}

{% comment %} Show extras for lessons or if this is the main workshop-template repo (where they contain documentation). {% endcomment %}
{% if site.kind == "lesson" %}
<li class="dropdown">
<a href="{{ page.root }}/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Extras <span class="caret"></span></a>
<a href="{{ relative_root_path }}/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Extras <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="{{ page.root }}{% link reference.md %}">Reference</a></li>
<li><a href="{{ relative_root_path }}{% link reference.md %}">Reference</a></li>
{% for extra in site.extras %}
<li><a href="{{ page.root }}{{ extra.url }}">{{ extra.title }}</a></li>
<li><a href="{{ relative_root_path }}{{ extra.url }}">{{ extra.title }}</a></li>
{% endfor %}
</ul>
</li>
{% endif %}

{% comment %} Always show license. {% endcomment %}
<li><a href="{{ page.root }}{% link LICENSE.md %}">License</a></li>
<li><a href="{{ relative_root_path }}{% link LICENSE.md %}">License</a></li>
{% if page.source %}
{% if page.source == "Rmd" %}
<li><a href="{{site.github.repository_url}}/edit/gh-pages/{{page.path|replace: "_episodes", "_episodes_rmd" | replace: ".md", ".Rmd"}}">Improve this page <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a></li>
Expand Down
4 changes: 2 additions & 2 deletions _includes/syllabus.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h2 id="schedule">Schedule</h2>
<tr>
{% if multiday %}<td class="col-md-1"></td>{% endif %}
<td class="{% if multiday %}col-md-1{% else %}col-md-2{% endif %}"></td>
<td class="col-md-3"><a href="{{ page.root }}{% link setup.md %}">Setup</a></td>
<td class="col-md-3"><a href="{{ relative_root_path }}{% link setup.md %}">Setup</a></td>
<td class="col-md-7">Download files required for the lesson</td>
</tr>
{% for episode in site.episodes %}
Expand All @@ -42,7 +42,7 @@ <h2 id="schedule">Schedule</h2>
<td class="{% if multiday %}col-md-1{% else %}col-md-2{% endif %}">{% if hours < 10 %}0{% endif %}{{ hours }}:{% if minutes < 10 %}0{% endif %}{{ minutes }}</td>
<td class="col-md-3">
{% assign lesson_number = lesson_number | plus: 1 %}
{{ lesson_number }}. <a href="{{ page.root }}{{ episode.url }}">{{ episode.title }}</a>
{{ lesson_number }}. <a href="{{ relative_root_path }}{{ episode.url }}">{{ episode.title }}</a>
</td>
<td class="col-md-7">
{% if episode.break %}
Expand Down
11 changes: 6 additions & 5 deletions _layouts/base.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
---

{% comment %}
When rendering websites locally, `site.github.url` doesn't get resolved
properly unless GitHub PAT are set up and available in the environment.
Expand All @@ -16,6 +15,8 @@
{% assign search_domain_url = site.github.url %}
{% endif %}

{% include base_path.html %}

<!doctype html>
<html lang="en">
<head>
Expand All @@ -25,10 +26,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- meta "search-domain" used for google site search function google_search() -->
<meta name="search-domain" value="{{ search_domain_url }}">
<link rel="stylesheet" type="text/css" href="{{ page.root }}/assets/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="{{ page.root }}/assets/css/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="{{ page.root }}/assets/css/lesson.css" />
<link rel="stylesheet" type="text/css" href="{{ page.root }}/assets/css/syntax.css" />
<link rel="stylesheet" type="text/css" href="{{ relative_root_path }}/assets/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="{{ relative_root_path }}/assets/css/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="{{ relative_root_path }}/assets/css/lesson.css" />
<link rel="stylesheet" type="text/css" href="{{ relative_root_path }}/assets/css/syntax.css" />

{% include favicons.html %}

Expand Down
7 changes: 4 additions & 3 deletions _layouts/workshop.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="last-modified" content="{{ site.time }}">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="{{ page.root }}/assets/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="{{ page.root }}/assets/css/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="{{ page.root }}/assets/css/lesson.css" />
<link rel="stylesheet" type="text/css" href="{{ relative_root_path }}/assets/css/bootstrap.css" />

<link rel="stylesheet" type="text/css" href="{{ relative_root_path }}/assets/css/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="{{ relative_root_path }}/assets/css/lesson.css" />

{% include favicons.html %}

Expand Down
2 changes: 1 addition & 1 deletion bin/boilerplate/_extras/figures.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ title: Figures
}
}
}
episode_url = "{{ page.root }}" + lesson_episodes[i];
episode_url = "{{ base }}" + lesson_episodes[i];
xmlHttp[i].open("GET", episode_url);
xmlHttp[i].send(null);
}
Expand Down
2 changes: 1 addition & 1 deletion bin/boilerplate/aio.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
article_here.innerHTML = htmlDocArticle.innerHTML;
}
}
episode_url = "{{ page.root }}" + lesson_episodes[i];
episode_url = "{{ base }}" + lesson_episodes[i];
xmlHttp[i].open("GET", episode_url);
xmlHttp[i].send(null);
}
Expand Down

0 comments on commit 39ed62b

Please sign in to comment.