Skip to content

Commit

Permalink
Start of generating markdown checklist (#2104)
Browse files Browse the repository at this point in the history
* Start of generating markdown checklist

This isn't complete, but starts down the path of creating
a checklist in GitHub Flavored Markdown.

Signed-off-by: David A. Wheeler <[email protected]>

* Provide markdown format (as a todo list)

Signed-off-by: David A. Wheeler <[email protected]>

* Remove checkboxes from heading lines

Signed-off-by: David A. Wheeler <[email protected]>

* Mark description as html_safe

If this isn't marked as html_safe, some of the descriptions look
bad because it shows the HTML code instead of its intent.

Signed-off-by: David A. Wheeler <[email protected]>

* Append current time to markdown output

Include the current time, so people who use the checklist as a workflow
will know when the checklist was generated.

Signed-off-by: David A. Wheeler <[email protected]>

---------

Signed-off-by: David A. Wheeler <[email protected]>
  • Loading branch information
david-a-wheeler authored Jan 31, 2024
1 parent b81895c commit 5fa6e01
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
12 changes: 9 additions & 3 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ class ProjectsController < ApplicationController
skip_before_action :redir_missing_locale, only: :badge

before_action :set_project,
only: %i[edit update delete_form destroy show show_json]
only: %i[edit update delete_form destroy show show_json show_markdown]
before_action :require_logged_in, only: :create
before_action :can_edit_else_redirect, only: %i[edit update]
before_action :can_control_else_redirect, only: %i[destroy delete_form]
before_action :require_adequate_deletion_rationale, only: :destroy
before_action :set_criteria_level, only: %i[show edit update]
before_action :set_criteria_level, only: %i[show edit update show_markdown]

# Cache with Fastly CDN. We can't use this header, because logged-in
# and not-logged-in users see different things (and thus we can't
# have a cached version that works for everyone):
# before_action :set_cache_control_headers, only: [:index, :show, :badge]
# We *can* cache the badge result, and that's what matters anyway.
before_action :set_cache_control_headers, only: %i[badge show_json]
before_action :set_cache_control_headers, only: %i[badge show_json show_markdown]

helper_method :repo_data

Expand Down Expand Up @@ -168,6 +168,12 @@ def show_json
set_surrogate_key_header @project.record_key
end

# GET /projects/1.md
def show_markdown
# Tell CDN the surrogate key so we can quickly erase it later
set_surrogate_key_header @project.record_key
end

# GET /projects/:id/delete_form(.:format)
def delete_form; end

Expand Down
48 changes: 48 additions & 0 deletions app/views/projects/show_markdown.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<%-
criteria_level = @criteria_level || '0'
def criterion_to_checkbox(value)
case value
when 'Met', 'N/A'
'[x]'
else
'[ ]'
end
end
# TODO: Major & minor levels should be marked "done" if contents done
-%>
# <%= @project.name %>

[<%= @project.name
%>](https://bestpractices.dev/projects/<%= @project.id
%>?criteria_level=<%= criteria_level %>):

<%- FullCriteriaHash[criteria_level].each do |major, major_info| -%>
- **<%= t("headings.#{major}") %>**
<%- major_info.each do |minor, minor_info| -%>
- *<%= t("headings.#{minor}") %>*
<%- minor_info.each do |criterion, criterion_info| -%>
- <%= criterion_to_checkbox(@project["#{criterion}_status".to_sym])
%> <%=
t("criteria.#{criteria_level}.#{criterion}.description").html_safe
%><%=
# Only add a "details" tag with details if there *are* any details
# "Details" and the other strings here are from trusted sources.
details = I18n.t("criteria.#{criteria_level}.#{criterion}.details",
default: '')
if details.present?
'<details><summary> '.html_safe
else
' '
end
%>[<%= criterion %>]<%=
if details.present?
"</summary>#{details}</details>".html_safe
else
''
end
%>
<%- end -%>
<%- end -%>
<%- end -%>
<%= Time.new.iso8601 %>
8 changes: 8 additions & 0 deletions config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@
# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
Mime::Type.register 'image/svg+xml', :svg
Mime::Type.register 'text/plain; charset=UTF-8', :md

# We really should register markdown as:
# Mime::Type.register 'text/markdown; charset=UTF-8', :md
# but then it won't be inline, even when we do this:
# Rails.application.config.active_storage.content_types_allowed_inline += [
# 'text/markdown'
# ]
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@
get 'delete_form' => 'projects#delete_form'
get '' => 'projects#show_json',
constraints: ->(req) { req.format == :json }
get '' => 'projects#show_markdown',
constraints: ->(req) { req.format == :md }
get ':criteria_level(.:format)' => 'projects#show',
constraints: { criteria_level: VALID_CRITERIA_LEVEL }
get ':criteria_level/edit(.:format)' => 'projects#edit',
Expand Down
6 changes: 6 additions & 0 deletions test/controllers/projects_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ def only_correct_criteria_selectable(level)
assert_equal [], body['additional_rights']
end

test 'should show markdown with locale' do
get "/en/projects/#{@project.id}.md"
assert_response :success
assert_includes @response.body, 'The project website MUST provide information on how'
end

test 'should get edit' do
log_in_as(@project.user)
assert_equal(
Expand Down

0 comments on commit 5fa6e01

Please sign in to comment.