Merge pull request #8283 from tchak/feat-more-lax-parser

fix(tags): relax parser roules
This commit is contained in:
Paul Chavard 2022-12-14 18:16:47 +01:00 committed by GitHub
commit 232245ffed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 6 deletions

View file

@ -12,6 +12,13 @@ module TagsSubstitutionConcern
doc.parse io
end
def self.normalize(str)
str
.sub(/^[[:space:]]+/, '')
.sub(/[[:space:]]+$/, '')
.gsub(/[[:space:]]/, ' ')
end
define_combinator :doc do
many(tag | text) < eof
end
@ -24,7 +31,7 @@ module TagsSubstitutionConcern
define_combinator :tag do
between(tag_delimiter, tag_delimiter, tag_text).fmap do |tag|
{ tag: tag }
{ tag: TagsParser.normalize(tag) }
end
end

View file

@ -9,7 +9,7 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
stable_id = @type_de_champ.stable_id
tags.push(
{
libelle: "#{libelle}/primaire",
libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)}/primaire",
id: "tdc#{stable_id}/primaire",
description: "#{description} (menu primaire)",
lambda: -> (champs) {
@ -19,7 +19,7 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
)
tags.push(
{
libelle: "#{libelle}/secondaire",
libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)}/secondaire",
id: "tdc#{stable_id}/secondaire",
description: "#{description} (menu secondaire)",
lambda: -> (champs) {

View file

@ -16,7 +16,7 @@ class TypesDeChamp::TypeDeChampBase
stable_id = self.stable_id
[
{
libelle: libelle.gsub(/[[:space:]]/, ' '),
libelle: TagsSubstitutionConcern::TagsParser.normalize(libelle),
id: "tdc#{stable_id}",
description: description,
lambda: -> (champs) {

View file

@ -512,10 +512,16 @@ describe TagsSubstitutionConcern, type: :model do
end
it 'allow for - before tag' do
tokens = TagsSubstitutionConcern::TagsParser.parse("hello --yolo-- world ---numéro-du - dossier--")
tokens = TagsSubstitutionConcern::TagsParser.parse("hello --yolo-- -- before-- --after -- -- around -- world ---numéro-du - dossier--")
expect(tokens).to eq([
{ text: "hello " },
{ tag: "yolo" },
{ text: " " },
{ tag: "before" },
{ text: " " },
{ tag: "after" },
{ text: " " },
{ tag: "around" },
{ text: " world -" },
{ tag: "numéro-du - dossier" }
])

View file

@ -38,7 +38,7 @@ describe Mails::InitiatedMail, type: :model do
context 'template with invalid tag' do
let(:email_body) { 'foo --numéro du -- bar' }
it { expect(subject.errors.full_messages).to eq(["Le contenu de lemail de notification de passage du dossier en instruction réfère au champ \"numéro du \" qui nexiste pas"]) }
it { expect(subject.errors.full_messages).to eq(["Le contenu de lemail de notification de passage du dossier en instruction réfère au champ \"numéro du\" qui nexiste pas"]) }
end
context 'template with unpublished tag' do