Skip to content

Commit 9398240

Browse files
committed
Resolve broken links using the original sources
This further improves the normalization of PO files generated with post-0.1.0 versions of `mdbook-xgettext`. Such files don’t include the link definitions at all and instead rely on the original source text at `mdbook build` time. With this step, we end up with less than 5 broken links in most of the translation files from Comprehensive Rust.
1 parent 5edebfc commit 9398240

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/bin/mdbook-i18n-normalize.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//! losing existing translations.
1212
1313
use std::collections::HashMap;
14+
use std::fs::File;
15+
use std::io::Read;
1416
use std::path::Path;
1517

1618
use anyhow::{bail, Context};
@@ -130,7 +132,10 @@ impl<'a> SourceMap<'a> {
130132
None => return Ok(extract_messages(document)),
131133
};
132134

133-
// Construct a full document using all messages from `path`.
135+
// First, we try constructing a document using other messages
136+
// from the catalog. Catalogs from pre-0.1.0 included the link
137+
// definitions.
138+
//
134139
// This will have quadratic complexity in case every message
135140
// from `path` has a "[some text][1]" link which needs to be
136141
// resolved using a table of link definitions as the bottom.
@@ -146,6 +151,15 @@ impl<'a> SourceMap<'a> {
146151
full_document.push_str(msg);
147152
}
148153

154+
// Second, we attempt to add the original source file.
155+
// Catalogs made with version 0.1.0 to 0.2.0 did not include
156+
// the link definitions at all, so we need to rely on the
157+
// source data (if we can find it).
158+
if let Ok(mut file) = File::open(path) {
159+
full_document.push_str("\n\n");
160+
let _ = file.read_to_string(&mut full_document);
161+
}
162+
149163
let mut messages = extract_messages(&full_document);
150164
// Truncate away the messages from `full_document` which start
151165
// after `document`.

0 commit comments

Comments
 (0)