block: count indent in chars instead of bytes
This commit is contained in:
parent
ca7f3c7e89
commit
59be7070de
2 changed files with 18 additions and 12 deletions
25
src/block.rs
25
src/block.rs
|
@ -313,13 +313,15 @@ impl<'s> TreeParser<'s> {
|
||||||
lines.iter_mut().skip(1).for_each(|sp| {
|
lines.iter_mut().skip(1).for_each(|sp| {
|
||||||
let src = sp.of(self.src);
|
let src = sp.of(self.src);
|
||||||
let src_t = src.trim();
|
let src_t = src.trim();
|
||||||
let spaces = src.len() - src.trim_start().len();
|
let spaces = src.chars().take_while(|c| c.is_whitespace()).count();
|
||||||
let skip = match k {
|
let skip = match k {
|
||||||
Kind::Blockquote => {
|
Kind::Blockquote => {
|
||||||
if src_t == ">" {
|
if src_t == ">" {
|
||||||
spaces + 1
|
spaces + 1
|
||||||
} else if src_t.starts_with("> ") {
|
} else if src_t.starts_with('>')
|
||||||
spaces + "> ".len()
|
&& src_t.chars().nth(1).map_or(false, char::is_whitespace)
|
||||||
|
{
|
||||||
|
spaces + 1 + usize::from(src_t.len() > 1)
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -329,8 +331,8 @@ impl<'s> TreeParser<'s> {
|
||||||
| Kind::Fenced { indent, .. } => spaces.min(*indent),
|
| Kind::Fenced { indent, .. } => spaces.min(*indent),
|
||||||
_ => panic!("non-container {:?}", k),
|
_ => panic!("non-container {:?}", k),
|
||||||
};
|
};
|
||||||
let len = sp.len() - usize::from(sp.of(self.src).ends_with('\n'));
|
let count = sp.of(self.src).chars().take_while(|c| *c != '\n').count();
|
||||||
*sp = sp.skip(skip.min(len));
|
*sp = sp.skip_chars(skip.min(count), self.src);
|
||||||
});
|
});
|
||||||
|
|
||||||
if let ListItem(ty) = c {
|
if let ListItem(ty) = c {
|
||||||
|
@ -571,18 +573,17 @@ struct IdentifiedBlock {
|
||||||
|
|
||||||
impl IdentifiedBlock {
|
impl IdentifiedBlock {
|
||||||
fn new(line: &str) -> Self {
|
fn new(line: &str) -> Self {
|
||||||
let indent = line
|
let mut chars = line.chars();
|
||||||
.chars()
|
let indent = chars
|
||||||
|
.clone()
|
||||||
.take_while(|c| *c != '\n' && c.is_whitespace())
|
.take_while(|c| *c != '\n' && c.is_whitespace())
|
||||||
.map(char::len_utf8)
|
.count();
|
||||||
.sum();
|
(&mut chars).take(indent).last();
|
||||||
let line = &line[indent..];
|
let line = chars.as_str();
|
||||||
let line_t = line.trim_end();
|
let line_t = line.trim_end();
|
||||||
let l = line.len();
|
let l = line.len();
|
||||||
let lt = line_t.len();
|
let lt = line_t.len();
|
||||||
|
|
||||||
let mut chars = line.chars();
|
|
||||||
|
|
||||||
let first = if let Some(c) = chars.next() {
|
let first = if let Some(c) = chars.next() {
|
||||||
c
|
c
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -89,6 +89,11 @@ impl Span {
|
||||||
&s[self.start()..self.end()]
|
&s[self.start()..self.end()]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn skip_chars(self, n: usize, s: &str) -> Self {
|
||||||
|
let n_bytes: usize = self.of(s).chars().take(n).map(char::len_utf8).sum();
|
||||||
|
Self::new(self.start() + n_bytes, self.end())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn trim_start_matches<P: FnMut(char) -> bool>(self, s: &str, pat: P) -> Self {
|
pub fn trim_start_matches<P: FnMut(char) -> bool>(self, s: &str, pat: P) -> Self {
|
||||||
Self::from_slice(s, self.of(s).trim_start_matches(pat))
|
Self::from_slice(s, self.of(s).trim_start_matches(pat))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue