3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "InsetLayout.h"
23 #include "ParagraphParameters.h"
24 #include "Paragraph.h"
25 #include "output_docbook.h"
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30 #include "support/TempFile.h"
38 using namespace lyx::support;
42 InsetERT::InsetERT(Buffer * buf, CollapseStatus status)
43 : InsetCollapsible(buf)
49 InsetERT::InsetERT(InsetERT const & old)
50 : InsetCollapsible(old)
54 void InsetERT::write(ostream & os) const
57 InsetCollapsible::write(os);
61 int InsetERT::plaintext(odocstringstream & os,
62 OutputParams const & rp, size_t max_length) const
65 // do not output TeX code
68 ParagraphList::const_iterator par = paragraphs().begin();
69 ParagraphList::const_iterator end = paragraphs().end();
71 while (par != end && os.str().size() <= max_length) {
72 pos_type siz = par->size();
73 for (pos_type i = 0; i < siz; ++i) {
74 char_type const c = par->getChar(i);
75 // output the active characters
92 void InsetERT::docbook(XMLStream & xs, OutputParams const & runparams) const
94 auto const begin = paragraphs().begin();
96 auto const end = paragraphs().end();
98 odocstringstream os; // No need for XML handling here.
100 // Recreate the logic of makeParagraph in output_docbook.cpp, but much simplified: never open <para>
101 // in an ERT, use simple line breaks.
102 // New line after each paragraph of the ERT, save the last one.
103 while (true) { // For each paragraph in the ERT...
104 std::vector<docstring> pars_prepend;
105 std::vector<docstring> pars;
106 std::vector<docstring> pars_append;
107 tie(pars_prepend, pars, pars_append) = par->simpleDocBookOnePar(buffer(), runparams, text().outerFont(distance(begin, par)), 0, false, true);
109 for (docstring const & parXML : pars_prepend)
110 xs << XMLStream::ESCAPE_NONE << parXML;
111 auto p = pars.begin();
112 while (true) { // For each line of this ERT paragraph...
120 for (docstring const & parXML : pars_append)
121 xs << XMLStream::ESCAPE_NONE << parXML;
130 // // Implement the special case of \and: split the current item.
131 // if (os.str() == "\\and" || os.str() == "\\and ") {
132 // auto lay = getLayout();
135 // Try to recognise some commands to have a nicer DocBook output.
136 bool output_as_comment = true;
138 // First step: some commands have a direct mapping to DocBook, mostly because the mapping is simply text or
140 // Logic is similar to that of convertLaTeXCommands in BiblioInfo.cpp.
141 // TODO: make the code even more similar by looping over the string and applying all conversions. (What is not
142 // recognised should simply be put in comments: have a list of elements that are either already recognised or are
143 // not yet recognised? Global transformations like \string should then come first.)
145 docstring os_trimmed = trim(os.str());
147 // Rewrite \"u to \"{u}.
148 static regex const regNoBraces(R"(^\\\W\w)");
149 if (regex_search(to_utf8(os_trimmed), regNoBraces)) {
150 os_trimmed.insert(3, from_ascii("}"));
151 os_trimmed.insert(2, from_ascii("{"));
154 // Rewrite \" u to \"{u}.
155 static regex const regSpace(R"(^\\\W \w)");
156 if (regex_search(to_utf8(os_trimmed), regSpace)) {
158 os_trimmed.insert(4, from_ascii("}"));
161 std::cout << to_utf8(os_trimmed) << std::endl;
163 // Look into the global table of Unicode characters if there is a match.
166 docstring const converted = Encodings::fromLaTeXCommand(os_trimmed,
167 Encodings::TEXT_CMD, termination, rem);
168 if (!converted.empty()) {
169 // Don't output the characters directly, even if the document should be encoded in UTF-8, for editors that
170 // do not support all these funky characters.
171 for (const char_type& character : converted) {
172 xs << XMLStream::ESCAPE_NONE << from_ascii("&#" + std::to_string(character) + ';');
174 output_as_comment = false;
178 // Second step: the command \string can be ignored. If that's the only command in the ERT, then done.
179 // There may be several occurrences. (\string is 7 characters long.)
180 if (os.str().length() >= 7) {
181 docstring os_str = os.str();
183 while (os_str.length() >= 7) {
184 auto os_text = os_str.find(from_ascii("\\string"));
186 if (os_text != lyx::docstring::npos && !std::isalpha(static_cast<int>(os_str[os_text + 7]))) {
187 os_str = os_str.substr(0, os_text) + os_str.substr(os_text + 7, os_str.length());
189 if (os_str.find('\\') == std::string::npos) {
191 output_as_comment = false;
200 // Otherwise, output the ERT as a comment with the appropriate escaping if the command is not recognised.
201 if (output_as_comment) {
202 xs << XMLStream::ESCAPE_NONE << "<!-- ";
203 xs << XMLStream::ESCAPE_COMMENTS << os.str();
204 xs << XMLStream::ESCAPE_NONE << " -->";
209 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
211 switch (cmd.action()) {
212 case LFUN_INSET_MODIFY:
213 if (cmd.getArg(0) == "ert") {
214 cur.recordUndoInset(this);
215 setStatus(cur, string2params(to_utf8(cmd.argument())));
220 InsetCollapsible::doDispatch(cur, cmd);
227 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
228 FuncStatus & status) const
230 switch (cmd.action()) {
231 case LFUN_INSET_INSERT:
232 status.setEnabled(false);
234 case LFUN_INSET_MODIFY:
235 if (cmd.getArg(0) == "ert") {
236 status.setEnabled(true);
242 return InsetCollapsible::getStatus(cur, cmd, status);
248 docstring const InsetERT::buttonLabel(BufferView const & bv) const
251 docstring const locked = tempfile_ ? docstring(1, 0x1F512) : docstring();
252 if (decoration() == InsetDecoration::CLASSIC)
253 return locked + (isOpen(bv) ? _("ERT") : getNewLabel(_("ERT")));
254 return locked + getNewLabel(_("ERT"));
258 InsetCollapsible::CollapseStatus InsetERT::string2params(string const & in)
262 istringstream data(in);
265 lex.setContext("InsetERT::string2params");
269 return static_cast<CollapseStatus>(s);
273 string InsetERT::params2string(CollapseStatus status)
276 data << "ert" << ' ' << status;
281 docstring InsetERT::xhtml(XMLStream &, OutputParams const &) const