namespace {
-std::string fontToDocBookTag(xml::FontTypes type)
+std::string const fontToDocBookTag(xml::FontTypes type)
{
switch (type) {
case xml::FontTypes::FT_EMPH:
}
}
-
string fontToRole(xml::FontTypes type)
{
// Specific fonts are achieved with roles. The only common ones are "" for basic emphasis,
return "";
case xml::FontTypes::FT_BOLD:
return "bold";
- case xml::FontTypes::FT_NOUN: // Outputs a <person>
- case xml::FontTypes::FT_TYPE: // Outputs a <code>
- return "";
+ case xml::FontTypes::FT_NOUN:
+ return ""; // Outputs a <person>
+ case xml::FontTypes::FT_TYPE:
+ return ""; // Outputs a <code>
case xml::FontTypes::FT_UBAR:
return "underline";
- // All other roles are non-standard for DocBook.
+ // All other roles are non-standard for DocBook.
case xml::FontTypes::FT_WAVE:
return "wave";
// convenience functions
-void openParTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
+void openParTag(XMLStream &xs, Layout const &lay)
{
- Layout const & lay = par->layout();
-
- if (par == prevpar)
- prevpar = nullptr;
-
- // When should the wrapper be opened here? Only if the previous paragraph has the SAME wrapper tag
- // (usually, they won't have the same layout) and the CURRENT one allows merging.
- // The main use case is author information in several paragraphs: if the name of the author is the
- // first paragraph of an author, then merging with the previous tag does not make sense. Say the
- // next paragraph is the affiliation, then it should be output in the same <author> tag (different
- // layout, same wrapper tag).
- bool openWrapper = lay.docbookwrappertag() != "NONE";
- if (prevpar != nullptr) {
- Layout const & prevlay = prevpar->layout();
- if (prevlay.docbookwrappertag() != "NONE") {
- openWrapper = prevlay.docbookwrappertag() == lay.docbookwrappertag()
- && !lay.docbookwrappermergewithprevious();
- }
- }
-
- // Main logic.
- if (openWrapper)
+ if (lay.docbookwrappertag() != "NONE") {
xs << xml::StartTag(lay.docbookwrappertag(), lay.docbookwrapperattr());
+ }
string tag = lay.docbooktag();
if (tag == "Plain Layout")
tag = "para";
xs << xml::ParTag(tag, lay.docbookattr());
-
- if (lay.docbookitemtag() != "NONE")
- xs << xml::StartTag(lay.docbookitemtag(), lay.docbookitemattr());
}
-void closeTag(XMLStream & xs, Paragraph const * par, Paragraph const * nextpar)
+void closeTag(XMLStream &xs, Layout const &lay)
{
- Layout const & lay = par->layout();
-
- if (par == nextpar)
- nextpar = nullptr;
-
- // See comment in openParTag.
- bool closeWrapper = lay.docbookwrappertag() != "NONE";
- if (nextpar != nullptr) {
- Layout const & nextlay = nextpar->layout();
- if (nextlay.docbookwrappertag() != "NONE") {
- closeWrapper = nextlay.docbookwrappertag() == lay.docbookwrappertag()
- && !nextlay.docbookwrappermergewithprevious();
- }
- }
-
- // Main logic.
- if (lay.docbookitemtag() != "NONE")
- xs << xml::EndTag(lay.docbookitemtag());
-
string tag = lay.docbooktag();
if (tag == "Plain Layout")
tag = "para";
xs << xml::EndTag(tag);
- if (closeWrapper)
+ if (lay.docbookwrappertag() != "NONE")
xs << xml::EndTag(lay.docbookwrappertag());
}
}
-void openItemTag(XMLStream & xs, Layout const & lay)
+void openItemTag(XMLStream &xs, Layout const &lay)
{
xs << xml::StartTag(lay.docbookitemtag(), lay.docbookitemattr());
}
// Return true when new elements are output in a paragraph, false otherwise.
-bool openInnerItemTag(XMLStream & xs, Layout const & lay)
+bool openInnerItemTag(XMLStream &xs, Layout const &lay)
{
if (lay.docbookiteminnertag() != "NONE") {
xs << xml::CR();
}
-void closeInnerItemTag(XMLStream & xs, Layout const & lay)
+void closeInnerItemTag(XMLStream &xs, Layout const &lay)
{
if (lay.docbookiteminnertag()!= "NONE") {
xs << xml::EndTag(lay.docbookiteminnertag());
}
-inline void closeItemTag(XMLStream & xs, Layout const & lay)
+inline void closeItemTag(XMLStream &xs, Layout const &lay)
{
xs << xml::EndTag(lay.docbookitemtag());
xs << xml::CR();
// end of convenience functions
-ParagraphList::const_iterator findLast(
+ParagraphList::const_iterator findLastParagraph(
ParagraphList::const_iterator p,
- ParagraphList::const_iterator const & pend,
- LatexType type) {
- for (++p; p != pend && p->layout().latextype == type; ++p);
+ ParagraphList::const_iterator const & pend) {
+ for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p);
return p;
}
ParagraphList::const_iterator const & pend)
{
ParagraphList::const_iterator p = pstart;
+ Layout const &bstyle = p->layout();
size_t const depth = p->params().depth();
-
for (++p; p != pend; ++p) {
Layout const &style = p->layout();
// It shouldn't happen that e.g. a section command occurs inside
// FIXME I am not sure about the first check.
// Surely we *could* have different layouts that count as
// LATEX_PARAGRAPH, right?
- if (style.latextype == LATEX_PARAGRAPH || style != p->layout())
+ if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
return p;
}
-
return pend;
}
ParagraphList::const_iterator const & pbegin,
ParagraphList::const_iterator const & pend)
{
- auto const begin = text.paragraphs().begin();
- auto const end = text.paragraphs().end();
+ ParagraphList::const_iterator const begin = text.paragraphs().begin();
ParagraphList::const_iterator par = pbegin;
- ParagraphList::const_iterator prevpar = pbegin;
+ for (; par != pend; ++par) {
+ Layout const &lay = par->layout();
- for (; par != pend; prevpar = par, ++par) {
// We want to open the paragraph tag if:
// (i) the current layout permits multiple paragraphs
// (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
// because of branches, e.g., a branch that contains an entire new section.
// We do not really want to wrap that whole thing in a <div>...</div>.
bool special_case = false;
- Inset const *specinset = par->size() == 1 ? par->getInset(0) : nullptr;
+ Inset const *specinset = par->size() == 1 ? par->getInset(0) : 0;
if (specinset && !specinset->getLayout().htmlisblock()) { // TODO: Convert htmlisblock to a DocBook parameter?
Layout const &style = par->layout();
FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
}
// Plain layouts must be ignored.
- if (!special_case && buf.params().documentClass().isPlainLayout(par->layout()) && !runparams.docbook_force_pars)
+ if (!special_case && buf.params().documentClass().isPlainLayout(lay) && !runparams.docbook_force_pars)
special_case = true;
// TODO: Could get rid of this with a DocBook equivalent to htmlisblock?
if (!special_case && par->size() == 1 && par->getInset(0)) {
// or we're not in the last paragraph, anyway.
// (ii) We didn't open it and docbook_in_par is true,
// but we are in the first par, and there is a next par.
- auto nextpar = par;
+ ParagraphList::const_iterator nextpar = par;
++nextpar;
bool const close_par =
((open_par && (!runparams.docbook_in_par || nextpar != pend))
if (!cleaned.empty()) {
if (open_par)
- openParTag(xs, &*par, &*prevpar);
+ openParTag(xs, lay);
xs << XMLStream::ESCAPE_NONE << os2.str();
if (close_par) {
- closeTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
+ closeTag(xs, lay);
xs << xml::CR();
}
}
ParagraphList::const_iterator const & pbegin,
ParagraphList::const_iterator const & pend)
{
- auto const begin = text.paragraphs().begin();
- auto const end = text.paragraphs().end();
+ ParagraphList::const_iterator const begin = text.paragraphs().begin();
ParagraphList::const_iterator par = pbegin;
+ Layout const &bstyle = par->layout();
depth_type const origdepth = pbegin->params().depth();
- // Find the previous paragraph.
- auto prevpar = begin;
- if (prevpar != par) {
- auto prevpar_next = prevpar;
- ++prevpar_next;
-
- while (prevpar_next != par) {
- ++prevpar_next;
- ++prevpar;
- }
- }
-
// open tag for this environment
- openParTag(xs, &*par, &*prevpar);
+ openParTag(xs, bstyle);
xs << xml::CR();
// we will on occasion need to remember a layout from before.
// One is that we are still in the environment in which we
// started---which we will be if the depth is the same.
if (par->params().depth() == origdepth) {
- LATTEST(par->layout() == style);
+ LATTEST(bstyle == style);
if (lastlay != nullptr) {
closeItemTag(xs, *lastlay);
if (lastlay->docbookitemwrappertag() != "NONE") {
break;
}
case LATEX_PARAGRAPH:
- send = findLast(par, pend, LATEX_PARAGRAPH);
+ send = findLastParagraph(par, pend);
par = makeParagraphs(buf, xs, runparams, text, par, send);
break;
case LATEX_BIB_ENVIRONMENT:
- send = findLast(par, pend, LATEX_BIB_ENVIRONMENT);
+ send = findLastBibliographyParagraph(par, pend);
par = makeParagraphBibliography(buf, xs, runparams, text, par, send);
break;
case LATEX_COMMAND:
xs << xml::CR();
}
}
- auto nextpar = par;
- ++nextpar;
- closeTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
+ closeTag(xs, bstyle);
xs << xml::CR();
return pend;
}
Text const & text,
ParagraphList::const_iterator const & pbegin)
{
+ Layout const &style = pbegin->layout();
+
// No need for labels, as they are handled by DocBook tags.
- auto const begin = text.paragraphs().begin();
- auto const end = text.paragraphs().end();
- auto nextpar = pbegin;
- ++nextpar;
-
- // Find the previous paragraph.
- auto prevpar = begin;
- if (prevpar != pbegin) {
- auto prevpar_next = prevpar;
- ++prevpar_next;
-
- while (prevpar_next != pbegin) {
- ++prevpar_next;
- ++prevpar;
- }
- }
- // Generate this command.
- openParTag(xs, &*pbegin, &*prevpar);
+ openParTag(xs, style);
+ ParagraphList::const_iterator const begin = text.paragraphs().begin();
pbegin->simpleDocBookOnePar(buf, xs, runparams,
text.outerFont(distance(begin, pbegin)));
-
- closeTag(xs, &*pbegin, (nextpar != end) ? &*nextpar : nullptr);
+ closeTag(xs, style);
xs << xml::CR();
}
}
case LATEX_ENVIRONMENT:
case LATEX_LIST_ENVIRONMENT:
- case LATEX_ITEM_ENVIRONMENT:
+ case LATEX_ITEM_ENVIRONMENT: {
// FIXME Same fix here.
send = findEndOfEnvironment(par, pend);
par = makeEnvironment(buf, xs, ourparams, text, par, send);
break;
- case LATEX_PARAGRAPH:
- send = findLast(par, pend, LATEX_PARAGRAPH);
- par = makeParagraphs(buf, xs, ourparams, text, par, send);
- break;
- case LATEX_BIB_ENVIRONMENT:
- send = findLast(par, pend, LATEX_BIB_ENVIRONMENT);
+ }
+ case LATEX_BIB_ENVIRONMENT: {
+ send = findLastBibliographyParagraph(par, pend);
par = makeParagraphBibliography(buf, xs, ourparams, text, par, send);
break;
+ }
+ case LATEX_PARAGRAPH: {
+ send = findLastParagraph(par, pend);
+ par = makeParagraphs(buf, xs, ourparams, text, par, send);
+ break;
+ }
}
return make_pair(par, send);
using DocBookDocumentSectioning = tuple<bool, pit_type>;
-
-
-struct DocBookInfoTag
-{
- const set<pit_type> shouldBeInInfo;
- const set<pit_type> mustBeInInfo;
- const set<pit_type> abstract;
- pit_type bpit;
- pit_type epit;
-
- DocBookInfoTag(const set<pit_type> & shouldBeInInfo, const set<pit_type> & mustBeInInfo,
- const set<pit_type> & abstract, pit_type bpit, pit_type epit) :
- shouldBeInInfo(shouldBeInInfo), mustBeInInfo(mustBeInInfo), abstract(abstract),
- bpit(bpit), epit(epit) {}
-};
+using DocBookInfoTag = tuple<set<pit_type>, set<pit_type>, pit_type, pit_type>;
DocBookDocumentSectioning hasDocumentSectioning(ParagraphList const ¶graphs, pit_type bpit, pit_type const epit) {
}
-bool hasOnlyNotes(Paragraph const & par)
-{
- for (int i = 0; i < par.size(); ++i)
- if (!par.isInset(i) || !dynamic_cast<InsetNote *>(par.insetList().get(i)))
- return false;
- return true;
-}
-
-
-DocBookInfoTag getParagraphsWithInfo(ParagraphList const ¶graphs, pit_type bpit, pit_type const epit) {
+DocBookInfoTag getParagraphsWithInfo(ParagraphList const ¶graphs, pit_type const bpit, pit_type const epit) {
set<pit_type> shouldBeInInfo;
set<pit_type> mustBeInInfo;
- set<pit_type> abstract;
- // Find the first non empty paragraph by mutating bpit.
- while (bpit < epit) {
- Paragraph const &par = paragraphs[bpit];
- if (par.empty() || hasOnlyNotes(par))
- bpit += 1;
- else
- break;
- }
-
- // Find the last info-like paragraph.
pit_type cpit = bpit;
- bool hasAbstractLayout = false;
while (cpit < epit) {
// Skip paragraphs only containing one note.
- Paragraph const & par = paragraphs[cpit];
- if (hasOnlyNotes(par)) {
+ Paragraph const &par = paragraphs[cpit];
+ if (par.size() == 1 && dynamic_cast<InsetNote*>(paragraphs[cpit].insetList().get(0))) {
cpit += 1;
continue;
}
- if (par.layout().name() == from_ascii("Abstract"))
- hasAbstractLayout = true;
-
// Based on layout information, store this paragraph in one set: should be in <info>, must be.
Layout const &style = par.layout();
shouldBeInInfo.emplace(cpit);
} else {
// Hypothesis: the <info> parts should be grouped together near the beginning bpit.
- // There may be notes in between, but nothing else.
break;
}
cpit += 1;
}
// Now, cpit points to the last paragraph that has things that could go in <info>.
- // bpit is the beginning of the <info> part.
-
- // Go once again through the list of paragraphs to find the abstract. If there is an abstract
- // layout, only consider it. Otherwise, an abstract is just a sequence of paragraphs with text.
- if (hasAbstractLayout) {
- pit_type pit = bpit;
- while (pit < cpit) { // Don't overshoot the <info> part.
- if (paragraphs[pit].layout().name() == from_ascii("Abstract"))
- abstract.emplace(pit);
- pit++;
- }
- } else {
- pit_type lastAbstract = epit + 1; // A nonsensical value.
- docstring lastAbstractLayout;
-
- pit_type pit = bpit;
- while (pit < cpit) { // Don't overshoot the <info> part.
- const Paragraph & par = paragraphs.at(pit);
- if (!par.insetList().empty()) {
- for (const auto &i : par.insetList()) {
- if (i.inset->getText(0) != nullptr) {
- if (lastAbstract == epit + 1) {
- // First paragraph that matches the heuristic definition of abstract.
- lastAbstract = pit;
- lastAbstractLayout = par.layout().name();
- } else if (pit > lastAbstract + 1 || par.layout().name() != lastAbstractLayout) {
- // This is either too far from the last abstract paragraph or doesn't
- // have the right layout name, BUT there has already been an abstract
- // in this document: done with detecting the abstract.
- goto done; // Easier to get out of two nested loops.
- }
+ // bpit is still the beginning of the <info> part.
- abstract.emplace(pit);
- break;
- }
+ return make_tuple(shouldBeInInfo, mustBeInInfo, bpit, cpit);
+}
+
+
+bool hasAbstractBetween(ParagraphList const ¶graphs, pit_type const bpitAbstract, pit_type const epitAbstract)
+{
+ // Hypothesis: the paragraphs between bpitAbstract and epitAbstract can be considered an abstract because they
+ // are just after a document or part title.
+ if (epitAbstract - bpitAbstract <= 0)
+ return false;
+
+ // If there is something between these paragraphs, check if it's compatible with an abstract (i.e. some text).
+ pit_type bpit = bpitAbstract;
+ while (bpit < epitAbstract) {
+ const Paragraph &p = paragraphs.at(bpit);
+
+ if (p.layout().name() == from_ascii("Abstract"))
+ return true;
+
+ if (!p.insetList().empty()) {
+ for (const auto &i : p.insetList()) {
+ if (i.inset->getText(0) != nullptr) {
+ return true;
}
}
- pit++;
}
+ bpit++;
}
-
- done:
- return DocBookInfoTag(shouldBeInInfo, mustBeInInfo, abstract, bpit, cpit);
+ return false;
}
XMLStream & xs,
OutputParams const & runparams,
ParagraphList const & paragraphs,
- DocBookInfoTag const & info)
+ DocBookInfoTag const & info,
+ pit_type bpitAbstract,
+ pit_type const epitAbstract)
{
+ // Consider everything between bpitAbstract and epitAbstract (excluded) as paragraphs for the abstract.
+ // Use bpitAbstract >= epitAbstract to indicate there is no abstract.
+
+ set<pit_type> shouldBeInInfo;
+ set<pit_type> mustBeInInfo;
+ pit_type bpitInfo;
+ pit_type epitInfo;
+ tie(shouldBeInInfo, mustBeInInfo, bpitInfo, epitInfo) = 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 = !info.abstract.empty();
+ bool hasAbstract = hasAbstractBetween(paragraphs, bpitAbstract, epitAbstract);
docstring abstract;
if (hasAbstract) {
- pit_type bpitAbstract = *std::min_element(info.abstract.begin(), info.abstract.end());
- pit_type epitAbstract = *std::max_element(info.abstract.begin(), info.abstract.end());
-
odocstringstream os2;
XMLStream xs2(os2);
generateDocBookParagraphWithoutSectioning(text, buf, xs2, runparams, paragraphs, bpitAbstract, epitAbstract);
}
// The abstract must go in <info>.
- bool needInfo = !info.mustBeInInfo.empty() || hasAbstract;
+ bool needInfo = !mustBeInInfo.empty() || hasAbstract;
// Start the <info> tag if required.
if (needInfo) {
}
// Output the elements that should go in <info>.
- generateDocBookParagraphWithoutSectioning(text, buf, xs, runparams, paragraphs, info.bpit, info.epit);
+ generateDocBookParagraphWithoutSectioning(text, buf, xs, runparams, paragraphs, bpitInfo, epitInfo);
if (hasAbstract && !abstract.empty()) { // The second test is probably superfluous.
- string tag = paragraphs[*info.abstract.begin()].layout().docbookforceabstracttag();
+ string tag = paragraphs[bpitAbstract].layout().docbookforceabstracttag();
if (tag == "NONE")
tag = "abstract";
ParagraphList const ¶graphs = text.paragraphs();
pit_type bpit = runparams.par_begin;
DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit);
- outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
+ outputDocBookInfo(text, buf, xs, runparams, paragraphs, info, get<3>(info), epit);
}
pit_type bpit = runparams.par_begin;
pit_type const epit = runparams.par_end;
DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit);
- outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
- bpit = info.bpit;
+ outputDocBookInfo(text, buf, xs, runparams, paragraphs, info, 0, 0);
+ bpit = get<3>(info); // Generate the content starting from the end of the <info> part.
// Then, the content.
ParagraphList::const_iterator const pend =