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

Fix broken tables whose msgstr is a specific type of HTML #253

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions i18n-helpers/src/gettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,34 @@ mod tests {
);
}

#[test]
fn test_translate_html_in_table() {
let catalog = create_catalog(&[
("Icon", "ICON"),
("Description", "DESCRIPTION"),
(
"<img src=\"some-icon.svg\" alt=\"Some icon.\"/>",
"<img src=\"some-icon.svg\" alt=\"SOME ICON.\"/>",
),
("Some description.", "SOME DESCRIPTION."),
]);
// The alignment is lost when we generate new Markdown.
assert_eq!(
translate(
"\
| Icon | Description |\n\
|-------------------------------------------------|-------------------|\n\
| <img src=\"some-icon.svg\" alt=\"Some icon.\"/> | Some description. |",
&catalog
)
.unwrap(),
"\
|ICON|DESCRIPTION|\n\
|----|-----------|\n\
|<img src=\"some-icon.svg\" alt=\"SOME ICON.\"/>|SOME DESCRIPTION.|"
);
}

#[test]
fn test_footnote() {
let catalog = create_catalog(&[
Expand Down
63 changes: 61 additions & 2 deletions i18n-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ pub fn new_cmark_parser<'input, F: BrokenLinkCallback<'input>>(
/// Extract Markdown events from `text`.
///
/// The `state` can be used to give the parsing context. In
/// particular, if a code block has started, the text should be parsed
/// without interpreting special Markdown characters.
/// particular:
///
/// - If a code block has started, the text should be parsed
/// without interpreting special Markdown characters.
/// - In a table cell, the text should be parsed as inlines.
///
/// The events are labeled with the line number where they start in
/// the document.
Expand Down Expand Up @@ -137,6 +140,33 @@ pub fn extract_events<'a>(text: &'a str, state: Option<State<'a>>) -> Vec<(usize
.enumerate()
.map(|(idx, line)| (idx + 1, Event::Text(line.into())))
.collect(),
// If we're in a table cell, we put the text in a minimal table, parse the
// table, and return the contents of the cell. This matches the behavior of
// the parser in this case.
Some(state) if state.in_table_cell => {
let text = format!("|{}|\n|-|", text);
new_cmark_parser::<'_, DefaultBrokenLinkCallback>(&text, None)
.filter_map(|event| {
let event = match event {
Event::Start(Tag::Table(..) | Tag::TableHead | Tag::TableCell)
| Event::End(TagEnd::Table | TagEnd::TableHead | TagEnd::TableCell) => {
return None
}
Event::SoftBreak => Event::Text(" ".into()),
// Shortcut links like "[foo]" end up as "[foo]"
// in output. By changing them to a reference
// link, the link is expanded on the fly and the
// output becomes self-contained.
Event::Start(tag @ (Tag::Link { .. } | Tag::Image { .. })) => {
Event::Start(expand_shortcut_link(tag))
}
_ => event,
};
// The line number is always 1 because tables don't allow newlines
Some((1, event.into_static()))
})
.collect()
}
// Otherwise, we parse the text line normally.
_ => new_cmark_parser::<'a, DefaultBrokenLinkCallback>(text, None)
.into_offset_iter()
Expand Down Expand Up @@ -851,6 +881,7 @@ pub fn translate_events<'a>(
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use pulldown_cmark::Alignment;
use pulldown_cmark::CodeBlockKind;
use pulldown_cmark::Event::*;
use pulldown_cmark::HeadingLevel::*;
Expand Down Expand Up @@ -977,6 +1008,34 @@ mod tests {
);
}

#[test]
fn extract_events_html_block() {
let (_, state) = reconstruct_markdown(
&[
(1, Start(Table(vec![Alignment::None]))),
(1, Start(TableHead)),
(1, Start(TableCell)),
],
None,
)
.unwrap();
// Should be parsed as an inline in a table.
assert_eq!(
extract_events("<img />", Some(state)),
vec![(1, InlineHtml("<img />".into()))]
);

// Compare with extraction without state:
assert_eq!(
extract_events("<img />", None),
vec![
(1, Start(HtmlBlock)),
(1, Html("<img />".into())),
(1, End(TagEnd::HtmlBlock)),
]
);
}

#[test]
fn extract_messages_empty() {
assert_extract_messages("", &[]);
Expand Down
Loading