Skip to content

Commit

Permalink
T257141: Builder for internal links + T267474: Trailing characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas authored and Nicolas committed Nov 15, 2020
1 parent 7aad8b6 commit f69c41d
Show file tree
Hide file tree
Showing 21 changed files with 459 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import org.wikipediacleaner.api.constants.ArticleUrl;
import org.wikipediacleaner.api.data.Namespace;
import org.wikipediacleaner.api.data.PageElementExternalLink;
import org.wikipediacleaner.api.data.PageElementInternalLink;
import org.wikipediacleaner.api.data.PageElementTag;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;


/**
Expand Down Expand Up @@ -108,7 +108,7 @@ public boolean analyze(
automatic = false;
}
errorResult.addReplacement(
PageElementInternalLink.createInternalLink(article, internalText),
ContentsInternalLinkBuilder.from(article).withText(internalText).toString(),
automatic);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.wikipediacleaner.api.data.Namespace;
import org.wikipediacleaner.api.data.PageElementInternalLink;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;


/**
Expand Down Expand Up @@ -136,7 +137,7 @@ public boolean analyze(
analysis, link.getBeginIndex(), link.getEndIndex());
boolean emptyLink = false;
if ((link.getFullLink() == null) || (link.getFullLink().trim().length() == 0)) {
errorResult.addReplacement(PageElementInternalLink.createInternalLink(link.getText(), null));
errorResult.addReplacement(ContentsInternalLinkBuilder.from(link.getText()).toString());
emptyLink = true;
}
for (String replacement : replacements) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.wikipediacleaner.api.data.PageElementTitle;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ContentsUtil;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;
import org.wikipediacleaner.gui.swing.component.MWPane;
import org.wikipediacleaner.i18n.GT;

Expand Down Expand Up @@ -168,9 +169,10 @@ private boolean analyzeInternalLink(
(link.getText().trim().length() == 0)) {
automatic = false;
}
String replacement = PageElementInternalLink.createInternalLink(
"#" + anchor,
link.getDisplayedTextNotTrimmed());
String replacement = ContentsInternalLinkBuilder.from(null)
.withAnchor(anchor)
.withText(link.getDisplayedTextNotTrimmed())
.toString();
errorResult.addReplacement(replacement, automatic);
}
errors.add(errorResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.wikipediacleaner.api.data.PageElementTag;
import org.wikipediacleaner.api.data.PageElementTemplate;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;
import org.wikipediacleaner.gui.swing.component.MWPane;
import org.wikipediacleaner.i18n.GT;

Expand Down Expand Up @@ -231,42 +232,42 @@ public boolean analyze(
if (!automatic) {
addReplacement(
config, errorResult, replacements,
paddingLeft + PageElementInternalLink.createInternalLink(text, null) + paddingRight + extraRight,
paddingLeft + ContentsInternalLinkBuilder.from(text).toString() + paddingRight + extraRight,
prefix, suffix, false);
}
addReplacement(
config, errorResult, replacements,
paddingLeft + PageElementInternalLink.createInternalLink(text, null) + paddingRight + extraFullRight,
paddingLeft + ContentsInternalLinkBuilder.from(text).toString() + paddingRight + extraFullRight,
prefix, suffix, automatic);
addReplacement(
config, errorResult, replacements,
paddingLeft + PageElementInternalLink.createInternalLink(text, null) + paddingRight + extraRight,
paddingLeft + ContentsInternalLinkBuilder.from(text).toString() + paddingRight + extraRight,
prefix, suffix, false);
addReplacement(
config, errorResult, replacements,
paddingLeft + PageElementInternalLink.createInternalLink(cleanedText, null) + paddingRight + extraFullRight,
paddingLeft + ContentsInternalLinkBuilder.from(cleanedText).toString() + paddingRight + extraFullRight,
prefix, suffix, false);
addReplacement(
config, errorResult, replacements,
paddingLeft + PageElementInternalLink.createInternalLink(cleanedText, null) + paddingRight + extraRight,
paddingLeft + ContentsInternalLinkBuilder.from(cleanedText).toString() + paddingRight + extraRight,
prefix, suffix, false);
paddingLeft = paddingLeft.replaceAll("\'", "");
paddingRight = paddingRight.replaceAll("\'", "");
addReplacement(
config, errorResult, replacements,
paddingLeft + PageElementInternalLink.createInternalLink(text, null) + paddingRight + extraFullRight,
paddingLeft + ContentsInternalLinkBuilder.from(text).toString() + paddingRight + extraFullRight,
prefix, suffix, false);
addReplacement(
config, errorResult, replacements,
paddingLeft + PageElementInternalLink.createInternalLink(text, null) + paddingRight + extraRight,
paddingLeft + ContentsInternalLinkBuilder.from(text).toString() + paddingRight + extraRight,
prefix, suffix, false);
addReplacement(
config, errorResult, replacements,
paddingLeft + PageElementInternalLink.createInternalLink(cleanedText, null) + paddingRight + extraFullRight,
paddingLeft + ContentsInternalLinkBuilder.from(cleanedText).toString() + paddingRight + extraFullRight,
prefix, suffix, false);
addReplacement(
config, errorResult, replacements,
paddingLeft + PageElementInternalLink.createInternalLink(cleanedText, null) + paddingRight + extraRight,
paddingLeft + ContentsInternalLinkBuilder.from(cleanedText).toString() + paddingRight + extraRight,
prefix, suffix, false);
errors.add(errorResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.wikipediacleaner.api.configuration.WPCConfiguration;
import org.wikipediacleaner.api.constants.EnumWikipedia;
import org.wikipediacleaner.api.data.Interwiki;
import org.wikipediacleaner.api.data.PageElementInternalLink;
import org.wikipediacleaner.api.data.PageElementInterwikiLink;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;
import org.wikipediacleaner.gui.swing.action.ActionExternalViewer;
import org.wikipediacleaner.gui.swing.basic.Utilities;
import org.wikipediacleaner.gui.swing.component.MWPane;
Expand Down Expand Up @@ -294,15 +294,15 @@ public String fix(String fixName, PageAnalysis analysis, MWPane textPane) {
// List possible replacements
List<String> possibleValues = new ArrayList<String>();
String possible = null;
possible = PageElementInternalLink.createInternalLink(toTitle, link.getText());
possible = ContentsInternalLinkBuilder.from(toTitle).withText(link.getText()).toString();
if (!possibleValues.contains(possible)) {
possibleValues.add(possible);
}
possible = PageElementInternalLink.createInternalLink(toTitle, link.getFullLink());
possible = ContentsInternalLinkBuilder.from(toTitle).withText(link.getFullLink()).toString();
if (!possibleValues.contains(possible)) {
possibleValues.add(possible);
}
possible = PageElementInternalLink.createInternalLink(toTitle, null);
possible = ContentsInternalLinkBuilder.from(toTitle).toString();
if (!possibleValues.contains(possible)) {
possibleValues.add(possible);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.wikipediacleaner.api.data.PageElementExternalLink;
import org.wikipediacleaner.api.data.PageElementInternalLink;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;
import org.wikipediacleaner.gui.swing.component.MWPane;
import org.wikipediacleaner.i18n.GT;

Expand Down Expand Up @@ -62,9 +63,8 @@ public boolean analyze(
CheckErrorResult errorResult = createCheckErrorResult(
analysis, link.getBeginIndex(), link.getEndIndex());
errorResult.addReplacement(
PageElementInternalLink.createInternalLink(
link.getFullLink().replaceAll("\\%20", " "),
link.getText()),
ContentsInternalLinkBuilder.from(link.getFullLink().replaceAll("\\%20", " "))
.withText(link.getText()).toString(),
GT._T("Replace %20 by space character"));
errors.add(errorResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.wikipediacleaner.api.data.PageElementTag;
import org.wikipediacleaner.api.data.PageElementImage.Parameter;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;
import org.wikipediacleaner.api.data.PageElementInternalLink;
import org.wikipediacleaner.api.data.PageElementTemplate;
import org.wikipediacleaner.api.data.SpecialPage;
Expand Down Expand Up @@ -678,8 +679,7 @@ private boolean reportLinkWithoutText(AnalysisInformation info) {
CheckErrorResult errorResult = createCheckErrorResult(
info.analysis, tmpIndex - info.article.length(), info.endIndex);
errorResult.addReplacement(
PageElementInternalLink.createInternalLink(
(info.needColon ? ":" : "") + info.articleUrl.getTitleAndFragment(), textBefore),
ContentsInternalLinkBuilder.from(info.articleUrl.getTitleAndFragment()).withColon(info.needColon).withText(textBefore).toString(),
info.automatic);
info.errors.add(errorResult);
return true;
Expand Down Expand Up @@ -709,8 +709,7 @@ private boolean reportLinkWithoutText(AnalysisInformation info) {
private void addBasicReplacement(
AnalysisInformation info, CheckErrorResult errorResult) {
errorResult.addReplacement(
PageElementInternalLink.createInternalLink(
(info.needColon ? ":" : "") + info.articleUrl.getTitleAndFragment(), info.text),
ContentsInternalLinkBuilder.from(info.articleUrl.getTitleAndFragment()).withColon(info.needColon).withText(info.text).toString(),
info.automatic);
if (info.text != null) {
errorResult.addReplacement(info.text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.wikipediacleaner.api.data.PageElementInternalLink;
import org.wikipediacleaner.api.data.PageElementTag;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;


/**
Expand Down Expand Up @@ -91,11 +92,12 @@ public boolean analyze(
}
CheckErrorResult errorResult = createCheckErrorResult(
analysis, link.getBeginIndex(), link.getEndIndex());
String replacement = PageElementInternalLink.createInternalLink(
target,
target.substring(beginIndex, endIndex));
String replacement = ContentsInternalLinkBuilder
.from(target)
.withText(target.substring(beginIndex, endIndex))
.toString();
errorResult.addReplacement(replacement);
replacement = PageElementInternalLink.createInternalLink(target, null);
replacement = ContentsInternalLinkBuilder.from(target).toString();
errorResult.addReplacement(replacement);
errors.add(errorResult);
}
Expand All @@ -112,10 +114,9 @@ public boolean analyze(
errorFound = true;
CheckErrorResult errorResult = createCheckErrorResult(
analysis, link.getBeginIndex(), link.getEndIndex());
String replacement = PageElementInternalLink.createInternalLink(
target + "/", null);
String replacement = ContentsInternalLinkBuilder.from(target + "/").toString();
errorResult.addReplacement(replacement);
replacement = PageElementInternalLink.createInternalLink(target, null);
replacement = ContentsInternalLinkBuilder.from(target).toString();
errorResult.addReplacement(replacement);
errors.add(errorResult);
}
Expand All @@ -132,10 +133,12 @@ public boolean analyze(
errorFound = true;
CheckErrorResult errorResult = createCheckErrorResult(
analysis, link.getBeginIndex(), link.getEndIndex());
String replacement = PageElementInternalLink.createInternalLink(
target, target.substring(index + 1));
String replacement = ContentsInternalLinkBuilder
.from(target)
.withText(target.substring(index + 1))
.toString();
errorResult.addReplacement(replacement);
replacement = PageElementInternalLink.createInternalLink(target, null);
replacement = ContentsInternalLinkBuilder.from(target).toString();
errorResult.addReplacement(replacement);
errors.add(errorResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.wikipediacleaner.api.data.PageElementTag;
import org.wikipediacleaner.api.data.PageElementTemplate;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;
import org.wikipediacleaner.i18n.GT;
import org.wikipediacleaner.utils.Configuration;
import org.wikipediacleaner.utils.ConfigurationValueInteger;
Expand Down Expand Up @@ -245,11 +246,12 @@ public boolean analyzeFullTag(
if (link != null) {
String displayed = link.getDisplayedTextNotTrimmed();
if (displayed.endsWith(" ")) {
errorResult.addReplacement(PageElementInternalLink.createInternalLink(
link.getFullLink(), displayed.trim()) + " " + textAfter);
errorResult.addReplacement(
ContentsInternalLinkBuilder.from(link.getFullLink()).withText(displayed.trim()).toString() +
" " + textAfter);
}
errorResult.addReplacement(PageElementInternalLink.createInternalLink(
link.getFullLink(), displayed + textAfter));
errorResult.addReplacement(ContentsInternalLinkBuilder.from(link.getFullLink())
.withText(displayed + textAfter).toString());
}
errorResult.addReplacement(textBefore + " " + textAfter);
errorResult.addReplacement(textBefore + textAfter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.wikipediacleaner.api.data.Page;
import org.wikipediacleaner.api.data.PageElementInternalLink;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;
import org.wikipediacleaner.i18n.GT;
import org.wikipediacleaner.utils.Configuration;
import org.wikipediacleaner.utils.ConfigurationValueInteger;
Expand Down Expand Up @@ -167,17 +168,19 @@ private boolean analyzeInternalLink(
int yearCorrected = Integer.parseInt(text + extraText);
if (yearLinked == yearCorrected) {
errorResult.addReplacement(
PageElementInternalLink.createInternalLink(target, text + extraText),
ContentsInternalLinkBuilder.from(target).withText(text + extraText).toString(),
true);
}
} catch (NumberFormatException e) {
// Nothing to do, it's just not an integer
}
}
errorResult.addReplacement(
PageElementInternalLink.createInternalLink(target, target) + extraText);
ContentsInternalLinkBuilder.from(target).withText(target).toString() +
extraText);
errorResult.addReplacement(
PageElementInternalLink.createInternalLink(text, text) + extraText);
ContentsInternalLinkBuilder.from(text).withText(text).toString() +
extraText);
if (!askHelpList.isEmpty()) {
boolean firstReplacement = true;
for (String askHelpElement : askHelpList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.wikipediacleaner.api.data.PageElementInterwikiLink;
import org.wikipediacleaner.api.data.PageElementTag;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;


/**
Expand Down Expand Up @@ -127,9 +128,7 @@ public boolean analyze(
contents.substring(valueEndIndex, fontTag.getValueEndIndex());
String text =
contents.substring(fontTag.getValueBeginIndex(), valueBeginIndex) +
PageElementInternalLink.createInternalLink(
iLink.getLink(),
openFont + "..." + closeFont) +
ContentsInternalLinkBuilder.from(iLink.getLink()).withText(openFont + "..." + closeFont).toString() +
contents.substring(valueEndIndex, fontTag.getValueEndIndex());
errorResult.addReplacement(replacement, text, automatic);
errors.add(errorResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.wikipediacleaner.api.data.PageElementInternalLink;
import org.wikipediacleaner.api.data.PageElementTag;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;


/**
Expand Down Expand Up @@ -211,7 +212,7 @@ private boolean analyzeInternalLinks(

// Suggestion to group the links
errorResult.addReplacement(
PageElementInternalLink.createInternalLink(fullLink, buffer.toString()),
ContentsInternalLinkBuilder.from(fullLink).withText(buffer.toString()).toString(),
automatic);

// Suggestion to keep only the text for the last link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.wikipediacleaner.api.data.PageElementInternalLink;
import org.wikipediacleaner.api.data.PageElementTag;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.ilink.ContentsInternalLinkBuilder;
import org.wikipediacleaner.i18n.GT;


Expand Down Expand Up @@ -142,7 +143,7 @@ public boolean analyzeTag(
String fullLink = link.getFullLink();
String text = link.getDisplayedText() + extraText;
String replacement =
PageElementInternalLink.createInternalLink(fullLink, text) +
ContentsInternalLinkBuilder.from(fullLink).withText(text).toString() +
contents.substring(endText, endIndex);
boolean safeLink = isSafeLink(link, text);
errorResult.addReplacement(replacement, automatic || safeLink);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
public class PageElementInternalLink extends PageElement {

/** Characters included in the link when they follow the link */
public static final String EXTENSION_CHARACTERS =
"abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

private final String linkNotTrimmed;
private final String link;
private final String anchorNotTrimmed;
Expand Down Expand Up @@ -336,17 +341,6 @@ public String toString() {
return createInternalLink(linkNotTrimmed, anchorNotTrimmed, textNotTrimmed);
}

/**
* Create an internal link.
*
* @param link Link.
* @param text Displayed text.
* @return Internal link.
*/
public static String createInternalLink(String link, String text) {
return createInternalLink(link, null, text);
}

/**
* Create an internal link.
*
Expand Down
Loading

0 comments on commit f69c41d

Please sign in to comment.