Skip to content

Commit

Permalink
Merge branch 'notriddle/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 6, 2024
2 parents ab22a57 + c3a0f32 commit ed4ff6f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
45 changes: 33 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub struct State<'a> {
pub is_in_code_block: bool,
/// True if the last event was text and the text does not have trailing newline. Used to inject additional newlines before code block end fence.
pub last_was_text_without_trailing_newline: bool,
/// True if the last event was a paragraph start. Used to escape spaces at start of line (prevent spurrious indented code).
pub last_was_paragraph_start: bool,
/// Currently open links
pub link_stack: Vec<LinkCategory<'a>>,
/// Currently open images
Expand Down Expand Up @@ -230,6 +232,8 @@ where

let last_was_text_without_trailing_newline = state.last_was_text_without_trailing_newline;
state.last_was_text_without_trailing_newline = false;
let last_was_paragraph_start = state.last_was_paragraph_start;
state.last_was_paragraph_start = false;
match event.borrow() {
Rule => {
consume_newlines(formatter, state)?;
Expand Down Expand Up @@ -271,21 +275,25 @@ where
let consumed_newlines = state.newlines_before_start != 0;
consume_newlines(formatter, state)?;
match tag {
Item => match state.list_stack.last_mut() {
Some(inner) => {
state.padding.push(padding_of(*inner));
match inner {
Some(n) => {
let bullet_number = *n;
if options.increment_ordered_list_bullets {
*n += 1;
Item => {
// lazy lists act like paragraphs with no event
state.last_was_paragraph_start = true;
match state.list_stack.last_mut() {
Some(inner) => {
state.padding.push(padding_of(*inner));
match inner {
Some(n) => {
let bullet_number = *n;
if options.increment_ordered_list_bullets {
*n += 1;
}
write!(formatter, "{}{} ", bullet_number, options.ordered_list_token)
}
write!(formatter, "{}{} ", bullet_number, options.ordered_list_token)
None => write!(formatter, "{} ", options.list_token),
}
None => write!(formatter, "{} ", options.list_token),
}
None => Ok(()),
}
None => Ok(()),
},
Table(alignments) => {
state.table_alignments = alignments.iter().map(From::from).collect();
Expand Down Expand Up @@ -344,7 +352,10 @@ where
state.padding.push(" ".into());
write!(formatter, "[^{name}]: ")
}
Paragraph => Ok(()),
Paragraph => {
state.last_was_paragraph_start = true;
Ok(())
},
Heading {
level,
id,
Expand Down Expand Up @@ -614,13 +625,23 @@ where
HardBreak => formatter.write_str(" \n").and(padding(formatter, &state.padding)),
SoftBreak => formatter.write_char('\n').and(padding(formatter, &state.padding)),
Text(text) => {
let mut text = &text[..];
if let Some(shortcut_text) = state.current_shortcut_text.as_mut() {
shortcut_text.push_str(text);
}
if let Some(text_for_header) = state.text_for_header.as_mut() {
text_for_header.push_str(text);
}
consume_newlines(formatter, state)?;
if last_was_paragraph_start {
if text.starts_with('\t') {
formatter.write_str("&#9;")?;
text = &text[1..];
} else if text.starts_with(' ') {
formatter.write_str("&#32;")?;
text = &text[1..];
}
}
state.last_was_text_without_trailing_newline = !text.ends_with('\n');
print_text_without_trailing_newline(
&escape_leading_special_characters(text, state.is_in_code_block, options),
Expand Down
4 changes: 2 additions & 2 deletions src/text_modifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ where

pub fn escape_leading_special_characters<'a>(
t: &'a str,
is_in_block_quote: bool,
is_in_code_block: bool,
options: &Options<'a>,
) -> Cow<'a, str> {
if is_in_block_quote || t.is_empty() {
if is_in_code_block || t.is_empty() {
return Cow::Borrowed(t);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/common-mark.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ foo
* | < > #
````

### Entity escapes

&copy; 2280 one world government, inc

&#9;paragraph starts with a literal tab.

&#32;&#32;&#32;&#32;I'm using markdown like a typewriter,
&#32;&#32;&#32;&#32;probably because this document started life in a WYSIWYG
&#32;&#32;&#32;&#32;editor and got converted...

[Links]: http://www.example.com/shortcut
[`diam`]: http://www.example.com/shortcut_code_diam
[`voluptua`]: http://www.example.com/shortcut_code_voluptua
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/snapshots/stupicat-event-by-event-output
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ foo
* | < > #
````

### Entity escapes

© 2280 one world government, inc

&#9;paragraph starts with a literal tab.

&#32; I'm using markdown like a typewriter,
probably because this document started life in a WYSIWYG
editor and got converted...

[Links]: http://www.example.com/shortcut
[`diam`]: http://www.example.com/shortcut_code_diam
[`voluptua`]: http://www.example.com/shortcut_code_voluptua
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/snapshots/stupicat-output
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ foo
* | < > #
````

### Entity escapes

© 2280 one world government, inc

&#9;paragraph starts with a literal tab.

&#32; I'm using markdown like a typewriter,
probably because this document started life in a WYSIWYG
editor and got converted...

[Links]: http://www.example.com/shortcut
[`diam`]: http://www.example.com/shortcut_code_diam
[`voluptua`]: http://www.example.com/shortcut_code_voluptua
Expand Down
7 changes: 7 additions & 0 deletions tests/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,13 @@ mod escapes {
);
});
}

#[test]
fn entity_escape_is_not_code_block_indent() {
source_range_fmt::assert_events_eq("&#9;foo");
source_range_fmt::assert_events_eq("&#32; foo");
source_range_fmt::assert_events_eq(" * &#32; foo\n * &#9;foo");
}
}

mod list {
Expand Down

0 comments on commit ed4ff6f

Please sign in to comment.