aboutsummaryrefslogtreecommitdiffstats
path: root/src/construct/character_reference.rs
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-07-05 13:03:09 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-07-05 13:03:09 +0200
commitfd860a975b84da9a79abfa247787e6adbd5ea34c (patch)
treebd9db168c57478f4f37c234eac4087c2d69a6445 /src/construct/character_reference.rs
parent0bc099f8f8b6541a962e604b7ac25445a2a9252a (diff)
downloadmarkdown-rs-fd860a975b84da9a79abfa247787e6adbd5ea34c.tar.gz
markdown-rs-fd860a975b84da9a79abfa247787e6adbd5ea34c.tar.bz2
markdown-rs-fd860a975b84da9a79abfa247787e6adbd5ea34c.zip
Refactor code style
Diffstat (limited to 'src/construct/character_reference.rs')
-rw-r--r--src/construct/character_reference.rs69
1 files changed, 23 insertions, 46 deletions
diff --git a/src/construct/character_reference.rs b/src/construct/character_reference.rs
index bc42d21..65e49ca 100644
--- a/src/construct/character_reference.rs
+++ b/src/construct/character_reference.rs
@@ -138,21 +138,18 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
/// a&|#x9;b
/// ```
fn open(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
+ let info = Info {
+ buffer: vec![],
+ kind: Kind::Named,
+ };
if let Code::Char('#') = code {
tokenizer.enter(TokenType::CharacterReferenceMarkerNumeric);
tokenizer.consume(code);
tokenizer.exit(TokenType::CharacterReferenceMarkerNumeric);
- (State::Fn(Box::new(numeric)), None)
+ (State::Fn(Box::new(|t, c| numeric(t, c, info))), None)
} else {
tokenizer.enter(TokenType::CharacterReferenceValue);
- value(
- tokenizer,
- code,
- Info {
- buffer: vec![],
- kind: Kind::Named,
- },
- )
+ value(tokenizer, code, info)
}
}
@@ -163,37 +160,18 @@ fn open(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
/// a&#|123;b
/// a&#|x9;b
/// ```
-fn numeric(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
+fn numeric(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> StateFnResult {
if let Code::Char('x' | 'X') = code {
tokenizer.enter(TokenType::CharacterReferenceMarkerHexadecimal);
tokenizer.consume(code);
tokenizer.exit(TokenType::CharacterReferenceMarkerHexadecimal);
tokenizer.enter(TokenType::CharacterReferenceValue);
-
- (
- State::Fn(Box::new(|t, c| {
- value(
- t,
- c,
- Info {
- buffer: vec![],
- kind: Kind::Hexadecimal,
- },
- )
- })),
- None,
- )
+ info.kind = Kind::Hexadecimal;
+ (State::Fn(Box::new(|t, c| value(t, c, info))), None)
} else {
tokenizer.enter(TokenType::CharacterReferenceValue);
-
- value(
- tokenizer,
- code,
- Info {
- buffer: vec![],
- kind: Kind::Decimal,
- },
- )
+ info.kind = Kind::Decimal;
+ value(tokenizer, code, info)
}
}
@@ -210,20 +188,19 @@ fn numeric(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
fn value(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> StateFnResult {
match code {
Code::Char(';') if !info.buffer.is_empty() => {
- tokenizer.exit(TokenType::CharacterReferenceValue);
- let value = info.buffer.iter().collect::<String>();
-
- if let Kind::Named = info.kind {
- if !CHARACTER_REFERENCE_NAMES.contains(&value.as_str()) {
- return (State::Nok, None);
- }
+ if Kind::Named == info.kind
+ && !CHARACTER_REFERENCE_NAMES
+ .contains(&info.buffer.iter().collect::<String>().as_str())
+ {
+ (State::Nok, None)
+ } else {
+ tokenizer.exit(TokenType::CharacterReferenceValue);
+ tokenizer.enter(TokenType::CharacterReferenceMarkerSemi);
+ tokenizer.consume(code);
+ tokenizer.exit(TokenType::CharacterReferenceMarkerSemi);
+ tokenizer.exit(TokenType::CharacterReference);
+ (State::Ok, None)
}
-
- tokenizer.enter(TokenType::CharacterReferenceMarkerSemi);
- tokenizer.consume(code);
- tokenizer.exit(TokenType::CharacterReferenceMarkerSemi);
- tokenizer.exit(TokenType::CharacterReference);
- (State::Ok, None)
}
Code::Char(char) => {
if info.buffer.len() < info.kind.max() && info.kind.allowed(char) {