Skip to content

Commit

Permalink
minor refactor to Subject::parse_inline
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeando authored and kivikakk committed May 20, 2022
1 parent dc2d4b5 commit 37e5e32
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions src/parser/inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,61 +102,55 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
}

pub fn parse_inline(&mut self, node: &'a AstNode<'a>) -> bool {
let new_inl: Option<&'a AstNode<'a>>;
let c = match self.peek_char() {
None => return false,
Some(ch) => *ch as char,
};

match c {
let new_inl: Option<&'a AstNode<'a>> = match c {
'\0' => return false,
'\r' | '\n' => new_inl = Some(self.handle_newline()),
'`' => new_inl = Some(self.handle_backticks()),
'\\' => new_inl = Some(self.handle_backslash()),
'&' => new_inl = Some(self.handle_entity()),
'<' => new_inl = Some(self.handle_pointy_brace()),
'*' | '_' | '\'' | '"' => new_inl = Some(self.handle_delim(c as u8)),
'-' => new_inl = Some(self.handle_hyphen()),
'.' => new_inl = Some(self.handle_period()),
'\r' | '\n' => Some(self.handle_newline()),
'`' => Some(self.handle_backticks()),
'\\' => Some(self.handle_backslash()),
'&' => Some(self.handle_entity()),
'<' => Some(self.handle_pointy_brace()),
'*' | '_' | '\'' | '"' => Some(self.handle_delim(c as u8)),
'-' => Some(self.handle_hyphen()),
'.' => Some(self.handle_period()),
'[' => {
self.pos += 1;
let inl = make_inline(self.arena, NodeValue::Text(b"[".to_vec()));
new_inl = Some(inl);
self.push_bracket(false, inl);
Some(inl)
}
']' => new_inl = self.handle_close_bracket(),
']' => self.handle_close_bracket(),
'!' => {
self.pos += 1;
if self.peek_char() == Some(&(b'[')) && self.peek_char_n(1) != Some(&(b'^')) {
self.pos += 1;
let inl = make_inline(self.arena, NodeValue::Text(b"![".to_vec()));
new_inl = Some(inl);
self.push_bracket(true, inl);
Some(inl)
} else {
new_inl = Some(make_inline(self.arena, NodeValue::Text(b"!".to_vec())));
Some(make_inline(self.arena, NodeValue::Text(b"!".to_vec())))
}
}
'~' if self.options.extension.strikethrough => Some(self.handle_delim(b'~')),
'^' if self.options.extension.superscript => Some(self.handle_delim(b'^')),
_ => {
if self.options.extension.strikethrough && c == '~' {
new_inl = Some(self.handle_delim(b'~'));
} else if self.options.extension.superscript && c == '^' {
new_inl = Some(self.handle_delim(b'^'));
} else {
let endpos = self.find_special_char();
let mut contents = self.input[self.pos..endpos].to_vec();
self.pos = endpos;

if self
.peek_char()
.map_or(false, |&c| strings::is_line_end_char(c))
{
strings::rtrim(&mut contents);
}
let endpos = self.find_special_char();
let mut contents = self.input[self.pos..endpos].to_vec();
self.pos = endpos;

new_inl = Some(make_inline(self.arena, NodeValue::Text(contents)));
if self
.peek_char()
.map_or(false, |&c| strings::is_line_end_char(c))
{
strings::rtrim(&mut contents);
}

Some(make_inline(self.arena, NodeValue::Text(contents)))
}
}
};

if let Some(inl) = new_inl {
node.append(inl);
Expand Down

0 comments on commit 37e5e32

Please sign in to comment.