From: Thibaut Cuvelier Date: Sun, 6 Feb 2022 05:20:50 +0000 (+0100) Subject: DocBook: support @ in index when used for sorting. X-Git-Tag: 2.4-beta2~899 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=34ea4080;p=lyx.git DocBook: support @ in index when used for sorting. --- diff --git a/autotests/export/docbook/index.lyx b/autotests/export/docbook/index.lyx new file mode 100644 index 0000000000..875e6336e4 --- /dev/null +++ b/autotests/export/docbook/index.lyx @@ -0,0 +1,147 @@ +#LyX 2.4 created this file. For more info see https://www.lyx.org/ +\lyxformat 608 +\begin_document +\begin_header +\save_transient_properties true +\origin unavailable +\textclass article +\use_default_options true +\maintain_unincluded_children no +\language american +\language_package default +\inputencoding utf8 +\fontencoding auto +\font_roman "default" "default" +\font_sans "default" "default" +\font_typewriter "default" "default" +\font_math "auto" "auto" +\font_default_family default +\use_non_tex_fonts false +\font_sc false +\font_roman_osf false +\font_sans_osf false +\font_typewriter_osf false +\font_sf_scale 100 100 +\font_tt_scale 100 100 +\use_microtype false +\use_dash_ligatures true +\graphics default +\default_output_format default +\output_sync 0 +\bibtex_command default +\index_command default +\float_placement class +\float_alignment class +\paperfontsize default +\use_hyperref false +\papersize default +\use_geometry false +\use_package amsmath 1 +\use_package amssymb 1 +\use_package cancel 1 +\use_package esint 1 +\use_package mathdots 1 +\use_package mathtools 1 +\use_package mhchem 1 +\use_package stackrel 1 +\use_package stmaryrd 1 +\use_package undertilde 1 +\cite_engine basic +\cite_engine_type default +\use_bibtopic false +\use_indices false +\paperorientation portrait +\suppress_date false +\justification true +\use_refstyle 1 +\use_minted 0 +\use_lineno 0 +\index Index +\shortcut idx +\color #008000 +\end_index +\secnumdepth 3 +\tocdepth 3 +\paragraph_separation indent +\paragraph_indentation default +\is_math_indent 0 +\math_numbering_side default +\quotes_style english +\dynamic_quotes 0 +\papercolumns 1 +\papersides 1 +\paperpagestyle default +\tablestyle default +\tracking_changes false +\output_changes false +\change_bars false +\postpone_fragile_content true +\html_math_output 0 +\html_css_as_file 0 +\html_be_strict false +\docbook_table_output 0 +\docbook_mathml_prefix 1 +\end_header + +\begin_body + +\begin_layout Title +Index tests +\end_layout + +\begin_layout Standard +Text +\begin_inset Index idx +status open + +\begin_layout Plain Layout +Text +\end_layout + +\end_inset + + +\begin_inset Index idx +status open + +\begin_layout Plain Layout +SortedAs@Text +\end_layout + +\end_inset + + +\begin_inset Index idx +status open + +\begin_layout Plain Layout +Primary!Secondary +\end_layout + +\end_inset + + +\begin_inset Index idx +status open + +\begin_layout Plain Layout +SortedPrimary@Primary!Secondary +\end_layout + +\end_inset + + +\begin_inset Index idx +status open + +\begin_layout Plain Layout +Primary!Secondary!Tertiary +\end_layout + +\end_inset + +. +\end_layout + +\end_body +\end_document diff --git a/autotests/export/docbook/index.xml b/autotests/export/docbook/index.xml new file mode 100644 index 0000000000..21e73c597f --- /dev/null +++ b/autotests/export/docbook/index.xml @@ -0,0 +1,7 @@ + + +
+Index tests +TextTextTextPrimarySecondaryPrimarySecondaryPrimarySecondaryTertiary. +
\ No newline at end of file diff --git a/src/insets/InsetIndex.cpp b/src/insets/InsetIndex.cpp index 7b4bb88013..c578c0379f 100644 --- a/src/insets/InsetIndex.cpp +++ b/src/insets/InsetIndex.cpp @@ -194,16 +194,15 @@ void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const InsetText::latex(ots, runparams); docstring latexString = trim(odss.str()); - // Check whether there are unsupported things. - if (latexString.find(from_utf8("@")) != latexString.npos) { - docstring error = from_utf8("Unsupported feature: an index entry contains an @. " + // Check whether there are unsupported things. @ is supported, but only for sorting, without specific formatting. + if (latexString.find(from_utf8("@\\")) != lyx::docstring::npos) { + docstring error = from_utf8("Unsupported feature: an index entry contains an @\\. " "Complete entry: \"") + latexString + from_utf8("\""); LYXERR0(error); xs << XMLStream::ESCAPE_NONE << (from_utf8("\n")); - // TODO: implement @ using the sortas attribute (on primary, secondary, tertiary). } - // Handle several indices. + // Handle several indices (indicated in the inset instead of the raw latexString). docstring indexType = from_utf8(""); if (buffer().masterBuffer()->params().use_indices) { indexType += " type=\"" + params_.index + "\""; @@ -212,14 +211,25 @@ void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const // Split the string into its main constituents: terms, and command (see, see also, range). size_t positionVerticalBar = latexString.find(from_ascii("|")); // What comes before | is (sub)(sub)entries. docstring indexTerms = latexString.substr(0, positionVerticalBar); - docstring command = latexString.substr(positionVerticalBar + 1); + docstring command; + if (positionVerticalBar != lyx::docstring::npos) { + command = latexString.substr(positionVerticalBar + 1); + } + + // Handle sorting issues, with @. + vector sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false); + docstring sortAs; + if (sortingElements.size() == 2) { + sortAs = sortingElements[0]; + indexTerms = sortingElements[1]; + } // Handle primary, secondary, and tertiary terms (entries, subentries, and subsubentries, for LaTeX). vector terms = getVectorFromString(indexTerms, from_ascii("!"), false); - // Handle ranges. Happily, (| and |) can only be at the end of the string! However, | may be trapped by the - bool hasStartRange = latexString.find(from_ascii("|(")) != latexString.npos; - bool hasEndRange = latexString.find(from_ascii("|)")) != latexString.npos; + // Handle ranges. Happily, (| and |) can only be at the end of the string! + bool hasStartRange = latexString.find(from_ascii("|(")) != lyx::docstring::npos; + bool hasEndRange = latexString.find(from_ascii("|)")) != lyx::docstring::npos; if (hasStartRange || hasEndRange) { // Remove the ranges from the command if they do not appear at the beginning. size_t index = 0; @@ -258,7 +268,7 @@ void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const } else { see = list; - if (see.find(from_ascii(",")) != see.npos) { + if (see.find(from_ascii(",")) != std::string::npos) { docstring error = from_utf8("Several index terms found as \"see\"! Only one is acceptable. " "Complete entry: \"") + latexString + from_utf8("\""); LYXERR0(error); @@ -275,6 +285,12 @@ void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const // If there are such things in the index entry, then this code may miserably fail. For example, for "Peter|(textbf", // no range will be detected. // TODO: Could handle formatting as significance="preferred"? + if (!command.empty()) { + docstring error = from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ") + + command + from_utf8(". ") + from_utf8("Complete entry: \"") + latexString + from_utf8("\""); + LYXERR0(error); + xs << XMLStream::ESCAPE_NONE << (from_utf8("\n")); + } // Write all of this down. if (terms.empty() && !hasEndRange) { @@ -335,7 +351,12 @@ void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const } else { xs << xml::StartTag("indexterm", attrs); if (!terms.empty()) { // hasEndRange has no content. - xs << xml::StartTag("primary"); + docstring attr; + if (!sortAs.empty()) { + attr = from_utf8("sortas='") + sortAs + from_utf8("'"); + } + + xs << xml::StartTag("primary", attr); xs << terms[0]; xs << xml::EndTag("primary"); }