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: escape issue in additives knowledge panel - remove latex formulas #8340

Merged
merged 7 commits into from
May 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
30 changes: 26 additions & 4 deletions lib/ProductOpener/KnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,13 @@ The function converts the multiline string into a single line string.

sub convert_multiline_string_to_singleline ($line) {

# Escape " and \ unless they have been escaped already
# negative look behind to not convert \n to \\n or \" to \\" or \\ to \\\\
$line =~ s/(?<!\\)("|\\)/\\$1/g;

# \R will match all Unicode newline sequence
$line =~ s/\R/\\n/sg;
# Escape quotes unless they have been escaped already
# negative look behind to not convert \" to \\"
$line =~ s/(?<!\\)"/\\"/g;

return '"' . $line . '"';
}

Expand Down Expand Up @@ -1290,6 +1292,26 @@ sub create_ingredients_analysis_panel ($product_ref, $target_lc, $target_cc, $op
return;
}

sub remove_latex_sequences ($string) {

# Some wikipedia abstracts have chemical formulas like {\\displaystyle \\mathrm {NaNO_{3}} +\\mathrm {Pb} \\to \\mathrm {NaNO_{2}} +\\mathrm {PbO} }
# In practice, we remove everything between { }

$string =~ s/
(
{ # match an opening {
(?:
[^{}]++ # one or more non angle brackets, non backtracking
|
(?1) # found { or }, so recurse to capture group 1
)*
} # match a closing }
)
//xg;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So good you used extended format for regexp ❤️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexgarel Well I adapted some code I found on Stack Overflow that had this extended format which is indeed quite cool. That's after ChatGPT gave me 3 wrong answers. ;)


return $string;
}

=head2 add_taxonomy_properties_in_target_languages_to_object ( $object_ref, $tagtype, $tagid, $properties_ref, $target_lcs_ref )

This function adds to the hash ref $object_ref (for instance a data structure passed to a template) the values
Expand Down Expand Up @@ -1332,7 +1354,7 @@ sub add_taxonomy_properties_in_target_languages_to_object ($object_ref, $tagtype
}
}
if (defined $property_value) {
$object_ref->{$property} = $property_value;
$object_ref->{$property} = remove_latex_sequences($property_value);
$object_ref->{$property . "_lc"} = $property_lc;
$object_ref->{$property . "_language"}
= display_taxonomy_tag($target_lcs_ref->[0], "languages", $language_codes{$property_lc});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"type": "summary",
"html": `
<p>[% lang('source_ademe_agribalyse_for_category') %]
<a href=\"https://agribalyse.ademe.fr/app/aliments/[% product.ecoscore_data.agribalyse.code %]\">[% panel.agribalyse_category_name.dquote %]</a>
<a href="https://agribalyse.ademe.fr/app/aliments/[% product.ecoscore_data.agribalyse.code %]">[% panel.agribalyse_category_name.dquote %]</a>
([% lang('source_ademe_agribalyse') %])
</p>
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"type": "summary",
"html": `
<p>[% lang('categories_s') FILTER ucfirst %][% sep %]:
<a href=\"https://agribalyse.ademe.fr/app/aliments/[% product.ecoscore_data.agribalyse.code %]\">[% panel.agribalyse_category_name %]</a>
<a href="https://agribalyse.ademe.fr/app/aliments/[% product.ecoscore_data.agribalyse.code %]">[% panel.agribalyse_category_name %]</a>
</p>
<ul>
<li>
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/knowledge_panels.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/perl -w

use Modern::Perl '2017';
use utf8;

use Test::More;
use Log::Any::Adapter 'TAP';

use ProductOpener::KnowledgePanels qw/:all/;

is(
ProductOpener::KnowledgePanels::convert_multiline_string_to_singleline('
test
muti-line string
a slash A / B and anti-slash \
HTML <a href="https://url.com">test</a>
'),
'"\ntest\nmuti-line string\na slash A / B and anti-slash \\\\\nHTML <a href=\"https://url.com\">test</a>\n"'
);

is(
ProductOpener::KnowledgePanels::convert_multiline_string_to_singleline(
'<a href="https://agribalyse.ademe.fr/app/aliments/[% product.ecoscore_data.agribalyse.code %]">'),
'"<a href=\"https://agribalyse.ademe.fr/app/aliments/[% product.ecoscore_data.agribalyse.code %]\">"'
);

done_testing();