Skip to content

Commit

Permalink
Wrap source lines in mdbook-i18n-normalize
Browse files Browse the repository at this point in the history
Noticed by @henrif75 in
google/comprehensive-rust#317.

Part of #32 and a followup to #93.
  • Loading branch information
mgeisler committed Nov 10, 2023
1 parent 3044744 commit 251f6d0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 1 addition & 4 deletions i18n-helpers/src/bin/mdbook-xgettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ fn strip_link(text: &str) -> String {
}

fn add_message(catalog: &mut Catalog, msgid: &str, source: &str) {
let wrap_options = textwrap::Options::new(76)
.break_words(false)
.word_splitter(textwrap::WordSplitter::NoHyphenation);
let sources = match catalog.find_message(None, msgid, None) {
Some(msg) => textwrap::refill(&format!("{}\n{}", msg.source(), source), wrap_options),
Some(msg) => wrap_sources(&format!("{}\n{}", msg.source(), source)),
None => String::from(source),
};
let message = Message::build_singular()
Expand Down
11 changes: 11 additions & 0 deletions i18n-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ use syntect::parsing::{ParseState, Scope, ScopeStack, SyntaxSet};
pub mod gettext;
pub mod normalize;

/// Re-wrap the sources field of a message.
///
/// This function tries to wrap the `file:lineno` pairs so they look
/// the same as what you get from `msgcat` or `msgmerge`.
pub fn wrap_sources(sources: &str) -> String {
let wrap_options = textwrap::Options::new(76)
.break_words(false)
.word_splitter(textwrap::WordSplitter::NoHyphenation);
textwrap::refill(sources, wrap_options)
}

/// Like `mdbook::utils::new_cmark_parser`, but also passes a
/// `BrokenLinkCallback`.
pub fn new_cmark_parser<'input, 'callback>(
Expand Down
4 changes: 2 additions & 2 deletions i18n-helpers/src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashMap;
use std::fs::File;
use std::io::Read;

use super::{extract_messages, new_cmark_parser};
use crate::{extract_messages, new_cmark_parser, wrap_sources};
use polib::catalog::Catalog;
use polib::message::{Message, MessageFlags, MessageMutView, MessageView};
use pulldown_cmark::{Event, LinkType, Tag};
Expand All @@ -30,7 +30,7 @@ fn compute_source(source: &str, delta: usize) -> String {
}
}

new_source
wrap_sources(&new_source)
}

/// Check if `text` contains one or more broken reference links.
Expand Down

0 comments on commit 251f6d0

Please sign in to comment.