Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev --> master #2373

Merged
merged 12 commits into from
Mar 30, 2017
3 changes: 0 additions & 3 deletions .bundle/config

This file was deleted.

3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ source 'https://rubygems.org'

ruby '2.3.1'

gem 'accesslint-ci', '0.2.6'
gem 'accesslint-ci', '0.2.8'
gem 'html-proofer'
gem 'jekyll', '~> 3.3.0'
gem 'jemoji'
gem 'parallel'

group :jekyll_plugins do
gem 'jekyll-archives', git: 'git://github.com/jekyll/jekyll-archives.git', ref: '112c508'
Expand Down
5 changes: 3 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GIT
GEM
remote: https://rubygems.org/
specs:
accesslint-ci (0.2.6)
accesslint-ci (0.2.8)
rest-client (~> 2.0)
thor (~> 0.19)
activesupport (4.2.8)
Expand Down Expand Up @@ -190,7 +190,7 @@ PLATFORMS
ruby

DEPENDENCIES
accesslint-ci (= 0.2.6)
accesslint-ci (= 0.2.8)
capybara
chromedriver-helper
codeclimate-test-reporter (~> 1.0.0)
Expand All @@ -208,6 +208,7 @@ DEPENDENCIES
jekyll_pages_api
jekyll_pages_api_search
jemoji
parallel
pry
rack-jekyll
rb-readline
Expand Down
5 changes: 1 addition & 4 deletions _config-accesslint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ exclude:
- README.md
- robots.txt
- search-index*
- serve
- serve-blog
- serve-fast
- serve-accesslint
- spec/
- system-security-plan.yml
- tags/
Expand Down Expand Up @@ -372,5 +368,6 @@ exclude:
- _posts/2017-01-06-open-source-collaboration-across-agencies-to-improve-https-deployment.md
- _posts/2017-01-10-mark-hopson-negotiating-on-americas-behalf.md
- _posts/2017-01-11-the-best-way-to-build-big-is-to-start-small.md
- _posts/2017-01-17-life-changing-magic-writing-release-notes.md
---

5 changes: 1 addition & 4 deletions _config-blog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ exclude:
- README.md
- robots.txt
- search-index*
- serve
- serve-blog
- serve-fast
- serve-accesslint
- spec/
- system-security-plan.yml
- tests/
Expand Down Expand Up @@ -369,5 +365,6 @@ exclude:
- _posts/2017-01-06-open-source-collaboration-across-agencies-to-improve-https-deployment.md
- _posts/2017-01-10-mark-hopson-negotiating-on-americas-behalf.md
- _posts/2017-01-11-the-best-way-to-build-big-is-to-start-small.md
- _posts/2017-01-17-life-changing-magic-writing-release-notes.md
---

4 changes: 0 additions & 4 deletions _config-fast-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ exclude:
- README.md
- robots.txt
- search-index*
- serve
- serve-blog
- serve-fast
- serve-accesslint
- sitemap.xml
- spec/
- system-security-plan.yml
Expand Down
7 changes: 3 additions & 4 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ feature_image: /assets/img/feature-background.jpg
tag_dir: tags
github_repo: https://github.com/18F/18f.gsa.gov/blob/master

related_posts: 3


analytics:
google:
code: 'UA-48605964-1' # Change this to your GSA analytics code
Expand Down Expand Up @@ -142,10 +145,6 @@ exclude:
- pages/our-work.html
- PULL_REQUEST_TEMPLATE.md
- README.md
- serve
- serve-blog
- serve-fast
- serve-accesslint
- spec/
- system-security-plan.yml
- tests/
Expand Down
3 changes: 2 additions & 1 deletion _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@

<!-- CSS
=================================================== -->
<link rel="stylesheet" href="{{ "/assets/css/main.css" | prepend: site.baseurl }}">
<link rel="stylesheet" href="{{ "/assets/css/main.css" | prepend: site.baseurl }}" media="all" />
<link rel="stylesheet" href="{{ "/assets/css/print.css" | prepend: site.baseurl }}" media="print" />

77 changes: 34 additions & 43 deletions _includes/related-posts.html
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
<section class="usa-grid usa-section posts_feature">
{% assign hasSimilar = '' %}
{% for post in site.related_posts %}
{% assign postHasSimilar = false %}
{% for tag in post.tags %}
{% for thisTag in page.tags %}
{% if postHasSimilar == false and hasSimilar.size < 3 and post != page and tag == thisTag %}
{% if hasSimilar.size == 0 %}
<h2>Related Posts</h4>
<ul>
{% endif %}
<li class="usa-width-one-third">
<article>
<p class="post-date">{{ post.date | date: "%B %-d, %Y" }}</p>
<h3 class="posts_feature-heading">
<a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}
{% if post.series %}
(Series: {{ post.series }})
{% endif %}
</a>
</h3>
<p>{{ post.excerpt }}</p>
<p>
<a class="link-arrow-right post-link-continue_reading" href="{{ post.url | prepend: site.baseurl }}">
Continue reading
<span class="usa-sr-only">about {{ post.title }}</span>
{% include svg/icons/arrow-right.svg %}
</a>
</p>
<span class="post-tags" itemprop="keywords">
{% for tag in post.tags %}
<a class="usa-label" href="{{ site.baseurl }}/{{ site.tag_dir }}/{{ tag | slugify }}/">{{ tag }}
</a>
{% endfor %}
</span>
</article>
</li>
{% capture hasSimilar %}{{ hasSimilar }}*{% endcapture %}
{% assign postHasSimilar = true %}
{% if site.related_posts.size > 0 %}
<h2>Related posts</h2>
{% endif %}
<ul>
{% for post in page.related_posts %}
<li class="usa-width-one-third">
<article>
<p class="post-date">{{ post.date | date: "%B %-d, %Y" }}</p>
<h3 class="posts_feature-heading">
<a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}
{% if post.series %}
(Series: {{ post.series }})
{% endif %}
{% endfor %}
{% endfor %}
</a>
</h3>
<p>{{ post.excerpt }}</p>
<p>
<a class="link-arrow-right post-link-continue_reading" href="{{ post.url | prepend: site.baseurl }}">
Continue reading
<span class="usa-sr-only">about {{ post.title }}</span>
{% include svg/icons/arrow-right.svg %}
</a>
</p>
<span class="post-tags" itemprop="keywords">
{% for tag in post.tags %}
<a class="usa-label" href="{{ site.baseurl }}/{{ site.tag_dir }}/{{ tag | slugify }}/">{{ tag }}
</a>
{% endfor %}
</span>
</article>
</li>
{% if forloop.index > 2 %}
{% break %}
{% endif %}
{% endfor %}
{% if hasSimilar.size > 0 %}
</ul>
{% endif %}
</ul>
</section>
2 changes: 1 addition & 1 deletion _includes/svg/icons/folderwithclock.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion _includes/svg/icons/gavel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion _includes/svg/icons/monitor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions _includes/svg/icons/open-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 83 additions & 54 deletions _plugins/related_posts.rb
Original file line number Diff line number Diff line change
@@ -1,69 +1,98 @@
require 'parallel'

module Jekyll
class RelGenerator < Liquid::Tag
def initialize(related_posts, text, tokens)
super
@params = text.split(' ')
@baseurl = Jekyll.sites[0].config['baseurl']
end
class RelatedPostsGenerator < Generator
safe :true
priority :lower

# Gets all posts from the site by tag and adds those that match the tags on
# the current page. Returns a flattened, deduped, array of post objects
def get_posts_by_tag(page, site_tags)
tags = page['tags']
other_posts = []
for tag in tags
other_posts.push(site_tags[tag])
other_posts = other_posts.flatten.uniq
other_posts.delete_if { |post| page['title'] == post['title'] }
end
other_posts
end
# Calculate related posts.
# Returns [<Post>]
def related_posts(me, posts)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for related_posts is too high. [43.91/15]
Cyclomatic complexity for related_posts is too high. [11/6]
Method has too many lines. [29/10]
Perceived complexity for related_posts is too high. [11/7]

return [] unless posts.docs.size > 1
highest_freq = @tag_freq.values.max
related_scores = Hash.new(0)

# Grabs all posts from the site and matches the authors on the current page
# to other posts authored on the site. This may take a long time for large
# sites. Returns an array of posts
def get_posts_by_author(other_posts, page, site)
if page['authors']
authors = page['authors']
authors.map do |author|
site['posts'].map do |post|
if post.data['authors'] && post.data['authors'].index(author)
then other_posts.push(post)
posts.docs.each do |post|
if @use_categories
post.data['categories'].each do |category|
if me.data['categories'].include?(category) && post != me
cat_freq = @tag_freq[category]
related_scores[post] += (1 + highest_freq - cat_freq)
end
end
end
if @use_tags
post.data['tags'].each do |tag|
if me.data['tags'].include?(tag) && post != me
cat_freq = @tag_freq[tag]
related_scores[post] += (1 + highest_freq - cat_freq)
end
end
end

next unless @use_authors
post.data['authors'].each do |author|
if me.data['authors'].include?(author) && post != me
cat_freq = @tag_freq[author]
related_scores[post] += (1 + highest_freq - cat_freq)
end
end
end
other_posts.delete_if { |post| page['title'] == post['title'] }
other_posts

sort_related_posts(related_scores)
end

# Creates a list of posts from an array of post objects.
def list_posts(other_posts)
external = @params[0] || 'ul'
internal = @params[1] || 'li'
if other_posts
related_posts = "<#{external}>"
other_posts.flatten.map do |post|
related_posts << "<#{internal}><a href='#{@baseurl}#{post.url}' \
class='related_posts'>&ldquo;#{post.data['title']}&rdquo;</a></#{internal}>"
end
related_posts << "</#{external}"
else
related_posts = '<p>No related posts</p>'
# Calculate the frequency of each tag.
# Returns {tag => freq, tag => freq, ...}
def tag_freq(posts)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for tag_freq is too high. [15.81/15]

@tag_freq = Hash.new(0)
posts.docs.each do |post|
post.data['categories'].each { |category| @tag_freq[category] += 1 } if @use_categories
post.data['tags'].each { |tag| @tag_freq[tag] += 1 } if @use_tags

post.data['authors'].each { |author| @tag_freq[author] += 1 } if @use_authors
end
end

# Grabs an array of posts by tag. If that array has fewer than 3 posts,
# grabs more posts by author. Returns a list of the first five posts in the
# array.
def render(context)
page = context['page']
site = context['site']
site_tags = site['tags']
other_posts = get_posts_by_tag(page, site_tags)
other_posts = get_posts_by_author(other_posts, page, site) if other_posts.flatten.length < 3
list_posts(other_posts.uniq.take(3))
# Sort the related posts in order of their score and date
# and return just the posts
def sort_related_posts(related_scores)
related_scores.sort do |a, b|
if a[1] < b[1]
1
elsif a[1] > b[1]
-1
else
b[0].date <=> a[0].date
end
end.collect { |post, _freq| post }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid multi-line chains of blocks.

end

def create_presets(site)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for create_presets is too high. [17.8/15]

@use_tags = true
@use_authors = true
@use_categories = false
@use_categories = true if site.config['related_categories']
@use_tags = false if !site.config['related_tags'].nil? && site.config['related_tags'] != true
@use_authors = false if !site.config['related_authors'].nil? && site.config['related_authors'] != true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [108/100]

end

def in_threads(site)
site.config['n_cores'] ? site.config['n_cores'] : 1
end

def generate(site)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for generate is too high. [19.21/15]

return unless site.config['related_posts']
n_posts = site.config['related_posts']

create_presets(site)
tag_freq(site.posts)

Parallel.map(site.posts.docs.flatten, in_threads: in_threads(site)) do |post|
rp = related_posts(post, site.posts)[0, n_posts]

post.data.merge!('related_posts' => rp) if rp.size.positive?
end
end
end
end
Liquid::Template.register_tag('related_posts', Jekyll::RelGenerator)
1 change: 0 additions & 1 deletion _sass/_components/card.scss
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ $card-image-height: 200px;
background-position: center;
background-repeat: no-repeat;
background-size: cover;

height: $card-image-height;
margin: 0;
padding: 0;
Expand Down
Loading