3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "MathStream.h"
15 #include "MathFactory.h"
17 #include "MathExtern.h"
22 #include "support/debug.h"
23 #include "support/docstring.h"
24 #include "support/textutils.h"
35 //////////////////////////////////////////////////////////////////////
38 NormalStream & operator<<(NormalStream & ns, MathAtom const & at)
45 NormalStream & operator<<(NormalStream & ns, MathData const & ar)
52 NormalStream & operator<<(NormalStream & ns, docstring const & s)
59 NormalStream & operator<<(NormalStream & ns, const string & s)
61 ns.os() << from_utf8(s);
66 NormalStream & operator<<(NormalStream & ns, char const * s)
73 NormalStream & operator<<(NormalStream & ns, char c)
80 NormalStream & operator<<(NormalStream & ns, int i)
88 /////////////////////////////////////////////////////////////////
91 WriteStream & operator<<(WriteStream & ws, docstring const & s)
93 // Skip leading '\n' if we had already output a newline char
95 (s.length() > 0 && (s[0] != '\n' || ws.canBreakLine())) ? 0 : 1;
97 // Check whether there's something to output
98 if (s.length() <= first)
101 if (ws.pendingBrace()) {
103 ws.pendingBrace(false);
104 ws.pendingSpace(false);
106 } else if (ws.pendingSpace()) {
107 if (isAlphaASCII(s[first]))
109 else if (s[first] == ' ' && ws.textMode())
111 ws.pendingSpace(false);
113 ws.os() << s.substr(first);
115 char_type lastchar = 0;
116 docstring::const_iterator dit = s.begin() + first;
117 docstring::const_iterator end = s.end();
118 for (; dit != end; ++dit) {
120 if (lastchar == '\n')
124 ws.canBreakLine(lastchar != '\n');
129 WriteStream::WriteStream(otexrowstream & os, bool fragile, bool latex,
130 OutputType output, Encoding const * encoding)
131 : os_(os), fragile_(fragile), latex_(latex),
132 output_(output), encoding_(encoding)
136 WriteStream::~WriteStream()
140 else if (pendingspace_)
145 void WriteStream::addlines(unsigned int n)
151 void WriteStream::pendingSpace(bool how)
157 void WriteStream::pendingBrace(bool brace)
159 pendingbrace_ = brace;
163 void WriteStream::textMode(bool textmode)
165 textmode_ = textmode;
169 void WriteStream::lockedMode(bool locked)
175 void WriteStream::asciiOnly(bool ascii)
181 Changer WriteStream::changeRowEntry(TexRow::RowEntry entry)
183 return changeVar(row_entry_, entry);
187 bool WriteStream::startOuterRow()
189 if (TexRow::isNone(row_entry_))
191 return texrow().start(row_entry_);
195 WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
202 WriteStream & operator<<(WriteStream & ws, MathData const & ar)
209 WriteStream & operator<<(WriteStream & ws, char const * s)
216 WriteStream & operator<<(WriteStream & ws, char c)
218 if (c == '\n' && !ws.canBreakLine())
221 if (ws.pendingBrace()) {
223 ws.pendingBrace(false);
224 ws.pendingSpace(false);
226 } else if (ws.pendingSpace()) {
229 else if (c == ' ' && ws.textMode())
231 ws.pendingSpace(false);
236 ws.canBreakLine(c != '\n');
241 WriteStream & operator<<(WriteStream & ws, int i)
243 if (ws.pendingBrace()) {
245 ws.pendingBrace(false);
249 ws.canBreakLine(true);
254 WriteStream & operator<<(WriteStream & ws, unsigned int i)
256 if (ws.pendingBrace()) {
258 ws.pendingBrace(false);
262 ws.canBreakLine(true);
267 //////////////////////////////////////////////////////////////////////
270 MathStream::MathStream(odocstream & os, std::string const & xmlns, bool xmlMode)
271 : os_(os), tab_(0), line_(0), in_text_(false), xmlns_(xmlns), xml_mode_(xmlMode)
275 void MathStream::cr()
278 for (int i = 0; i < tab(); ++i)
283 void MathStream::defer(docstring const & s)
289 void MathStream::defer(string const & s)
291 deferred_ << from_utf8(s);
295 docstring MathStream::deferred() const
297 return deferred_.str();
301 MathStream & operator<<(MathStream & ms, MathAtom const & at)
308 MathStream & operator<<(MathStream & ms, MathData const & ar)
315 MathStream & operator<<(MathStream & ms, char const * s)
322 MathStream & operator<<(MathStream & ms, char c)
329 MathStream & operator<<(MathStream & ms, char_type c)
336 MathStream & operator<<(MathStream & ms, MTag const & t)
340 ms.os() << '<' << from_ascii(ms.namespacedTag(t.tag_));
341 if (!t.attr_.empty())
342 ms.os() << " " << from_ascii(t.attr_);
348 MathStream & operator<<(MathStream & ms, ETag const & t)
353 ms.os() << "</" << from_ascii(ms.namespacedTag(t.tag_)) << ">";
358 MathStream & operator<<(MathStream & ms, CTag const & t)
361 ms.os() << "<" << from_ascii(ms.namespacedTag(t.tag_));
362 if (!t.attr_.empty())
363 ms.os() << " " << from_utf8(t.attr_);
369 MathStream & operator<<(MathStream & ms, docstring const & s)
376 //////////////////////////////////////////////////////////////////////
379 HtmlStream::HtmlStream(odocstream & os)
380 : os_(os), tab_(0), line_(0), in_text_(false)
384 void HtmlStream::defer(docstring const & s)
390 void HtmlStream::defer(string const & s)
392 deferred_ << from_utf8(s);
396 docstring HtmlStream::deferred() const
398 return deferred_.str();
402 HtmlStream & operator<<(HtmlStream & ms, MathAtom const & at)
409 HtmlStream & operator<<(HtmlStream & ms, MathData const & ar)
416 HtmlStream & operator<<(HtmlStream & ms, char const * s)
423 HtmlStream & operator<<(HtmlStream & ms, char c)
430 HtmlStream & operator<<(HtmlStream & ms, char_type c)
437 HtmlStream & operator<<(HtmlStream & ms, MTag const & t)
439 ms.os() << '<' << from_ascii(t.tag_);
440 if (!t.attr_.empty())
441 ms.os() << " " << from_ascii(t.attr_);
447 HtmlStream & operator<<(HtmlStream & ms, ETag const & t)
449 ms.os() << "</" << from_ascii(t.tag_) << '>';
454 HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
461 //////////////////////////////////////////////////////////////////////
464 SetMode::SetMode(MathStream & ms, bool text)
467 was_text_ = ms_.inText();
468 ms_.setTextMode(text);
474 ms_.setTextMode(was_text_);
478 //////////////////////////////////////////////////////////////////////
481 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
484 was_text_ = os_.inText();
485 os_.setTextMode(text);
489 SetHTMLMode::~SetHTMLMode()
491 os_.setTextMode(was_text_);
495 //////////////////////////////////////////////////////////////////////
498 MapleStream & operator<<(MapleStream & ms, MathAtom const & at)
505 MapleStream & operator<<(MapleStream & ms, MathData const & ar)
512 MapleStream & operator<<(MapleStream & ms, char const * s)
519 MapleStream & operator<<(MapleStream & ms, char c)
526 MapleStream & operator<<(MapleStream & ms, int i)
533 MapleStream & operator<<(MapleStream & ms, char_type c)
540 MapleStream & operator<<(MapleStream & ms, docstring const & s)
547 //////////////////////////////////////////////////////////////////////
550 MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at)
557 MaximaStream & operator<<(MaximaStream & ms, MathData const & ar)
564 MaximaStream & operator<<(MaximaStream & ms, char const * s)
571 MaximaStream & operator<<(MaximaStream & ms, char c)
578 MaximaStream & operator<<(MaximaStream & ms, int i)
585 MaximaStream & operator<<(MaximaStream & ms, docstring const & s)
592 MaximaStream & operator<<(MaximaStream & ms, char_type c)
599 //////////////////////////////////////////////////////////////////////
602 MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at)
609 MathematicaStream & operator<<(MathematicaStream & ms, MathData const & ar)
616 MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
623 MathematicaStream & operator<<(MathematicaStream & ms, char c)
630 MathematicaStream & operator<<(MathematicaStream & ms, int i)
637 MathematicaStream & operator<<(MathematicaStream & ms, docstring const & s)
644 MathematicaStream & operator<<(MathematicaStream & ms, char_type c)
651 //////////////////////////////////////////////////////////////////////
654 OctaveStream & operator<<(OctaveStream & ns, MathAtom const & at)
661 OctaveStream & operator<<(OctaveStream & ns, MathData const & ar)
668 OctaveStream & operator<<(OctaveStream & ns, char const * s)
675 OctaveStream & operator<<(OctaveStream & ns, char c)
682 OctaveStream & operator<<(OctaveStream & ns, int i)
689 OctaveStream & operator<<(OctaveStream & ns, docstring const & s)
696 OctaveStream & operator<<(OctaveStream & ns, char_type c)
703 OctaveStream & operator<<(OctaveStream & os, string const & s)
705 os.os() << from_utf8(s);
710 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode)
712 // For the basic symbols, no difference between XML and HTML.
713 if (name.size() == 1) {
714 char_type const c = name[0];
716 return from_ascii("<");
718 return from_ascii(">");
721 } else if (name.size() == 2 && name[0] == '\\') {
722 char_type const c = name[1];
724 return from_ascii("{");
726 return from_ascii("}");
728 MathWordList const & words = mathedWordList();
729 MathWordList::const_iterator it = words.find(name);
730 if (it != words.end()) {
731 // Only difference between XML and HTML, based on the contents read by MathFactory.
732 docstring const escape = xmlmode ? it->second.xmlname : it->second.htmlname;
735 LYXERR0("Unable to find `" << name <<"' in the mathWordList.");