Skip to content

Commit 5817351

Browse files
committed
tidy: exempt URLs from the line length restriction
The length of a URL is usually not under our control, and Markdown provides no way to split a URL in the middle. Therefore, comment lines consisting _solely_ of a URL (possibly with a Markdown link label in front) should be exempt from the line-length restriction. Inline hyperlink destinations ( `[foo](http://...)` notation ) are _not_ exempt, because it is my arrogant opinion that long lines of that type make the source text illegible. The patch adds dependencies on the `regex` and `lazy_static` crates to the tidy utility. This _appears_ to Just Work, but if you would rather not have that dependency I am willing to provide a hand-written parser instead.
1 parent 10f6a5c commit 5817351

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/tools/tidy/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ version = "0.1.0"
44
authors = ["Alex Crichton <[email protected]>"]
55

66
[dependencies]
7+
regex = "*"
8+
lazy_static = "*"

src/tools/tidy/src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
//! etc. This is run by default on `make check` and as part of the auto
1515
//! builders.
1616
17+
extern crate regex;
18+
#[macro_use] extern crate lazy_static;
19+
1720
use std::fs;
1821
use std::path::{PathBuf, Path};
1922
use std::env;

src/tools/tidy/src/style.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use std::fs::File;
2626
use std::io::prelude::*;
2727
use std::path::Path;
2828

29+
use regex::Regex;
30+
2931
const COLS: usize = 100;
3032
const LICENSE: &'static str = "\
3133
Copyright <year> The Rust Project Developers. See the COPYRIGHT
@@ -38,6 +40,32 @@ http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3840
option. This file may not be copied, modified, or distributed
3941
except according to those terms.";
4042

43+
/// True if LINE is allowed to be longer than the normal limit.
44+
///
45+
/// Currently there is only one exception: if the line is within a
46+
/// comment, and its entire text is one URL (possibly with a Markdown
47+
/// link label in front), then it's allowed to be overlength. This is
48+
/// because Markdown offers no way to split a line in the middle of a
49+
/// URL, and the length of URLs for external references is beyond our
50+
/// control.
51+
fn long_line_is_ok(line: &str) -> bool {
52+
lazy_static! {
53+
static ref URL_RE: Regex = Regex::new(
54+
// This regexp uses the CommonMark definition of link
55+
// label. It thinks any sequence of nonwhitespace
56+
// characters beginning with "http://" or "https://" is a
57+
// URL. Add more schemas as necessary.
58+
r"^\s*//[!/]?\s+(?:\[(?:[^\]\\]|\\.){1,999}\]:\s+)?https?://\S+$"
59+
).unwrap();
60+
}
61+
62+
if URL_RE.is_match(line) {
63+
return true;
64+
}
65+
66+
false
67+
}
68+
4169
pub fn check(path: &Path, bad: &mut bool) {
4270
let mut contents = String::new();
4371
super::walk(path, &mut super::filter_dirs, &mut |file| {
@@ -61,8 +89,9 @@ pub fn check(path: &Path, bad: &mut bool) {
6189
println!("{}:{}: {}", file.display(), i + 1, msg);
6290
*bad = true;
6391
};
64-
if line.chars().count() > COLS && !skip_length {
65-
err(&format!("line longer than {} chars", COLS));
92+
if !skip_length && line.chars().count() > COLS
93+
&& !long_line_is_ok(line) {
94+
err(&format!("line longer than {} chars", COLS));
6695
}
6796
if line.contains("\t") && !skip_tab {
6897
err("tab character");

0 commit comments

Comments
 (0)