]> git.lyx.org Git - lyx.git/commitdiff
XHTML/DocBook: merge code duplicates for horizontal alignment.
authorThibaut Cuvelier <tcuvelier@lyx.org>
Fri, 1 Apr 2022 02:01:40 +0000 (04:01 +0200)
committerThibaut Cuvelier <tcuvelier@lyx.org>
Fri, 1 Apr 2022 02:01:40 +0000 (04:01 +0200)
src/insets/InsetTabular.cpp
src/insets/InsetTabular.h

index 092c868a0005a9ffcc96fde80675338b62a9875f..761cb4c6ebe3695ff3d6c81b64aa57be0d34b73a 100644 (file)
@@ -3639,6 +3639,33 @@ std::string Tabular::getVAlignAsXmlAttribute(idx_type cell) const
 }
 
 
 }
 
 
+std::string Tabular::getHAlignAsXmlAttribute(idx_type cell, bool is_xhtml) const
+{
+       // TODO: the Boolean flag isn't really clean; switch to an enum at some point.
+       switch (getAlignment(cell)) {
+       case LYX_ALIGN_LEFT:
+               return "align='left'";
+       case LYX_ALIGN_RIGHT:
+               return "align='right'";
+
+       default:
+               // HTML only supports left, right, and center.
+               if (is_xhtml)
+                       return "align='center'";
+
+               // DocBook also has justify and decimal.
+               if (getAlignment(cell) == LYX_ALIGN_BLOCK) {
+                       return "align='justify'";
+               } else if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
+                       Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
+                       return "align='char' char='" + to_utf8(tlang->decimalSeparator()) + "'";
+               } else {
+                       return "align='center'";
+               }
+       }
+}
+
+
 void Tabular::docbookRowAsHTML(XMLStream & xs, row_type row,
                   OutputParams const & runparams, bool header) const
 {
 void Tabular::docbookRowAsHTML(XMLStream & xs, row_type row,
                   OutputParams const & runparams, bool header) const
 {
@@ -3659,27 +3686,7 @@ void Tabular::docbookRowAsHTML(XMLStream & xs, row_type row,
                        attr << "style=\"width: " << hwidth << ";\" ";
                }
 
                        attr << "style=\"width: " << hwidth << ";\" ";
                }
 
-               attr << "align='";
-               switch (getAlignment(cell)) {
-               case LYX_ALIGN_BLOCK:
-                       attr << "justify";
-                       break;
-               case LYX_ALIGN_DECIMAL: {
-                       Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
-                       attr << "char' char='" << to_utf8(tlang->decimalSeparator());
-               }
-                       break;
-               case LYX_ALIGN_LEFT:
-                       attr << "left";
-                       break;
-               case LYX_ALIGN_RIGHT:
-                       attr << "right";
-                       break;
-               default:
-                       attr << "center";
-                       break;
-               }
-               attr << "' " << getVAlignAsXmlAttribute(cell);
+               attr << getHAlignAsXmlAttribute(cell, false) << " " << getVAlignAsXmlAttribute(cell);
 
                if (isMultiColumn(cell))
                        attr << " colspan='" << columnSpan(cell) << "'";
 
                if (isMultiColumn(cell))
                        attr << " colspan='" << columnSpan(cell) << "'";
@@ -3713,28 +3720,7 @@ void Tabular::docbookRowAsCALS(XMLStream & xs, row_type row,
 
                stringstream attr;
 
 
                stringstream attr;
 
-               attr << "align='";
-               switch (getAlignment(cell)) {
-               case LYX_ALIGN_BLOCK:
-                       attr << "justify";
-                       break;
-               case LYX_ALIGN_DECIMAL: {
-                       Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
-                       attr << "char' char='" << to_utf8(tlang->decimalSeparator());
-               }
-                       break;
-               case LYX_ALIGN_LEFT:
-                       attr << "left";
-                       break;
-               case LYX_ALIGN_RIGHT:
-                       attr << "right";
-                       break;
-
-               default:
-                       attr << "center";
-                       break;
-               }
-               attr << "' " << getVAlignAsXmlAttribute(cell);
+               attr << getHAlignAsXmlAttribute(cell, false) << " " << getVAlignAsXmlAttribute(cell);
 
                if (isMultiColumn(cell))
                        attr << " colspan='" << columnSpan(cell) << "'";
 
                if (isMultiColumn(cell))
                        attr << " colspan='" << columnSpan(cell) << "'";
@@ -3874,19 +3860,7 @@ docstring Tabular::xhtmlRow(XMLStream & xs, row_type row,
                        attr << "style=\"width: " << hwidth << ";\" ";
                }
 
                        attr << "style=\"width: " << hwidth << ";\" ";
                }
 
-               attr << "align='";
-               switch (getAlignment(cell)) {
-               case LYX_ALIGN_LEFT:
-                       attr << "left";
-                       break;
-               case LYX_ALIGN_RIGHT:
-                       attr << "right";
-                       break;
-               default:
-                       attr << "center";
-                       break;
-               }
-               attr << "' " << getVAlignAsXmlAttribute(cell);
+               attr << getHAlignAsXmlAttribute(cell, true) << " " << getVAlignAsXmlAttribute(cell);
 
                if (isMultiColumn(cell))
                        attr << " colspan='" << columnSpan(cell) << "'";
 
                if (isMultiColumn(cell))
                        attr << " colspan='" << columnSpan(cell) << "'";
index 8420a48fefe2f1503a735c3b333e7505328ec03d..f42f5bc77bd4bbf07c9c6a5b68fccd3a4443f42b 100644 (file)
@@ -946,6 +946,8 @@ public:
        docstring xhtmlRow(XMLStream & xs, row_type, OutputParams const &,
                           bool header = false) const;
 
        docstring xhtmlRow(XMLStream & xs, row_type, OutputParams const &,
                           bool header = false) const;
 
+       /// Transforms the vertical alignment of the given cell as a prebaked XML attribute (for HTML and CALS).
+       std::string getHAlignAsXmlAttribute(idx_type cell, bool is_xhtml = true) const;
        /// Transforms the vertical alignment of the given cell as a prebaked XML attribute (for HTML and CALS).
        std::string getVAlignAsXmlAttribute(idx_type cell) const;
 
        /// Transforms the vertical alignment of the given cell as a prebaked XML attribute (for HTML and CALS).
        std::string getVAlignAsXmlAttribute(idx_type cell) const;