Skip to content

Commit

Permalink
Merge pull request #121 from google/wrap-sources
Browse files Browse the repository at this point in the history
Wrap source lines in `mdbook-i18n-normalize`
  • Loading branch information
mgeisler authored Nov 11, 2023
2 parents 7c60056 + c6b77a8 commit 2cdf82e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 2 additions & 5 deletions i18n-helpers/src/bin/mdbook-xgettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use anyhow::{anyhow, Context};
use mdbook::renderer::RenderContext;
use mdbook::BookItem;
use mdbook_i18n_helpers::{extract_events, extract_messages, reconstruct_markdown};
use mdbook_i18n_helpers::{extract_events, extract_messages, reconstruct_markdown, wrap_sources};
use polib::catalog::Catalog;
use polib::message::Message;
use polib::metadata::CatalogMetadata;
Expand All @@ -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 options = textwrap::Options::new(76)
.break_words(false)
.word_splitter(textwrap::WordSplitter::NoHyphenation);
textwrap::refill(sources, 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 2cdf82e

Please sign in to comment.