-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.R
302 lines (261 loc) · 8.16 KB
/
main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
library(lubridate)
library(stringr)
library(tidyr)
library(dplyr)
library(purrr)
library(readr)
# Macros
VAULT = "~/Dropbox/PKM/Jakubrain"
VAULT_TEMPLATE_FILE = "Efforts - Academic - Template" #skip this file
YAML_CHECK = "acdCode:" #start of relevant yaml
COLLECTION_CHECK = "\\[\\[Academic Efforts\\]\\]"
MONTH <- lubridate::month(today())
OUTPUT_DIR = paste0(VAULT, "/Efforts/Notes/Data Bytes/")
# Functions --------------------------------------------------------------------
# Check if properties/metadata/yaml in document
has_properties <- function(lines) {
length(grep("^---$", lines[1:5])) > 0
}
# Extract yaml properties
extract_properties <- function(lines) {
lines[2:(grep("^---$", lines)[2] - 1)]
}
grepl_any <- function(pattern, x) {
sum(grepl(pattern, x)) > 0
}
grepl_any <- function(pattern, x, i = NULL) {
if (is.null(i)) {
sum(grepl(pattern, x)) > 0
} else {
sum(grepl(pattern, x)) > 0
}
# Check if data project file
is_data_project <- function(yml) {
if (sum(grepl(YAML_CHECK, yml)) > 0 & sum(grepl(COLLECTION_CHECK, yml)) > 0) {
TRUE
} else {
FALSE
}
}
# Project hierarchy
factorize_group <- function(x) {
factor(x, c("yls-faculty",
"yls-student",
"yls-admin",
"yls-library",
"yale-faculty",
"yale-student",
"yale-library",
"data-service"))
}
# Add period
add_period <- function(x) {
ifelse(grepl("\\.$", x), x, paste0(x, "."))
}
# Get filename from path
get_filename <- function(x) {
gsub("\\.md$", "", gsub("^.+/", "", x))
}
make_client <- function(df) {
txt <-
}
# Add markdown
add_markdown <- function(df) {
df |>
mutate(
title = paste0("**", title, "**"),
projectPeople =
case_when(
grepl("yls-library|data-service", projectGroup) ~ "",
TRUE ~ paste0("(", projectPeople, ")")
),
assignedTo = paste("Personnel: ", acdAssigned),
projectStatus = paste0("*STATUS: ", projectStatus, "*"),
lineout = paste(
str_trim(paste0(title, " ", projectPeople)),
str_trim(projectSummary),
str_trim(projectStatus),
"\n",
sep = "\n"
)
)
}
# add_markdown <- function(df) {
# df %>%
# mutate(
# title = paste0("**", title, "**"),
# projectPeople =
# case_when(
# grepl("yls-library|data-service", projectGroup) ~ "",
# TRUE ~ paste0("(", projectPeople, ")")
# ),
# projectStatus = paste0("*STATUS: ", projectStatus, "*"),
# lineout = paste(
# str_trim(paste0(title, " ", projectPeople)),
# str_trim(projectSummary),
# str_trim(projectStatus),
# "\n",
# sep = "\n"
# )
# )
# }
# Create dataframe from file
md_to_df <- function(f, checked_databytes = TRUE, this_month_only = FALSE) {
if (get_filename(f) != VAULT_TEMPLATE_FILE) {
y <- readr::read_file(f)
y_chr <- readLines(f, warn = FALSE)
yaml_pos <- str_locate_all(y, "---")
start <- yaml_pos[[1]][1, 2] + 1
stop <- yaml_pos[[1]][2, 1] - 1
y <- str_trim(substr(y, start, stop))
y <- gsub(":\n ", ": ", z)
y <- gsub("\n ", " ", z)
y <- unlist(str_split(y, "\n"))
#y <- readLines(f, warn = FALSE)
if (has_properties(y_chr)) {
if (is_data_project(y)) {
df <- data.frame(
name = str_split_i(y, ":", 1),
value = str_trim(str_split_i(y, ":", 2))
) |>
filter(grepl("created|updated|acd|databytes", name)) |>
pivot_wider() |>
mutate(
title = get_filename(f),
projectGroup = acdOrgType, #factorize_group(projectGroup),
across(
c(created, updated), as_date
),
across(
c(acdSummary, acdCurrent), add_period
),
last_name = word(str_split_i(projectPeople, ",", 1), -1)
) %>%
add_markdown()
if (this_month_only) {
df <- df %>%
filter(month(updated) == MONTH)
}
if (checked_databytes) {
df <- df %>%
filter(databytes == "true")
}
return(df)
}
}
}
}
# Write markdown header
write_header <- function() {
paste(
"*Updates and news from Empirical Research and Data Services*",
paste0("`", format(Sys.Date(), format="%B %d, %Y"), "`", "\n"),
"--",
paste("***NOTE: The following items are stakeholder works-in-progress",
"and should be considered confidential. Please do not circulate",
"these updates outside of the Law Library. Thank you!***\n"),
sep = "\n"
)
}
# Write to markdown
write_contents <- function(df) {
# Header
txt <- write_header()
type <- df$acdType
grp <- df$projectGroup
# Sort by type
if (grepl_any("Research", type)) {
txt <- paste(txt, "# Research Projects and Consultations\n", sep = "\n")
if (grepl_any("yls", grp)) {
txt <- paste(txt, "## At YLS\n", sep = "\n")
}
else if (grepl_any("yale", grp)) {
txt <- paste(txt, "## Around Yale\n", sep = "\n")
}
else {
txt <- paste(txt, "## External\n", sep = "\n")
}
else if (grepl_any("Data", type)) {
txt <- paste(txt, "# Data Projects and Services\n", sep = "\n")
}
else if (grepl_any("Outreach|Professional", type)) {
txt <- paste(txt, " #Outreach and Professional Services\n", sep = "\n")
}
else {
txt <- paste(txt, "# Other Efforts\n", sep = "\n")
}
# # law school stakeholders
# if (grepl_any("faculty|student", grp) & grepl()) {
# txt <- paste(txt, "## YLS Faculty\n", sep = "\n")
# txt_add <- df %>%
# filter(projectGroup =="yls-faculty") %>%
# pull(lineout) %>%
# paste(., collapse = "\n")
# txt <- paste(txt, txt_add, collapse = "\n")
# }
# if (grepl_any("yls-student", grp)) {
# txt <- paste(txt, "## YLS Students and Fellows\n", sep = "\n")
# txt_add <- df %>%
# filter(projectGroup =="yls-student") %>%
# pull(lineout) %>%
# paste(., collapse = "\n")
# txt <- paste(txt, txt_add, collapse = "\n")
# }
# if (grepl_any("yls-admin", grp)) {
# txt <- paste(txt, "## YLS Administration\n", sep = "\n")
# txt_add <- df %>%
# filter(projectGroup =="yls-admin") %>%
# pull(lineout) %>%
# paste(., collapse = "\n")
# txt <- paste(txt, txt_add, collapse = "\n")
# }
# if (grepl_any("yls-library", grp)) {
# txt <- paste(txt, "## Law Library\n", sep = "\n")
# txt_add <- df %>%
# filter(projectGroup =="yls-library") %>%
# pull(lineout) %>%
# paste(., collapse = "\n")
# txt <- paste(txt, txt_add, collapse = "\n")
# }
# #around yale
# if (grepl_any("yale-", grp)) {
# txt <- paste(txt, "## Around Yale\n", sep = "\n")
# txt_add <- df %>%
# filter(grepl("yale-", projectGroup)) %>%
# pull(lineout) %>%
# paste(., collapse = "\n")
# txt <- paste(txt, txt_add, collapse = "\n")
# }
# #external
# ###pending....
# }
# # Data Services/Projects
# if (grepl_any("data-service", grp)) {
# txt <- paste(txt, "# Data Projects and Services\n", sep = "\n")
# txt_add <- df %>%
# filter(projectGroup =="data-service") %>%
# pull(lineout) %>%
# paste(., collapse = "\n")
# txt <- paste(txt, txt_add, collapse = "\n")
# }
# Clean up multiple spaces and ensure each new line does not start with space
gsub("[\n]{2,}", "\n\n", gsub("\n ", "\n", txt))
}
# Main
main <- function() {
f <- list.files(VAULT,
pattern = "\\.md$",
recursive = TRUE,
full.names = TRUE)
print("Creating data frame of relevant projects and statuses...")
df <- map(f, md_to_df) %>%
list_rbind() %>%
arrange(projectGroup, desc(last_name))
print("Converting to markdown...")
txt <- write_contents(df)
fileout <- paste0(OUTPUT_DIR, "YLS Data Bytes ", Sys.Date(), ".md")
write_lines(txt, file = fileout, sep = "")
message(paste("Updates successfully written to", fileout))
}
# Execute ----------------------------------------------------------------------
main()