Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap source lines in mdbook-i18n-normalize #121

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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