Skip to content

Commit

Permalink
DRYer nav include "functions"
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadianriver committed Mar 25, 2016
1 parent bd4c0e7 commit f75b5ae
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 70 deletions.
18 changes: 18 additions & 0 deletions _includes/fn_groupsort_reverse.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% comment %}
# Get a grouped and sorted array of posts
# include-params: unsorted, groupby, sortby
# returns: sorted_list array
# Grouped sort from:
# https://gist.github.com/budparr/0ad4a7449f9604f47eec
# Jekyll group_by structures have the form
# [{ 'name'=>group_by_field, 'items'=>[items_in_group] },...]
{% endcomment
%}{% assign sorted_list = site.empty_array
%}{% assign items_grouped = include.unsorted | group_by: include.groupby | sort: 'name' | reverse
%}{% for group in items_grouped
%}{% assign items = group.items | sort: include.sortby | reverse
%}{% for item in items
%}{% assign sorted_list = sorted_list | push: item
%}{% endfor
%}{% endfor
%}
42 changes: 42 additions & 0 deletions _includes/fn_sortednav.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% comment %}
# Find this post in sorted_list and get back/next
# depends: sorted_list of posts
# include-params: posclass - indicates whether top or bottom style
# returns: displays navigation using included backnextnav.html
# Original idea:
# https://gist.github.com/budparr/3e637e575471401d01ec
{% endcomment
%}{% for post in sorted_list
%}{% if post.title == page.title
%}{% unless forloop.first
%}{% assign prevbutton = '< Back'
%}{% assign prevurl = prev.url
%}{% assign prevtitle = prev.title
%}{% else
%}{% assign prevbutton = 'Index'
%}{% assign prevurl = perma | prepend: baseurl
%}{% assign prevtitle = 'Works Index'
%}{% endunless
%}{% unless forloop.last
%}{% assign next = sorted_list[forloop.index]
%}{% assign nextbutton = 'Next >'
%}{% assign nexturl = next.url
%}{% assign nexttitle = next.title
%}{% else
%}{% assign nextbutton = 'Index'
%}{% assign nexturl = perma | prepend: baseurl
%}{% assign nexttitle = 'Works Index'
%}{% endunless
%}{% endif
%}{% assign prev = post
%}{% endfor
%}
<div class="backnext {{ include.posclass }}">
{% include backnextnav.html %}
</div>
<script>
document.body.onkeyup = function(e){
if (e.keyCode == '37') { window.location = '{{ prevurl }}'; }
if (e.keyCode == '39') { window.location = '{{ nexturl }}'; }
};
</script>
82 changes: 25 additions & 57 deletions _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,29 @@
---
<section class="wrapper{% if page.options contains 'minihead' %} minihead{% endif %} style5">
<div class="inner{% if page.options contains 'fullwidth' %} fullwidth{% endif %}">{%
if page.categories contains 'works'
%}{% unless site.works_navlinks == 'none'
%}{% comment %}
Construct exact same sorted_list as in works.html.
{% endcomment
unless site.navlinks == 'none'
%}{% if page.categories contains 'works'
%}{% assign perma = '/works/'
%}{% include fn_groupsort_reverse.html unsorted=site.categories.works groupby='priority' sortby='date'
%}{% endif
%}{% comment %}
# <exclude> for now due to
# comparison of Jekyll::Drops::DocumentDrop with Jekyll::Drops::DocumentDrop failed
{% if page.categories.first == 'topics' and page.categories.last != 'topics'
%}{% assign perma = page.categories | join: '/' | prepend: '/' | append: '/'
%}{% assign sorted_list = site.empty_array
%}{% assign items_grouped = site.categories.works | group_by: 'priority' | sort: 'name' | reverse
%}{% for group in items_grouped
%}{% assign items = group.items | sort: 'date' | reverse
%}{% for item in items
%}{% assign sorted_list = sorted_list | push: item
%}{% endfor
%}{% endfor
%}{% comment %}
- from https://gist.github.com/budparr/3e637e575471401d01ec
Find this post in sorted_list and get back/next
{% endcomment
%}{% for work in sorted_list
%}{% if work.title == page.title
%}{% unless forloop.first
%}{% assign prevbutton = '&lt; Back'
%}{% assign prevurl = prev.url
%}{% assign prevtitle = prev.title
%}{% else
%}{% assign prevbutton = 'Index'
%}{% assign prevurl = '/works/' | prepend: baseurl
%}{% assign prevtitle = 'Works Index'
%}{% endunless
%}{% unless forloop.last
%}{% assign next = sorted_list[forloop.index]
%}{% assign nextbutton = 'Next &gt;'
%}{% assign nexturl = next.url
%}{% assign nexttitle = next.title
%}{% else
%}{% assign nextbutton = 'Index'
%}{% assign nexturl = '/works/' | prepend: baseurl
%}{% assign nexttitle = 'Works Index'
%}{% endunless
%}{% for topic in site.categories.topics
%}{% if topic.categories.last == page.categories.last
%}{% assign sorted_list = sorted_list | push: topic
%}{% endif
%}{% assign prev = work
%}{% endfor
%}{% endunless
%}{% if site.works_navlinks == 'top' or site.works_navlinks == 'both' %}
<div class="backnext bntop">
{% include backnextnav.html %}
</div>{%
endif
%}{% assign sorted_list = sorted_list | sort
%}{% endif
%} # </endexclude>
{% endcomment
%}{% endunless
%}{% if sorted_list and (site.navlinks == 'top' or site.navlinks == 'both')
%}{% include fn_sortednav.html posclass='btntop'
%}{% endif
%}{% unless page.options contains 'minihead' %}
<span id="post-date">{{ page.date | date: "%-d %B %Y" }}</span><hr
Expand All @@ -65,19 +41,11 @@ <h4>Skills</h4>
{{ content }}{%


if page.categories contains 'works'
%}{% if site.works_navlinks == 'bottom' or site.works_navlinks == 'both'
%}
<div class="backnext bnbottom">
{% include backnextnav.html %}
</div>
<script>
document.body.onkeyup = function(e){
if (e.keyCode == '37') { window.location = '{{ prevurl }}'; }
if (e.keyCode == '39') { window.location = '{{ nexturl }}'; }
};
</script>{%
endif
if sorted_list and (site.navlinks == 'bottom' or site.navlinks == 'both')
%}{% unless perma
%}{% assign perma = '/topics/'
%}{% endunless
%}{% include fn_sortednav.html posclass='btnbottom'
%}{% endif
%}{% unless page.author == 'none'
%}{% include author-block.html %}
Expand Down
14 changes: 1 addition & 13 deletions _pages/works.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,7 @@
<div class="table-wrapper">
<table>
<tbody>{%
comment %}
- from https://gist.github.com/budparr/0ad4a7449f9604f47eec
Jekyll group_by structures have the form
[{ 'name'=>group_by_field, 'items'=>[items_in_group] },...]
{% endcomment
%}{% assign sorted_list = site.empty_array
%}{% assign items_grouped = site.categories.works | group_by: 'priority' | sort: 'name' | reverse
%}{% for group in items_grouped
%}{% assign items = group.items | sort: 'date' | reverse
%}{% for item in items
%}{% assign sorted_list = sorted_list | push: item
%}{% endfor
%}{% endfor
include fn_groupsort_reverse.html unsorted=site.categories.works groupby='priority' sortby='date'
%}{% for work in sorted_list %}
<tr>
<td><b><a href="{{ work.url | prepend: site.baseurl }}">{{ work.title }}</a></b></td>
Expand Down

0 comments on commit f75b5ae

Please sign in to comment.