diff --git a/cypress/fixtures/example-ffas.xml b/cypress/fixtures/example-ffas.xml index 7847bd92..a5c35f47 100644 --- a/cypress/fixtures/example-ffas.xml +++ b/cypress/fixtures/example-ffas.xml @@ -435,6 +435,23 @@ val2 + + + test-unitdate-1 Année 1900 + + + + test-unitdate-2 Année 2000 + Année 2000 + + + + + test-unitdate-3 Année 3000 + Année 3000 + + + diff --git a/cypress/integration/recipes.js b/cypress/integration/recipes.js index 418a6a5e..d028eda2 100644 --- a/cypress/integration/recipes.js +++ b/cypress/integration/recipes.js @@ -374,4 +374,42 @@ describe("Recipe unit test", function () { expect(xpathFilter(doc, '//c[@id="test-genreform-physdesc"]/did/physdesc/genreform[@role="test"]').length).to.be.equal(1); expect(xpathFilter(doc, '//c[@id="test-genreform-physdesc"]/did/physdesc/genreform[@source="genreform"]').length).to.be.equal(2); }); + it("deplacer_unitdate_unittitle", function () { + const ficheC1 = xpathFilter(doc, '//c[@id="test-unitdate-1"]')[0]; + expect(xpathFilter(doc, ficheC1, 'did/unittitle/unitdate').length).to.be.equal(1); + expect(xpathFilter(doc, ficheC1, 'did/unitdate').length).to.be.equal(0); + + const ficheC2 = xpathFilter(doc, '//c[@id="test-unitdate-2"]')[0]; + expect(xpathFilter(doc, ficheC2, 'did/unittitle/unitdate').length).to.be.equal(1); + expect(xpathFilter(doc, ficheC2, 'did/unitdate').length).to.be.equal(1); + + const ficheC3 = xpathFilter(doc, '//c[@id="test-unitdate-3"]')[0]; + expect(xpathFilter(doc, ficheC3, 'did/unittitle/unitdate').length).to.be.equal(1); + expect(xpathFilter(doc, ficheC3, 'did/unitdate').length).to.be.equal(1); + + doc = recipes.deplacer_unitdate_unittitle()(doc); + + expect(xpathFilter(doc, ficheC1, 'did/unittitle/unitdate').length).to.be.equal(0); + expect(xpathFilter(doc, ficheC1, 'did/unitdate').length).to.be.equal(1); + expect(xpathFilter(doc, ficheC1, 'did/unitdate')[0].getAttribute("normal")).to.be.equal("1900"); + expect(xpathFilter(doc, ficheC1, 'did/unitdate')[0].textContent).to.be.equal("Année 1900"); + expect(xpathFilter(doc, ficheC1, 'did/unittitle')[0].textContent).to.be.equal("test-unitdate-1 Année 1900"); + + expect(xpathFilter(doc, ficheC2, 'did/unittitle/unitdate').length).to.be.equal(0); + expect(xpathFilter(doc, ficheC2, 'did/unitdate').length).to.be.equal(2); + expect(xpathFilter(doc, ficheC2, 'did/unitdate')[0].getAttribute("normal")).to.be.equal("2000"); + expect(xpathFilter(doc, ficheC2, 'did/unitdate')[0].hasAttribute("type")).to.be.false; + expect(xpathFilter(doc, ficheC2, 'did/unitdate')[1].getAttribute("normal")).to.be.equal("2000"); + expect(xpathFilter(doc, ficheC2, 'did/unitdate')[1].hasAttribute("type")).to.be.true; + expect(xpathFilter(doc, ficheC2, 'did/unitdate')[1].getAttribute("type")).to.be.equal("test"); + expect(xpathFilter(doc, ficheC2, 'did/unitdate')[1].textContent).to.be.equal("Année 2000"); + expect(xpathFilter(doc, ficheC2, 'did/unittitle')[0].textContent).to.be.equal("test-unitdate-2 Année 2000"); + + expect(xpathFilter(doc, ficheC3, 'did/unittitle/unitdate').length).to.be.equal(0); + expect(xpathFilter(doc, ficheC3, 'did/unitdate').length).to.be.equal(1); + expect(xpathFilter(doc, ficheC3, 'did/unitdate')[0].getAttribute("normal")).to.be.equal("3000"); + expect(xpathFilter(doc, ficheC3, 'did/unitdate')[0].getAttribute("type")).to.be.equal("test"); + expect(xpathFilter(doc, ficheC3, 'did/unitdate')[0].textContent).to.be.equal("Année 3000"); + expect(xpathFilter(doc, ficheC3, 'did/unittitle')[0].textContent).to.be.equal("test-unitdate-3 Année 3000"); + }); }); diff --git a/package.json b/package.json index f408e5fa..ea9fd458 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cuisine-ead", - "version": "1.4.0", + "version": "1.5.0", "description": "", "main": "public/index.html", "scripts": { diff --git a/src/lib/recipes/index.js b/src/lib/recipes/index.js index cc31ea54..689e9b4b 100644 --- a/src/lib/recipes/index.js +++ b/src/lib/recipes/index.js @@ -78,6 +78,7 @@ import separerControlaccessLb from "./individual-recipes/separer-controlaccess-l import separerControlaccessSeparator from "./individual-recipes/separer-controlaccess-separator.js"; import deplacerGenreformPhysdesc from "./individual-recipes/deplacer-genreform-physdesc.js"; import insertIntoDocument from "./insert-into-document.js"; +import deplacerUnitdateUnittitle from "./individual-recipes/deplacer-unitdate-unittitle.js"; /** * Returns an array of 'simple' recipes creators : functions that create functions that take a single DOM `Document` as argument and returns @@ -156,6 +157,7 @@ export const getRecipes = () => { { key: "remplace_dao_href", fn: remplaceDaoHref }, { key: "separer_controlaccess_lb", fn: separerControlaccessLb }, { key: "separer_controlaccess_separator", fn: separerControlaccessSeparator }, + { key: "deplacer_unitdate_unittitle", fn: deplacerUnitdateUnittitle }, ]; }; diff --git a/src/lib/recipes/individual-recipes/deplacer-unitdate-unittitle.js b/src/lib/recipes/individual-recipes/deplacer-unitdate-unittitle.js new file mode 100644 index 00000000..96342afc --- /dev/null +++ b/src/lib/recipes/individual-recipes/deplacer-unitdate-unittitle.js @@ -0,0 +1,53 @@ +//@flow +import { xpathFilter } from "../../xml.js"; +import { each, getAttributesMap } from "../utils.js"; +import { last, head } from "ramda"; + +/** + * Si on trouve un unitdate dans le unittitle, + * on le déplace dans le did en laissant juste la valeur texte dans le unittitle, + * S'il y a déjà un unitdate avec exactement la même valeur dans le did, on ne rajoute pas un doublon. + */ +export default () => (doc: Document): Document => { + const unitdatesDansTitle = xpathFilter(doc, "//c/did/unittitle/unitdate"); + console.log("unitdatesDansTitle", unitdatesDansTitle.length); + each(unitdatesDansTitle, (elem) => { + const attributesMap = getAttributesMap(elem); + const textContent = (""+elem?.textContent).trim(); + const did = head(xpathFilter(doc, elem, "ancestor::c/did")); + console.log(elem, attributesMap, textContent); + /** + * Pour qu'on considère que 2 unitdates sont identiques, ils doivent avoir la même valeur texte, la même valeur normalisée + * et le même type (s'il y en a un). + */ + const existingDidUnitdate = xpathFilter(doc, did, "unitdate").find((didUnitdate) => { + const didUnitdateAttributesMap = getAttributesMap(didUnitdate); + return ( + didUnitdateAttributesMap?.type === attributesMap?.type && + didUnitdateAttributesMap?.normal === attributesMap?.normal && + (""+didUnitdate?.textContent).trim() === textContent + ); + }); + console.log("existing = ", existingDidUnitdate); + if (!existingDidUnitdate) { + // ajouter le unitdate au did + if (!did) { + console.log("Pas de did pour créer le unitdate."); + } else { + const newElem = elem.cloneNode(true); + did.appendChild(newElem); + console.log("append dans did", newElem); + } + } + // remplacer elem dans son parent par textContent + const parent = elem.parentNode; + if (!parent) { + console.log("Pas de parent pour remplacer le unitdate."); + return; + } + const textNode = doc.createTextNode(textContent); + parent.replaceChild(textNode, elem); + console.log("replace", elem, "by", textNode, "in", parent); + }); + return doc; +}; diff --git a/src/lib/recipes/recipes-lib.js b/src/lib/recipes/recipes-lib.js index a12d2d26..dc7eb792 100644 --- a/src/lib/recipes/recipes-lib.js +++ b/src/lib/recipes/recipes-lib.js @@ -302,6 +302,14 @@ const availables: Array<[string, RecipeInfo]> = [ category: "Spécifique", }, ], + [ + "deplacer_unitdate_unittitle", + { + label: "Sortir les unitdate des unittitles", + complement: "Le texte de l'unitdate est laissé dans le unittitle. Si une unitdate existe déjà dans le did avec la même valeur normale et type, on ne rajoute pas un doublon.", + category: "Spécifique", + }, + ], [ "supprimer_internal", { diff --git a/src/sw.js b/src/sw.js index 49dd4d8f..21e685cb 100644 --- a/src/sw.js +++ b/src/sw.js @@ -6,7 +6,7 @@ const { CacheableResponsePlugin } = workbox.cacheableResponse; const { ExpirationPlugin } = workbox.expiration; const { skipWaiting, clientsClaim, setCacheNameDetails } = workbox.core; -var VERSION = "v1.4.0"; +var VERSION = "v1.5.0"; setCacheNameDetails({ prefix: "cuisine-ead", suffix: VERSION,