- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdownIndexGenerator.java
More file actions
Latest commit
116 lines (96 loc) · 4.35 KB
/
Copy pathMarkdownIndexGenerator.java
File metadata and controls
116 lines (96 loc) · 4.35 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
importjava.io.IOException;
importjava.nio.file.*;
importjava.util.*;
importjava.util.regex.*;
importjava.util.stream.Collectors;
publicclassMarkdownIndexGenerator {
privatestaticfinalPatternFILE_PATTERN = Pattern.compile("^(\\d+)(?:_(\\d+))?");
publicstaticvoidmain(String[] args) {
StringprojectFolder = Paths.get("classNotes").toAbsolutePath().toString();
generateIndex(Paths.get(projectFolder));
}
privatestaticvoidgenerateIndex(PathrootFolder) {
StringBuilderindexContent = newStringBuilder("# 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 -> newFileWithIndex(file, convertFileNameToSortableIndex(file.getFileName().toString())))
.sorted(Comparator.comparing(FileWithIndex::getIndex))
.collect(Collectors.toList());
// Generate the index file content
for (FileWithIndexfileWithIndex : filesWithIndex) {
Pathfile = fileWithIndex.getFile();
PathrelativePath = rootFolder.relativize(file);
StringlinkPath = "classNotes/" + relativePath.toString().replace("\\", "/");
indexContent.append("## ").append(file.getFileName()).append("\n\n");
List<Heading> headings = extractHeadings(Files.readString(file));
for (Headingheading : headings) {
Stringlink = generateLink(linkPath, heading.title);
Stringindent = " ".repeat(heading.level - 1);
indexContent.append(indent).append("- ").append(link).append("\n");
}
indexContent.append("\n");
}
// Write to index.md
PathindexFilePath = Paths.get("index.md");
Files.writeString(indexFilePath, indexContent.toString(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
System.out.println("Index generated at: " + indexFilePath.toAbsolutePath());
} catch (IOExceptione) {
System.err.println("Error processing files: " + e.getMessage());
}
}
privatestaticStringconvertFileNameToSortableIndex(StringfileName) {
// Match patterns like 1_1, 2_10, etc. and convert to sortable format
Matchermatcher = FILE_PATTERN.matcher(fileName);
if (matcher.find()) {
Stringpart1 = matcher.group(1);
Stringpart2 = matcher.group(2);
returnpart2 != null ? String.format("%03d.%02d", Integer.parseInt(part1), Integer.parseInt(part2)) : String.format("%03d", Integer.parseInt(part1));
}
returnfileName; // Return as-is if no match
}
privatestaticList<Heading> extractHeadings(StringfileContent) {
List<Heading> headings = newArrayList<>();
String[] lines = fileContent.split("\n");
for (Stringline : lines) {
Matchermatcher = Pattern.compile("^(#{1,6})\\s+(.*)").matcher(line);
if (matcher.find()) {
intlevel = matcher.group(1).length();
Stringtitle = matcher.group(2).trim();
headings.add(newHeading(level, title));
}
}
returnheadings;
}
privatestaticStringgenerateLink(Stringfilename, Stringtitle) {
Stringanchor = title.toLowerCase()
.replaceAll("[^a-z0-9\\- ]", "")
.replace(" ", "-");
filename = filename.replace(" ", "%20");
return"[" + title + "](" + filename + "#" + anchor + ")";
}
privatestaticclassHeading {
intlevel;
Stringtitle;
Heading(intlevel, Stringtitle) {
this.level = level;
this.title = title;
}
}
privatestaticclassFileWithIndex {
Pathfile;
Stringindex;
FileWithIndex(Pathfile, Stringindex) {
this.file = file;
this.index = index;
}
publicPathgetFile() {
returnfile;
}
publicStringgetIndex() {
returnindex;
}
}
}