Skip to content

Commit

Permalink
double backticks
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet committed Oct 16, 2024
1 parent c81a152 commit bfc4513
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
30 changes: 30 additions & 0 deletions lib/ProductOpener/KnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ sub create_knowledge_panels ($product_ref, $target_lc, $target_cc, $options_ref,
Helper function to allow to enter multiline strings in JSON templates.
The function converts the multiline string into a single line string.
New lines are converted to \n, and quotes " and \ are escaped if not escaped already.
=cut

sub convert_multiline_string_to_singleline ($line) {
Expand All @@ -287,6 +289,30 @@ sub convert_multiline_string_to_singleline ($line) {
return '"' . $line . '"';
}

=head2 convert_multiline_string_to_singleline_without_line_breaks_and_extra_spaces($line)
Helper function to allow to enter multiline strings in JSON templates.
The function converts the multiline string into a single line string.
Line breaks are converted to spaces, and multiple spaces are converted to a single space.
This function is useful in templates where we use IF statements etc. to generate a single value like a title.
=cut

sub convert_multiline_string_to_singleline_without_line_breaks_and_extra_spaces ($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;

$line =~ s/\s+/ /g;
$line =~ s/^\s+//;
$line =~ s/\s+$//;

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

=head2 create_panel_from_json_template ( $panel_id, $panel_template, $panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref )
Creates a knowledge panel from a JSON template.
Expand All @@ -301,6 +327,8 @@ Some special features that are not included in the JSON format are supported:
- The multiline strings will be converted to a single string.
- Quotes " are automatically escaped unless they are already escaped
Using two backticks at the start and end of the string removes line breaks and extra spaces.
3. Comments can be included by starting a line with //
- Comments will be removed in the resulting JSON, they are only intended to make the source template easier to understand.
Expand Down Expand Up @@ -386,6 +414,8 @@ sub create_panel_from_json_template ($panel_id, $panel_template, $panel_data_ref

# Also escape quotes " to \"

$panel_json
=~ s/\`\`([^\`]*)\`\`/convert_multiline_string_to_singleline_without_line_breaks_and_extra_spaces($1)/seg;
$panel_json =~ s/\`([^\`]*)\`/convert_multiline_string_to_singleline($1)/seg;

# Remove trailing commas at the end of a string delimited by quotes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@
"expanded": false,
[% END %]
"title_element": {
// Note: the app displays line feeds as line feeds...
"title": ` [% dash = "-" %] [% dash.repeat(panel.level) %] [% display_taxonomy_tag_name("ingredients", panel.ingredient_id) %] [% IF panel.ingredient.percent.defined %] ([% round(panel.ingredient.percent) %]%) [% ELSIF panel.ingredient.percent_estimate.defined %] ([% round(panel.ingredient.percent_estimate) %]% [% lang("estimate") %]) [% END %] `,
// double backticks: convert to a single line without newlines and extra spaces
"title": ``
[% dash = "-" %]
[% dash.repeat(panel.level) %]

[% display_taxonomy_tag_name("ingredients", panel.ingredient_id) %]
[% IF panel.ingredient.percent.defined %]
([% round(panel.ingredient.percent) %]%)
[% ELSIF panel.ingredient.percent_estimate.defined %]
([% round(panel.ingredient.percent_estimate) %]% [% lang("estimate") %])
[% END %]
``,
},
"elements": [
[% IF panel.ingredient_description -%]
[% IF panel.ingredient_description %]
{
"element_type": "text",
"text_element": {
"html": `[% panel.ingredient_description %]`
}
}
[% ELSIF panel.wikipedia_abstract -%]
[% ELSIF panel.wikipedia_abstract %]
{
"element_type": "text",
"text_element": {
Expand Down

0 comments on commit bfc4513

Please sign in to comment.