mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
Merge pull request #1898 from StDenis1980/tinta/upstream-application-name
docx: set_application_name for apps that embed the writer
This commit is contained in:
3 files changed
+33
-1
No files matched your search
@@ -20,6 +20,19 @@ mod custom;
|
||||
mod flat;
|
||||
mod fonts;
|
||||
mod package;
|
||||
|
||||
static APPLICATION_NAME: std::sync::OnceLock<String> = std::sync::OnceLock::new();
|
||||
|
||||
/// Names the application written to `docProps/app.xml` (`<Application>`), for an app that
|
||||
/// embeds this writer under its own name; "WordCraft" until set. Only the first call counts.
|
||||
pub fn set_application_name(name: &str) {
|
||||
let _ = APPLICATION_NAME.set(name.to_owned());
|
||||
}
|
||||
|
||||
/// The application `docProps/app.xml` names (see [`set_application_name`]).
|
||||
pub fn application_name() -> &'static str {
|
||||
APPLICATION_NAME.get().map_or("WordCraft", String::as_str)
|
||||
}
|
||||
mod read;
|
||||
mod smart_art;
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -353,7 +353,7 @@ pub(super) fn app_xml(doc: &Document) -> Vec<u8> {
|
||||
("xmlns:vt", "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"),
|
||||
],
|
||||
);
|
||||
w.leaf("Application", &[], "WordCraft");
|
||||
w.leaf("Application", &[], crate::application_name());
|
||||
w.leaf("Words", &[], &doc.word_count().to_string());
|
||||
w.leaf("Paragraphs", &[], &doc.paragraph_count().to_string());
|
||||
w.leaf("DocSecurity", &[], "0");
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
//! An app that embeds the writer names itself in `docProps/app.xml` (its own test binary: the
|
||||
//! name is set once per process).
|
||||
|
||||
use std::io::Read;
|
||||
|
||||
#[test]
|
||||
fn the_embedding_app_names_itself_in_app_xml() {
|
||||
assert_eq!(craft_docx::application_name(), "WordCraft", "the default");
|
||||
craft_docx::set_application_name("Example Writer");
|
||||
craft_docx::set_application_name("Other");
|
||||
assert_eq!(craft_docx::application_name(), "Example Writer", "only the first call counts");
|
||||
let mut d = craft_wordmodel::Document::new();
|
||||
d.body = vec![craft_wordmodel::para_block(craft_wordmodel::Paragraph::with_text("Text", Default::default()))];
|
||||
let bytes = craft_docx::write(&d).unwrap();
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap();
|
||||
let mut app = String::new();
|
||||
z.by_name("docProps/app.xml").unwrap().read_to_string(&mut app).unwrap();
|
||||
assert!(app.contains("<Application>Example Writer</Application>"), "{app}");
|
||||
}
|
||||
Reference in new issue
Block a user