From 0eeff9148e327183e532752f46421a75506dd7a6 Mon Sep 17 00:00:00 2001
From: Titus Wormer ");
+ context.push(" ");
+ context.last_was_tag = true;
}
}
/// Handle [`Enter`][EventType::Enter]:[`Resource`][Token::Resource].
fn on_enter_resource(context: &mut CompileContext) {
context.buffer(); // We can have line endings in the resource, ignore them.
- let media = context.media_stack.last_mut().unwrap();
- media.destination = Some("".to_string());
+ context.media_stack.last_mut().unwrap().destination = Some("".to_string());
}
/// Handle [`Enter`][EventType::Enter]:[`ResourceDestinationString`][Token::ResourceDestinationString].
@@ -599,47 +598,67 @@ fn on_enter_resource_destination_string(context: &mut CompileContext) {
/// Handle [`Enter`][EventType::Enter]:[`Strong`][Token::Strong].
fn on_enter_strong(context: &mut CompileContext) {
- context.tag("");
+ if !context.in_image_alt {
+ context.push("");
+ context.last_was_tag = true;
+ }
}
/// Handle [`Exit`][EventType::Exit]:[`AutolinkEmail`][Token::AutolinkEmail].
fn on_exit_autolink_email(context: &mut CompileContext) {
- let value = Slice::from_position(
+ let slice = Slice::from_position(
context.bytes,
&Position::from_exit_event(context.events, context.index),
- )
- .serialize();
+ );
+ let value = slice.as_str();
- context.tag(&*format!(
- "",
- sanitize_uri(
- format!("mailto:{}", value.as_str()).as_str(),
- &context.protocol_href
- )
- ));
- context.push_raw(&*value);
- context.tag("");
+ if !context.in_image_alt {
+ context.push("");
+ context.last_was_tag = true;
+ }
+
+ context.push_raw(value);
+
+ if !context.in_image_alt {
+ context.push("");
+ context.last_was_tag = true;
+ }
}
/// Handle [`Exit`][EventType::Exit]:[`AutolinkProtocol`][Token::AutolinkProtocol].
fn on_exit_autolink_protocol(context: &mut CompileContext) {
- let value = Slice::from_position(
+ let slice = Slice::from_position(
context.bytes,
&Position::from_exit_event(context.events, context.index),
- )
- .serialize();
+ );
+ let value = slice.as_str();
- context.tag(&*format!(
- "",
- sanitize_uri(value.as_str(), &context.protocol_href)
- ));
- context.push_raw(&*value);
- context.tag("");
+ if !context.in_image_alt {
+ context.push("");
+ context.last_was_tag = true;
+ }
+
+ context.push_raw(value);
+
+ if !context.in_image_alt {
+ context.push("");
+ context.last_was_tag = true;
+ }
}
/// Handle [`Exit`][EventType::Exit]:{[`HardBreakEscape`][Token::HardBreakEscape],[`HardBreakTrailing`][Token::HardBreakTrailing]}.
fn on_exit_break(context: &mut CompileContext) {
- context.tag("");
+ context.push("
");
+ context.last_was_tag = true;
}
/// Handle [`Exit`][EventType::Exit]:[`CharacterReferenceMarker`][Token::CharacterReferenceMarker].
fn on_exit_character_reference_marker(context: &mut CompileContext) {
- context.character_reference_kind = Some(CharacterReferenceKind::Named);
+ context.character_reference_marker = Some(b'&');
}
/// Handle [`Exit`][EventType::Exit]:[`CharacterReferenceMarkerHexadecimal`][Token::CharacterReferenceMarkerHexadecimal].
fn on_exit_character_reference_marker_hexadecimal(context: &mut CompileContext) {
- context.character_reference_kind = Some(CharacterReferenceKind::Hexadecimal);
+ context.character_reference_marker = Some(b'x');
}
/// Handle [`Exit`][EventType::Exit]:[`CharacterReferenceMarkerNumeric`][Token::CharacterReferenceMarkerNumeric].
fn on_exit_character_reference_marker_numeric(context: &mut CompileContext) {
- context.character_reference_kind = Some(CharacterReferenceKind::Decimal);
+ context.character_reference_marker = Some(b'#');
}
/// Handle [`Exit`][EventType::Exit]:[`CharacterReferenceValue`][Token::CharacterReferenceValue].
fn on_exit_character_reference_value(context: &mut CompileContext) {
- let kind = context
- .character_reference_kind
+ let marker = context
+ .character_reference_marker
.take()
.expect("expected `character_reference_kind` to be set");
- let reference = Slice::from_position(
+ let slice = Slice::from_position(
context.bytes,
&Position::from_exit_event(context.events, context.index),
- )
- .serialize();
+ );
+ let value = slice.as_str();
- let ref_string = reference.as_str();
- let value = match kind {
- CharacterReferenceKind::Decimal => decode_numeric(ref_string, 10).to_string(),
- CharacterReferenceKind::Hexadecimal => decode_numeric(ref_string, 16).to_string(),
- CharacterReferenceKind::Named => decode_named(ref_string),
+ let value = match marker {
+ b'#' => decode_numeric(value, 10),
+ b'x' => decode_numeric(value, 16),
+ b'&' => decode_named(value),
+ _ => panic!("impossible"),
};
- context.push_raw(&*value);
+ context.push_raw(&value);
}
/// Handle [`Exit`][EventType::Exit]:[`CodeFlowChunk`][Token::CodeFlowChunk].
fn on_exit_code_flow_chunk(context: &mut CompileContext) {
- let value = Slice::from_position(
- context.bytes,
- &Position::from_exit_event(context.events, context.index),
- )
- .serialize();
-
context.code_flow_seen_data = Some(true);
- context.push_raw(&*value);
+ context.push_raw(
+ &Slice::from_position(
+ context.bytes,
+ &Position::from_exit_event(context.events, context.index),
+ )
+ // Must serialize to get virtual spaces.
+ .serialize(),
+ );
}
/// Handle [`Exit`][EventType::Exit]:[`CodeFencedFence`][Token::CodeFencedFence].
@@ -715,7 +736,8 @@ fn on_exit_code_fenced_fence(context: &mut CompileContext) {
};
if count == 0 {
- context.tag(">");
+ context.push(">");
+ context.last_was_tag = true;
context.slurp_one_line_ending = true;
}
@@ -725,7 +747,10 @@ fn on_exit_code_fenced_fence(context: &mut CompileContext) {
/// Handle [`Exit`][EventType::Exit]:[`CodeFencedFenceInfo`][Token::CodeFencedFenceInfo].
fn on_exit_code_fenced_fence_info(context: &mut CompileContext) {
let value = context.resume();
- context.tag(&*format!(" class=\"language-{}\"", value));
+ context.push(" class=\"language-");
+ context.push(&value);
+ context.push("\"");
+ context.last_was_tag = true;
}
/// Handle [`Exit`][EventType::Exit]:{[`CodeFenced`][Token::CodeFenced],[`CodeIndented`][Token::CodeIndented]}.
@@ -752,7 +777,8 @@ fn on_exit_code_flow(context: &mut CompileContext) {
context.line_ending_if_needed();
}
- context.tag("");
+ context.push("");
+ context.last_was_tag = true;
if let Some(count) = context.code_fenced_fences_count.take() {
if count < 2 {
@@ -781,12 +807,16 @@ fn on_exit_code_text(context: &mut CompileContext) {
}
context.code_text_inside = false;
- context.push(&*if trim {
+ context.push(&if trim {
result[1..(result.len() - 1)].to_string()
} else {
result
});
- context.tag("");
+
+ if !context.in_image_alt {
+ context.push("");
+ context.last_was_tag = true;
+ }
}
/// Handle [`Exit`][EventType::Exit]:*.
@@ -798,72 +828,63 @@ fn on_exit_drop(context: &mut CompileContext) {
/// Handle [`Exit`][EventType::Exit]:{[`CodeTextData`][Token::CodeTextData],[`Data`][Token::Data],[`CharacterEscapeValue`][Token::CharacterEscapeValue]}.
fn on_exit_data(context: &mut CompileContext) {
- let value = Slice::from_position(
- context.bytes,
- &Position::from_exit_event(context.events, context.index),
- )
- .serialize();
-
- // Just output it.
- context.push_raw(&*value);
+ context.push_raw(
+ Slice::from_position(
+ context.bytes,
+ &Position::from_exit_event(context.events, context.index),
+ )
+ .as_str(),
+ );
}
/// Handle [`Exit`][EventType::Exit]:[`Definition`][Token::Definition].
fn on_exit_definition(context: &mut CompileContext) {
- let definition = context.media_stack.pop().unwrap();
- let reference_id = normalize_identifier(&definition.reference_id.unwrap());
- let destination = definition.destination;
- let title = definition.title;
-
context.resume();
-
- let mut index = 0;
-
- while index < context.definitions.len() {
- if context.definitions[index].0 == reference_id {
- return;
- }
-
- index += 1;
- }
-
- context
- .definitions
- .push((reference_id, Definition { destination, title }));
+ let media = context.media_stack.pop().unwrap();
+ let id = normalize_identifier(&media.reference_id.unwrap());
+
+ context.definitions.push((
+ id,
+ Definition {
+ destination: media.destination,
+ title: media.title,
+ },
+ ));
}
/// Handle [`Exit`][EventType::Exit]:[`DefinitionDestinationString`][Token::DefinitionDestinationString].
fn on_exit_definition_destination_string(context: &mut CompileContext) {
let buf = context.resume();
- let definition = context.media_stack.last_mut().unwrap();
- definition.destination = Some(buf);
+ context.media_stack.last_mut().unwrap().destination = Some(buf);
context.encode_html = true;
}
/// Handle [`Exit`][EventType::Exit]:[`DefinitionLabelString`][Token::DefinitionLabelString].
fn on_exit_definition_label_string(context: &mut CompileContext) {
- let value = Slice::from_position(
- context.bytes,
- &Position::from_exit_event(context.events, context.index),
- )
- .serialize();
-
// Discard label, use the source content instead.
context.resume();
- let definition = context.media_stack.last_mut().unwrap();
- definition.reference_id = Some(value);
+ context.media_stack.last_mut().unwrap().reference_id = Some(
+ // To do: lifetimes, reference bytes?
+ Slice::from_position(
+ context.bytes,
+ &Position::from_exit_event(context.events, context.index),
+ )
+ .serialize(),
+ );
}
/// Handle [`Exit`][EventType::Exit]:[`DefinitionTitleString`][Token::DefinitionTitleString].
fn on_exit_definition_title_string(context: &mut CompileContext) {
let buf = context.resume();
- let definition = context.media_stack.last_mut().unwrap();
- definition.title = Some(buf);
+ context.media_stack.last_mut().unwrap().title = Some(buf);
}
/// Handle [`Exit`][EventType::Exit]:[`Strong`][Token::Emphasis].
fn on_exit_emphasis(context: &mut CompileContext) {
- context.tag("");
+ if !context.in_image_alt {
+ context.push("");
+ context.last_was_tag = true;
+ }
}
/// Handle [`Exit`][EventType::Exit]:[`HeadingAtx`][Token::HeadingAtx].
@@ -873,7 +894,10 @@ fn on_exit_heading_atx(context: &mut CompileContext) {
.take()
.expect("`atx_opening_sequence_size` must be set in headings");
- context.tag(&*format!("", rank));
+ context.push("");
+ context.last_was_tag = true;
}
/// Handle [`Exit`][EventType::Exit]:[`HeadingAtxSequence`][Token::HeadingAtxSequence].
@@ -884,17 +908,20 @@ fn on_exit_heading_atx_sequence(context: &mut CompileContext) {
context.bytes,
&Position::from_exit_event(context.events, context.index),
)
- .size();
+ .len();
context.line_ending_if_needed();
context.atx_opening_sequence_size = Some(rank);
- context.tag(&*format!("");
+ context.last_was_tag = true;
}
/// Handle [`Enter`][EventType::Enter]:[`CodeIndented`][Token::CodeIndented].
fn on_enter_code_indented(context: &mut CompileContext) {
context.code_flow_seen_data = Some(false);
context.line_ending_if_needed();
- context.tag("
");
+ context.push("");
+ context.push("
");
+ context.last_was_tag = true;
}
/// Handle [`Enter`][EventType::Enter]:[`CodeFenced`][Token::CodeFenced].
@@ -413,14 +404,18 @@ fn on_enter_code_fenced(context: &mut CompileContext) {
context.code_flow_seen_data = Some(false);
context.line_ending_if_needed();
// Note that no `>` is used, which is added later.
- context.tag("
");
+ if !context.in_image_alt {
+ context.push("
");
+ context.last_was_tag = true;
+ }
context.buffer();
}
@@ -445,7 +440,10 @@ fn on_enter_definition_destination_string(context: &mut CompileContext) {
/// Handle [`Enter`][EventType::Enter]:[`Emphasis`][Token::Emphasis].
fn on_enter_emphasis(context: &mut CompileContext) {
- context.tag("");
+ if !context.in_image_alt {
+ context.push("");
+ context.last_was_tag = true;
+ }
}
/// Handle [`Enter`][EventType::Enter]:[`HtmlFlow`][Token::HtmlFlow].
@@ -473,7 +471,7 @@ fn on_enter_image(context: &mut CompileContext) {
destination: None,
title: None,
});
- context.tags = false; // Disallow tags.
+ context.in_image_alt = true; // Disallow tags.
}
/// Handle [`Enter`][EventType::Enter]:[`Link`][Token::Link].
@@ -546,14 +544,12 @@ fn on_enter_list(context: &mut CompileContext) {
context.tight_stack.push(!loose);
context.line_ending_if_needed();
// Note: no `>`.
- context.tag(&*format!(
- "<{}",
- if *token_type == Token::ListOrdered {
- "ol"
- } else {
- "ul"
- }
- ));
+ context.push(if *token_type == Token::ListOrdered {
+ "
");
+ context.push(">");
+ context.last_was_tag = true;
}
context.line_ending_if_needed();
- context.tag("
");
+ if !context.in_image_alt {
+ context.push("
");
+ context.last_was_tag = true;
+ }
}
/// Handle [`Exit`][EventType::Exit]:[`BlankLineEnding`][Token::BlankLineEnding].
@@ -654,56 +673,58 @@ fn on_exit_block_quote(context: &mut CompileContext) {
context.tight_stack.pop();
context.line_ending_if_needed();
context.slurp_one_line_ending = false;
- context.tag("