-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Formatting chars can be escaped with
\
It's a breaking change. It's possible to compile without the "escaping" feature to keep the old behaviour.
- Loading branch information
Showing
5 changed files
with
113 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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,45 @@ | ||
use { | ||
crate::*, | ||
std::cmp, | ||
}; | ||
|
||
/// count the number of '#' at start. Return 0 if they're | ||
/// not followed by a ' ' or if they're too many | ||
pub fn header_level(src: &str) -> usize { | ||
let src = src.as_bytes(); | ||
let mut l: usize = src.len(); | ||
if l > 2 { | ||
l = cmp::min(src.len() - 1, MAX_HEADER_DEPTH + 1); | ||
for i in 0..l { | ||
match src[i] { | ||
b'#' => {} | ||
b' ' => { | ||
return i; | ||
} | ||
_ => { | ||
return 0; | ||
} | ||
} | ||
} | ||
} | ||
0 | ||
} | ||
|
||
#[test] | ||
fn header_level_count() { | ||
assert_eq!(header_level(""), 0); | ||
assert_eq!(header_level("#"), 0); | ||
assert_eq!(header_level("# "), 0); // we don't allow empty headers | ||
assert_eq!(header_level("# A"), 1); | ||
assert_eq!(header_level(" "), 0); | ||
assert_eq!(header_level("test"), 0); | ||
assert_eq!(header_level("###b"), 0); | ||
assert_eq!(header_level("###"), 0); | ||
assert_eq!(header_level("### b"), 3); | ||
assert_eq!(header_level(" a b"), 0); | ||
assert_eq!(header_level("# titre"), 1); | ||
assert_eq!(header_level("#### *titre*"), 4); | ||
assert_eq!(header_level("######## a b"), 8); | ||
assert_eq!(header_level("######### a b"), 0); // too deep | ||
} | ||
|
This file contains 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 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 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