Skip to content

Commit 95bf093

Browse files
Merge pull request #52 from angelina-momin/feature-list-codecheckers
Feature-list-codecheckers
2 parents 6efab59 + 2aa7d84 commit 95bf093

26 files changed

+454
-97
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: codecheck
22
Title: Helper Functions for CODECHECK Project
3-
Version: 0.3.0
3+
Version: 0.4.0
44
Authors@R:
55
c(person(given = "Stephen",
66
family = "Eglen",

R/register.R

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
CONFIG <- new.env()
2-
# Registers can be further divided into filter subgroups
3-
# For cases where an entry does not fall into any subgroup, it's rendered files
4-
# are stored in a folder with its own name
5-
CONFIG$FILTER_SUB_GROUPS <- list(
6-
venues = list("community", "journal", "conference")
7-
)
8-
CONFIG$MD_COLUMNS_WIDTHS <- "|:-------|:--------------------------------|:------------------|:---|:--------------------------|:----------|"
9-
CONFIG$REGISTER_COLUMNS <- list("Certificate", "Repository", "Type", "Issue", "Report", "Check date")
10-
CONFIG$DICT_ORCID_ID_NAME <- list()
11-
CONFIG$DIR_TEMP_REGISTER_CODECHECKER <- "docs/temp_register_codechecker.csv"
12-
131
#' Function for rendering the register into different view
142
#'
153
#' NOTE: You should put a GitHub API token inth the environment variable `GITHUB_PAT` to fix rate limits. Acquire one at see https://github.com/settings/tokens.
@@ -33,9 +21,14 @@ CONFIG$DIR_TEMP_REGISTER_CODECHECKER <- "docs/temp_register_codechecker.csv"
3321
register_render <- function(register = read.csv("register.csv", as.is = TRUE),
3422
filter_by = c("venues", "codecheckers"),
3523
outputs = c("html", "md", "json")) {
36-
CONFIG$MD_TEMPLATE <- system.file("extdata", "templates/template_register.md", package = "codecheck")
37-
24+
# Loading config.R file
25+
source(system.file("extdata", "config.R", package = "codecheck"))
26+
3827
register_table <- preprocess_register(register, filter_by)
28+
29+
# Setting number of codechecks now for later use. This is done to avoid double counting codechecks
30+
# done by multiple authors.
31+
CONFIG$NO_CODECHECKS <- nrow(register_table)
3932

4033
# Creating list of of register tables with indices being the filter types
4134
list_register_tables <- c()
@@ -53,8 +46,20 @@ register_render <- function(register = read.csv("register.csv", as.is = TRUE),
5346

5447
# Rendering files
5548
if ("md" %in% outputs) render_register_mds(list_register_tables)
56-
if ("html" %in% outputs) render_register_htmls(list_register_tables)
57-
if ("json" %in% outputs) render_register_jsons(list_register_tables)
49+
if ("html" %in% outputs) {
50+
render_register_htmls(list_register_tables)
51+
52+
for (filter in filter_by){
53+
render_non_register_htmls(list_register_tables[[filter]], page_type = filter)
54+
}
55+
}
56+
if ("json" %in% outputs) {
57+
render_register_jsons(list_register_tables)
58+
59+
for (filter in filter_by){
60+
render_non_register_jsons(list_register_tables[[filter]], page_type = filter)
61+
}
62+
}
5863

5964
return(register_table)
6065
}

R/utils_create_filtered_register_csvs.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ get_output_dir <- function(filter, column_value) {
116116
#' @param venue_name The venue_name obtained from the "Type" column of the register
117117
#' @return The venue category. If the venue does not belong to any category NULL is returned
118118
determine_venue_category <- function(venue_name){
119-
list_venue_categories <- CONFIG$FILTER_SUB_GROUPS[["venues"]]
119+
list_venue_categories <- CONFIG$FILTER_SUBCATEGORIES[["venues"]]
120120
for (category in list_venue_categories){
121121
if (grepl(category, venue_name, ignore.case=TRUE)) {
122122
return(category)

R/utils_render_register_mds.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ render_register_md <- function(filter, register_table, register_table_name, for_
9090
register_table <- add_repository_links_md(register_table)
9191
}
9292
# Fill in the content
93-
md_table <- load_md_template(CONFIG$MD_TEMPLATE)
93+
md_table <- load_md_template(CONFIG$TEMPLATE_DIR[["reg"]][["md_template"]])
9494

9595
markdown_content <- capture.output(kable(register_table, format = "markdown"))
9696
md_table <- add_markdown_title(filter, md_table, register_table_name)

R/utils_render_reigster_html.r

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ add_repository_links_html <- function(register_table) {
3232
#' Dynamically generates a html_document.yml with the full paths to the index header, prefix
3333
#' and postfix.html files.
3434
#'
35-
#' @param filter The filter name
36-
#' @param register_table_name The register table name
37-
generate_html_document_yml <- function(filter, register_table_name) {
38-
dir <- paste0(getwd(), "/", get_output_dir(filter, register_table_name))
35+
#' @param output_dir The output directory where the html_document needs to be saved.
36+
generate_html_document_yml <- function(output_dir) {
37+
output_dir <- paste0(getwd(), "/", output_dir)
3938

4039
yaml_content <- sprintf(
4140
"# DO NOT EDIT THIS FILE MANUALLY
@@ -48,54 +47,63 @@ generate_html_document_yml <- function(filter, register_table_name) {
4847
highlight: null
4948
self_contained: false
5049
lib_dir: libs",
51-
dir, dir, dir
50+
output_dir, output_dir, output_dir
5251
)
53-
writeLines(yaml_content, paste0(dir, "html_document.yml"))
52+
writeLines(yaml_content, paste0(output_dir, "html_document.yml"))
5453
}
5554

5655
#' Dynamically generates the index_postfix.html from a template file
5756
#'
57+
#' @param output_dir The output directory
5858
#' @param filter The filter name
59-
#' @param register_table_name The register table name
59+
#' @param register_table_name The register table name. If this is NULL we are generating list of venues/ codecheckers
6060
#'
6161
#' @importFrom whisker whisker.render
62-
create_index_postfix_html <- function(filter, register_table_name){
63-
hrefs <- generate_html_postfix_hrefs(filter, register_table_name)
62+
create_index_postfix_html <- function(output_dir, filter, register_table_name = NULL){
63+
64+
# When we have register table names, we are handling the case of reg tables
65+
if (!is.null(register_table_name)){
66+
postfix_template <- readLines(CONFIG$TEMPLATE_DIR[["reg"]][["postfix"]], warn = FALSE)
67+
# Render the template with the correct hrefs
68+
hrefs <- generate_html_postfix_hrefs_reg(filter, register_table_name)
69+
}
70+
71+
# Generating the postfix for non-register table pages (e.g. list of venues and codecheckers)
72+
else{
73+
postfix_template <- readLines(CONFIG$TEMPLATE_DIR[["non_reg"]][["postfix"]], warn = FALSE)
74+
hrefs <- list(
75+
json_href = paste0("https://codecheck.org.uk/register/", filter, "/index.json")
76+
)
77+
}
6478

65-
# Using the index_postfix_template
66-
postfix_template <- readLines(paste0(getwd(), "/docs/index_postfix_template.html"), warn = FALSE)
67-
# Render the template with the correct hrefs
6879
output <- whisker.render(postfix_template, hrefs)
69-
writeLines(output, paste0(get_output_dir(filter, register_table_name), "index_postfix.html"))
80+
writeLines(output, paste0(output_dir, "index_postfix.html"))
7081
}
7182

7283
#' Dynamically generates the index_prefix.html from a template file
7384
#'
74-
#' @param filter The filter name
75-
#' @param register_table_name The register table name
76-
create_index_prefix_html <- function(filter, register_table_name){
85+
#' @param output_dir The output directory
86+
create_index_prefix_html <- function(output_dir){
7787
# Using the index_prefix_template
78-
prefix_template <- readLines(paste0(getwd(), "/docs/index_prefix_template.html"), warn = FALSE)
79-
80-
writeLines(prefix_template, paste0(get_output_dir(filter, register_table_name), "index_prefix.html"))
88+
prefix_template <- readLines(CONFIG$TEMPLATE_DIR[["reg"]][["prefix"]], warn = FALSE)
89+
writeLines(prefix_template, paste0(output_dir, "index_prefix.html"))
8190
}
8291

8392
#' Dynamically generates the index_header.html from a template file
8493
#'
85-
#' @param filter The filter name
86-
#' @param register_table_name The register table name
87-
create_index_header_html <- function(filter, register_table_name){
94+
#' @param output_dir The output directory
95+
create_index_header_html <- function(output_dir){
8896
# Using the index_header_template
89-
header_template <- readLines(paste0(getwd(), "/docs/index_header_template.html"), warn = FALSE)
97+
header_template <- readLines(CONFIG$TEMPLATE_DIR[["reg"]][["header"]], warn = FALSE)
9098

91-
writeLines(header_template, paste0(get_output_dir(filter, register_table_name), "index_header.html"))
99+
writeLines(header_template, paste0(output_dir, "index_header.html"))
92100
}
93101

94-
#' Generates the hrefs to set in the postfix.html file.
102+
#' Generates the hrefs to set in the postfix.html file for the rendering of register tables.
95103
#'
96104
#' @param filter The filter name
97105
#' @param register_table_name The register table name
98-
generate_html_postfix_hrefs <- function(filter, register_table_name) {
106+
generate_html_postfix_hrefs_reg <- function(filter, register_table_name) {
99107
hrefs <- list(
100108
csv_source_href = generate_href(filter, register_table_name, "csv_source"),
101109
searchable_csv_href = generate_href(filter, register_table_name, "searchable_csv"),
@@ -155,14 +163,15 @@ generate_href <- function(filter, register_table_name, href_type) {
155163
}
156164
}
157165

158-
#' Creates html files for each index section- the index postfix, prefix and the header
166+
#' Creates index postfix, prefix and the header
159167
#'
168+
#' @param output_dir The output directory of the section files
160169
#' @param filter The filter name
161170
#' @param register_table_name The register table name
162-
create_index_section_files <- function(filter, register_table_name) {
163-
create_index_postfix_html(filter, register_table_name)
164-
create_index_prefix_html(filter, register_table_name)
165-
create_index_header_html(filter, register_table_name)
171+
create_index_section_files <- function(output_dir, filter, register_table_name = NULL) {
172+
create_index_postfix_html(output_dir, filter, register_table_name)
173+
create_index_prefix_html(output_dir)
174+
create_index_header_html(output_dir)
166175
}
167176

168177
#' Renders register html for a single register_table
@@ -171,14 +180,14 @@ create_index_section_files <- function(filter, register_table_name) {
171180
#' @param register_table The register table
172181
#' @param register_table_name The register table name
173182
render_register_html <- function(filter, register_table, register_table_name){
174-
# Add icons to the Repository column for HTML output, use a copy of the register.md
183+
184+
output_dir <- get_output_dir(filter, register_table_name)
175185
register_table <- add_repository_links_html(register_table)
176186

177187
# Dynamically create the index header, prefix and postfix files
178-
create_index_section_files(filter, register_table_name)
179-
generate_html_document_yml(filter, register_table_name)
188+
create_index_section_files(output_dir, filter, register_table_name)
189+
generate_html_document_yml(output_dir)
180190

181-
output_dir <- get_output_dir(filter, register_table_name)
182191
# Capture the HTML output from a markdown file
183192
# Note that the temp md file must be created even if a md exists because the register table
184193
# now has different icons under "Repository" column
@@ -214,8 +223,10 @@ render_register_html <- function(filter, register_table, register_table_name){
214223
render_register_htmls <- function(list_register_tables) {
215224
# Loop over each register table
216225
for (filter in names(list_register_tables)){
217-
for (register_table_name in names(list_register_tables[[filter]])) {
218-
register_table <- list_register_tables[[filter]][[register_table_name]]
226+
list_filtered_reg_tables <- list_register_tables[[filter]]
227+
228+
for (register_table_name in names(list_filtered_reg_tables)) {
229+
register_table <- list_filtered_reg_tables[[register_table_name]]
219230
render_register_html(filter, register_table, register_table_name)
220231
}
221232
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#' Renders codecheckers table in JSON format.
2+
#'
3+
#' @param list_codechecker_reg_tables The list of codechecker register tables. The indices are the ORCID IDs.
4+
render_table_codecheckers_json <- function(list_codechecker_reg_tables){
5+
list_orcid_ids <- names(list_codechecker_reg_tables)
6+
table_codecheckers <- data.frame(matrix(ncol=0, nrow = length(list_orcid_ids)), stringsAsFactors = FALSE)
7+
8+
col_names <- CONFIG$CODECHECKER_TABLE_COL_NAMES[["codechecker_table"]]
9+
10+
# Column- codechecker names
11+
table_codecheckers[[col_names[["codechecker"]]]] <- sapply(
12+
X = list_orcid_ids,
13+
FUN = function(orcid_id) {
14+
paste0(CONFIG$DICT_ORCID_ID_NAME[[orcid_id]])
15+
}
16+
)
17+
18+
table_codecheckers[[col_names[["orcid"]]]] <- list_orcid_ids
19+
20+
# Column- No. of codechecks
21+
table_codecheckers[[col_names[["no_codechecks"]]]] <- sapply(
22+
X = list_orcid_ids,
23+
FUN = function(orcid_id) {
24+
paste0(nrow(list_codechecker_reg_tables[[orcid_id]]))
25+
}
26+
)
27+
28+
return(table_codecheckers)
29+
}
30+
31+
#' Renders codecheckers table in HTML format.
32+
#' Each codechecker name links to the register table for that specific
33+
#' codechecker. The ORCID IDs link to their ORCID pages.
34+
#'
35+
#' @param list_codechecker_reg_tables The list of codechecker register tables. The indices are the ORCID IDs.
36+
render_table_codecheckers_html <- function(list_codechecker_reg_tables){
37+
38+
list_orcid_ids <- names(list_codechecker_reg_tables)
39+
col_names <- CONFIG$NON_REG_TABLE_COL_NAMES[["codechecker_table"]]
40+
41+
no_rows <- length(list_orcid_ids)
42+
43+
# Initializing the table
44+
table_codecheckers <- data.frame(matrix(ncol= 0, nrow = no_rows), stringsAsFactors = FALSE)
45+
46+
# Column- codechecker names
47+
# Each codechecker name will be a hyperlink to the register table
48+
# with all their codechecks
49+
table_codecheckers[col_names[["codechecker"]]] <- sapply(
50+
X = list_orcid_ids,
51+
FUN = function(orcid_id) {
52+
codechecker_name <- CONFIG$DICT_ORCID_ID_NAME[[orcid_id]]
53+
paste0("[", codechecker_name, "](https://codecheck.org.uk/register/codecheckers/",
54+
orcid_id, "/)")
55+
}
56+
)
57+
# Column- ORCID ID
58+
table_codecheckers[col_names[["orcid"]]] <- sapply(
59+
X = list_orcid_ids,
60+
FUN = function(orcid_id) {
61+
paste0("[", orcid_id, "](https://orcid.org/", orcid_id, ")")
62+
}
63+
)
64+
# Column- No. of codechecks
65+
# Shown as "no_codechecks (sell all checks)" where "see all checks" links to the checks by
66+
# the codechecker
67+
table_codecheckers[col_names[["no_codechecks"]]] <- sapply(
68+
X = list_orcid_ids,
69+
FUN = function(orcid_id) {
70+
no_codechecks <- nrow(list_codechecker_reg_tables[[orcid_id]])
71+
paste0(nrow(list_codechecker_reg_tables[[orcid_id]])," [(see all checks)](https://codecheck.org.uk/register/codecheckers/",
72+
orcid_id, "/)")
73+
}
74+
)
75+
76+
return(table_codecheckers)
77+
}
78+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#' Renders non-register pages such as codecheckers or venues page.
2+
#'
3+
#' @param list_reg_tables The list of register tables to link to in this html page
4+
render_non_register_htmls <- function(list_reg_tables, page_type){
5+
output_dir <- paste0("docs/", page_type, "/")
6+
7+
if (page_type == "codecheckers"){
8+
table <- render_table_codecheckers_html(list_reg_tables)
9+
table <- kable(table)
10+
# Counting number of codecheckers based of number of codechecker reg tables
11+
# The table is a kable table and hence we cannot count rows
12+
no_codecheckers <- length(list_reg_tables)
13+
# Using number of codechecks from CONFIG instead of "no. of codechecks" column to avoid double count
14+
subtext <- paste("In total,", no_codecheckers, "codecheckers contributed", CONFIG$NO_CODECHECKS, "codechecks*")
15+
16+
# Extra text to explain why total_codechecks != SUM(no.of codechecks)
17+
extra_text <- "<i>\\*Note that the total codechecks is less than the collective sum of
18+
individual codecheckers' number of codechecks.
19+
This is because some codechecks involved more than one codechecker.</i>"
20+
}
21+
22+
else{
23+
return()
24+
}
25+
# Creating and adjusting the markdown table
26+
md_table <- load_md_template(CONFIG$TEMPLATE_DIR[["non_reg"]][["md_template"]])
27+
title <- paste0("CODECHECK List of ", page_type)
28+
md_table <- gsub("\\$title\\$", title, md_table)
29+
md_table <- gsub("\\$subtitle\\$", subtext, md_table)
30+
md_table <- gsub("\\$content\\$", paste(table, collapse = "\n"), md_table)
31+
md_table <- gsub("\\$extra_text\\$", extra_text, md_table)
32+
33+
# Saving the table to a temp md file
34+
temp_md_path <- paste0(output_dir, "temp.md")
35+
writeLines(md_table, temp_md_path)
36+
37+
# Creating the correct html yaml and index files
38+
create_index_section_files(output_dir, page_type)
39+
generate_html_document_yml(output_dir)
40+
yaml_path <- normalizePath(file.path(getwd(), paste0(output_dir, "html_document.yml")))
41+
42+
# Render index.html from markdown
43+
rmarkdown::render(
44+
input = temp_md_path,
45+
output_file = "index.html",
46+
output_dir = output_dir,
47+
output_yaml = yaml_path
48+
)
49+
50+
# Deleting the temp file
51+
file.remove(temp_md_path)
52+
53+
# Changing the html file so that the path to the libs folder refers to
54+
# the libs folder "docs/libs".
55+
# This is done to remove duplicates of "libs" folders.
56+
html_file_path <- paste0(output_dir, "index.html")
57+
edit_html_lib_paths(html_file_path)
58+
# Deleting the libs folder after changing the html lib path
59+
unlink(paste0(output_dir, "/libs"), recursive = TRUE)
60+
}
61+
62+
#' Renders JSON file of non register tables such as list of venues, list of codecheckers
63+
#'
64+
#' @param list_reg_tables The list of register tables needed for the information.
65+
render_non_register_jsons <- function(list_reg_tables, page_type){
66+
output_dir <- paste0("docs/", page_type, "/")
67+
68+
if (page_type == "codeheckers"){
69+
table <- render_table_codecheckers_json(list_reg_tables)
70+
}
71+
72+
else{
73+
return()
74+
}
75+
jsonlite::write_json(
76+
table,
77+
path = paste0(output_dir, "index.json"),
78+
pretty = TRUE
79+
)
80+
}

0 commit comments

Comments
 (0)