Skip to content

Commit

Permalink
Check for duplicate sources in messages (#156)
Browse files Browse the repository at this point in the history
* Add a check to prevent duplicate sources in messages
* Add a test
* Deduplicate sources after all messages are added to the catalog
  • Loading branch information
zachcmadsen authored Feb 8, 2024
1 parent c6e2cb5 commit 4853f55
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions i18n-helpers/src/xgettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use anyhow::{anyhow, Context};
use mdbook::renderer::RenderContext;
use mdbook::BookItem;
use polib::catalog::Catalog;
use polib::message::Message;
use polib::message::{Message, MessageMutView, MessageView};
use polib::metadata::CatalogMetadata;
use pulldown_cmark::{Event, Tag};

Expand All @@ -41,7 +41,7 @@ fn strip_link(text: &str) -> String {

fn add_message(catalog: &mut Catalog, msgid: &str, source: &str) {
let sources = match catalog.find_message(None, msgid, None) {
Some(msg) => wrap_sources(&format!("{}\n{}", msg.source(), source)),
Some(msg) => format!("{}\n{}", msg.source(), source),
None => String::from(source),
};
let message = Message::build_singular()
Expand Down Expand Up @@ -76,6 +76,16 @@ fn build_source<P: AsRef<path::Path>>(path: P, lineno: usize, granularity: usize
}
}

fn dedup_sources(catalog: &mut Catalog) {
for mut message in catalog.messages_mut() {
let mut lines: Vec<&str> = message.source().lines().collect();
lines.dedup();

let wrapped_source = wrap_sources(&lines.join("\n"));
*message.source_mut() = wrapped_source;
}
}

/// Build catalog from RenderContext
///
/// # Arguments
Expand Down Expand Up @@ -144,6 +154,8 @@ where
}
}

dedup_sources(&mut catalog);

Ok(catalog)
}

Expand Down Expand Up @@ -398,4 +410,39 @@ mod tests {

Ok(())
}

#[test]
fn test_create_catalog_lineno_granularity_duplicates() -> anyhow::Result<()> {
let (ctx, _tmp) = create_render_context(&[
(
"book.toml",
"[book]\n\
[output.xgettext]\n\
granularity = 3",
),
("src/SUMMARY.md", "- [Foo](foo.md)"),
(
"src/foo.md",
"Bar\n\
\n\
Bar\n\
\n\
Bar\n",
),
])?;

let catalog = create_catalog(&ctx, std::fs::read_to_string)?;
assert_eq!(
catalog
.messages()
.map(|msg| (msg.source(), msg.msgid()))
.collect::<Vec<_>>(),
&[
("src/SUMMARY.md:1", "Foo"),
("src/foo.md:1 src/foo.md:3", "Bar"),
]
);

Ok(())
}
}

0 comments on commit 4853f55

Please sign in to comment.