[LyX/master] DocBook: new logic to handle the new lines, only used in output_docbook for now.

Thibaut Cuvelier tcuvelier at lyx.org
Sat Sep 19 18:18:55 UTC 2020


commit 9393ca74ba93b70c2aa610e6c3c2b6aff651eb42
Author: Thibaut Cuvelier <tcuvelier at lyx.org>
Date:   Tue Aug 18 00:37:34 2020 +0200

    DocBook: new logic to handle the new lines, only used in output_docbook for now.
---
 lib/layouts/stdlayouts.inc  |    2 +-
 lib/layouts/stdlists.inc    |    2 +-
 lib/layouts/stdstruct.inc   |    2 +-
 lib/layouts/stdtitle.inc    |    6 +-
 src/insets/InsetNewline.cpp |    2 +-
 src/output_docbook.cpp      |  231 ++++++++++++++++++++-----------------------
 src/xml.h                   |    6 +-
 7 files changed, 121 insertions(+), 130 deletions(-)

diff --git a/lib/layouts/stdlayouts.inc b/lib/layouts/stdlayouts.inc
index 1670695..e550518 100644
--- a/lib/layouts/stdlayouts.inc
+++ b/lib/layouts/stdlayouts.inc
@@ -7,7 +7,7 @@
 # quotations and such.
 
 
-Format 82
+Format 84
 
 Style Quotation
 	Category              MainText
diff --git a/lib/layouts/stdlists.inc b/lib/layouts/stdlists.inc
index 16a9e81..c0b346d 100644
--- a/lib/layouts/stdlists.inc
+++ b/lib/layouts/stdlists.inc
@@ -6,7 +6,7 @@
 # This include files contains various standard environments for lists.
 
 
-Format 82
+Format 84
 
 Input stdlyxlist.inc
 
diff --git a/lib/layouts/stdstruct.inc b/lib/layouts/stdstruct.inc
index c8b7eb4..2a81117 100644
--- a/lib/layouts/stdstruct.inc
+++ b/lib/layouts/stdstruct.inc
@@ -8,7 +8,7 @@
 # a document, like abstract, bibliography and such.
 
 
-Format 82
+Format 84
 
 Style Abstract
 	Margin                Static
diff --git a/lib/layouts/stdtitle.inc b/lib/layouts/stdtitle.inc
index ad989a6..44b1b8e 100644
--- a/lib/layouts/stdtitle.inc
+++ b/lib/layouts/stdtitle.inc
@@ -8,7 +8,7 @@
 # a document, like title, author and such.
 
 
-Format 82
+Format 84
 
 Style Title
 	Margin                Static
@@ -29,6 +29,7 @@ Style Title
 	HTMLTag               h1
 	HTMLTitle             true
 	DocBookTag            title
+	DocBookTagType        paragraph
 	DocBookInInfo         maybe
 End
 
@@ -50,7 +51,9 @@ Style Author
 	  Size                Large
 	EndFont
 	DocBookTag            personname
+	DocBookTagType        paragraph
 	DocBookWrapperTag     author
+	DocBookWrapperTagType inline
 	DocBookInInfo         always
 End
 
@@ -72,5 +75,6 @@ Style Date
 	  Size                Large
 	EndFont
 	DocBookTag            date
+	DocBookTagType        paragraph
 	DocBookInInfo         always
 End
diff --git a/src/insets/InsetNewline.cpp b/src/insets/InsetNewline.cpp
index 00b276e..7cba0f7 100644
--- a/src/insets/InsetNewline.cpp
+++ b/src/insets/InsetNewline.cpp
@@ -181,7 +181,7 @@ void InsetNewline::docbook(XMLStream & xs, OutputParams const & runparams) const
 		// about the paragraph's layout... Good for now, though, this should not happen in DocBook, only maybe
 		// extensions.
 		xs << XMLStream::ESCAPE_NONE << from_utf8("<!-- Is para open? " + string((xs.isTagOpen(xml::StartTag("para"))) ? "yes" : "no") +" -->");
-		xs << XMLStream::ESCAPE_NONE << from_utf8("</para>\n<para");
+		xs << XMLStream::ESCAPE_NONE << from_utf8("</para>\n<para>");
 		// TODO: that's a hack...
 //		xs << xml::EndTag("para");
 //		xs << xml::CR();
diff --git a/src/output_docbook.cpp b/src/output_docbook.cpp
index b29da56..b7b5941 100644
--- a/src/output_docbook.cpp
+++ b/src/output_docbook.cpp
@@ -43,8 +43,6 @@
 #include <algorithm>
 #include <sstream>
 
-// #define DOCBOOK_DEBUG_NEWLINES
-
 using namespace std;
 using namespace lyx::support;
 
@@ -189,14 +187,100 @@ xml::EndFontTag docbookEndFontTag(xml::FontTypes type)
 
 namespace {
 
-// convenience functions
+// Convenience functions to open and close tags. First, very low-level ones to ensure a consistent new-line behaviour.
+// Block style:
+//	  Content before
+//	  <blocktag>
+//	    Contents of the block.
+//	  </blocktag>
+//	  Content after
+// Paragraph style:
+//	  Content before
+//	    <paratag>Contents of the paragraph.</paratag>
+//	  Content after
+// Inline style:
+//    Content before<inlinetag>Contents of the paragraph.</inlinetag>Content after
+
+void openInlineTag(XMLStream & xs, const std::string & tag, const std::string & attr)
+{
+	xs << xml::StartTag(tag, attr);
+}
+
 
-void openParTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
+void closeInlineTag(XMLStream & xs, const std::string & tag)
+{
+	xs << xml::EndTag(tag);
+}
+
+
+void openParTag(XMLStream & xs, const std::string & tag, const std::string & attr)
+{
+	if (!xs.isLastTagCR())
+		xs << xml::CR();
+	xs << xml::StartTag(tag, attr);
+}
+
+
+void closeParTag(XMLStream & xs, const std::string & tag)
+{
+	xs << xml::EndTag(tag);
+	xs << xml::CR();
+}
+
+
+void openBlockTag(XMLStream & xs, const std::string & tag, const std::string & attr)
+{
+	if (!xs.isLastTagCR())
+		xs << xml::CR();
+	xs << xml::StartTag(tag, attr);
+	xs << xml::CR();
+}
+
+
+void closeBlockTag(XMLStream & xs, const std::string & tag)
+{
+	xs << xml::CR();
+	xs << xml::EndTag(tag);
+	xs << xml::CR();
+}
+
+
+void openTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype)
+{
+	if (tag.empty() || tag == "NONE")
+		return;
+
+	if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
+		openParTag(xs, tag, attr);
+	else if (tagtype == "block")
+		openBlockTag(xs, tag, attr);
+	else if (tagtype == "inline")
+		openInlineTag(xs, tag, attr);
+	else
+		xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + " " + attr + "'");
+}
+
+
+void closeTag(XMLStream & xs, const std::string & tag, const std::string & tagtype)
 {
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- openParTag -->";
-#endif
+	if (tag.empty() || tag == "NONE")
+		return;
 
+	if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
+		closeParTag(xs, tag);
+	else if (tagtype == "block")
+		closeBlockTag(xs, tag);
+	else if (tagtype == "inline")
+		closeInlineTag(xs, tag);
+	else
+		xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + "'");
+}
+
+
+// Higher-level convenience functions.
+
+void openParTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
+{
 	Layout const & lay = par->layout();
 
 	if (par == prevpar)
@@ -218,38 +302,25 @@ void openParTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar
 	}
 
 	// Main logic.
-	if (openWrapper) {
-		xs << xml::StartTag(lay.docbookwrappertag(), lay.docbookwrapperattr());
-		xs << xml::CR();
-	}
+	if (openWrapper)
+		openTag(xs, lay.docbookwrappertag(), lay.docbookwrapperattr(), lay.docbookwrappertagtype());
 
-	string tag = lay.docbooktag();
+	const string & tag = lay.docbooktag();
 	if (tag != "NONE") {
 		auto xmltag = xml::ParTag(tag, lay.docbookattr());
-		if (!xs.isTagOpen(xmltag, 1)) // Don't nest a paragraph directly in a paragraph. TODO: required or not?
-			xs << xmltag;
-	}
-
-	if (lay.docbookitemtag() != "NONE") {
-		xs << xml::StartTag(lay.docbookitemtag(), lay.docbookitemattr());
-		xs << xml::CR();
+		if (!xs.isTagOpen(xmltag, 1)) // Don't nest a paragraph directly in a paragraph.
+			// TODO: required or not?
+			// TODO: avoid creating a ParTag object just for this query...
+			openTag(xs, lay.docbooktag(), lay.docbookattr(), lay.docbooktagtype());
 	}
 
-	if (lay.docbookiteminnertag() != "NONE")
-		xs << xml::StartTag(lay.docbookiteminnertag(), lay.docbookiteminnerattr());
-
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- /openParTag -->";
-#endif
+	openTag(xs, lay.docbookitemtag(), lay.docbookitemattr(), lay.docbookitemtagtype());
+	openTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnerattr(), lay.docbookiteminnertagtype());
 }
 
 
 void closeParTag(XMLStream & xs, Paragraph const * par, Paragraph const * nextpar)
 {
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- closeParTag -->";
-#endif
-
 	if (par == nextpar)
 		nextpar = nullptr;
 
@@ -265,119 +336,35 @@ void closeParTag(XMLStream & xs, Paragraph const * par, Paragraph const * nextpa
 	}
 
 	// Main logic.
-	if (lay.docbookiteminnertag() != "NONE") {
-		xs << xml::EndTag(lay.docbookiteminnertag());
-		xs << xml::CR();
-	}
-
-	if (lay.docbookitemtag() != "NONE") {
-		xs << xml::EndTag(lay.docbookitemtag());
-		xs << xml::CR();
-	}
-
-	if (lay.docbooktag() != "NONE") {
-		xs << xml::EndTag(lay.docbooktag());
-		xs << xml::CR();
-	}
-
-	if (closeWrapper) {
-		xs << xml::EndTag(lay.docbookwrappertag());
-		xs << xml::CR();
-	}
-
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- /closeParTag -->";
-#endif
-}
-
-
-void openBlockTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
-{
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- openBlockTag -->";
-#endif
-
-	// Similar as openParTag, but with a line feed after.
-	openParTag(xs, par, prevpar);
-	xs << xml::CR();
-
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- /openBlockTag -->";
-#endif
-}
-
-
-void closeBlockTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
-{
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- closeBlockTag -->";
-#endif
-
-	// Similar as closeParTag, but with a line feed before.
-	xs << xml::CR();
-	closeParTag(xs, par, prevpar);
-
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- /closeBlockTag -->";
-#endif
+	closeTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnertagtype());
+	closeTag(xs, lay.docbookitemtag(), lay.docbookitemtagtype());
+	closeTag(xs, lay.docbooktag(), lay.docbooktagtype());
+	if (closeWrapper)
+		closeTag(xs, lay.docbookwrappertag(), lay.docbookwrappertagtype());
 }
 
 
 void openLabelTag(XMLStream & xs, Layout const & lay) // Mostly for definition lists.
 {
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- openLabelTag -->";
-#endif
-
-	xs << xml::StartTag(lay.docbookitemlabeltag(), lay.docbookitemlabelattr());
-
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- /openLabelTag -->";
-#endif
+	openTag(xs, lay.docbookitemlabeltag(), lay.docbookitemlabelattr(), lay.docbookitemlabeltagtype());
 }
 
 
 void closeLabelTag(XMLStream & xs, Layout const & lay)
 {
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- closeLabelTag -->";
-#endif
-
-	xs << xml::EndTag(lay.docbookitemlabeltag());
-	xs << xml::CR();
-
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- closeLabelTag -->";
-#endif
+	closeTag(xs, lay.docbookitemlabeltag(), lay.docbookitemlabeltagtype());
 }
 
 
 void openItemTag(XMLStream & xs, Layout const & lay)
 {
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- openItemTag -->";
-#endif
-
-	xs << xml::StartTag(lay.docbookitemtag(), lay.docbookitemattr());
-
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- /openItemTag -->";
-#endif
+	openTag(xs, lay.docbookitemtag(), lay.docbookitemattr(), lay.docbookitemtagtype());
 }
 
 
 void closeItemTag(XMLStream & xs, Layout const & lay)
 {
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- closeItemTag -->";
-#endif
-
-	xs << xml::EndTag(lay.docbookitemtag());
-	xs << xml::CR();
-
-#ifdef DOCBOOK_DEBUG_NEWLINES
-	xs << XMLStream::ESCAPE_NONE << "<!-- /closeItemTag -->";
-#endif
+	closeTag(xs, lay.docbookitemtag(), lay.docbookitemtagtype());
 }
 
 
diff --git a/src/xml.h b/src/xml.h
index 581cca5..0948143 100644
--- a/src/xml.h
+++ b/src/xml.h
@@ -100,13 +100,13 @@ public:
 	bool isTagPending(xml::StartTag const &, int maxdepth = -1) const;
 	///
 	bool isLastTagCR() const { return is_last_tag_cr_; };
-private:
-	///
-	void clearTagDeque();
 	///
 	void writeError(std::string const &) const;
 	///
 	void writeError(docstring const &) const;
+private:
+	///
+	void clearTagDeque();
 	///
 	odocstream & os_;
 	///


More information about the lyx-cvs mailing list