Commit ce54dcfe authored by Hendrik van Antwerpen's avatar Hendrik van Antwerpen

Add tests for grouping and sorting.

parent 5c004b21
......@@ -49,6 +49,9 @@ default configuration is as follows:
sort_by: none
order: ascending
group_by: none
group_order: ascending
source: ./_bibliography
bibliography: references.bib
bibliography_template: "{{reference}}"
......@@ -170,7 +173,8 @@ use the `--max` option:
{% bibliography --max 5 %}
This would generate a bibliography containing only the first 5 entries
of your bibliography (after query filters and sort options have been applied).
of your bibliography (after query filters and sort options have been
applied). Limiting entries is disabled if grouping is active.
### Bibliography Template
......
Feature: Grouping BibTeX Bibliographies
As a scholar who likes to blog
I want to group my bibliographies according to configurable parameters
@tags @grouping
Scenario: Group By Year
Given I have a scholar configuration with:
| key | value |
| group_by | year |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{ruby1,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
}
@book{ruby2,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2007},
publisher = {O'Reilly Media}
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography -f references %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
Then I should see "<h2 class=\"bibliography\">2007</h2>" in "_site/scholar.html"
And I should see "<h2 class=\"bibliography\">2008</h2>" in "_site/scholar.html"
@tags @grouping
Scenario: Group Order
Given I have a scholar configuration with:
| key | value |
| group_by | year |
| group_order | ascending |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{ruby1,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
}
@book{ruby2,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2007},
publisher = {O'Reilly Media}
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography -f references %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
Then "<h2 class=\"bibliography\">2007</h2>" should come before "<h2 class=\"bibliography\">2008</h2>" in "_site/scholar.html"
@tags @grouping
Scenario: Reverse Group Order
Given I have a scholar configuration with:
| key | value |
| group_by | year |
| group_order | descending |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{ruby1,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
}
@book{ruby2,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2007},
publisher = {O'Reilly Media}
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography -f references %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
Then "<h2 class=\"bibliography\">2008</h2>" should come before "<h2 class=\"bibliography\">2007</h2>" in "_site/scholar.html"
@tags @grouping
Scenario: Multi-level Group Order
Given I have a scholar configuration with:
| key | value |
| group_by | year,month |
| group_order | descending,ascending |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{ruby1,
title = {November 08},
year = {2008},
month = {nov}
}
@book{ruby2,
title = {March 08},
year = {2008},
month = {mar}
}
@book{ruby3,
title = {June 07},
year = {2007},
month = {jun}
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography -f references %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
Then "March" should come before "November" in "_site/scholar.html"
And "November" should come before "June" in "_site/scholar.html"
@tags @grouping
Scenario: Group by Type
Given I have a scholar configuration with:
| key | value |
| group_by | type |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{ruby1,
title = {Book 1},
}
@article{ruby2,
title = {Article 1},
}
@book{ruby3,
title = {Book 2},
}
@article{ruby4,
title = {Article 2},
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography -f references %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
Then "Journal Articles" should come before "Article 1" in "_site/scholar.html"
And "Journal Articles" should come before "Article 2" in "_site/scholar.html"
Then "Books" should come before "Book 1" in "_site/scholar.html"
And "Books" should come before "Book 2" in "_site/scholar.html"
@tags @grouping
Scenario: Type Order
Given I have a scholar configuration with:
| key | value |
| group_by | type |
| type_order | [article,book] |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{ruby1,
title = {Book 1},
}
@article{ruby2,
title = {Article 1},
}
@book{ruby3,
title = {Book 2},
}
@techreport{ruby4,
title = {Book 2},
}
@article{ruby5,
title = {Article 2},
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography -f references %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
Then "Journal Articles" should come before "Books" in "_site/scholar.html"
And "Books" should come before "Technical Reports" in "_site/scholar.html"
@tags @grouping
Scenario: Type Names
Given I have a scholar configuration with:
| key | value |
| group_by | type |
| type_names | { article: 'Long Papers' } |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@article{ruby1,
title = {Article},
}
@book{ruby2,
title = {Book},
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography -f references %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
Then I should see "Long Papers" in "_site/scholar.html"
And I should not see "Journal Articles" in "_site/scholar.html"
And I should see "Books" in "_site/scholar.html"
@tags @grouping
Scenario: Month Names
Given I have a scholar configuration with:
| key | value |
| group_by | month |
| month_names | [Januar,Februar,März,April,Mai,Juni,Juli,August,September,Oktober,November,Dezember] |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{ruby1,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
month = jan
}
@book{ruby2,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
month = dec
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography -f references %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
Then I should see "Januar" in "_site/scholar.html"
And I should not see "January" in "_site/scholar.html"
And I should see "Dezember" in "_site/scholar.html"
And I should not see "December" in "_site/scholar.html"
\ No newline at end of file
......@@ -262,3 +262,40 @@ Feature: Sorting BibTeX Bibliographies
And the "_site/scholar.html" file should exist
Then "August 07" should come before "March 08" in "_site/scholar.html"
And "March 08" should come before "December 08" in "_site/scholar.html"
@tags @sorting
Scenario: Multi-level Sort Order
Given I have a scholar configuration with:
| key | value |
| sort_by | year, month |
| order | descending, ascending |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{ruby1,
title = {August 07},
year = {2007},
month = aug
}
@book{ruby2,
title = {March 08},
year = {2008},
month = mar
}
@book{ruby3,
title = {December 08},
year = {2008},
month = dec
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
Then "March 08" should come before "December 08" in "_site/scholar.html"
And "December 08" should come before "August 07" in "_site/scholar.html"
......@@ -55,6 +55,7 @@ module Jekyll
},
'type_order' => [],
'month_names' => nil,
}.freeze
class << self
......
module Jekyll
class Scholar
require 'date'
# Load styles into static memory.
# They should be thread safe as long as they are
......@@ -201,12 +202,10 @@ module Jekyll
group_value(keys.first,item)
end
.sort do |e1, e2|
v1 = group_sort_value(keys.first,e1[0])
v2 = group_sort_value(keys.first,e2[0])
if order.first =~ /^(desc|reverse)/i
v2 <=> v1
group_compare(keys.first,e2[0],e1[0])
else
v1 <=> v2
group_compare(keys.first,e1[0],e2[0])
end
end
.to_h
......@@ -238,12 +237,22 @@ module Jekyll
.flatten
end
def group_sort_value(key,value)
def group_compare(key,v1,v2)
case key
when 'type'
config['type_order'].find_index(value) || 99
o1 = type_order.find_index(v1)
o2 = type_order.find_index(v2)
if o1.nil? && o2.nil?
0
elsif o1.nil?
1
elsif o2.nil?
-1
else
o1 <=> o2
end
else
value
v1 <=> v2
end
end
......@@ -274,43 +283,26 @@ module Jekyll
def group_name(key,value)
case key
when 'type'
config['type_names'][value] || value.to_s
type_names[value] || value.to_s
when 'month_numeric'
month_name(value)
month_names[value] || "(unknown)"
else
value.to_s
end
end
def month_name(month)
case month
when 1
'January'
when 2
'February'
when 3
'March'
when 4
'April'
when 5
'May'
when 6
'June'
when 7
'July'
when 8
'August'
when 9
'September'
when 10
'October'
when 11
'November'
when 12
'December'
else
'Unknown'
end
def type_order
@type_order ||= config['type_order']
end
def type_names
@type_names ||= Scholar.defaults['type_names'].merge(config['type_names'])
end
def month_names
return @month_names unless @month_names.nil?
@month_names = config['month_names'].nil? ? Date::MONTHNAMES : config['month_names'].unshift(nil)
end
def suppress_author?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment