From 8e0bc01d50fc7d7af454cb8bed4d098f85b3f73b Mon Sep 17 00:00:00 2001 From: Thibaut Cuvelier Date: Mon, 26 Feb 2024 16:04:22 +0100 Subject: [PATCH] DocBook: when a similar tag is present multiple times in the bibliography entry, output it only once. Other copies have an error message. --- autotests/export/docbook/basic.xml | 32 ++---------- src/insets/InsetBibtex.cpp | 80 +++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 44 deletions(-) diff --git a/autotests/export/docbook/basic.xml b/autotests/export/docbook/basic.xml index 87c951f359..c03a26c12d 100644 --- a/autotests/export/docbook/basic.xml +++ b/autotests/export/docbook/basic.xml @@ -1,5 +1,5 @@ -
@@ -67,30 +67,8 @@ - - - I  - a - m  - a  - f - o - r - m - u - l - a  - w - i - t - h  - a  - r - e - f - . - - + I am a formula with a ref + . (1) @@ -285,8 +263,8 @@ I am no more code.
I am the sixth section and I really like bibliographies -This text has references. First reference: . Second reference: . Both at the same time: , . A book: . -Many things, just testing for completeness: , , , , , . +This text has references. First reference: . Second reference: . Both at the same time: , . A book: . +Many things, just testing for completeness: , , , , , .
I am the seventh section and I deal with indices diff --git a/src/insets/InsetBibtex.cpp b/src/insets/InsetBibtex.cpp index 6d38631f81..8a2a697648 100644 --- a/src/insets/InsetBibtex.cpp +++ b/src/insets/InsetBibtex.cpp @@ -1128,7 +1128,6 @@ void InsetBibtex::docbook(XMLStream & xs, OutputParams const &) const make_pair("url", "uri") }; // Relations between documents. - // TODO: some elements should be mutually exclusive; right now, all of them are output. vector> relations = { // make_pair("journal", "journal"), make_pair("journaltitle", "journal"), @@ -1149,7 +1148,6 @@ void InsetBibtex::docbook(XMLStream & xs, OutputParams const &) const toDocBookTag["fullbynames:editor"] = "SPECIFIC"; // No direct translation to DocBook: . toDocBookTag["institution"] = "SPECIFIC"; // No direct translation to DocBook: . - // TODO: some elements should be mutually exclusive; right now, all of them are output. toDocBookTag["title"] = "title"; toDocBookTag["fulltitle"] = "title"; toDocBookTag["quotetitle"] = "title"; @@ -1162,7 +1160,6 @@ void InsetBibtex::docbook(XMLStream & xs, OutputParams const &) const toDocBookTag["year"] = "SPECIFIC"; // No direct translation to DocBook: . toDocBookTag["month"] = "SPECIFIC"; // No direct translation to DocBook: . - // TODO: some elements should be mutually exclusive; right now, all of them are output. toDocBookTag["journal"] = "SPECIFIC"; // No direct translation to DocBook: . toDocBookTag["journaltitle"] = "SPECIFIC"; // No direct translation to DocBook: . toDocBookTag["fulljournaltitle"] = "SPECIFIC"; // No direct translation to DocBook: . @@ -1210,22 +1207,35 @@ void InsetBibtex::docbook(XMLStream & xs, OutputParams const &) const map delayedTags; // Read all tags from HTML and convert those that have a 1:1 matching. + // Avoid outputting the same tag twice in DocBook: several bibliography tags might map to the same DocBook + // element, avoid outputting the same DocBook tag twice to keep a valid output. "SPECIFIC" tags are handled in + // a more specific way later on (among the delayed tags). + set alreadyOutputDocBookTags; while (tagIt != tagEnd) { string tag = tagIt->str(); // regex_match cannot work with temporary strings. ++tagIt; if (regex_match(tag, match, tagRegex)) { - if (toDocBookTag[match[1]] == "SPECIFIC") { + const string docbookTag = toDocBookTag[match[1]]; + if (docbookTag == "SPECIFIC") { delayedTags[match[1]] = match[2]; } else { - xs << xml::StartTag(toDocBookTag[match[1]]); - xs << from_utf8(match[2].str()); - xs << xml::EndTag(toDocBookTag[match[1]]); - xs << xml::CR(); + if (alreadyOutputDocBookTags.contains(docbookTag)) { + xs << XMLStream::ESCAPE_NONE << + from_utf8("\n"); + } else { + xs << xml::StartTag(docbookTag); + xs << from_utf8(match[2].str()); + xs << xml::EndTag(docbookTag); + xs << xml::CR(); + } } } else { LYXERR0("The BibTeX field " << match[1].str() << " is unknown."); - xs << XMLStream::ESCAPE_NONE << from_utf8("\n"); + xs << XMLStream::ESCAPE_NONE << + from_utf8("\n"); } } @@ -1341,15 +1351,50 @@ void InsetBibtex::docbook(XMLStream & xs, OutputParams const &) const // // Example: http://tdg.docbook.org/tdg/5.1/biblioset.html for (auto const & id: relations) { + std::string keptJournal; + std::string keptBook; + if (hasTag(id.first)) { - xs << xml::StartTag("biblioset", "relation=\"" + id.second + "\""); - xs << xml::CR(); - xs << xml::StartTag("title"); - xs << getTag(id.first); - xs << xml::EndTag("title"); - xs << xml::CR(); - xs << xml::EndTag("biblioset"); - xs << xml::CR(); + bool outputThisTag = true; + + // Deal with duplicate entries for the same semantics. + if (id.first == "journal" || id.first == "journaltitle" || id.first == "fulljournaltitle") { + if (!keptJournal.empty()) { + xs << XMLStream::ESCAPE_NONE << + from_utf8("\n"); + outputThisTag = false; + } else { + keptJournal = id.first; + } + } else if (id.first == "booktitle" || id.first == "fullbooktitle") { + if (!keptBook.empty()) { + xs << XMLStream::ESCAPE_NONE << + from_utf8("\n"); + outputThisTag = false; + } else { + keptBook = id.first; + } + } + + // Output this tag only if it is not a duplicate of a previously output tag. + if (outputThisTag) { + xs << xml::StartTag("biblioset", "relation=\"" + id.second + "\""); + xs << xml::CR(); + xs << xml::StartTag("title"); + xs << getTag(id.first); + xs << xml::EndTag("title"); + xs << xml::CR(); + xs << xml::EndTag("biblioset"); + xs << xml::CR(); + } + + // In all cases, erase this tag: it has been dealt with. eraseTag(id.first); } } @@ -1383,6 +1428,7 @@ void InsetBibtex::docbook(XMLStream & xs, OutputParams const &) const if (hasTag("editor") && hasTag("fullbynames:editor")) { xs << XMLStream::ESCAPE_NONE << from_utf8("\n"); }