incremental work on markdown processing

This commit is contained in:
Jonathan Shook 2020-05-07 15:20:24 -05:00
parent 9c24a5608a
commit 4278c79006
8 changed files with 128 additions and 16 deletions

View File

@ -26,16 +26,22 @@ public class CompositeMarkdownInfo implements MarkdownInfo {
@Override
public FrontMatterInfo getFrontmatter() {
// calculate included topics
return null;
return elements.get(0).getFrontmatter();
}
@Override
public boolean hasAggregations() {
// was true, but now it is false after compositing
return false;
}
@Override
public MarkdownInfo withTopics(List<String> assigning) {
MarkdownInfo leader = elements.get(0);
leader = leader.withTopics(assigning);
elements.set(0,leader);
return this;
}
public <T extends MarkdownInfo> CompositeMarkdownInfo add(T element) {
elements.add(element);
return this;

View File

@ -10,6 +10,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.*;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -55,24 +56,54 @@ public class MarkdownDocs {
List<? extends MarkdownInfo> markdownWithTopicGlobs =
ListSplitterWhyDoesJavaNotDoThisAlready.partition(markdownInfos, MarkdownInfo::hasTopicGlobs);
List<? extends MarkdownInfo> markdownWithOnlyTopicGlobs =
ListSplitterWhyDoesJavaNotDoThisAlready.partition(markdownWithTopicGlobs, m -> m.getTopics().size()==0);
int loopsremaining=100;
// TODO: add logic to deal with leaf nodes and kick intermediate nodes to the end of the processing list.
// TODO: Double check exit conditions and warn user
while (markdownWithTopicGlobs.size()>0 && loopsremaining>0) {
for (MarkdownInfo markdownWithTopicGlob : markdownWithTopicGlobs) {
markdownWithTopicGlob.getTopicGlobs();
for (MarkdownInfo allInfo : markdownInfos) {
// allInfo.getTopics()
List<MarkdownInfo> ordered = new ArrayList<>();
// At this point, we have three set of markdown infos
// a) with only globs
// b) with globs and literals
// c) with only literals
// We can do an O((n/2)^2) association check, which is better than O(n^2)
ordered.addAll(markdownWithOnlyTopicGlobs);
ordered.addAll(markdownWithTopicGlobs);
ordered.addAll(markdownInfos);
List<Edge<List<String>>> edges = new ArrayList<>();
List<String> assigning = null;
for (int i = 0; i < ordered.size()-1; i++) {
MarkdownInfo mdHavingGlobs = ordered.get(i);
List<Pattern> topicGlobs = mdHavingGlobs.getTopicGlobs();
// TODO track and warn if a glob doesn't match anything
for (int j = i+1; j < ordered.size(); j++) {
MarkdownInfo mdHavingTopics = ordered.get(j);
List<String> topics = mdHavingTopics.getTopics();
for (Pattern topicGlob : topicGlobs) {
for (String topic : topics) {
if (topicGlob.matcher(topic).matches()) {
assigning=assigning==null ? new ArrayList<>() : assigning;
assigning.add(topic);
logger.debug("added topic=" + topic + " to " + i + "->" + j + " with " + topicGlob);
}
}
if (assigning!=null) {
assigning.addAll(mdHavingGlobs.getTopics());
ordered.set(i,mdHavingGlobs.withTopics(assigning));
logger.debug("assigned new mdinfo");
}
}
}
loopsremaining--;
}
if (markdownWithTopicGlobs.size()>0) {
throw new RuntimeException("Non-terminal condition in markdown graph processing, unable to resolve all " +
"topic globs, " + markdownWithTopicGlobs.size() + " remaining: " + markdownWithTopicGlobs);
}
int loopsremaining=100;
// Assign glob topics to non-glob topics that match
@ -142,4 +173,28 @@ public class MarkdownDocs {
|| srcTopic.endsWith("$");
}
private static class Edge<T> {
private final int from;
private final int to;
private T edgeProps;
public Edge(int from, int to, Supplier<T> forT) {
this.from = from;
this.to = to;
edgeProps = forT.get();
}
public int from() {
return from;
}
public int to() {
return to;
}
public T props() {
return edgeProps;
}
}
}

View File

@ -96,4 +96,18 @@ public class ParsedFrontMatter implements FrontMatterInfo {
// TODO: allow functional version of this
// this.data.put(FrontMatterInfo.TOPICS,newTopics);
}
public ParsedFrontMatter withTopics(List<String> assigning) {
HashMap<String, List<String>> newmap = new HashMap<>();
newmap.putAll(this.data);
newmap.put(FrontMatterInfo.TOPICS,assigning);
return new ParsedFrontMatter(newmap);
}
@Override
public String toString() {
return "ParsedFrontMatter{" +
"data=" + data +
'}';
}
}

View File

@ -36,6 +36,11 @@ public class ParsedMarkdown implements MarkdownInfo, HasDiagnostics {
logger.debug("created " + this.toString());
}
private ParsedMarkdown(ParsedFrontMatter frontMatter, Content<?> content) {
this.frontMatter = frontMatter;
this.content = content;
}
@Override
public Path getPath() {
return content.asPath();
@ -82,4 +87,15 @@ public class ParsedMarkdown implements MarkdownInfo, HasDiagnostics {
public boolean hasAggregations() {
return getFrontmatter().getAggregations().size()>0;
}
@Override
public MarkdownInfo withTopics(List<String> assigning) {
return new ParsedMarkdown(frontMatter.withTopics(assigning), this.content);
}
@Override
public String toString() {
return "ParsedMarkdown/" +
frontMatter.toString();
}
}

View File

@ -39,4 +39,6 @@ public interface MarkdownInfo {
default List<Pattern> getAggregators() {
return getFrontmatter().getAggregations();
}
MarkdownInfo withTopics(List<String> assigning);
}

View File

@ -1,6 +1,7 @@
---
title: srctest-Entry 2-1-L
weight: 39
topics: srctest-entry-2-1-L
---
# Title Heading for srctest-Entry 2-1-L

View File

@ -0,0 +1,9 @@
---
title: srctest-having-glob
topics: srctest-entry-.+
weight: 39
---
# Title Heading for srctest-having-glob

View File

@ -0,0 +1,9 @@
---
title: srctest-having-globs-and-topics
topics: some-random-topic,srctest-entry-.+
weight: 39
---
# Title Heading for srctest-having-globs-and-topics