Skip to content

Commit

Permalink
Merge pull request #101 from ConnorGray/connorgray/refactor-3
Browse files Browse the repository at this point in the history
refactor: Add `State::set_minimum_newlines_before_start()` (Part 3/X)
  • Loading branch information
Byron authored Feb 19, 2025
2 parents 29c8c20 + 31a49d0 commit ac3aaec
Showing 1 changed file with 25 additions and 43 deletions.
68 changes: 25 additions & 43 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,7 @@ where
let res = match event.borrow() {
Rule => {
consume_newlines(formatter, state)?;
if state.newlines_before_start < options.newlines_after_rule {
state.newlines_before_start = options.newlines_after_rule;
}
state.set_minimum_newlines_before_start(options.newlines_after_rule);
formatter.write_str("---")
}
Code(text) => {
Expand Down Expand Up @@ -370,8 +368,8 @@ where
Start(tag) => {
if let List(list_type) = tag {
state.list_stack.push(*list_type);
if state.list_stack.len() > 1 && state.newlines_before_start < options.newlines_after_rest {
state.newlines_before_start = options.newlines_after_rest;
if state.list_stack.len() > 1 {
state.set_minimum_newlines_before_start(options.newlines_after_rest);
}
}
let consumed_newlines = state.newlines_before_start != 0;
Expand Down Expand Up @@ -579,9 +577,7 @@ where
Strikethrough => formatter.write_str("~~"),
DefinitionList => Ok(()),
DefinitionListTitle => {
if state.newlines_before_start < options.newlines_after_rest {
state.newlines_before_start = options.newlines_after_rest;
}
state.set_minimum_newlines_before_start(options.newlines_after_rest);
Ok(())
}
DefinitionListDefinition => {
Expand Down Expand Up @@ -699,21 +695,15 @@ where
formatter.write_char(' ')?;
formatter.write_char('}')?;
}
if state.newlines_before_start < options.newlines_after_headline {
state.newlines_before_start = options.newlines_after_headline;
}
state.set_minimum_newlines_before_start(options.newlines_after_headline);
Ok(())
}
TagEnd::Paragraph => {
if state.newlines_before_start < options.newlines_after_paragraph {
state.newlines_before_start = options.newlines_after_paragraph;
}
state.set_minimum_newlines_before_start(options.newlines_after_paragraph);
Ok(())
}
TagEnd::CodeBlock => {
if state.newlines_before_start < options.newlines_after_codeblock {
state.newlines_before_start = options.newlines_after_codeblock;
}
state.set_minimum_newlines_before_start(options.newlines_after_codeblock);
if last_was_text_without_trailing_newline {
write_padded_newline(formatter, &state)?;
}
Expand All @@ -732,27 +722,19 @@ where
Ok(())
}
TagEnd::HtmlBlock => {
if state.newlines_before_start < options.newlines_after_htmlblock {
state.newlines_before_start = options.newlines_after_htmlblock;
}
state.set_minimum_newlines_before_start(options.newlines_after_htmlblock);
Ok(())
}
TagEnd::MetadataBlock(MetadataBlockKind::PlusesStyle) => {
if state.newlines_before_start < options.newlines_after_metadata {
state.newlines_before_start = options.newlines_after_metadata;
}
state.set_minimum_newlines_before_start(options.newlines_after_metadata);
formatter.write_str("+++\n")
}
TagEnd::MetadataBlock(MetadataBlockKind::YamlStyle) => {
if state.newlines_before_start < options.newlines_after_metadata {
state.newlines_before_start = options.newlines_after_metadata;
}
state.set_minimum_newlines_before_start(options.newlines_after_metadata);
formatter.write_str("---\n")
}
TagEnd::Table => {
if state.newlines_before_start < options.newlines_after_table {
state.newlines_before_start = options.newlines_after_table;
}
state.set_minimum_newlines_before_start(options.newlines_after_table);
state.table_alignments.clear();
state.table_headers.clear();
Ok(())
Expand All @@ -765,9 +747,7 @@ where
Ok(())
}
t @ (TagEnd::TableRow | TagEnd::TableHead) => {
if state.newlines_before_start < options.newlines_after_rest {
state.newlines_before_start = options.newlines_after_rest;
}
state.set_minimum_newlines_before_start(options.newlines_after_rest);
formatter.write_char('|')?;

if let TagEnd::TableHead = t {
Expand Down Expand Up @@ -807,24 +787,20 @@ where
}
TagEnd::Item => {
state.padding.pop();
if state.newlines_before_start < options.newlines_after_rest {
state.newlines_before_start = options.newlines_after_rest;
}
state.set_minimum_newlines_before_start(options.newlines_after_rest);
Ok(())
}
TagEnd::List(_) => {
state.list_stack.pop();
if state.list_stack.is_empty() && state.newlines_before_start < options.newlines_after_list {
state.newlines_before_start = options.newlines_after_list;
if state.list_stack.is_empty() {
state.set_minimum_newlines_before_start(options.newlines_after_list);
}
Ok(())
}
TagEnd::BlockQuote(_) => {
state.padding.pop();

if state.newlines_before_start < options.newlines_after_blockquote {
state.newlines_before_start = options.newlines_after_blockquote;
}
state.set_minimum_newlines_before_start(options.newlines_after_blockquote);

Ok(())
}
Expand All @@ -834,9 +810,7 @@ where
}
TagEnd::Strikethrough => formatter.write_str("~~"),
TagEnd::DefinitionList => {
if state.newlines_before_start < options.newlines_after_list {
state.newlines_before_start = options.newlines_after_list;
}
state.set_minimum_newlines_before_start(options.newlines_after_list);
Ok(())
}
TagEnd::DefinitionListTitle => formatter.write_char('\n'),
Expand Down Expand Up @@ -989,6 +963,14 @@ impl State<'_> {
}
Ok(self)
}

/// Ensure that [`State::newlines_before_start`] is at least as large as
/// the provided option value.
fn set_minimum_newlines_before_start(&mut self, option_value: usize) {
if self.newlines_before_start < option_value {
self.newlines_before_start = option_value
}
}
}

/// As [`cmark_resume_with_options()`], but with the [`State`] finalized.
Expand Down

0 comments on commit ac3aaec

Please sign in to comment.