#include <config.h>
#include "tex2lyx.h"
+#include "context.h"
#include "FloatList.h"
-#include "lyxtextclass.h"
#include "support/lstrings.h"
#include "support/tostr.h"
using lyx::support::rtrim;
using lyx::support::suffixIs;
-// Do we need to output some \begin_layout command before the next characters?
-bool need_layout = true;
-// We may need to add something after this \begin_layout command
-string extra_stuff;
-// Do we need to output some \end_layout command
-bool need_end_layout = false;
-void check_end_layout(ostream & os)
+// thin wrapper around parse_text using a string
+string parse_text(Parser & p, unsigned flags, const bool outer,
+ Context & context)
{
- if (need_end_layout) {
- os << "\n\\end_layout\n";
- need_end_layout = false;
- }
+ ostringstream os;
+ parse_text(p, os, flags, outer, context);
+ return os.str();
+}
+
+// parses a subdocument, usually useful in insets (whence the name)
+void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
+ Context & context)
+{
+ Context newcontext(true, context.textclass);
+ parse_text(p, os, flags, outer, newcontext);
+ newcontext.check_end_layout(os);
+}
+
+
+// parses a paragraph snippet, useful for example for \emph{...}
+void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
+ Context & context)
+{
+ Context newcontext(false, context.textclass);
+ parse_text(p, os, flags, outer, newcontext);
+ // should not be needed
+ newcontext.check_end_layout(os);
}
char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
"small", "normal", "large", "larger", "largest", "huge", "giant", 0};
-string cap(string s)
-{
- if (s.size())
- s[0] = toupper(s[0]);
- return s;
-}
-
-
// splits "x=z, y=b" into a map
map<string, string> split_map(string const & s)
{
}
-void check_layout(ostream & os, LyXLayout_ptr layout)
-{
- if (need_layout) {
- check_end_layout(os);
- os << "\n\\begin_layout " << layout->name() << "\n\n";
- need_end_layout = true;
- need_layout=false;
- if (!extra_stuff.empty()) {
- os << extra_stuff;
- extra_stuff.erase();
- }
- }
-}
-
-
void begin_inset(ostream & os, string const & name)
{
os << "\n\\begin_inset " << name;
}
-void handle_ert(ostream & os, string const & s)
+void handle_ert(ostream & os, string const & s, Context const & context)
{
+ Context newcontext(true, context.textclass);
begin_inset(os, "ERT");
- os << "\nstatus Collapsed\n\n\\begin_layout Standard\n\n";
+ os << "\nstatus Collapsed\n";
+ newcontext.check_layout(os);
for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
if (*it == '\\')
os << "\n\\backslash \n";
else
os << *it;
}
- need_end_layout = true;
- check_end_layout(os);
+ newcontext.check_end_layout(os);
end_inset(os);
}
}
-void output_command_layout(ostream & os, LyXLayout_ptr const & layout,
- Parser & p, bool outer, LyXTextClass const & textclass)
+void output_command_layout(ostream & os, Parser & p, bool outer,
+ Context & parent_context,
+ LyXLayout_ptr newlayout)
{
- need_layout = true;
- check_layout(os, layout);
- if (layout->optionalargs > 0) {
+// parent_context.dump(os, "#parent_context before output_command_layout");
+ parent_context.check_end_layout(os);
+ Context context(true, parent_context.textclass, newlayout,
+ parent_context.layout);
+ context.check_layout(os);
+ if (context.layout->optionalargs > 0) {
string s;
if (p.next_token().character() == '[') {
p.get_token(); // eat '['
begin_inset(os, "OptArg\n");
os << "collapsed true\n";
- parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, textclass);
+ parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
end_inset(os);
}
}
- parse_text(p, os, FLAG_ITEM, outer, textclass, layout);
- need_layout = true;
+ parse_text_snippet(p, os, FLAG_ITEM, outer, context);
+ context.check_end_layout(os);
+// context.dump(os, "#context after output_command_layout");
+// parent_context.dump(os, "#parent_context after output_command_layout");
}
-} // anonymous namespace
-
-
void parse_environment(Parser & p, ostream & os, bool outer,
- LyXTextClass const & textclass, LyXLayout_ptr layout)
+ Context & parent_context)
{
+// parent_context.dump(os, "#parent_context before parse_environment");
LyXLayout_ptr newlayout;
string const name = p.getArg('{', '}');
const bool is_starred = suffixIs(name, '*');
string const unstarred_name = rtrim(name, "*");
active_environments.push_back(name);
if (is_math_env(name)) {
- check_layout(os, layout);
+ parent_context.check_layout(os);
begin_inset(os, "Formula ");
os << "\\begin{" << name << "}";
parse_math(p, os, FLAG_END, MATH_MODE);
os << "\\end{" << name << "}";
end_inset(os);
} else if (name == "tabular") {
- check_layout(os, layout);
+ parent_context.check_layout(os);
begin_inset(os, "Tabular ");
- handle_tabular(p, os, textclass);
+ handle_tabular(p, os, parent_context);
end_inset(os);
- } else if (textclass.floats().typeExist(unstarred_name)) {
- check_layout(os, layout);
+ } else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
+ parent_context.check_layout(os);
begin_inset(os, "Float " + unstarred_name + "\n");
if (p.next_token().asInput() == "[") {
os << "placement " << p.getArg('[', ']') << '\n';
}
os << "wide " << tostr(is_starred)
<< "\ncollapsed false\n";
- parse_text_in_inset(p, os, FLAG_END, outer, textclass);
+ parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
end_inset(os);
} else if (name == "center") {
- parse_text(p, os, FLAG_END, outer, textclass);
+ parse_text(p, os, FLAG_END, outer, parent_context);
// The single '=' is meant here.
- } else if ((newlayout = findLayout(textclass, name)).get() &&
+ } else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
newlayout->isEnvironment()) {
- size_t const n = active_environments.size();
- string const s = active_environments[n - 2];
- bool const deeper = s == "enumerate" || s == "itemize"
- || s == "lyxlist";
- if (deeper)
- os << "\n\\begin_deeper";
- switch (newlayout->latextype) {
+ Context context(true, parent_context.textclass, newlayout,
+ parent_context.layout);
+ parent_context.check_end_layout(os);
+// context.dump(os, "#context in parse_environment");
+ switch (context.layout->latextype) {
case LATEX_LIST_ENVIRONMENT:
- extra_stuff = "\\labelwidthstring "
+ context.extra_stuff = "\\labelwidthstring "
+ p.verbatim_item() + '\n';
break;
case LATEX_BIB_ENVIRONMENT:
default:
break;
}
- need_layout = true;
- parse_text(p, os, FLAG_END, outer, textclass, newlayout);
- check_end_layout(os);
- if (deeper)
- os << "\n\\end_deeper\n";
- need_layout = true;
+ //context.check_layout(os);
+ parse_text(p, os, FLAG_END, outer, context);
+// context.dump(os, "#context after parse_environment");
+ context.check_end_layout(os);
} else {
- cerr << "why are we here?" << endl;
- parse_text(p, os, FLAG_END, outer, textclass);
+ parent_context.check_layout(os);
+ handle_ert(os, "\\begin{" + name + "}", parent_context);
+ parse_text_snippet(p, os, FLAG_END, outer, parent_context);
+ handle_ert(os, "\\end{" + name + "}", parent_context);
}
}
+} // anonymous namespace
+
+
+
void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
- LyXTextClass const & textclass, LyXLayout_ptr layout)
+ Context & context)
{
- if (!layout.get())
- layout = textclass.defaultLayout();
LyXLayout_ptr newlayout;
while (p.good()) {
Token const & t = p.get_token();
//
if (t.cat() == catMath) {
// we are inside some text mode thingy, so opening new math is allowed
- check_layout(os, layout);
+ context.check_layout(os);
begin_inset(os, "Formula ");
Token const & n = p.get_token();
if (n.cat() == catMath && outer) {
// quote...)
else if (t.asInput() == "`"
&& p.next_token().asInput() == "`") {
- check_layout(os, layout);
+ context.check_layout(os);
begin_inset(os, "Quotes ");
os << "eld";
end_inset(os);
}
else if (t.asInput() == "'"
&& p.next_token().asInput() == "'") {
- check_layout(os, layout);
+ context.check_layout(os);
begin_inset(os, "Quotes ");
os << "erd";
end_inset(os);
t.cat() == catOther ||
t.cat() == catAlign ||
t.cat() == catParameter) {
- check_layout(os, layout);
+ context.check_layout(os);
os << t.character();
}
else if (t.cat() == catNewline) {
if (p.next_token().cat() == catNewline) {
p.get_token();
- need_layout = true;
+ context.need_layout = true;
+ // this should be done by the parser already
+ cerr << "what are we doing here?" << endl;
} else {
os << " "; // note the space
}
}
else if (t.cat() == catActive) {
- check_layout(os, layout);
+ context.check_layout(os);
if (t.character() == '~') {
- if (layout->free_spacing)
+ if (context.layout->free_spacing)
os << ' ';
else
os << "\\InsetSpace ~\n";
else if (t.cat() == catBegin) {
// FIXME???
// special handling of size changes
- check_layout(os, layout);
+ context.check_layout(os);
bool const is_size = is_known(p.next_token().cs(), known_sizes);
- need_end_layout = false;
- string const s = parse_text(p, FLAG_BRACE_LAST, outer, textclass, layout);
- need_end_layout = true;
+ Context newcontext(false, context.textclass);
+// need_end_layout = false;
+ string const s = parse_text(p, FLAG_BRACE_LAST, outer, newcontext);
+// need_end_layout = true;
if (s.empty() && p.next_token().character() == '`')
; // ignore it in {}``
else if (is_size || s == "[" || s == "]" || s == "*")
os << s;
else {
- handle_ert(os, "{");
+ handle_ert(os, "{", context);
os << s;
- handle_ert(os, "}");
+ handle_ert(os, "}", context);
}
}
else if (t.cat() == catEnd) {
if (flags & FLAG_BRACE_LAST) {
- check_end_layout(os);
+ context.check_end_layout(os);
return;
}
cerr << "stray '}' in text\n";
- handle_ert(os, "}");
+ handle_ert(os, "}", context);
}
else if (t.cat() == catComment)
//
else if (t.cs() == "(") {
- check_layout(os, layout);
+ context.check_layout(os);
begin_inset(os, "Formula");
os << " \\(";
parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
}
else if (t.cs() == "[") {
- check_layout(os, layout);
+ context.check_layout(os);
begin_inset(os, "Formula");
os << " \\[";
parse_math(p, os, FLAG_EQUATION, MATH_MODE);
}
else if (t.cs() == "begin")
- parse_environment(p, os, outer, textclass, layout);
+ parse_environment(p, os, outer, context);
else if (t.cs() == "end") {
if (flags & FLAG_END) {
cerr << "\\end{" + name + "} does not match \\begin{"
+ active_environment() + "}\n";
active_environments.pop_back();
+ context.check_end_layout(os);
return;
}
p.error("found 'end' unexpectedly");
string s;
if (p.next_token().character() == '[') {
p.get_token(); // eat '['
- s = parse_text(p, FLAG_BRACK_LAST, outer, textclass, layout);
+ Context newcontext(false, context.textclass);
+ s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
}
- need_layout = true;
- check_layout(os, layout);
+ context.need_layout = true;
+ context.check_layout(os);
if (s.size())
os << s << ' ';
}
string name = p.get_token().cs();
while (p.next_token().cat() != catBegin)
name += p.get_token().asString();
- handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}');
+ handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
}
else if (t.cs() == "par") {
p.skip_spaces();
- need_layout = true;
+ context.check_end_layout(os);
+ context.need_layout = true;
// if (p.next_token().cs() != "\\begin")
// handle_par(os);
//cerr << "next token: '" << p.next_token().cs() << "'\n";
// Must attempt to parse "Section*" before "Section".
else if ((p.next_token().asInput() == "*") &&
// The single '=' is meant here.
- (newlayout = findLayout(textclass,
+ (newlayout = findLayout(context.textclass,
t.cs() + '*')).get() &&
newlayout->isCommand()) {
p.get_token();
- output_command_layout(os, newlayout, p, outer, textclass);
+ output_command_layout(os, p, outer, context, newlayout);
}
// The single '=' is meant here.
- else if ((newlayout = findLayout(textclass, t.cs())).get() &&
+ else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
newlayout->isCommand()) {
- output_command_layout(os, newlayout, p, outer, textclass);
+ output_command_layout(os, p, outer, context, newlayout);
}
else if (t.cs() == "includegraphics") {
map<string, string> opts = split_map(p.getArg('[', ']'));
string name = p.verbatim_item();
- check_layout(os, layout);
+ context.check_layout(os);
begin_inset(os, "Graphics ");
os << "\n\tfilename " << name << '\n';
if (opts.find("width") != opts.end())
}
else if (t.cs() == "footnote") {
- check_layout(os, layout);
+ context.check_layout(os);
begin_inset(os, "Foot\n");
os << "collapsed true\n";
- parse_text_in_inset(p, os, FLAG_ITEM, false, textclass);
+ parse_text_in_inset(p, os, FLAG_ITEM, false, context);
end_inset(os);
}
- else if (t.cs() == "ensuremath") {
- check_layout(os, layout);
- string s = parse_text(p, FLAG_ITEM, false, textclass);
- if (s == "±" || s == "³" || s == "²" || s == "µ")
- os << s;
- else
- handle_ert(os, "\\ensuremath{" + s + "}");
- }
-
else if (t.cs() == "marginpar") {
- check_layout(os, layout);
+ context.check_layout(os);
begin_inset(os, "Marginal\n");
os << "collapsed true\n";
- need_layout = true;
- parse_text(p, os, FLAG_ITEM, false, textclass);
+ parse_text_in_inset(p, os, FLAG_ITEM, false, context);
end_inset(os);
- need_end_layout = true;
+ }
+
+ else if (t.cs() == "ensuremath") {
+ context.check_layout(os);
+ Context newcontext(false, context.textclass);
+ string s = parse_text(p, FLAG_ITEM, false, newcontext);
+ if (s == "±" || s == "³" || s == "²" || s == "µ")
+ os << s;
+ else
+ handle_ert(os, "\\ensuremath{" + s + "}",
+ context);
}
else if (t.cs() == "hfill") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\hfill\n";
skip_braces(p);
}
skip_braces(p); // swallow this
else if (t.cs() == "tableofcontents") {
- check_layout(os, layout);
+ context.check_layout(os);
begin_inset(os, "LatexCommand ");
os << '\\' << t.cs() << "{}\n";
end_inset(os);
else if (t.cs() == "textrm") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\family roman \n";
- parse_text(p, os, FLAG_ITEM, outer, textclass);
+ parse_text_snippet(p, os, FLAG_ITEM, outer, context);
os << "\n\\family default \n";
}
else if (t.cs() == "textsf") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\family sans \n";
- parse_text(p, os, FLAG_ITEM, outer, textclass);
+ parse_text_snippet(p, os, FLAG_ITEM, outer, context);
os << "\n\\family default \n";
}
else if (t.cs() == "texttt") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\family typewriter \n";
- parse_text(p, os, FLAG_ITEM, outer, textclass);
+ parse_text_snippet(p, os, FLAG_ITEM, outer, context);
os << "\n\\family default \n";
}
else if (t.cs() == "textit") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\shape italic \n";
- parse_text(p, os, FLAG_ITEM, outer, textclass);
+ parse_text_snippet(p, os, FLAG_ITEM, outer, context);
os << "\n\\shape default \n";
}
else if (t.cs() == "textsc") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\noun on \n";
- parse_text(p, os, FLAG_ITEM, outer, textclass);
+ parse_text_snippet(p, os, FLAG_ITEM, outer, context);
os << "\n\\noun default \n";
}
else if (t.cs() == "textbf") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\series bold \n";
- parse_text(p, os, FLAG_ITEM, outer, textclass);
+ parse_text_snippet(p, os, FLAG_ITEM, outer, context);
os << "\n\\series default \n";
}
else if (t.cs() == "underbar") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\bar under \n";
- parse_text(p, os, FLAG_ITEM, outer, textclass);
+ parse_text_snippet(p, os, FLAG_ITEM, outer, context);
os << "\n\\bar default \n";
}
else if (t.cs() == "emph" || t.cs() == "noun") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\" << t.cs() << " on \n";
- parse_text(p, os, FLAG_ITEM, outer, textclass);
+ parse_text_snippet(p, os, FLAG_ITEM, outer, context);
os << "\n\\" << t.cs() << " default \n";
}
else if (t.cs() == "bibitem") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\\bibitem ";
os << p.getOpt();
os << '{' << p.verbatim_item() << '}' << "\n";
}
else if (is_known(t.cs(), known_latex_commands)) {
- check_layout(os, layout);
+ context.check_layout(os);
begin_inset(os, "LatexCommand ");
os << '\\' << t.cs();
os << p.getOpt();
else if (is_known(t.cs(), known_sizes)) {
char const ** where = is_known(t.cs(), known_sizes);
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
}
else if (t.cs() == "LyX" || t.cs() == "TeX"
|| t.cs() == "LaTeX") {
- check_layout(os, layout);
+ context.check_layout(os);
os << t.cs();
skip_braces(p); // eat {}
}
else if (t.cs() == "LaTeXe") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "LaTeX2e";
skip_braces(p); // eat {}
}
else if (t.cs() == "ldots") {
- check_layout(os, layout);
+ context.check_layout(os);
skip_braces(p);
os << "\\SpecialChar \\ldots{}\n";
}
else if (t.cs() == "lyxarrow") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\\SpecialChar \\menuseparator\n";
skip_braces(p);
}
else if (t.cs() == "ldots") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\\SpecialChar \\ldots{}\n";
skip_braces(p);
}
else if (t.cs() == "@" && p.next_token().asInput() == ".") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\\SpecialChar \\@.\n";
p.get_token();
}
else if (t.cs() == "-") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\\SpecialChar \\-\n";
}
else if (t.cs() == "textasciitilde") {
- check_layout(os, layout);
+ context.check_layout(os);
os << '~';
skip_braces(p);
}
else if (t.cs() == "textasciicircum") {
- check_layout(os, layout);
+ context.check_layout(os);
os << '^';
skip_braces(p);
}
else if (t.cs() == "textbackslash") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\backslash \n";
skip_braces(p);
}
else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
|| t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
|| t.cs() == "%") {
- check_layout(os, layout);
+ context.check_layout(os);
os << t.cs();
}
else if (t.cs() == "char") {
- check_layout(os, layout);
+ context.check_layout(os);
if (p.next_token().character() == '`') {
p.get_token();
if (p.next_token().cs() == "\"") {
os << '"';
skip_braces(p);
} else {
- handle_ert(os, "\\char`");
+ handle_ert(os, "\\char`", context);
}
} else {
- handle_ert(os, "\\char");
+ handle_ert(os, "\\char", context);
}
}
else if (t.cs() == "\"") {
- check_layout(os, layout);
+ context.check_layout(os);
string const name = p.verbatim_item();
if (name == "a") os << 'ä';
else if (name == "o") os << 'ö';
else if (name == "A") os << 'Ä';
else if (name == "O") os << 'Ö';
else if (name == "U") os << 'Ü';
- else handle_ert(os, "\"{" + name + "}");
+ else handle_ert(os, "\"{" + name + "}", context);
}
else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
|| t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
// we need the trim as the LyX parser chokes on such spaces
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\i \\" << t.cs() << "{"
- << trim(parse_text(p, FLAG_ITEM, outer, textclass), " ") << "}\n";
+ << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
}
else if (t.cs() == "ss") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "ß";
}
else if (t.cs() == "i" || t.cs() == "j") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\\" << t.cs() << ' ';
}
else if (t.cs() == "\\") {
- check_layout(os, layout);
+ context.check_layout(os);
os << "\n\\newline \n";
}
else if (t.cs() == "input") {
- check_layout(os, layout);
- handle_ert(os, "\\input{" + p.verbatim_item() + "}\n");
+ context.check_layout(os);
+ handle_ert(os, "\\input{" + p.verbatim_item() + "}\n",
+ context);
}
else if (t.cs() == "fancyhead") {
- check_layout(os, layout);
+ context.check_layout(os);
ostringstream ss;
ss << "\\fancyhead";
ss << p.getOpt();
ss << '{' << p.verbatim_item() << "}\n";
- handle_ert(os, ss.str());
+ handle_ert(os, ss.str(), context);
}
else {
z = p.verbatim_item();
}
cerr << "found ERT: " << s << endl;
- handle_ert(os, s + ' ');
+ handle_ert(os, s + ' ', context);
*/
- check_layout(os, layout);
- handle_ert(os, t.asInput() + ' ');
+ context.check_layout(os);
+ handle_ert(os, t.asInput() + ' ', context);
}
if (flags & FLAG_LEAVE) {
}
-string parse_text(Parser & p, unsigned flags, const bool outer,
- LyXTextClass const & textclass,
- LyXLayout_ptr layout)
-{
- ostringstream os;
- parse_text(p, os, flags, outer, textclass, layout);
- return os.str();
-}
-
-void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
- LyXTextClass const & textclass, LyXLayout_ptr layout)
-{
- need_layout = true;
- need_end_layout = false;
- parse_text(p, os, flags, outer, textclass, layout);
- check_end_layout(os);
- need_end_layout = true;
-}
// }])