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

Issue 241 #248

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.bundle/
.jekyll-cache/
.sass-cache/
src/_data/code.json
public/
12 changes: 12 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ defaults:
path: "code.json"
values:
layout: null
- scope:
path: "projects"
values:
layout: project

theme: uswds-jekyll

Expand All @@ -36,3 +40,11 @@ scripts:
styles:
- /assets/css/main.css
- /assets/css/custom.css

page_gen:
- index_files: true
data: code.releases
name: name
template: project
dir: projects
page_gen-dirs: true
3 changes: 3 additions & 0 deletions src/_data/navigation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ secondary: &secondary_navigation
href: /why-open-source.html
- text: Getting Started
href: /getting-started.html
- text: Projects
href: /projects.html
- text: How to Open Source
href: /how-to-open-source.html
- text: FAQs
href: /frequently-asked-questions.html


sidebar: *secondary_navigation
28 changes: 28 additions & 0 deletions src/_layouts/project.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
layout: default
main:
class: usa-grid usa-section usa-content usa-layout-docs
---

<div class="usa-grid usa-section">
<div class="usa-layout-docs-main_content">
<header>
{%- if page.title -%}
<h1>{{ page.title }}</h1>

{%- if page.date.created -%}
<small class="usa-font-lead">Last updated <time datetime="{{ page.date.lastModified }}">{{ page.date.lastModified | date_to_string: 'ordinal', 'US' }}</time></small>
{%- endif -%}
{%- endif -%}
</header>

<p>
{{ page.description }}
</p>
<p>Status: {{ page.status }}</p>
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, thinking this might be a <ul>? They're not really paragraphs :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am open to all suggestions. If you are sure you want me to change it to

    I will.

<p>Organization: {{ page.organization }}</p>
<p>Source: <a href="{{ page.repositoryURL }}">{{ page.repositoryURL }}</a></p>
<p>License: <a href="{{ page.permissions.licenses[0].URL }}">{{ page.permissions.licenses[0].name }}</a></p>
<p>Contact: <a href="mailto://{{ page.contact.email }}">{% if page.contact.name %} {{ page.contact.name }} {% else %} email {% endif %}</a></p>
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be page.contact.email in the displayed portion of the link (when there is not contact name)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are situations in the code.json file where there is an email, but not a name. But I will enhance this with a check for email too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Enhanced it with a couple of checks as there is some pollution in the data

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I meant at the end... it says {% if page.contact.name %} {{ page.contact.name }} {% else %} email {% endif %} and my question was, at the end there, should that be page.contact.email? Like this:

{% if page.contact.name %} {{ page.contact.name }} {% else %} {{page.contact.email}} {% endif %}

Just a thought. I guess I'm open to either just the word "email" or the actual email...

</div>
</div>
40 changes: 40 additions & 0 deletions src/_plugins/code_json_generate_once.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
##
#
# This module will generate an code.json file inside the src/_data directory
# BEFORE the site is generated. The code.json can therefor be used inside
# page templating syntax by using the variables
# `data.code.<properties>`
#
##

module RunMeOnce
milovanderlinden marked this conversation as resolved.
Show resolved Hide resolved

def self.process(site, payload)
return if @processed
input_dir ||= File.expand_path('../_releases', __dir__)
file_paths ||= Dir.glob(File.join(input_dir, '**', '*.json')).sort
output = {
agency: 'DOD',
version: '2.0.0',
measurementType: {
method: 'projects'
},
releases: []
}

file_paths.each do |file_path|
output[:releases] << JSON.parse(File.read(file_path))
end

path = 'src/_data/code.json'
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') do |f|
f.write(JSON.pretty_generate(output))
end
@processed = true
end
end

Jekyll::Hooks.register :site, :after_init do |site, payload|
RunMeOnce.process(site, payload)
end
138 changes: 138 additions & 0 deletions src/_plugins/data_page_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# coding: utf-8
# Generate pages from individual records in yml files
# (c) 2014-2016 Adolfo Villafiorita
# Distributed under the conditions of the MIT License

module Jekyll

module Sanitizer
# strip characters and whitespace to create valid filenames, also lowercase
def sanitize_filename(name)
if(name.is_a? Integer)
return name.to_s
end
return name.tr(
"ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÑñÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
"AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz"
).downcase.strip.gsub(' ', '-').gsub(/[^\w.-]/, '')
end
end

# this class is used to tell Jekyll to generate a page
class DataPage < Jekyll::Page
milovanderlinden marked this conversation as resolved.
Show resolved Hide resolved
include Sanitizer

# - site and base are copied from other plugins: to be honest, I am not sure what they do
#
# - `index_files` specifies if we want to generate named folders (true) or not (false)
# - `dir` is the default output directory
# - `data` is the data defined in `_data.yml` of the record for which we are generating a page
# - `name` is the key in `data` which determines the output filename
# - `template` is the name of the template for generating the page
# - `extension` is the extension for the generated file
def initialize(site, base, index_files, dir, data, name, template, extension)
@site = site
@base = base

# @dir is the directory where we want to output the page
# @name is the name of the page to generate
#
# the value of these variables changes according to whether we
# want to generate named folders or not
if data[name] == nil
puts "error (datapage_gen). empty value for field '#{name}' in record #{data}"
else
filename = sanitize_filename(data[name]).to_s

@dir = dir + (index_files ? "/" + filename + "/" : "")
@name = (index_files ? "index" : filename) + "." + extension.to_s

self.process(@name)
self.read_yaml(File.join(base, '_layouts'), template + ".html")
self.data['title'] = data[name]
# add all the information defined in _data for the current record to the
# current page (so that we can access it with liquid tags)
self.data.merge!(data)
end
end
end

class DataPagesGenerator < Jekyll::Generator
safe true

# generate loops over _config.yml/page_gen invoking the DataPage
# constructor for each record for which we want to generate a page

def generate(site)
# page_gen_dirs determines whether we want to generate index pages
# (name/index.html) or standard files (name.html). This information
# is passed to the DataPage constructor, which sets the @dir variable
# as required by this directive
index_files = site.config['page_gen-dirs'] == true

# data contains the specification of the data for which we want to generate
# the pages (look at the README file for its specification)
data = site.config['page_gen']
if data
data.each do |data_spec|
index_files_for_this_data = data_spec['index_files'] != nil ? data_spec['index_files'] : index_files
template = data_spec['template'] || data_spec['data']
name = data_spec['name']
dir = data_spec['dir'] || data_spec['data']
extension = data_spec['extension'] || "html"

if site.layouts.key? template
# records is the list of records defined in _data.yml
# for which we want to generate different pages
records = nil
data_spec['data'].split('.').each do |level|
if records.nil?
records = site.data[level]
else
records = records[level]
end
end

# apply filtering conditions:
# - filter requires the name of a boolean field
# - filter_condition evals a ruby expression
records = records.select { |r| r[data_spec['filter']] } if data_spec['filter']
records = records.select { |record| eval(data_spec['filter_condition']) } if data_spec['filter_condition']

records.each do |record|
site.pages << DataPage.new(site, site.source, index_files_for_this_data, dir, record, name, template, extension)
end
else
puts "error (datapage_gen). could not find template #{template}" if not site.layouts.key? template
end
end
end
end
end

module DataPageLinkGenerator
include Sanitizer

# use it like this: {{input | datapage_url: dir}}
# to generate a link to a data_page.
#
# the filter is smart enough to generate different link styles
# according to the data_page-dirs directive ...
#
# ... however, the filter is not smart enough to support different
# extensions for filenames.
#
# Thus, if you use the `extension` feature of this plugin, you
# need to generate the links by hand
def datapage_url(input, dir)
extension = Jekyll.configuration({})['page_gen-dirs'] ? '/' : '.html'
"#{dir}/#{sanitize_filename(input)}#{extension}"
end
end

end

Liquid::Template.register_filter(Jekyll::DataPageLinkGenerator)
Jekyll::Hooks.register :site, :after_init do |site, payload|
RunMeOnce.process(site, payload)
end
13 changes: 13 additions & 0 deletions src/projects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Projects
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a good start, but I'd like to see a bit more style and layout on this page before we push it. Maybe add a couple more details to this list? Perhaps we could steal the layout/styles of the Code.gov browser projects page?

layout: page
---

<div class="usa-layout-docs-main_content" markdown="1">

This is an incomplete list of projects that are available as open source.

{% for project in site.data.code.releases %}
* [{{ project.name}}](/projects{{project.name | datapage_url: dir}}); {{ project.description}}
milovanderlinden marked this conversation as resolved.
Show resolved Hide resolved
{% endfor %}
</div>