Skip to content

Commit

Permalink
First anchor update (automatic)
Browse files Browse the repository at this point in the history
First anchor update, running a program
  • Loading branch information
artellador committed Feb 24, 2024
1 parent d7e14e2 commit 0b9ed28
Show file tree
Hide file tree
Showing 7 changed files with 2,957 additions and 204 deletions.
6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added spec/.Fixmd.java.swp
Binary file not shown.
Binary file added spec/Fixmd.class
Binary file not shown.
63 changes: 63 additions & 0 deletions spec/Fixmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

import java.util.List;
import java.util.Map;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class Fixmd {

public static void main(String[] args) throws Exception {
Pattern pattern = Pattern.compile("^\\#+\\s*\\<a\\s+\\w+=\"(.+)\"\s*>\s*</a>\s*(.*)\s*$");
var replacementMap = Files.lines(Path.of("asyncapi.md"))
.filter(l -> l.matches("^\\#+.*"))
.map(l -> patternGroupsToArray(pattern, l))
.collect(Collectors.groupingBy(x->x.get(0), Collectors.mapping(x->x.get(1), Collectors.toList())));

System.out.println(replacementMap);


Files.write(
Path.of("result.md"),
Files.lines(Path.of("asyncapi.md"))
.map(l -> l.matches(".*\\(\\#user-content-[\\w\\-]+\\).*") ? replaceOldAnchor(l, replacementMap) : l)
.toList(),
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE_NEW
);


}

public static String replaceOldAnchor(String line, Map<String, List<String>> replacementMap) {
Pattern pattern = Pattern.compile(".*\\((\\#user-content-([\\w\\-]+))\\).*");
Matcher matcher = pattern.matcher(line);
if(matcher.matches()) {
String oldAnchorKey = matcher.group(2);
if(replacementMap.containsKey(oldAnchorKey)) {
String oldAnchor = matcher.group(1);
String newAnchor = "#" + replacementMap.get(oldAnchorKey).get(0).trim().toLowerCase().replaceAll("\\s", "-");
System.out.println("Replacing %s with %s".formatted(oldAnchor, newAnchor));
return line.replace(oldAnchor, newAnchor);
} else {
System.out.println("Key not found:" + oldAnchorKey);
}
}

return line;

}

public static List<String> patternGroupsToArray(Pattern pattern, String s) {
Matcher matcher = pattern.matcher(s);
if(matcher.matches()) {
return List.of(matcher.group(1), matcher.group(2));
} else {
return List.of("", s);
}
}

}
408 changes: 204 additions & 204 deletions spec/asyncapi.md

Large diffs are not rendered by default.

2,678 changes: 2,678 additions & 0 deletions spec/result.md

Large diffs are not rendered by default.

0 comments on commit 0b9ed28

Please sign in to comment.