2bb8cae4 by rmrf

Patch issue #157

1 parent 926b5a7d
diff --git a/features/index.features b/features/index.features
new file mode 100644
index 0000000..09b2ee3
--- /dev/null
+++ b/features/index.features
@@ -0,0 +1,66 @@
+Feature: Grouping BibTeX Bibliographies
+ As a scholar who likes to blog
+ I want to group my bibliographies according to configurable parameters
+
+ @tags @grouping @global_index
+ Scenario: Group Order
+ Given I have a scholar configuration with:
+ | key | value |
+ | order | ascending |
+ | sort_by | year |
+ | group_by | year |
+ | group_order | descending |
+ | bibliography_template | bib-template |
+ And I have a "_bibliography" directory
+ And I have a file "_bibliography/references.bib":
+ """
+ @book{ruby1,
+ title = {Book 1},
+ year = {2017}
+ }
+ @article{ruby2,
+ title = {Article 1},
+ year = {2017}
+ }
+ @book{ruby3,
+ title = {Book 2},
+ year = {2016}
+ }
+ @techreport{ruby4,
+ title = {TechReport 2},
+ year = {2016}
+ }
+ @article{ruby5,
+ title = {Article 2},
+ year = {2015}
+ }
+ """
+ And I have a "_layouts" directory
+ And I have a file "_layouts/bib-template.html":
+ """
+ ---
+ ---
+ {{ reference }}
+ Index: {{ index }}, GroupIndex: {{ group_index }}
+ """
+ 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 "Book 1" in "_site/scholar.html"
+ Then I should see "GroupIndex: 2" in "_site/scholar.html"
+ Then I should see "Article 1" in "_site/scholar.html"
+ Then I should see "GroupIndex: 2" in "_site/scholar.html"
+ Then I should see "Book 2" in "_site/scholar.html"
+ Then I should see "GroupIndex: 1" in "_site/scholar.html"
+ Then I should see "TechReport 2" in "_site/scholar.html"
+ Then I should see "GroupIndex: 1" in "_site/scholar.html"
+ Then I should see "Article 2" in "_site/scholar.html"
+ Then I should see "GroupIndex: 1" in "_site/scholar.html"
+
+
diff --git a/lib/jekyll/scholar/tags/bibliography.rb b/lib/jekyll/scholar/tags/bibliography.rb
index 542f785..0c5c3e2 100644
--- a/lib/jekyll/scholar/tags/bibliography.rb
+++ b/lib/jekyll/scholar/tags/bibliography.rb
@@ -12,6 +12,7 @@ module Jekyll
optparse(arguments)
end
+
def render(context)
set_context_to context
@@ -42,6 +43,9 @@ module Jekyll
cited_keys.clear
end
+ # See #157
+ initialize_global_index(items)
+
if group?
groups = group(items)
bibliography = render_groups(groups)
@@ -55,6 +59,7 @@ module Jekyll
def render_groups(groups)
def group_renderer(groupsOrItems,keys,order,tags)
+
if keys.count == 0
renderer(force = true)
render_items(groupsOrItems)
@@ -81,8 +86,10 @@ module Jekyll
end
def render_items(items)
+
bibliography = items.each_with_index.map { |entry, index|
- reference = bibliography_tag(entry, index + 1)
+ reference = bibliography_tag(entry, index + 1, global_entry_index(entry) )
+ decrement_global_index(entry)
if generate_details?
reference << link_to(details_link_for(entry),
diff --git a/lib/jekyll/scholar/utilities.rb b/lib/jekyll/scholar/utilities.rb
index 350f783..290570d 100644
--- a/lib/jekyll/scholar/utilities.rb
+++ b/lib/jekyll/scholar/utilities.rb
@@ -454,7 +454,27 @@ module Jekyll
@liquid_template = Liquid::Template.parse(tmp)
end
- def bibliography_tag(entry, index)
+ def initialize_global_index(items)
+ @type_counts = Hash[type_names.keys.map { |k| [k,0] }]
+
+ @type_counts.keys.each { |type|
+ items.each { |entry|
+ if (entry.type.to_s == type.to_s)
+ @type_counts[type] = @type_counts[type] + 1
+ end
+ }
+ }
+ end
+
+ def global_entry_index(entry)
+ @type_counts[entry.type.to_s]
+ end
+
+ def decrement_global_index(entry)
+ @type_counts[entry.type.to_s] = @type_counts[entry.type.to_s].to_i - 1
+ end
+
+ def bibliography_tag(entry, index, group_index)
return missing_reference unless entry
tmp = liquid_template.render(
@@ -462,7 +482,8 @@ module Jekyll
.merge(site.site_payload)
.merge({
'index' => index,
- 'details' => details_link_for(entry)
+ 'details' => details_link_for(entry),
+ 'group_index' => group_index
}),
{
:registers => { :site => site },