-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
369 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,22 @@ | ||
package nbbrd.heylogs; | ||
|
||
import com.vladsch.flexmark.util.ast.Node; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public interface Rule { | ||
|
||
String getName(); | ||
|
||
Failure validate(Node node); | ||
|
||
@NotNull | ||
static List<Rule> getDefault() { | ||
return Stream.concat(Stream.of(GuidingPrinciples.values()), Stream.of(ExtendedRules.values())) | ||
.map(Rule.class::cast) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
heylogs-api/src/main/java/nbbrd/heylogs/VersionFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package nbbrd.heylogs; | ||
|
||
import com.vladsch.flexmark.ast.Heading; | ||
import com.vladsch.flexmark.ast.RefNode; | ||
import com.vladsch.flexmark.ast.Reference; | ||
import com.vladsch.flexmark.util.ast.Document; | ||
import com.vladsch.flexmark.util.ast.Node; | ||
|
||
import java.time.LocalDate; | ||
import java.time.Year; | ||
import java.time.YearMonth; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@lombok.Value | ||
@lombok.Builder | ||
public class VersionFilter { | ||
|
||
@lombok.NonNull | ||
@lombok.Builder.Default | ||
String ref = ""; | ||
|
||
@lombok.NonNull | ||
@lombok.Builder.Default | ||
LocalDate from = LocalDate.MIN; | ||
|
||
@lombok.NonNull | ||
@lombok.Builder.Default | ||
LocalDate to = LocalDate.MAX; | ||
|
||
@lombok.Builder.Default | ||
int limit = Integer.MAX_VALUE; | ||
|
||
public boolean contains(Heading heading) { | ||
return contains(Version.parse(heading)); | ||
} | ||
|
||
public boolean contains(Version version) { | ||
return version.getRef().contains(ref) | ||
&& from.compareTo(version.getDate()) <= 0 | ||
&& (to.isAfter(version.getDate()) || (to.equals(LocalDate.MAX) && version.isUnreleased())); | ||
} | ||
|
||
public void apply(Document root) { | ||
int found = 0; | ||
boolean keep = false; | ||
|
||
List<String> refNodes = new ArrayList<>(); | ||
List<Reference> references = new ArrayList<>(); | ||
|
||
for (Node current : root.getChildren()) { | ||
|
||
if (current instanceof Heading && Version.isVersionLevel((Heading) current)) { | ||
if (found >= getLimit() || !contains((Heading) current)) { | ||
keep = false; | ||
} else { | ||
found++; | ||
keep = true; | ||
} | ||
} | ||
|
||
if (keep) { | ||
Nodes.of(RefNode.class) | ||
.descendants(current) | ||
.map(node -> node.getReference().toString()) | ||
.forEach(refNodes::add); | ||
} else { | ||
if (current instanceof Reference) { | ||
references.add((Reference) current); | ||
} else { | ||
current.unlink(); | ||
} | ||
} | ||
} | ||
|
||
references | ||
.stream() | ||
.filter(reference -> !refNodes.contains(reference.getReference().toString())) | ||
.forEach(Node::unlink); | ||
} | ||
|
||
public static LocalDate parseLocalDate(CharSequence input) { | ||
try { | ||
return Year.parse(input).atDay(1); | ||
} catch (Exception ex1) { | ||
try { | ||
return YearMonth.parse(input).atDay(1); | ||
} catch (Exception ex2) { | ||
return LocalDate.parse(input); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.