Skip to content

Commit

Permalink
Merge pull request #23 from razziel89/feature/format-block-quotes
Browse files Browse the repository at this point in the history
Feature/format block quotes
  • Loading branch information
razziel89 authored Aug 15, 2024
2 parents 4b776be + 45a0806 commit 5213d15
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mdslw"
version = "0.11.1"
version = "0.12.0"
edition = "2021"

[profile.release]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ Values are resolved in the following order:
Do not replace spaces in link texts by [non-breaking spaces][wiki nbsp].
- `keep-linebreaks`:
Do not remove existing linebreaks during the line-wrapping process.
- `format-block-quotes`:
Format text in block quotes.
- `--completion <COMPLETION>`:
Output shell completion file for the given shell to stdout and exit.
The following shells are supported:
Expand Down
1 change: 1 addition & 0 deletions src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ pub struct CliArgs {
/// {n} * keep-spaces-in-links => do not replace spaces in link texts by non-breaking spaces
/// {n} * keep-linebreaks => do not remove existing linebreaks during the line-wrapping
/// process
/// {n} * format-block-quotes => format text in block quotes
/// {n} .
#[arg(long, env = "MDSLW_FEATURES", default_value = "\u{200b}")]
pub features: ValueWOrigin<String>,
Expand Down
7 changes: 6 additions & 1 deletion src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::parse::ParseCfg;
#[derive(Debug, PartialEq)]
pub struct FeatureCfg {
pub keep_spaces_in_links: bool,
pub format_block_quotes: bool,
pub break_cfg: BreakCfg,
pub parse_cfg: ParseCfg,
}
Expand All @@ -31,6 +32,7 @@ impl Default for FeatureCfg {
fn default() -> Self {
FeatureCfg {
keep_spaces_in_links: false,
format_block_quotes: false,
parse_cfg: ParseCfg {
keep_linebreaks: false,
},
Expand All @@ -57,6 +59,7 @@ impl std::str::FromStr for FeatureCfg {
{
match feature {
"keep-spaces-in-links" => cfg.keep_spaces_in_links = true,
"format-block-quotes" => cfg.format_block_quotes = true,
"keep-linebreaks" => {
cfg.parse_cfg.keep_linebreaks = true;
cfg.break_cfg.keep_linebreaks = true;
Expand Down Expand Up @@ -86,6 +89,7 @@ mod test {
let default = FeatureCfg::default();
let swapped = FeatureCfg {
keep_spaces_in_links: !default.keep_spaces_in_links,
format_block_quotes: !default.format_block_quotes,
parse_cfg: ParseCfg {
keep_linebreaks: !default.parse_cfg.keep_linebreaks,
},
Expand All @@ -94,7 +98,8 @@ mod test {
},
};

let parsed = "keep-spaces-in-links , keep-linebreaks".parse::<FeatureCfg>()?;
let parsed =
"keep-spaces-in-links , keep-linebreaks ,format-block-quotes".parse::<FeatureCfg>()?;

assert_eq!(parsed, swapped);
Ok(())
Expand Down
74 changes: 52 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,52 @@ fn generate_report(
}
}

struct Processor {
feature_cfg: features::FeatureCfg,
detector: detect::BreakDetector,
max_width: Option<usize>,
}

impl Processor {
fn process(&self, text: String, width_reduction: usize) -> String {
// At first, process all block quotes.
let text = if self.feature_cfg.format_block_quotes {
log::debug!("formatting text in block quotes");
parse::BlockQuotes::new(&text).apply_to_matches_and_join(|t| {
self.process(t, width_reduction + parse::BlockQuotes::FULL_PREFIX_LEN)
})
} else {
log::debug!("not formatting text in block quotes");
text
};
// Then process the actual text.
let ends_on_linebreak = text.ends_with('\n');
let after_space_replace = if self.feature_cfg.keep_spaces_in_links {
log::debug!("not replacing spaces in links by non-breaking spaces");
text
} else {
log::debug!("replacing spaces in links by non-breaking spaces");
replace::replace_spaces_in_links_by_nbsp(text)
};
let parsed = parse::parse_markdown(&after_space_replace, &self.feature_cfg.parse_cfg);
let filled = ranges::fill_markdown_ranges(parsed, &after_space_replace);
let width = &self
.max_width
.map(|el| el.checked_sub(width_reduction).unwrap_or(el));
let formatted =
wrap::add_linebreaks_and_wrap(filled, width, &self.detector, &after_space_replace);

// Keep newlines at the end of the file in tact. They disappear sometimes.
let file_end = if !formatted.ends_with('\n') && ends_on_linebreak {
log::debug!("adding missing trailing newline character");
"\n"
} else {
""
};
format!("{}{}", formatted, file_end)
}
}

fn process(
document: String,
file_dir: &PathBuf,
Expand All @@ -95,6 +141,11 @@ fn process(
log::debug!("limiting line length to {} characters", cfg.max_width);
Some(cfg.max_width)
};
let processor = Processor {
feature_cfg,
detector,
max_width,
};

// Actually process the text.
let (frontmatter, text) = frontmatter::split_frontmatter(document.clone());
Expand All @@ -107,28 +158,7 @@ fn process(
text
};

let after_space_replace = if feature_cfg.keep_spaces_in_links {
log::debug!("not replacing spaces in links by non-breaking spaces");
after_upstream
} else {
log::debug!("replacing spaces in links by non-breaking spaces");
replace::replace_spaces_in_links_by_nbsp(after_upstream)
};

let parsed = parse::parse_markdown(&after_space_replace, &feature_cfg.parse_cfg);
let filled = ranges::fill_markdown_ranges(parsed, &after_space_replace);
let formatted =
wrap::add_linebreaks_and_wrap(filled, &max_width, &detector, &after_space_replace);

// Keep newlines at the end of the file in tact. They disappear sometimes.
let file_end = if !formatted.ends_with('\n') && document.ends_with('\n') {
log::debug!("adding missing trailing newline character");
"\n"
} else {
""
};

let processed = format!("{}{}{}", frontmatter, formatted, file_end);
let processed = format!("{}{}", frontmatter, processor.process(after_upstream, 0));
Ok((processed, document))
}

Expand Down
Loading

0 comments on commit 5213d15

Please sign in to comment.