-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkdownIndexGenerator.java
116 lines (96 loc) · 4.35 KB
/
MarkdownIndexGenerator.java
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
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.regex.*;
import java.util.stream.Collectors;
public class MarkdownIndexGenerator {
private static final Pattern FILE_PATTERN = Pattern.compile("^(\\d+)(?:_(\\d+))?");
public static void main(String[] args) {
String projectFolder = Paths.get("classNotes").toAbsolutePath().toString();
generateIndex(Paths.get(projectFolder));
}
private static void generateIndex(Path rootFolder) {
StringBuilder indexContent = new StringBuilder("# ClassNotes Index\n\n");
try {
// Collect all markdown files and convert their index (1_1 -> 1.1)
List<FileWithIndex> filesWithIndex = Files.walk(rootFolder)
.filter(Files::isRegularFile)
.filter(file -> file.toString().endsWith(".md") && !file.getFileName().toString().equals("index.md"))
.map(file -> new FileWithIndex(file, convertFileNameToSortableIndex(file.getFileName().toString())))
.sorted(Comparator.comparing(FileWithIndex::getIndex))
.collect(Collectors.toList());
// Generate the index file content
for (FileWithIndex fileWithIndex : filesWithIndex) {
Path file = fileWithIndex.getFile();
Path relativePath = rootFolder.relativize(file);
String linkPath = "classNotes/" + relativePath.toString().replace("\\", "/");
indexContent.append("## ").append(file.getFileName()).append("\n\n");
List<Heading> headings = extractHeadings(Files.readString(file));
for (Heading heading : headings) {
String link = generateLink(linkPath, heading.title);
String indent = " ".repeat(heading.level - 1);
indexContent.append(indent).append("- ").append(link).append("\n");
}
indexContent.append("\n");
}
// Write to index.md
Path indexFilePath = Paths.get("index.md");
Files.writeString(indexFilePath, indexContent.toString(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
System.out.println("Index generated at: " + indexFilePath.toAbsolutePath());
} catch (IOException e) {
System.err.println("Error processing files: " + e.getMessage());
}
}
private static String convertFileNameToSortableIndex(String fileName) {
// Match patterns like 1_1, 2_10, etc. and convert to sortable format
Matcher matcher = FILE_PATTERN.matcher(fileName);
if (matcher.find()) {
String part1 = matcher.group(1);
String part2 = matcher.group(2);
return part2 != null ? String.format("%03d.%02d", Integer.parseInt(part1), Integer.parseInt(part2)) : String.format("%03d", Integer.parseInt(part1));
}
return fileName; // Return as-is if no match
}
private static List<Heading> extractHeadings(String fileContent) {
List<Heading> headings = new ArrayList<>();
String[] lines = fileContent.split("\n");
for (String line : lines) {
Matcher matcher = Pattern.compile("^(#{1,6})\\s+(.*)").matcher(line);
if (matcher.find()) {
int level = matcher.group(1).length();
String title = matcher.group(2).trim();
headings.add(new Heading(level, title));
}
}
return headings;
}
private static String generateLink(String filename, String title) {
String anchor = title.toLowerCase()
.replaceAll("[^a-z0-9\\- ]", "")
.replace(" ", "-");
filename = filename.replace(" ", "%20");
return "[" + title + "](" + filename + "#" + anchor + ")";
}
private static class Heading {
int level;
String title;
Heading(int level, String title) {
this.level = level;
this.title = title;
}
}
private static class FileWithIndex {
Path file;
String index;
FileWithIndex(Path file, String index) {
this.file = file;
this.index = index;
}
public Path getFile() {
return file;
}
public String getIndex() {
return index;
}
}
}