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
34 changes: 14 additions & 20 deletions 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
Expand Up @@ -13,7 +13,6 @@ readme = "README.md"
docx-rs = { git = "https://github.com/bokuweb/docx-rs.git" }
clap = { version = "4.0", features = ["derive"] }
serde = "1.0.218"
words-count = "0.1.6"
yaml-front-matter = "0.1.0"
regex = "1.11.1"
pulldown-cmark = { version = "0.13.0", default-features = false }
Expand All @@ -27,3 +26,4 @@ obsidian-rs = { git = "https://github.com/adamisrael/obsidian-rs.git" }
rand = "0.9.1"
thiserror = "2.0.12"
thousands = "0.2.0"
md-word-count = "0.1.1"
19 changes: 13 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Syntax: md2ms [options] <file>
// md2ms --output-dir <dir> <files>
use md2ms::constants;

use clap::Parser;
use docx_rs::*;
use md_word_count::count_words;
use thousands::Separable;
use yaml_front_matter::Document;

use std::path::PathBuf;

use docx_rs::*;
use md2ms::constants;
use md2ms::context::Context;
use md2ms::error::Md2msError;
use md2ms::markdown::flatten_markdown;
Expand Down Expand Up @@ -174,17 +175,23 @@ fn compile(ctx: &mut Context) -> Result<(), Md2msError> {

match flatten_markdown(ctx, mddoc) {
Ok(md) => {
// Using this crate for now, but maybe convert this to my own code
let wc = words_count::count(md.iter().map(|p| p.raw_text()).collect::<String>());
// Calculate the word count by iterating through the raw Markdown files.
let mut wc = 0;
for (f, markdown) in ctx.clone().files {
if f == "metadata.md" {
continue;
}
wc += count_words(markdown.content.as_str());
}

// If the author wants the word count, give them the exact count, not the approximate value.
if ctx.word_count {
println!("Exact word count: {}", wc.words.separate_with_commas());
println!("Exact word count: {}", wc.separate_with_commas());
return Ok(());
}

// Round up for the manuscript
let nwc = round_up(wc.words);
let nwc = round_up(wc);

// A PathBuf to build the path to the output file
let output_dir = shellexpand::tilde(&ctx.output_dir.to_string_lossy()).to_string();
Expand Down
7 changes: 4 additions & 3 deletions src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// use std::collections::HashMap;

use docx_rs::*;
use regex::Regex;
use yaml_front_matter::{Document, YamlFrontMatter};

use crate::cmark::parse_paragraph;
use crate::constants;
use crate::context::Context;
use crate::error::Md2msError;
use crate::metadata::Metadata;
use crate::pii::PII;
use docx_rs::*;
use regex::Regex;
use yaml_front_matter::{Document, YamlFrontMatter};

/// Strip Markdown comments out of the content
fn strip_comments(mut content: String) -> String {
Expand Down
Loading