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 @Override
public FrontMatterInfo getFrontmatter() { public FrontMatterInfo getFrontmatter() {
// calculate included topics return elements.get(0).getFrontmatter();
return null;
} }
@Override @Override
public boolean hasAggregations() { public boolean hasAggregations() {
// was true, but now it is false after compositing
return false; 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) { public <T extends MarkdownInfo> CompositeMarkdownInfo add(T element) {
elements.add(element); elements.add(element);
return this; return this;

View File

@ -10,6 +10,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.util.*; import java.util.*;
import java.util.function.Supplier;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -55,24 +56,54 @@ public class MarkdownDocs {
List<? extends MarkdownInfo> markdownWithTopicGlobs = List<? extends MarkdownInfo> markdownWithTopicGlobs =
ListSplitterWhyDoesJavaNotDoThisAlready.partition(markdownInfos, MarkdownInfo::hasTopicGlobs); ListSplitterWhyDoesJavaNotDoThisAlready.partition(markdownInfos, MarkdownInfo::hasTopicGlobs);
List<? extends MarkdownInfo> markdownWithOnlyTopicGlobs =
ListSplitterWhyDoesJavaNotDoThisAlready.partition(markdownWithTopicGlobs, m -> m.getTopics().size()==0);
int loopsremaining=100; List<MarkdownInfo> ordered = new ArrayList<>();
// 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) { // At this point, we have three set of markdown infos
for (MarkdownInfo markdownWithTopicGlob : markdownWithTopicGlobs) { // a) with only globs
markdownWithTopicGlob.getTopicGlobs(); // b) with globs and literals
for (MarkdownInfo allInfo : markdownInfos) { // c) with only literals
// allInfo.getTopics() // 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 // Assign glob topics to non-glob topics that match
@ -142,4 +173,28 @@ public class MarkdownDocs {
|| srcTopic.endsWith("$"); || 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 // TODO: allow functional version of this
// this.data.put(FrontMatterInfo.TOPICS,newTopics); // 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()); logger.debug("created " + this.toString());
} }
private ParsedMarkdown(ParsedFrontMatter frontMatter, Content<?> content) {
this.frontMatter = frontMatter;
this.content = content;
}
@Override @Override
public Path getPath() { public Path getPath() {
return content.asPath(); return content.asPath();
@ -82,4 +87,15 @@ public class ParsedMarkdown implements MarkdownInfo, HasDiagnostics {
public boolean hasAggregations() { public boolean hasAggregations() {
return getFrontmatter().getAggregations().size()>0; 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() { default List<Pattern> getAggregators() {
return getFrontmatter().getAggregations(); return getFrontmatter().getAggregations();
} }
MarkdownInfo withTopics(List<String> assigning);
} }

View File

@ -1,6 +1,7 @@
--- ---
title: srctest-Entry 2-1-L title: srctest-Entry 2-1-L
weight: 39 weight: 39
topics: srctest-entry-2-1-L
--- ---
# Title Heading for 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