mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-02 22:30:10 +00:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
43ccc15c24 | ||
|
|
7777eba6e4 | ||
|
|
554a7d689e | ||
|
|
2c32076416 | ||
|
|
19b8dfece6 | ||
|
|
1336bc27cb | ||
|
|
a525210f48 |
@@ -342,6 +342,8 @@ pub(crate) fn new_from_template(s: &mut Session, v: &Value) -> CmdResult {
|
||||
let path = p::req_str(v, "path")?;
|
||||
let (mut doc, _) = read_document(s, path, v)?;
|
||||
doc.settings.attached_template = Some(path.to_string());
|
||||
// A new document: not created when the template was. Saving stamps its creation date.
|
||||
doc.core.created.clear();
|
||||
if p::bool(v, "replaceBlank") == Some(true) && s.used_because().is_none() {
|
||||
s.set_document(doc);
|
||||
} else {
|
||||
|
||||
@@ -422,7 +422,19 @@ pub fn update_fields_in(s: &mut Session, all: bool) -> Result<(), CmdError> {
|
||||
"DATE" | "TIME" | "CREATEDATE" | "SAVEDATE" | "PRINTDATE" => {
|
||||
let lang = field_lang(&s.doc, p, k);
|
||||
let pic = date_picture(instr).unwrap_or_else(|| default_picture(&name, lang.as_deref()));
|
||||
updates.push((k, text_result(instr, crate::cmd::insert::format_date_in(&pic, lang.as_deref()))));
|
||||
let created = s.doc.core.created.trim();
|
||||
// CREATEDATE shows when the document was created (ECMA-376 §17.16.5.11). A new
|
||||
// document, never saved, has no creation date yet and saving stamps now. A
|
||||
// file opened without one, or with one that can't be read, keeps the result
|
||||
// it has.
|
||||
let text = if name != "CREATEDATE" || (created.is_empty() && s.path.is_none()) {
|
||||
crate::cmd::insert::format_date_in(&pic, lang.as_deref())
|
||||
} else if let Some((unix, offset)) = created_at(created) {
|
||||
crate::cmd::insert::format_date_at_in(&pic, lang.as_deref(), unix, offset)
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
updates.push((k, text_result(instr, text)));
|
||||
}
|
||||
"REF" | "PAGEREF" => {
|
||||
let f = FieldCode::parse(instr);
|
||||
@@ -572,6 +584,24 @@ fn field_lang(doc: &wordcraft_doc::Document, p: &wordcraft_doc::Paragraph, k: us
|
||||
doc.styles.resolve_char(p.props.style.as_deref(), p.props_of_char(off)).lang
|
||||
}
|
||||
|
||||
/// A core-properties creation date as seconds since the epoch and the UTC offset to show it at:
|
||||
/// an instant (`2019-05-05T12:00:00Z`, any offset or fraction, or without the seconds,
|
||||
/// `2019-05-05T12:00Z`, as W3CDTF allows) at the local offset, or a calendar date (`2019-05-05`)
|
||||
/// at its own midnight, whatever the time zone. `None` for another form or a date before 1970.
|
||||
fn created_at(stamp: &str) -> Option<(u64, i64)> {
|
||||
let instant = chrono::DateTime::parse_from_rfc3339(stamp).ok().or_else(|| {
|
||||
// `%:z` takes `+01:00` but not `Z`.
|
||||
let utc = stamp.strip_suffix(['Z', 'z']).map(|t| format!("{t}+00:00"));
|
||||
chrono::DateTime::parse_from_str(utc.as_deref().unwrap_or(stamp), "%Y-%m-%dT%H:%M%:z").ok()
|
||||
});
|
||||
if let Some(t) = instant {
|
||||
let unix = u64::try_from(t.timestamp()).ok()?;
|
||||
return Some((unix, crate::cmd::local_offset_secs(unix)));
|
||||
}
|
||||
let day = chrono::NaiveDate::parse_from_str(stamp, "%Y-%m-%d").ok()?;
|
||||
Some((u64::try_from(day.and_hms_opt(0, 0, 0)?.and_utc().timestamp()).ok()?, 0))
|
||||
}
|
||||
|
||||
/// Word's default date or time picture when a field instruction carries no `\@` picture switch.
|
||||
fn default_picture(name: &str, lang: Option<&str>) -> String {
|
||||
if name == "TIME" { "h:mm am/pm".into() } else { crate::cmd::insert::default_date_picture(lang).into() }
|
||||
|
||||
@@ -506,6 +506,56 @@ fn update_fields_all_covers_every_story() {
|
||||
s.run("references.updateFields", &json!({"all": "yes"})).unwrap();
|
||||
}
|
||||
|
||||
/// CREATEDATE shows when the document was created (ECMA-376 §17.16.5.11), not today's date.
|
||||
#[test]
|
||||
fn createdate_keeps_the_creation_date_on_update() {
|
||||
let mut s = Session::new(wordcraft_doc::Document::new());
|
||||
s.doc.core.created = "2019-05-05T12:00:00Z".into();
|
||||
s.run("insert.field", &json!({"instr": r#"CREATEDATE \@ "yyyy-MM""#, "result": "2019-05"})).unwrap();
|
||||
s.run("insert.field", &json!({"instr": "CREATEDATE"})).unwrap();
|
||||
s.run("references.updateFields", &json!({})).unwrap();
|
||||
let results: Vec<String> = fields(&s).into_iter().map(|f| f.1).collect();
|
||||
// 12:00 UTC is 5 May from UTC-12 to UTC+11:59, the next day east of that.
|
||||
let unix = 1_557_057_600;
|
||||
let (_, _, day, _, _) = crate::cmd::local_civil(unix, crate::cmd::local_offset_secs(unix));
|
||||
assert_eq!(results, ["2019-05".to_string(), format!("5/{day}/2019")]);
|
||||
assert!(day == 5 || day == 6, "{day}");
|
||||
// W3CDTF may leave out the seconds; that is the same instant.
|
||||
for minutes in ["2019-05-05T12:00Z", "2019-05-05T14:00+02:00", "2019-05-05T07:00-05:00"] {
|
||||
// From another date first, so an unread stamp, which keeps the result, shows.
|
||||
s.doc.core.created = "2001-02-03".into();
|
||||
s.run("references.updateFields", &json!({})).unwrap();
|
||||
assert_eq!(fields(&s)[1].1, "2/3/2001");
|
||||
s.doc.core.created = minutes.into();
|
||||
s.run("references.updateFields", &json!({})).unwrap();
|
||||
let results: Vec<String> = fields(&s).into_iter().map(|f| f.1).collect();
|
||||
assert_eq!(results, ["2019-05".to_string(), format!("5/{day}/2019")], "{minutes}");
|
||||
}
|
||||
// A date without a time is that calendar day in every time zone.
|
||||
s.doc.core.created = "2019-06-07".into();
|
||||
s.run("references.updateFields", &json!({})).unwrap();
|
||||
let results: Vec<String> = fields(&s).into_iter().map(|f| f.1).collect();
|
||||
assert_eq!(results, ["2019-06", "6/7/2019"]);
|
||||
}
|
||||
|
||||
/// A creation date that can't be read leaves the field's result as it was; a document that has
|
||||
/// never been saved is created now (saving stamps that time as its creation date).
|
||||
#[test]
|
||||
fn createdate_without_a_readable_creation_date() {
|
||||
let mut s = Session::new(wordcraft_doc::Document::new());
|
||||
s.doc.core.created = "sometime in May".into();
|
||||
s.run("insert.field", &json!({"instr": r#"CREATEDATE \@ "yyyy-MM""#, "result": "2019-05"})).unwrap();
|
||||
assert_eq!(fields(&s)[0].1, "2019-05");
|
||||
// A file opened without a creation date keeps the result too.
|
||||
s.doc.core.created.clear();
|
||||
s.path = Some(std::env::temp_dir().join("opened.docx"));
|
||||
s.run("references.updateFields", &json!({})).unwrap();
|
||||
assert_eq!(fields(&s)[0].1, "2019-05");
|
||||
s.path = None;
|
||||
s.run("references.updateFields", &json!({})).unwrap();
|
||||
assert_eq!(fields(&s)[0].1, crate::cmd::insert::format_date("yyyy-MM"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn toc_tabs_ignore_nonentries() {
|
||||
let mut s = Session::new(wordcraft_doc::Document::from_text("Before\nContents\nOld\t9\nAfter"));
|
||||
|
||||
@@ -337,6 +337,29 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
/// A document made from a template is created now, not when the template was: it has no
|
||||
/// creation date until it is saved, so CREATEDATE in it shows today.
|
||||
#[test]
|
||||
fn new_from_template_has_no_creation_date_of_its_own_yet() {
|
||||
let d = scratch("created");
|
||||
let path = d.join("Dated.dotx");
|
||||
let mut t = Session::new(wordcraft_doc::Document::new());
|
||||
t.run("document.setText", &json!({"text": "Dated"})).unwrap();
|
||||
t.doc.core.created = "2019-05-05T12:00:00Z".into();
|
||||
crate::io::save_path(&path, &t.doc).unwrap();
|
||||
let mut s = Session::new(wordcraft_doc::Document::new());
|
||||
s.run("file.newFromTemplate", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
assert_eq!(s.doc.core.created, "");
|
||||
s.run("insert.field", &json!({"instr": r#"CREATEDATE \@ "yyyy""#, "result": "2019"})).unwrap();
|
||||
s.run("references.updateFields", &json!({})).unwrap();
|
||||
assert!(s.doc.plain_text(wordcraft_doc::StoryRef::Body).contains(&crate::cmd::insert::format_date("yyyy")));
|
||||
// The template itself keeps its date.
|
||||
let mut again = Session::new(wordcraft_doc::Document::new());
|
||||
again.run("file.open", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
assert_eq!(again.doc.core.created, "2019-05-05T12:00:00Z");
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
/// #3433: a template the platform hands the app takes an untouched blank document's place
|
||||
/// (`replaceBlank`), as a file `file.open` opens does; otherwise, and without the flag, the
|
||||
/// new document opens beside the others, as File › New's does. Either way the template on
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user