Skip to content

Commit 42e435c

Browse files
committed
Move rules handling to its own module
There should otherwise be no code changes here.
1 parent 7ce3563 commit 42e435c

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

mdbook-spec/src/lib.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ use std::collections::BTreeMap;
1111
use std::io;
1212
use std::path::PathBuf;
1313

14+
mod rules;
1415
mod std_links;
1516

16-
/// The Regex for rules like `r[foo]`.
17-
static RULE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^r\[([^]]+)]$").unwrap());
18-
1917
/// The Regex for the syntax for blockquotes that have a specific CSS class,
2018
/// like `> [!WARNING]`.
2119
static ADMONITION_RE: Lazy<Regex> = Lazy::new(|| {
@@ -57,41 +55,6 @@ impl Spec {
5755
}
5856
}
5957

60-
/// Converts lines that start with `r[…]` into a "rule" which has special
61-
/// styling and can be linked to.
62-
fn rule_definitions(
63-
&self,
64-
chapter: &Chapter,
65-
found_rules: &mut BTreeMap<String, (PathBuf, PathBuf)>,
66-
) -> String {
67-
let source_path = chapter.source_path.clone().unwrap_or_default();
68-
let path = chapter.path.clone().unwrap_or_default();
69-
RULE_RE
70-
.replace_all(&chapter.content, |caps: &Captures<'_>| {
71-
let rule_id = &caps[1];
72-
if let Some((old, _)) =
73-
found_rules.insert(rule_id.to_string(), (source_path.clone(), path.clone()))
74-
{
75-
let message = format!(
76-
"rule `{rule_id}` defined multiple times\n\
77-
First location: {old:?}\n\
78-
Second location: {source_path:?}"
79-
);
80-
if self.deny_warnings {
81-
panic!("error: {message}");
82-
} else {
83-
eprintln!("warning: {message}");
84-
}
85-
}
86-
format!(
87-
"<div class=\"rule\" id=\"r-{rule_id}\">\
88-
<a class=\"rule-link\" href=\"#r-{rule_id}\">[{rule_id}]</a>\
89-
</div>\n"
90-
)
91-
})
92-
.to_string()
93-
}
94-
9558
/// Generates link references to all rules on all pages, so you can easily
9659
/// refer to rules anywhere in the book.
9760
fn auto_link_references(

mdbook-spec/src/rules.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Handling for rule identifiers.
2+
3+
use crate::Spec;
4+
use mdbook::book::Chapter;
5+
use once_cell::sync::Lazy;
6+
use regex::{Captures, Regex};
7+
use std::collections::BTreeMap;
8+
use std::path::PathBuf;
9+
10+
/// The Regex for rules like `r[foo]`.
11+
static RULE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^r\[([^]]+)]$").unwrap());
12+
13+
impl Spec {
14+
/// Converts lines that start with `r[…]` into a "rule" which has special
15+
/// styling and can be linked to.
16+
pub fn rule_definitions(
17+
&self,
18+
chapter: &Chapter,
19+
found_rules: &mut BTreeMap<String, (PathBuf, PathBuf)>,
20+
) -> String {
21+
let source_path = chapter.source_path.clone().unwrap_or_default();
22+
let path = chapter.path.clone().unwrap_or_default();
23+
RULE_RE
24+
.replace_all(&chapter.content, |caps: &Captures<'_>| {
25+
let rule_id = &caps[1];
26+
if let Some((old, _)) =
27+
found_rules.insert(rule_id.to_string(), (source_path.clone(), path.clone()))
28+
{
29+
let message = format!(
30+
"rule `{rule_id}` defined multiple times\n\
31+
First location: {old:?}\n\
32+
Second location: {source_path:?}"
33+
);
34+
if self.deny_warnings {
35+
panic!("error: {message}");
36+
} else {
37+
eprintln!("warning: {message}");
38+
}
39+
}
40+
format!(
41+
"<div class=\"rule\" id=\"r-{rule_id}\">\
42+
<a class=\"rule-link\" href=\"#r-{rule_id}\">[{rule_id}]</a>\
43+
</div>\n"
44+
)
45+
})
46+
.to_string()
47+
}
48+
}

0 commit comments

Comments
 (0)