Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ A repository adopts the standard in four steps:
- `scheduled-audit`: A workflow runs `just audit` (or `cargo deny`) on a `schedule` at least weekly: a cron whose day of the month and month are `*`.
- `pr-template`: `.github/pull_request_template.md` has the headings `## What and why` and `## Testing`.
- `docs-summary`: With a `book.toml`, every Markdown page under its `src` is linked from its `SUMMARY.md`, or included in a page with `{{#include …}}`.
- `docs-images`: Every image in a Markdown file, outside code blocks, has alt text: `![what it shows](…)`, or an `<img>` with a non-empty `alt`.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
57 changes: 57 additions & 0 deletions crates/sourceorigin-standard/src/rules/docs_images.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! `docs-images`: every image in the docs says what it shows.

use super::text_file::TextFile;
use super::{Finding, Repository};

impl TextFile {
/// The lines, from 1, outside fenced code blocks, without their inline
/// code spans.
pub(super) fn prose_lines(&self) -> impl Iterator<Item = (usize, String)> {
let mut fenced = false;
self.text
.lines()
.enumerate()
.filter_map(move |(index, line)| {
let fence =
line.trim_start().starts_with("```") || line.trim_start().starts_with("~~~");
if fence {
fenced = !fenced;
}
let prose = line.split('`').step_by(2).collect();
(!fence && !fenced).then_some((index + 1, prose))
})
}

/// The line of each image without alt text: `![](…)`, or an `<img>`
/// without a non-empty `alt`.
fn images_without_alt(&self) -> Vec<usize> {
self.prose_lines()
.filter(|(_, line)| {
line.contains("![](")
|| line.match_indices("<img").any(|(at, _)| {
let tag = line[at..].split('>').next().unwrap_or_default();
!tag.contains("alt=") || tag.contains("alt=\"\"") || tag.contains("alt=''")
})
})
.map(|(line, _)| line)
.collect()
}
}

impl Repository {
/// Each image in a Markdown file without alt text.
pub(super) fn docs_images(&self) -> Vec<Finding> {
self.markdown
.iter()
.flat_map(|file| {
file.images_without_alt().into_iter().map(|line| Finding {
path: file.path.clone(),
line,
message: "an image without alt text: say what it shows, as `![what it \
shows](…)` or `alt=\"what it shows\"`"
.to_owned(),
})
})
.collect()
}
}
4 changes: 4 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod clippy_config;
mod comments;
mod config_comments;
mod deny_config;
mod docs_images;
mod docs_summary;
mod fix;
mod generated_lints;
Expand Down Expand Up @@ -253,6 +254,9 @@ rules! {
/// With a `book.toml`, every Markdown page under its `src` is linked
/// from its `SUMMARY.md`, or included in a page with `{{#include …}}`.
DocsSummary = "docs-summary" => docs_summary,
/// Every image in a Markdown file, outside code blocks, has alt text:
/// `![what it shows](…)`, or an `<img>` with a non-empty `alt`.
DocsImages = "docs-images" => docs_images,
}

/// Why the rules couldn't be checked.
Expand Down
19 changes: 19 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/docs_images.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::{Fixture, INHERITS};
use crate::rules::Rule;

#[test]
fn every_image_says_what_it_shows() {
let fixture = Fixture::new();
fixture.add_crate("core", INHERITS).write(
"docs/guide.md",
"# Guide\n\n![The dashboard, listing two apps](dashboard.png)\n![](blank.png)\n\
<img src=\"a.png\" alt=\"The sign-in page\">\n<img src=\"b.png\">\n\nWrite `![](x.png)` with alt text.\n\n```md\n![](example.png)\n```\n",
);
let blank = |line: usize| {
format!(
"docs/guide.md:{line}: an image without alt text: say what it shows, as `![what it \
shows](…)` or `alt=\"what it shows\"` (docs-images)"
)
};
assert_eq!(fixture.violations(Rule::DocsImages), [blank(4), blank(6)]);
}
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod clippy_config;
mod comments;
mod config_comments;
mod deny_config;
mod docs_images;
mod docs_summary;
mod fix;
mod framework;
Expand Down