//! Turn events into a string of HTML.
use crate::constant::{SAFE_PROTOCOL_HREF, SAFE_PROTOCOL_SRC};
use crate::construct::character_reference::Kind as CharacterReferenceKind;
use crate::tokenizer::{Code, Event, EventType, TokenType};
use crate::util::{
decode_character_reference::{decode_named, decode_numeric},
encode::encode,
sanitize_uri::sanitize_uri,
span::{codes as codes_from_span, from_exit_event, serialize},
};
use std::collections::HashMap;
/// Type of line endings in markdown.
#[derive(Debug, Clone, PartialEq)]
pub enum LineEnding {
/// Both a carriage return (`\r`) and a line feed (`\n`).
///
/// ## Example
///
/// ```markdown
/// a␍␊
/// b
/// ```
CarriageReturnLineFeed,
/// Sole carriage return (`\r`).
///
/// ## Example
///
/// ```markdown
/// a␍
/// b
/// ```
CarriageReturn,
/// Sole line feed (`\n`).
///
/// ## Example
///
/// ```markdown
/// a␊
/// b
/// ```
LineFeed,
}
impl LineEnding {
/// Turn the line ending into a [str].
fn as_str(&self) -> &str {
match self {
LineEnding::CarriageReturnLineFeed => "\r\n",
LineEnding::CarriageReturn => "\r",
LineEnding::LineFeed => "\n",
}
}
/// Turn a [Code] into a line ending.
///
/// ## Panics
///
/// Panics if `code` is not `\r\n`, `\r`, or `\n`.
fn from_code(code: Code) -> LineEnding {
match code {
Code::CarriageReturnLineFeed => LineEnding::CarriageReturnLineFeed,
Code::Char('\r') => LineEnding::CarriageReturn,
Code::Char('\n') => LineEnding::LineFeed,
_ => unreachable!("invalid code"),
}
}
}
/// Representation of a link or image, resource or reference.
#[derive(Debug)]
struct Media {
/// Whether this represents an image (`true`) or a link (`false`).
image: bool,
/// The text between the brackets (`x` in `![x]()` and `[x]()`), as an
/// identifier, meaning that the original source characters are used
/// instead of interpreting them.
label_id: Option Hi, <i>venus</i>! Hi, venus! a a > a a > a ".to_string());
}
fn on_exit_label(context: &mut CompileContext, _event: &Event) {
let buf = context.resume();
let media = context.media_stack.last_mut().unwrap();
media.label = Some(buf);
}
fn on_exit_label_text(context: &mut CompileContext, _event: &Event) {
let media = context.media_stack.last_mut().unwrap();
media.label_id = Some(serialize(
context.codes,
&from_exit_event(context.events, context.index),
false,
));
}
fn on_exit_resource_destination_string(context: &mut CompileContext, _event: &Event) {
let buf = context.resume();
let media = context.media_stack.last_mut().unwrap();
media.destination = Some(buf);
context.ignore_encode = false;
}
fn on_exit_resource_title_string(context: &mut CompileContext, _event: &Event) {
let buf = context.resume();
let media = context.media_stack.last_mut().unwrap();
media.title = Some(buf);
}
fn on_exit_media(context: &mut CompileContext, _event: &Event) {
// let mut is_in_image = false;
// let mut index = 0;
// Skip current.
// while index < (media_stack.len() - 1) {
// if media_stack[index].image {
// is_in_image = true;
// break;
// }
// index += 1;
// }
// tags = is_in_image;
let media = context.media_stack.pop().unwrap();
println!("media: {:?}", media);
let label = media.label.unwrap();
// To do: get from definition.
let destination = media.destination.unwrap_or_else(|| "".to_string());
let title = if let Some(title) = media.title {
format!(" title=\"{}\"", title)
} else {
"".to_string()
};
let result = if media.image {
format!(
"\n
`.
///
/// To create that line ending, the document is checked for the first line
/// ending that is used.
/// If there is no line ending, `default_line_ending` is used.
/// If that isn’t configured, `\n` is used.
///
/// ## Examples
///
/// ```rust
/// use micromark::{micromark, micromark_with_options, Options, LineEnding};
///
/// // micromark is safe by default:
/// assert_eq!(
/// micromark("> a"),
/// // To do: block quote
/// // "\n
"
/// "\r\n
"
/// "".to_string());
}
fn on_enter_code_fenced(context: &mut CompileContext, _event: &Event) {
context.code_flow_seen_data = Some(false);
context.line_ending_if_needed();
// Note that no `>` is used, which is added later.
context.push("
".to_string());
}
fn on_exit_code_text_line_ending(context: &mut CompileContext, _event: &Event) {
context.push(" ".to_string());
}
fn on_exit_definition(context: &mut CompileContext, _event: &Event) {
context.resume();
context.slurp_one_line_ending = true;
}
fn on_exit_break(context: &mut CompileContext, _event: &Event) {
context.push("
".to_string());
if let Some(count) = context.code_fenced_fences_count.take() {
if count < 2 {
context.line_ending_if_needed();
}
}
context.slurp_one_line_ending = false;
}
fn on_exit_code_fenced_fence(context: &mut CompileContext, _event: &Event) {
let count = if let Some(count) = context.code_fenced_fences_count {
count
} else {
0
};
if count == 0 {
context.push(">".to_string());
// tag = true;
context.slurp_one_line_ending = true;
}
context.code_fenced_fences_count = Some(count + 1);
}
fn on_exit_code_fenced_fence_info(context: &mut CompileContext, _event: &Event) {
let value = context.resume();
context.push(format!(" class=\"language-{}\"", value));
// tag = true;
}
fn on_exit_resume(context: &mut CompileContext, _event: &Event) {
context.resume();
}
fn on_exit_code_flow_chunk(context: &mut CompileContext, _event: &Event) {
context.code_flow_seen_data = Some(true);
context.push(context.encode_opt(&serialize(
context.codes,
&from_exit_event(context.events, context.index),
false,
)));
}
fn on_exit_code_text(context: &mut CompileContext, _event: &Event) {
let result = context.resume();
let mut chars = result.chars();
let mut trim = false;
if Some(' ') == chars.next() && Some(' ') == chars.next_back() {
let mut next = chars.next();
while next != None && !trim {
if Some(' ') != next {
trim = true;
}
next = chars.next();
}
}
context.push(if trim {
result[1..(result.len() - 1)].to_string()
} else {
result
});
context.push("".to_string());
context.buffer();
}
fn on_enter_html_flow(context: &mut CompileContext, _event: &Event) {
context.line_ending_if_needed();
if context.allow_dangerous_html {
context.ignore_encode = true;
}
}
fn on_enter_html_text(context: &mut CompileContext, _event: &Event) {
if context.allow_dangerous_html {
context.ignore_encode = true;
}
}
fn on_enter_image(context: &mut CompileContext, _event: &Event) {
context.media_stack.push(Media {
image: true,
label_id: None,
label: None,
// reference_id: "".to_string(),
destination: None,
title: None,
});
// tags = undefined // Disallow tags.
}
fn on_enter_link(context: &mut CompileContext, _event: &Event) {
context.media_stack.push(Media {
image: false,
label_id: None,
label: None,
// reference_id: "".to_string(),
destination: None,
title: None,
});
}
fn on_enter_resource(context: &mut CompileContext, _event: &Event) {
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());
}
fn on_enter_destination_string(context: &mut CompileContext, _event: &Event) {
context.buffer();
// Ignore encoding the result, as we’ll first percent encode the url and
// encode manually after.
context.ignore_encode = true;
}
fn on_enter_paragraph(context: &mut CompileContext, _event: &Event) {
context.buf_tail_mut().push("
",
sanitize_uri(&destination, &context.protocol_src),
label,
title
)
} else {
format!(
"{}",
sanitize_uri(&destination, &context.protocol_href),
title,
label
)
};
context.push(result);
}
fn on_exit_push(context: &mut CompileContext, _event: &Event) {
// Just output it.
// last_was_tag = false;
context.push(context.encode_opt(&serialize(
context.codes,
&from_exit_event(context.events, context.index),
false,
)));
}
fn on_exit_autolink_email(context: &mut CompileContext, _event: &Event) {
let slice = serialize(
context.codes,
&from_exit_event(context.events, context.index),
false,
);
context.push(format!(
"{}",
sanitize_uri(slice.as_str(), &context.protocol_href),
context.encode_opt(&slice)
));
}
fn on_exit_autolink_protocol(context: &mut CompileContext, _event: &Event) {
let slice = serialize(
context.codes,
&from_exit_event(context.events, context.index),
false,
);
let href = sanitize_uri(slice.as_str(), &context.protocol_href);
println!("xxx: {:?} {:?}", href, &context.protocol_href);
context.push(format!(
"{}",
href,
context.encode_opt(&slice)
));
}
fn on_exit_character_reference_marker(context: &mut CompileContext, _event: &Event) {
context.character_reference_kind = Some(CharacterReferenceKind::Named);
}
fn on_exit_character_reference_marker_numeric(context: &mut CompileContext, _event: &Event) {
context.character_reference_kind = Some(CharacterReferenceKind::Decimal);
}
fn on_exit_character_reference_marker_hexadecimal(context: &mut CompileContext, _event: &Event) {
context.character_reference_kind = Some(CharacterReferenceKind::Hexadecimal);
}
fn on_exit_character_reference_value(context: &mut CompileContext, _event: &Event) {
let kind = context
.character_reference_kind
.take()
.expect("expected `character_reference_kind` to be set");
let reference = serialize(
context.codes,
&from_exit_event(context.events, context.index),
false,
);
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),
};
context.push(context.encode_opt(&value));
}
fn on_exit_code_flow(context: &mut CompileContext, _event: &Event) {
let seen_data = context
.code_flow_seen_data
.take()
.expect("`code_flow_seen_data` must be defined");
// To do: containers.
// One special case is if we are inside a container, and the fenced code was
// not closed (meaning it runs to the end).
// In that case, the following line ending, is considered *outside* the
// fenced code and block quote by micromark, but CM wants to treat that
// ending as part of the code.
// if fenced_count != None && fenced_count < 2 && tightStack.length > 0 && !last_was_tag {
// line_ending();
// }
// But in most cases, it’s simpler: when we’ve seen some data, emit an extra
// line ending when needed.
if seen_data {
context.line_ending_if_needed();
}
context.push("
".to_string());
}
fn on_exit_heading_atx(context: &mut CompileContext, _event: &Event) {
let rank = context
.atx_opening_sequence_size
.take()
.expect("`atx_opening_sequence_size` must be set in headings");
context.push(format!("", rank));
}
fn on_exit_heading_atx_sequence(context: &mut CompileContext, _event: &Event) {
// First fence we see.
if context.atx_opening_sequence_size.is_none() {
let rank = serialize(
context.codes,
&from_exit_event(context.events, context.index),
false,
)
.len();
context.atx_opening_sequence_size = Some(rank);
context.push(format!("