forked from egonw/blog2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindTags.groovy
More file actions
146 lines (132 loc) · 3.32 KB
/
findTags.groovy
File metadata and controls
146 lines (132 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
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
// Copyright (C) 2023 Egon Willighagen
// License: MIT
import groovy.io.FileType
// I have been pondering about using a Markdown parser here, but I did not want to learn another "powerful" API.
// So, below is a bit of custom code: extract "tags:" from the content from the first two "---" lines
def tags = new HashSet<String>()
def dir = new File("_posts/")
def posts = []
dir.eachFileRecurse (FileType.FILES) { file ->
if (!file.name.startsWith("template"))
posts << file
}
posts.each() { post ->
// println "Processing ${post}"
header = false
tagline = false
post.eachLine { line ->
if (header) {
if (line == "---") {
header = false
return true // stop parsing
} else {
if (tagline && line.startsWith(" ")) {
line.split().each { tag -> tags.add(tag) }
} else if (line.startsWith("tags:")) {
tagline = true
line = line.substring(5)
line.split().each { tag -> tags.add(tag) }
} else {
tagline = false
}
}
} else if (line == "---") {
header = true
}
}
}
// also check the tag pages for additional DOIs
dir = new File("tag/")
def tagFiles = []
dir.eachFileRecurse (FileType.FILES) { file ->
tagFiles << file
}
tagFiles.each() { tagFile ->
// println "Processing ${tagFile}"
header = false
tagline = false
tagFile.eachLine { line ->
if (header) {
if (line == "---") {
header = false
return true // stop parsing
} else {
if (tagline && line.startsWith(" ")) {
line.split().each { tag -> tags.add(tag) }
} else if (line.startsWith("tags:")) {
tagline = true
line = line.substring(5)
line.split().each { tag -> tags.add(tag) }
} else {
tagline = false
}
}
} else if (line == "---") {
header = true
}
}
}
// Now that we have the tags, look for the interesting ones
// First, works with DOIs
tags.grep{ it.toString().startsWith("doi:") }.each() { tag ->
tag = tag.substring(4).toUpperCase()
file = new File("work/${tag}.markdown")
if (!file.exists()) {
println "Creating $file"
file.text = """---
layout: work
title: "Work: XXXX"
type: ScholarlyArticle
tag: doi:${tag}
doi: doi:${tag}
---
"""
}
}
tags.grep{ it.toString().startsWith("mycito:") }.each() { tag ->
lastIndex = tag.lastIndexOf(":")
tag = tag.substring(lastIndex+1).toUpperCase()
file = new File("work/${tag}.markdown")
if (!file.exists()) {
println "Creating $file"
file.text = """---
layout: work
title: "Work: XXXX"
type: ScholarlyArticle
tag: doi:${tag}
doi: doi:${tag}
---
"""
}
}
// Next, substances with an InChIKeys
tags.grep{ it.toString().startsWith("inchikey:") }.each() { tag ->
tag = tag.substring(9).toUpperCase()
file = new File("molecule/${tag}.markdown")
if (!file.exists()) {
println "Creating $file"
file.text = """---
layout: molecule
title: "Molecule: ${tag}"
tag: inchikey:${tag}
smiles: XXXX
---
"""
}
}
// Finally, other tags (anything without an ':')
tags.grep{ !(it.toString().contains(":")) }.each() { tag ->
tag = tag.toLowerCase()
if (tag != ">") {
file = new File("tag/${tag}.markdown")
if (!file.exists()) {
println "Creating $file"
file.text = """---
layout: tagpage
title: "Tag: ${tag}"
tag: ${tag}
---
"""
}
}
}