-
Notifications
You must be signed in to change notification settings - Fork 7
Add markdown style guide #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+233
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "style-check" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
pulldown-cmark = "0.10.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use std::env; | ||
use std::error::Error; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
macro_rules! style_error { | ||
($bad:expr, $path:expr, $($arg:tt)*) => { | ||
*$bad = true; | ||
eprint!("error in {}: ", $path.display()); | ||
eprintln!("{}", format_args!($($arg)*)); | ||
}; | ||
} | ||
|
||
fn main() { | ||
let arg = env::args().nth(1).unwrap_or_else(|| { | ||
eprintln!("Please pass a src directory as the first argument"); | ||
std::process::exit(1); | ||
}); | ||
|
||
let mut bad = false; | ||
if let Err(e) = check_directory(&Path::new(&arg), &mut bad) { | ||
eprintln!("error: {}", e); | ||
std::process::exit(1); | ||
} | ||
if bad { | ||
eprintln!("some style checks failed"); | ||
std::process::exit(1); | ||
} | ||
eprintln!("passed!"); | ||
} | ||
|
||
fn check_directory(dir: &Path, bad: &mut bool) -> Result<(), Box<dyn Error>> { | ||
for entry in fs::read_dir(dir)? { | ||
let entry = entry?; | ||
let path = entry.path(); | ||
|
||
if path.is_dir() { | ||
check_directory(&path, bad)?; | ||
continue; | ||
} | ||
|
||
if !matches!( | ||
path.extension().and_then(|p| p.to_str()), | ||
Some("md") | Some("html") | ||
) { | ||
// This may be extended in the future if other file types are needed. | ||
style_error!(bad, path, "expected only md or html in src"); | ||
} | ||
|
||
let contents = fs::read_to_string(&path)?; | ||
if contents.contains("#![feature") { | ||
style_error!(bad, path, "#![feature] attributes are not allowed"); | ||
} | ||
if !cfg!(windows) && contents.contains('\r') { | ||
style_error!( | ||
bad, | ||
path, | ||
"CR characters not allowed, must use LF line endings" | ||
); | ||
} | ||
if contents.contains('\t') { | ||
style_error!(bad, path, "tab characters not allowed, use spaces"); | ||
} | ||
if contents.contains('\u{2013}') { | ||
style_error!(bad, path, "en-dash not allowed, use two dashes like --"); | ||
} | ||
if contents.contains('\u{2014}') { | ||
style_error!(bad, path, "em-dash not allowed, use three dashes like ---"); | ||
} | ||
if !contents.ends_with('\n') { | ||
style_error!(bad, path, "file must end with a newline"); | ||
} | ||
for line in contents.lines() { | ||
if line.ends_with(' ') { | ||
style_error!(bad, path, "lines must not end with spaces"); | ||
} | ||
} | ||
cmark_check(&path, bad, &contents)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn cmark_check(path: &Path, bad: &mut bool, contents: &str) -> Result<(), Box<dyn Error>> { | ||
use pulldown_cmark::{BrokenLink, CodeBlockKind, Event, Options, Parser, Tag}; | ||
|
||
macro_rules! cmark_error { | ||
($bad:expr, $path:expr, $range:expr, $($arg:tt)*) => { | ||
*$bad = true; | ||
let lineno = contents[..$range.start].chars().filter(|&ch| ch == '\n').count() + 1; | ||
eprint!("error in {} (line {}): ", $path.display(), lineno); | ||
eprintln!("{}", format_args!($($arg)*)); | ||
} | ||
} | ||
|
||
let options = Options::all(); | ||
// Can't use `bad` because it would get captured in closure. | ||
let mut link_err = false; | ||
let mut cb = |link: BrokenLink<'_>| { | ||
cmark_error!( | ||
&mut link_err, | ||
path, | ||
link.span, | ||
"broken {:?} link (reference `{}`)", | ||
link.link_type, | ||
link.reference | ||
); | ||
None | ||
}; | ||
let parser = Parser::new_with_broken_link_callback(contents, options, Some(&mut cb)); | ||
|
||
for (event, range) in parser.into_offset_iter() { | ||
match event { | ||
Event::Start(Tag::CodeBlock(CodeBlockKind::Indented)) => { | ||
cmark_error!( | ||
bad, | ||
path, | ||
range, | ||
"indented code blocks should use triple backtick-style \ | ||
with a language identifier" | ||
); | ||
} | ||
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(languages))) => { | ||
if languages.is_empty() { | ||
cmark_error!( | ||
bad, | ||
path, | ||
range, | ||
"code block should include an explicit language", | ||
); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
*bad |= link_err; | ||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the outcome of last week's discussion about semantic line breaks?
My memory of the consensus was that we were not going to impose/enforce semantic line breaks.
However, what I think was left up in the air was the question of whether we would 1. accept material that has been authored with a semantic line break style, and also would 2. make a best effort attempt to retain any such pre-existing structure as changes are suggested to the text.
(For the time being, I'm going to author my own draft text assuming the latter is the case. But if we decide that is not a tenable position, then we should say so in this authoring.md text.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there was a very explicit outcome.
If there is a PR that had linebreaks after sentences, I would not comment on it.
If there is a PR that wrapped at some arbitrary column width, I would not want to accept that.
Perhaps a different way to state this is "Do not wrap long lines based on column widths"? Do you have a suggestion on what you would like it to say?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I don't want to block this further. I agree with Eric that there wasn't a very explicit outcome. And the text that is proposed here is consistent with that status. We can refine it later.)