Skip to content

Commit

Permalink
Merge pull request #54 from emielbeinema/show-redirects
Browse files Browse the repository at this point in the history
Add flag to show redirected links
  • Loading branch information
filiph committed Aug 25, 2020
2 parents e63a470 + b757fcd commit 4cc296c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
40 changes: 36 additions & 4 deletions lib/linkcheck.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ const externalFlag = "external";
const helpFlag = "help";
const hostsFlag = "hosts";
const inputFlag = "input-file";
const redirectFlag = "show-redirects";
const skipFlag = "skip-file";
const version = "2.0.13";
const versionFlag = "version";
final _portOnlyRegExp = RegExp(r"^:\d+$");

void printStats(CrawlResult result, int broken, int withWarning, int withInfo,
bool ansiTerm, Stdout stdout) {
int withRedirect, bool showRedirects, bool ansiTerm, Stdout stdout) {
// Redirect printing for better testing.
void print(Object object) => stdout.writeln(object);

Expand Down Expand Up @@ -74,6 +75,11 @@ void printStats(CrawlResult result, int broken, int withWarning, int withInfo,
.lightGray()
.text(ignored > 0 ? ' ($ignored ignored)' : '')
.normal()
.text(showRedirects && withRedirect > 0
? withRedirect == 1
? ', 1 has redirect(s)'
: ', $withRedirect have redirect(s)'
: '')
.text(".")
.print();
} else if (broken == 0 && withWarning == 0) {
Expand All @@ -86,6 +92,11 @@ void printStats(CrawlResult result, int broken, int withWarning, int withInfo,
.text(ignored > 0 ? ' ($ignored ignored)' : '')
.normal()
.text(", ")
.text(showRedirects && withRedirect > 0
? withRedirect == 1
? '1 has redirect(s), '
: '$withRedirect have redirect(s), '
: '')
.text("0 have warnings or errors")
.text(withInfo > 0 ? ', $withInfo have info' : '')
.text(".")
Expand All @@ -100,6 +111,11 @@ void printStats(CrawlResult result, int broken, int withWarning, int withInfo,
.text(ignored > 0 ? ' ($ignored ignored)' : '')
.normal()
.text(", ")
.text(showRedirects && withRedirect > 0
? withRedirect == 1
? '1 has redirect(s), '
: '$withRedirect have redirect(s), '
: '')
.text(withWarning == 1
? "1 has a warning"
: "$withWarning have warnings")
Expand All @@ -116,6 +132,11 @@ void printStats(CrawlResult result, int broken, int withWarning, int withInfo,
.text(ignored > 0 ? ' ($ignored ignored)' : '')
.normal()
.text(", ")
.text(showRedirects && withRedirect > 0
? withRedirect == 1
? '1 has redirect(s), '
: '$withRedirect have redirect(s), '
: '')
.text(broken == 1 ? "1 has error(s), " : "$broken have errors, ")
.text(withWarning == 1
? "1 has warning(s)"
Expand All @@ -127,6 +148,9 @@ void printStats(CrawlResult result, int broken, int withWarning, int withInfo,
} else {
print("\nStats:");
print("${links.length.toString().padLeft(8)} links");
if (showRedirects) {
print("${withRedirect.toString().padLeft(8)} redirects");
}
print("${checked.toString().padLeft(8)} destination URLs");
print("${ignored.toString().padLeft(8)} URLs ignored");
print("${withWarning.toString().padLeft(8)} warnings");
Expand All @@ -151,6 +175,8 @@ Future<int> run(List<String> arguments, Stdout stdout) async {
negatable: false,
help: "Check external (remote) links, too. By "
"default, the tool only checks internal links.")
..addFlag(redirectFlag,
help: "Also report all links that point at a redirected URL.")
..addSeparator("Advanced")
..addOption(inputFlag,
abbr: 'i',
Expand Down Expand Up @@ -193,6 +219,7 @@ Future<int> run(List<String> arguments, Stdout stdout) async {
argResults[connectionFailuresAsWarnings] == true;
bool verbose = argResults[debugFlag] == true;
bool shouldCheckExternal = argResults[externalFlag] == true;
bool showRedirects = argResults[redirectFlag] == true;
String inputFile = argResults[inputFlag] as String;
String skipFile = argResults[skipFlag] as String;

Expand Down Expand Up @@ -264,18 +291,23 @@ Future<int> run(List<String> arguments, Stdout stdout) async {

var withInfo = result.links.where((link) => link.hasInfo).length;

var withRedirects =
result.links.where((link) => link.destination.isRedirected).length;

if (broken == 0 && withWarning == 0 && withInfo == 0) {
printStats(result, broken, withWarning, withInfo, ansiTerm, stdout);
printStats(result, broken, withWarning, withInfo, withRedirects,
showRedirects, ansiTerm, stdout);
} else {
if (ansiTerm) {
Console.write("\r");
Console.eraseLine(3);
print("Done crawling. ");
}

reportForWriters(result, ansiTerm, stdout);
reportForWriters(result, ansiTerm, showRedirects, stdout);

printStats(result, broken, withWarning, withInfo, ansiTerm, stdout);
printStats(result, broken, withWarning, withInfo, withRedirects,
showRedirects, ansiTerm, stdout);
}
print("");

Expand Down
7 changes: 5 additions & 2 deletions lib/src/writer_report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import 'destination.dart';

/// Writes the reports from the perspective of a website writer - which pages
/// reference broken links.
void reportForWriters(CrawlResult result, bool ansiTerm, Stdout stdout) {
void reportForWriters(
CrawlResult result, bool ansiTerm, bool showRedirects, Stdout stdout) {
void print(Object message) => stdout.writeln(message);

print("");
Expand All @@ -23,7 +24,9 @@ void reportForWriters(CrawlResult result, bool ansiTerm, Stdout stdout) {
!link.wasSkipped &&
(link.destination.isInvalid ||
link.destination.wasTried &&
(link.destination.isBroken || link.hasWarning)))
(link.destination.isBroken ||
link.hasWarning ||
link.destination.isRedirected)))
.toList(growable: false);

List<Destination> deniedByRobots = result.destinations
Expand Down

0 comments on commit 4cc296c

Please sign in to comment.