DocBook: better condition for abstracts.

This commit is contained in:
Thibaut Cuvelier 2020-07-31 01:20:08 +02:00
parent 6434b666bf
commit acdead0487

View File

@ -36,6 +36,8 @@
#include "support/lstrings.h" #include "support/lstrings.h"
#include "support/textutils.h" #include "support/textutils.h"
#include "support/regex.h"
#include <stack> #include <stack>
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
@ -849,8 +851,29 @@ void outputDocBookInfo(
pit_type epitInfo; pit_type epitInfo;
tie(shouldBeInInfo, mustBeInInfo, bpitInfo, epitInfo) = info; tie(shouldBeInInfo, mustBeInInfo, bpitInfo, epitInfo) = info;
// The abstract must go in <info>. // Perform an additional check on the abstract. Sometimes, there are many paragraphs that should go
// into the abstract, but none generates actual content. Thus, first generate to a temporary stream,
// then only create the <abstract> tag if these paragraphs generate some content.
// This check must be performed *before* a decision on whether or not to output <info> is made.
bool hasAbstract = hasAbstractBetween(paragraphs, bpitAbstract, epitAbstract); bool hasAbstract = hasAbstractBetween(paragraphs, bpitAbstract, epitAbstract);
docstring abstract;
if (hasAbstract) {
odocstringstream os2;
XMLStream xs2(os2);
generateDocBookParagraphWithoutSectioning(text, buf, xs2, runparams, paragraphs, bpitAbstract, epitAbstract);
// Actually output the abstract if there is something to do. Don't count line feeds or spaces in this,
// even though they must be properly output if there is some abstract.
docstring abstractContent = os2.str();
static const lyx::regex reg("[ \\r\\n]*");
abstractContent = from_utf8(lyx::regex_replace(to_utf8(abstractContent), reg, string("")));
// Nothing? Then there is no abstract!
if (abstractContent.empty())
hasAbstract = false;
}
// The abstract must go in <info>.
bool needInfo = !mustBeInInfo.empty() || hasAbstract; bool needInfo = !mustBeInInfo.empty() || hasAbstract;
// Start the <info> tag if required. // Start the <info> tag if required.
@ -863,26 +886,16 @@ void outputDocBookInfo(
// Output the elements that should go in <info>. // Output the elements that should go in <info>.
generateDocBookParagraphWithoutSectioning(text, buf, xs, runparams, paragraphs, bpitInfo, epitInfo); generateDocBookParagraphWithoutSectioning(text, buf, xs, runparams, paragraphs, bpitInfo, epitInfo);
if (hasAbstract) { if (hasAbstract && !abstract.empty()) { // The second test is probably superfluous.
// Sometimes, there are many paragraphs that should go into the abstract, but none generates actual content. string tag = paragraphs[bpitAbstract].layout().docbookforceabstracttag();
// Thus, first generate to a temporary stream, then only create the <abstract> tag if these paragraphs if (tag == "NONE")
// generate some content. tag = "abstract";
odocstringstream os2;
XMLStream xs2(os2);
generateDocBookParagraphWithoutSectioning(text, buf, xs2, runparams, paragraphs, bpitAbstract, epitAbstract);
// Actually output the abstract if there is something to do. xs << xml::StartTag(tag);
if (!os2.str().empty()) { xs << xml::CR();
string tag = paragraphs[bpitAbstract].layout().docbookforceabstracttag(); xs << XMLStream::ESCAPE_NONE << abstract;
if (tag == "NONE") xs << xml::EndTag(tag);
tag = "abstract"; xs << xml::CR();
xs << xml::StartTag(tag);
xs << xml::CR();
xs << XMLStream::ESCAPE_NONE << os2.str();
xs << xml::EndTag(tag);
xs << xml::CR();
}
} }
// End the <info> tag if it was started. // End the <info> tag if it was started.