about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2017-02-20 09:30:42 -0500
committerSteve Klabnik <steve@steveklabnik.com>2017-02-20 11:17:53 -0500
commitfc7bf8498bc8f664fb4837d610204cb88c6103c0 (patch)
tree8312d361e16b940c8e1761fc5cbf04ac1ef6a83b
parentb4cd3d92061de90f58669d620867d42466a563ab (diff)
downloadrust-fc7bf8498bc8f664fb4837d610204cb88c6103c0.tar.gz
rust-fc7bf8498bc8f664fb4837d610204cb88c6103c0.zip
Enable linkchecker on books
Previously, mdBook used JavaScript to add header links, so we
skipped checking the book. As of
https://github.com/rust-lang/rust/pull/39966, it no longer does,
so we can start checking again.

There is a twist, though: it uses name instead of id, so let's test
for both. They're both valid links anyway, so it's good to have the
checker check anyway.
-rw-r--r--src/tools/linkchecker/main.rs24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/tools/linkchecker/main.rs b/src/tools/linkchecker/main.rs
index 304c722bbe5..ba5ca44526b 100644
--- a/src/tools/linkchecker/main.rs
+++ b/src/tools/linkchecker/main.rs
@@ -65,6 +65,7 @@ enum Redirect {
 struct FileEntry {
     source: String,
     ids: HashSet<String>,
+    names: HashSet<String>,
 }
 
 type Cache = HashMap<PathBuf, FileEntry>;
@@ -81,6 +82,15 @@ impl FileEntry {
             });
         }
     }
+
+    fn parse_names(&mut self, contents: &str) {
+        if self.names.is_empty() {
+            with_attrs_in_source(contents, " name", |fragment, _| {
+                let frag = fragment.trim_left_matches("#").to_owned();
+                self.names.insert(frag);
+            });
+        }
+    }
 }
 
 fn walk(cache: &mut Cache, root: &Path, dir: &Path, errors: &mut bool) {
@@ -139,6 +149,9 @@ fn check(cache: &mut Cache,
         cache.get_mut(&pretty_file)
              .unwrap()
              .parse_ids(&pretty_file, &contents, errors);
+        cache.get_mut(&pretty_file)
+             .unwrap()
+             .parse_names(&contents);
     }
 
     // Search for anything that's the regex 'href[ ]*=[ ]*".*?"'
@@ -209,13 +222,6 @@ fn check(cache: &mut Cache,
                 Err(LoadError::IsRedirect) => unreachable!(),
             };
 
-            // we don't check the book for fragments because they're added via JS
-            for book in ["book/", "nomicon/"].iter() {
-                if !pretty_path.to_str().unwrap().starts_with(book) {
-                    return;
-                }
-            }
-
             if let Some(ref fragment) = fragment {
                 // Fragments like `#1-6` are most likely line numbers to be
                 // interpreted by javascript, so we're ignoring these
@@ -226,8 +232,9 @@ fn check(cache: &mut Cache,
 
                 let entry = &mut cache.get_mut(&pretty_path).unwrap();
                 entry.parse_ids(&pretty_path, &contents, errors);
+                entry.parse_names(&contents);
 
-                if !entry.ids.contains(*fragment) {
+                if !(entry.ids.contains(*fragment) || entry.names.contains(*fragment)) {
                     *errors = true;
                     print!("{}:{}: broken link fragment  ",
                            pretty_file.display(),
@@ -277,6 +284,7 @@ fn load_file(cache: &mut Cache,
                 entry.insert(FileEntry {
                     source: contents.clone(),
                     ids: HashSet::new(),
+                    names: HashSet::new(),
                 });
             }
             maybe