Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 52 additions & 30 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "comrak"
version = "0.22.0"
authors = ["Amelia Cuss <[email protected]>"]
rust-version = "1.62.1"
description = "A 100% CommonMark-compatible GitHub Flavored Markdown parser and formatter"
documentation = "https://docs.rs/comrak"
homepage = "https://hrzn.ee/kivikakk/comrak"
Expand Down Expand Up @@ -45,6 +46,7 @@ slug = "0.1.4"
emojis = { version = "0.5.2", optional = true }
arbitrary = { version = "1", optional = true, features = ["derive"] }
derive_builder = "0.12.0"
in-place = { version = "0.2.0", optional = true }

[dev-dependencies]
ntest = "0.9"
Expand All @@ -54,7 +56,7 @@ propfuzz = "0.0.1"

[features]
default = ["cli", "syntect"]
cli = ["clap", "shell-words", "xdg"]
cli = ["clap", "shell-words", "xdg", "in-place"]
shortcodes = ["emojis"]

[target.'cfg(all(not(windows), not(target_arch="wasm32")))'.dependencies]
Expand Down
14 changes: 6 additions & 8 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,12 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {

fn format_line_break(&mut self, entering: bool, next_is_block: bool) {
if entering {
if !self.options.render.hardbreaks {
if !next_is_block {
// If the next element is a block, a backslash means a
// literal backslash instead of a line break. In this case
// we can just skip the line break since it's meaningless
// before a block.
write!(self, "\\").unwrap();
}
if !self.options.render.hardbreaks && !next_is_block {
// If the next element is a block, a backslash means a
// literal backslash instead of a line break. In this case
// we can just skip the line break since it's meaningless
// before a block.
write!(self, "\\").unwrap();
}
self.cr();
}
Expand Down
44 changes: 36 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ use std::path::PathBuf;
use std::process;

use clap::{Parser, ValueEnum};
use in_place::InPlace;

const EXIT_SUCCESS: i32 = 0;
const EXIT_PARSE_CONFIG: i32 = 2;
const EXIT_READ_INPUT: i32 = 3;
const EXIT_CHECK_FILE_NUM: i32 = 4;

#[derive(Debug, Parser)]
#[command(about, author, version)]
Expand All @@ -35,6 +37,10 @@ struct Cli {
#[arg(short, long, value_name = "PATH", default_value = get_default_config_path())]
config_file: String,

/// To perform an in-place formatting
#[arg(short, long, conflicts_with_all(["format", "output"]))]
inplace: bool,

/// Treat newlines as hard line breaks
#[arg(long)]
hardbreaks: bool,
Expand Down Expand Up @@ -204,6 +210,18 @@ fn cli_with_config() -> Cli {
fn main() -> Result<(), Box<dyn Error>> {
let cli = cli_with_config();

if cli.inplace {
if let Some(ref files) = cli.files {
if files.len() != 1 {
eprintln!("cannot have more than 1 input file with in-place mode");
process::exit(EXIT_CHECK_FILE_NUM);
}
} else {
eprintln!("no input file specified: cannot use standard input with in-place mode");
process::exit(EXIT_CHECK_FILE_NUM);
}
}

let exts = &cli.extensions;

let mut extension = ExtensionOptionsBuilder::default();
Expand Down Expand Up @@ -272,8 +290,8 @@ fn main() -> Result<(), Box<dyn Error>> {
None => {
std::io::stdin().read_to_end(&mut s)?;
}
Some(fs) => {
for f in &fs {
Some(ref fs) => {
for f in fs {
match fs::File::open(f) {
Ok(mut io) => {
io.read_to_end(&mut s)?;
Expand All @@ -290,19 +308,29 @@ fn main() -> Result<(), Box<dyn Error>> {
let arena = Arena::new();
let root = comrak::parse_document(&arena, &String::from_utf8(s)?, &options);

let formatter = match cli.format {
Format::Html => {
plugins.render.codefence_syntax_highlighter = syntax_highlighter;
comrak::format_html_with_plugins
let formatter = if cli.inplace {
comrak::format_commonmark_with_plugins
} else {
match cli.format {
Format::Html => {
plugins.render.codefence_syntax_highlighter = syntax_highlighter;
comrak::format_html_with_plugins
}
Format::Xml => comrak::format_xml_with_plugins,
Format::CommonMark => comrak::format_commonmark_with_plugins,
}
Format::Xml => comrak::format_xml_with_plugins,
Format::CommonMark => comrak::format_commonmark_with_plugins,
};

if let Some(output_filename) = cli.output {
let mut bw = BufWriter::new(fs::File::create(output_filename)?);
formatter(root, &options, &mut bw, &plugins)?;
bw.flush()?;
} else if cli.inplace {
let inp: in_place::InPlaceFile =
InPlace::new(unsafe { cli.files.unwrap_unchecked().get_unchecked(0) }).open()?;
let mut bw = inp.writer();
formatter(root, &options, &mut bw, &plugins)?;
inp.save()?;
} else {
let stdout = std::io::stdout();
let mut bw = BufWriter::new(stdout.lock());
Expand Down
18 changes: 4 additions & 14 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,37 +290,27 @@ pub struct NodeDescriptionItem {
}

/// The type of list.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ListType {
/// A bullet list, i.e. an unordered list.
#[default]
Bullet,

/// An ordered list.
Ordered,
}

impl Default for ListType {
fn default() -> ListType {
ListType::Bullet
}
}

/// The delimiter for ordered lists, i.e. the character which appears after each number.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ListDelimType {
/// A period character `.`.
#[default]
Period,

/// A paren character `)`.
Paren,
}

impl Default for ListDelimType {
fn default() -> ListDelimType {
ListDelimType::Period
}
}

impl ListDelimType {
pub(crate) fn xml_name(&self) -> &'static str {
match *self {
Expand Down
Loading