-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcreateContents.r
More file actions
102 lines (92 loc) · 3.32 KB
/
createContents.r
File metadata and controls
102 lines (92 loc) · 3.32 KB
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
require(Hmisc)
method <- c('septables', 'onetable')[2]
trim <- function(x) {
x <- sub("[[:space:]]+$", "", x)
sub('^[[:space:]]+', '', x)
}
files <- list.files(pattern='\\.(Rmd|qmd)$')
rfiles <- setdiff(list.files(pattern='\\.r$'), 'createContents.r')
Files <- c(files, rfiles)
baseFiles <- sub('\\.(Rmd|r|qmd)$', '', Files)
htmls <- list.files(pattern='\\.html$')
n <- length(files)
m <- length(rfiles)
type <- c(rep('Report', n), rep('R', m))
N <- n + m
title <- character(N)
major <- vector('list', N)
minor <- vector('list', N)
i <- 0
for(f in files) {
i <- i + 1
a <- readLines(f)
ti <- a[grep('^title:', a)[1]]
ti <- gsub('^title: "', '', ti)
ti <- gsub('"$', '', trim(ti))
title[i] <- ti
maj <- a[grep('^major:', a)[1]]
maj <- gsub('^major:', '', maj)
major[[i]] <- trim(strsplit(maj, ';')[[1]])
mi <- a[grep('^minor:', a)]
if(length(mi)) mi <- gsub('^minor:', '', mi)
minor[[i]] <- if(length(mi)) trim(strsplit(mi[1], ';')[[1]]) else ''
}
for(f in rfiles) {
i <- i + 1
a <- readLines(f)
ti <- a[grep('^\\# title:', a)[1]]
title[i] <- trim(sub('^# title:', '', ti))
maj <- a[grep('^\\# major:', a)[1]]
maj <- sub('^\\# major:', '', maj)
major[[i]] <- trim(strsplit(maj, ';')[[1]])
mi <- a[grep('^\\# minor:', a)]
if(length(mi)) mi <- sub('^\\# minor:', '', mi)
minor[[i]] <- if(length(mi)) trim(strsplit(mi[1], ';')[[1]]) else ''
}
Major <- unique(sort(unlist(major)))
Minor <- unique(sort(unlist(minor)))
fwrap <- function(x, html=FALSE) {
link <- if(html)'http://htmlpreview.github.io/?https://github.com/harrelfe/rscripts/blob/master/' else 'https://github.com/harrelfe/rscripts/blob/master/'
nam <- if(html) 'report' else x
paste('[', nam, '](', link, x, ')', sep='')
}
f <- 'contents.md'
cat('', file=f, sep='')
cat('\nCategories Found:\n\n')
if(method == 'onetable') cat('*Major Category* | *Minor Category* | *File Name* | *Type* | *Description*\n---- | ---- | ---- | ---- | ----\n',
file=f)
lastmaj <- lastmin <- 'xxxxxx'
for(maj in Major) {
cat(maj, '\n')
if(method == 'septables')
cat('\n## ', upFirst(maj), '\n', sep='', file=f, append=TRUE)
containingMaj <- sapply(major, function(x) any(x == maj))
posminor <- unique(sort(unlist(minor[containingMaj])))
for(mi in posminor) {
cat(' ', mi, '\n')
if(method == 'septables') {
if(mi != '') cat('#### ', upFirst(mi), '\n', sep='', file=f, append=TRUE)
cat('*File Name* | *Type* | *Description*\n---- | ---- | ----\n',
file=f, append=TRUE)
}
containingMin <- sapply(minor, function(x) any(x == mi))
for(g in which(containingMaj & containingMin)) {
htmlf <- paste(baseFiles[g], 'html', sep='.')
tig <- title[g]
if(htmlf %in% htmls)
tig <- paste(tig, '; ', fwrap(htmlf, html=TRUE), sep='')
switch(method,
septables=cat(fwrap(Files[g]), '|', type[g], '|', tig,
'\n', file=f, append=TRUE),
onetable={
ma <- ifelse(maj == lastmaj, '"', maj)
m <- ifelse(maj == lastmaj && mi == lastmin, '"', mi)
cat('**', upFirst(ma), '**| ', upFirst(m), ' | ',
fwrap(Files[g]), ' | ',
type[g], ' | ', tig, '\n', file=f, append=TRUE, sep='')
} )
lastmaj <- maj
lastmin <- mi
}
}
}