Skip to content

Commit

Permalink
Merge dc4eeaa into c71e0bb
Browse files Browse the repository at this point in the history
  • Loading branch information
rseac committed Jan 5, 2018
2 parents c71e0bb + dc4eeaa commit a4cd0e0
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 27 deletions.
120 changes: 120 additions & 0 deletions features/186.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
Feature: BibTeX
As a scholar who likes to blog
I want to reference cool papers and books from my bibliography

@tags @bibliography
Scenario: Simple bibliography count
Given I have a scholar configuration with:
| key | value |
| source | ./_bibliography |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{smalltalk,
title = {Smalltalk Best Practice Patterns},
author = {Kent Beck},
year = {1996},
publisher = {Prentice Hall}
}
@book{ruby,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography_count -f references %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should not see "<i>The Ruby Programming Language</i>" in "_site/scholar.html"
And I should not see "<i>Smalltalk Best Practice Patterns</i>" in "_site/scholar.html"
And I should see "2" in "_site/scholar.html"

@tags @bibliography
Scenario: Simple bibliography count with query
Given I have a scholar configuration with:
| key | value |
| source | ./_bibliography |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{smalltalk,
title = {Smalltalk Best Practice Patterns},
author = {Kent Beck},
year = {1996},
publisher = {Prentice Hall}
}
@book{ruby,
title = {The Ruby Programming Language v1},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {1998},
publisher = {O'Reilly Media}
}
@book{ruby2,
title = {The Ruby Programming Language v2},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography_count -f references --query @book[year <= 2000] %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should not see "<i>The Ruby Programming Language v1</i>" in "_site/scholar.html"
And I should not see "<i>The Ruby Programming Language v2/i>" in "_site/scholar.html"
And I should not see "<i>Smalltalk Best Practice Patterns</i>" in "_site/scholar.html"
And I should see "2" in "_site/scholar.html"

@tags @bibliography
Scenario: Simple bibliography count with query with cited
Given I have a scholar configuration with:
| key | value |
| source | ./_bibliography |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{smalltalk,
title = {Smalltalk Best Practice Patterns},
author = {Kent Beck},
year = {1996},
publisher = {Prentice Hall}
}
@book{ruby,
title = {The Ruby Programming Language v1},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {1998},
publisher = {O'Reilly Media}
}
@book{ruby2,
title = {The Ruby Programming Language v2},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
}
"""
And I have a page "scholar.html":
"""
---
---
{% cite smalltalk %}
{% bibliography_count -f references --query @book[year <= 2000] --cited %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should not see "<i>The Ruby Programming Language v1</i>" in "_site/scholar.html"
And I should not see "<i>The Ruby Programming Language v2/i>" in "_site/scholar.html"
And I should not see "<i>Smalltalk Best Practice Patterns</i>" in "_site/scholar.html"
And I should see "1" in "_site/scholar.html"
1 change: 1 addition & 0 deletions lib/jekyll/scholar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

require 'jekyll/scholar/converters/bibtex'
require 'jekyll/scholar/tags/bibliography'
require 'jekyll/scholar/tags/bibliography_count'
require 'jekyll/scholar/tags/bibtex'
require 'jekyll/scholar/tags/cite'
require 'jekyll/scholar/tags/cite_details'
Expand Down
29 changes: 4 additions & 25 deletions lib/jekyll/scholar/tags/bibliography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,11 @@ def initialize(tag_name, arguments, tokens)
def render(context)
set_context_to context

# Add bibtex files to dependency tree
if context.registers[:page] and context.registers[:page].key? "path"
bibtex_paths.each do |bibtex_path|
site.regenerator.add_dependency(
site.in_source_dir(context.registers[:page]["path"]),
bibtex_path
)
end
end
# Add bibtex files to dependency tree.
build_dependency_tree

items = entries

if cited_only?
items = if skip_sort?
cited_references.uniq.map do |key|
items.detect { |e| e.key == key }
end
else
entries.select do |e|
cited_references.include? e.key
end
end

# See #90
cited_keys.clear
end
# Select cited items.
items = adjust_cited_items

if group?
groups = group(items)
Expand Down
33 changes: 33 additions & 0 deletions lib/jekyll/scholar/tags/bibliography_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Jekyll
class Scholar

class BibliographyCountTag < Liquid::Tag
include Scholar::Utilities

def initialize(tag_name, arguments, tokens)
super

@config = Scholar.defaults.dup

optparse(arguments)
end

def render(context)
set_context_to context

# Add bibtex files to dependency tree.
build_dependency_tree

# Select cited items.
items = adjust_cited_items

# Return number of items.
items.size
end

end

end
end

Liquid::Template.register_tag('bibliography_count', Jekyll::Scholar::BibliographyCountTag)
42 changes: 40 additions & 2 deletions lib/jekyll/scholar/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def optparse(arguments)
return if arguments.nil? || arguments.empty?

parser = OptionParser.new do |opts|
opts.on('-c', '--cited') do |cited|
opts.on('-c', '--cited') do |cited|
@cited = true
end

Expand Down Expand Up @@ -100,11 +100,15 @@ def optparse(arguments)
end
end

argv = arguments.split(/(\B-[cCfhqptTsgGOlLomA]|\B--(?:cited(_in_order)?|bibliography_list_tag|file|query|prefix|text|style|group_(?:by|order)|type_order|template|locator|label|offset|max|suppress_author|))/)
argv = arguments.split(/(\B-[bcCfhqptTsgGOlLomA]|\B--(?:cited(_in_order)?|bib_count|bibliography_list_tag|file|query|prefix|text|style|group_(?:by|order)|type_order|template|locator|label|offset|max|suppress_author|))/)

parser.parse argv.map(&:strip).reject(&:empty?)
end

def bib_count
@bib_count
end

def bibliography_list_tag
if @bibliography_list_tag.nil? then
return config['bibliography_list_tag']
Expand Down Expand Up @@ -724,6 +728,40 @@ def load_style(uri)
def styles(style)
STYLES[style] ||= load_style(style)
end

def build_dependency_tree
# Add bibtex files to dependency tree
if context.registers[:page] and context.registers[:page].key? "path"
bibtex_paths.each do |bibtex_path|
site.regenerator.add_dependency(
site.in_source_dir(context.registers[:page]["path"]),
bibtex_path
)
end
end


end

def adjust_cited_items
items = entries
if cited_only?
items = if skip_sort?
cited_references.uniq.map do |key|
items.detect { |e| e.key == key }
end
else
entries.select do |e|
cited_references.include? e.key
end
end

# See #90
cited_keys.clear
end

items
end
end

end
Expand Down

0 comments on commit a4cd0e0

Please sign in to comment.