Skip to content

Commit

Permalink
Resolve include correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmanusta committed Jan 8, 2024
1 parent e837641 commit dfdb275
Showing 1 changed file with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class XrefIncludeProcessor extends IncludeProcessor {
private static final Pattern NEWLINE_RX = Pattern.compile("\\r\\n?|\\n");
private static final Pattern TAG_DIRECTIVE_RX = Pattern.compile("\\b(?:tag|(e)nd)::(\\S+?)\\[\\](?=$|[ \\r])", Pattern.MULTILINE);

record FilterContent(String content, Integer lineNo){}

private HttpClient httpClient = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
Expand Down Expand Up @@ -62,8 +64,7 @@ public void process(Document document, PreprocessorReader reader, String target,
}
}

String readed = reader.read();
int startLineNumber = reader.getLineNumber();
int startLineNumber = 1;

Map<String, List<RefProps>> xrefMap = XrefHelper.parseXrefs(targetString, content);

Expand All @@ -72,19 +73,18 @@ public void process(Document document, PreprocessorReader reader, String target,

List<Integer> lineNums = getLines(attributes);
if (!lineNums.isEmpty()) {
Object[] tuple = filterLinesByLineNumbers(content, lineNums);
content = (String) tuple[0];
startLineNumber = (int) tuple[1];
FilterContent tuple = filterLinesByLineNumbers(content, lineNums);
content = tuple.content();
startLineNumber = tuple.lineNo();
} else {
Map<String, Boolean> tags = getTags(attributes);
if (!tags.isEmpty()) {
Object[] tuple = filterLinesByTags(content, target, tags);
content = (String) tuple[0];
startLineNumber = (int) tuple[1];
FilterContent tuple = filterLinesByTags(content, target, tags);
content = tuple.content();
startLineNumber = tuple.lineNo();
}
}

reader.pushInclude(readed, target, targetString, startLineNumber, attributes);
reader.pushInclude(content, target, targetString, startLineNumber, attributes);
}

Expand Down Expand Up @@ -195,9 +195,9 @@ private Path resolveTargetPath(Document document, Path dirPath, String target) {
return resolvedPath;
}

public Object[] filterLinesByLineNumbers(String fileContent, List<Integer> linenums) {
public FilterContent filterLinesByLineNumbers(String fileContent, List<Integer> linenums) {
int lineNum = 0;
int startLineNum = 0;
Integer startLineNum = null;
boolean selectRest = false;
List<String> lines = new ArrayList<>();

Expand All @@ -206,13 +206,13 @@ public Object[] filterLinesByLineNumbers(String fileContent, List<Integer> linen
lineNum++;

if (selectRest || (selectRest = linenums.get(0) == Integer.MAX_VALUE)) {
if (startLineNum == 0) {
if (Objects.isNull(startLineNum)) {
startLineNum = lineNum;
}
lines.add(line);
} else {
if (linenums.get(0) == lineNum) {
if (startLineNum == 0) {
if (Objects.isNull(startLineNum)) {
startLineNum = lineNum;
}
linenums.removeFirst();
Expand All @@ -224,10 +224,12 @@ public Object[] filterLinesByLineNumbers(String fileContent, List<Integer> linen
}
}

return new Object[]{String.join(System.lineSeparator(), lines), startLineNum};
String content = String.join(System.lineSeparator(), lines);
Integer lineNo = Objects.requireNonNullElse(startLineNum, 1);
return new FilterContent(content, lineNo);
}

public Object[] filterLinesByTags(String fileContent, String target, Map<String, Boolean> tags) {
public FilterContent filterLinesByTags(String fileContent, String target, Map<String, Boolean> tags) {
Boolean selectingDefault, selecting, wildcard = null;
Boolean globstar = tags.get("**");
Boolean star = tags.get("*");
Expand Down Expand Up @@ -263,7 +265,7 @@ public Object[] filterLinesByTags(String fileContent, String target, Map<String,
List<String> foundTags = new ArrayList<>();
String activeTag = null;
int lineNum = 0;
int startLineNum = 0;
Integer startLineNum = null;

for (String line : fileContent.split(NEWLINE_RX.pattern())) {
lineNum++;
Expand Down Expand Up @@ -296,7 +298,7 @@ public Object[] filterLinesByTags(String fileContent, String target, Map<String,
tagStack.add(0, new String[]{activeTag = thisTag, String.valueOf(selecting), String.valueOf(lineNum)});
}
} else if (selecting) {
if (startLineNum == 0) {
if (Objects.isNull(startLineNum)) {
startLineNum = lineNum;
}
lines.add(line);
Expand All @@ -319,7 +321,9 @@ public Object[] filterLinesByTags(String fileContent, String target, Map<String,
tags.size() > 1 ? "s" : "", String.join(", ", tags.keySet()), target);
}

return new Object[]{String.join(System.lineSeparator(), lines), startLineNum > 0 ? startLineNum : 1};
String content = String.join(System.lineSeparator(), lines);
Integer lineNo = Objects.requireNonNullElse(startLineNum, 1);
return new FilterContent(content, lineNo);
}

private boolean isHttpOrHttps(String url) {
Expand Down

0 comments on commit dfdb275

Please sign in to comment.