Revert "tex2lyx: towards proper support of "literal"/"latexified" inset commands"

This reverts commit 6659304f7f.
This commit is contained in:
Juergen Spitzmueller 2018-03-09 11:32:09 +01:00
parent 7b3a04b07f
commit 74d9277909

View File

@ -534,12 +534,8 @@ bool skip_braces(Parser & p)
/// replace LaTeX commands in \p s from the unicodesymbols file with their /// replace LaTeX commands in \p s from the unicodesymbols file with their
/// unicode points /// unicode points
pair<bool, docstring> convert_unicodesymbols(docstring s) docstring convert_unicodesymbols(docstring s)
{ {
bool res = true;
int const nchars_escape = 8;
static char_type const chars_escape[nchars_escape] = {
'&', '_', '$', '%', '#', '^', '{', '}'};
odocstringstream os; odocstringstream os;
for (size_t i = 0; i < s.size();) { for (size_t i = 0; i < s.size();) {
if (s[i] != '\\') { if (s[i] != '\\') {
@ -560,42 +556,24 @@ pair<bool, docstring> convert_unicodesymbols(docstring s)
s = rem; s = rem;
if (s.empty() || s[0] != '\\') if (s.empty() || s[0] != '\\')
i = 0; i = 0;
else { else
res = false;
for (int k = 0; k < nchars_escape; k++)
if (prefixIs(s, from_ascii("\\") + chars_escape[k]))
res = true;
i = 1; i = 1;
} }
} return os.str();
return make_pair(res, os.str());
} }
/// try to convert \p s to a valid InsetCommand argument /// try to convert \p s to a valid InsetCommand argument
/// return whether this succeeded. If not, these command insets string convert_command_inset_arg(string s)
/// get the "literate" flag.
pair<bool, string> convert_latexed_command_inset_arg(string s)
{ {
bool success = false; if (isAscii(s))
if (isAscii(s)) {
// since we don't know the input encoding we can't use from_utf8 // since we don't know the input encoding we can't use from_utf8
pair<bool, docstring> res = convert_unicodesymbols(from_ascii(s)); s = to_utf8(convert_unicodesymbols(from_ascii(s)));
success = res.first;
s = to_utf8(res.second);
}
// LyX cannot handle newlines in a latex command
return make_pair(success, subst(s, "\n", " "));
}
/// try to convert \p s to a valid InsetCommand argument
/// without trying to recode macros.
string convert_literate_command_inset_arg(string s)
{
// LyX cannot handle newlines in a latex command // LyX cannot handle newlines in a latex command
return subst(s, "\n", " "); return subst(s, "\n", " ");
} }
void output_ert(ostream & os, string const & s, Context & context) void output_ert(ostream & os, string const & s, Context & context)
{ {
context.check_layout(os); context.check_layout(os);
@ -3040,17 +3018,20 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
context.set_item(); context.set_item();
context.check_layout(os); context.check_layout(os);
eat_whitespace(p, os, context, false); eat_whitespace(p, os, context, false);
string label = p.verbatimOption(); string label = convert_command_inset_arg(p.verbatimOption());
pair<bool, string> lbl = convert_latexed_command_inset_arg(label); string key = convert_command_inset_arg(p.verbatim_item());
bool const literal = !lbl.first; if (contains(label, '\\') || contains(key, '\\')) {
label = literal ? subst(label, "\n", " ") : lbl.second; // LyX can't handle LaTeX commands in labels or keys
string lit = literal ? "\"true\"" : "\"false\""; output_ert_inset(os, t.asInput() + '[' + label +
string key = convert_literate_command_inset_arg(p.verbatim_item()); "]{" + p.verbatim_item() + '}',
context);
} else {
begin_command_inset(os, "bibitem", "bibitem"); begin_command_inset(os, "bibitem", "bibitem");
os << "label \"" << label << "\"\n" os << "label \"" << label << "\"\n"
<< "key \"" << key << "\"\n" << "key \"" << key << "\"\n"
<< "literal " << lit << "\n"; << "literal \"true\"\n";
end_inset(os); end_inset(os);
}
continue; continue;
} }
@ -3257,9 +3238,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
string opt_arg1; string opt_arg1;
string opt_arg2; string opt_arg2;
if (p.hasOpt()) { if (p.hasOpt()) {
opt_arg1 = convert_literate_command_inset_arg(p.getFullOpt()); opt_arg1 = convert_command_inset_arg(p.getFullOpt());
if (p.hasOpt()) if (p.hasOpt())
opt_arg2 = convert_literate_command_inset_arg(p.getFullOpt()); opt_arg2 = convert_command_inset_arg(p.getFullOpt());
} }
output_ert_inset(os, t.asInput() + opt_arg1 + opt_arg2 output_ert_inset(os, t.asInput() + opt_arg1 + opt_arg2
+ "{" + p.verbatim_item() + '}', context); + "{" + p.verbatim_item() + '}', context);
@ -3765,12 +3746,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
if (t.cs() == "href") { if (t.cs() == "href") {
context.check_layout(os); context.check_layout(os);
string target = convert_literate_command_inset_arg(p.verbatim_item()); string target = convert_command_inset_arg(p.verbatim_item());
string name = p.verbatim_item(); string name = convert_command_inset_arg(p.verbatim_item());
pair<bool, string> nm = convert_latexed_command_inset_arg(name);
bool const literal = !nm.first;
name = literal ? subst(name, "\n", " ") : nm.second;
string lit = literal ? "\"true\"" : "\"false\"";
string type; string type;
size_t i = target.find(':'); size_t i = target.find(':');
if (i != string::npos) { if (i != string::npos) {
@ -3787,7 +3764,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
os << "target \"" << target << "\"\n"; os << "target \"" << target << "\"\n";
if (type == "mailto:" || type == "file:") if (type == "mailto:" || type == "file:")
os << "type \"" << type << "\"\n"; os << "type \"" << type << "\"\n";
os << "literal " << lit << "\n"; os << "literal \"true\"\n";
end_inset(os); end_inset(os);
skip_spaces_braces(p); skip_spaces_braces(p);
continue; continue;
@ -3846,7 +3823,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
os << "reference \""; os << "reference \"";
os << known_refstyle_prefixes[where - known_refstyle_commands] os << known_refstyle_prefixes[where - known_refstyle_commands]
<< ":"; << ":";
os << convert_literate_command_inset_arg(p.verbatim_item()) os << convert_command_inset_arg(p.verbatim_item())
<< "\"\n"; << "\"\n";
os << "plural \"false\"\n"; os << "plural \"false\"\n";
os << "caps \"false\"\n"; os << "caps \"false\"\n";
@ -3866,7 +3843,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
begin_command_inset(os, "ref", begin_command_inset(os, "ref",
known_coded_ref_commands[where - known_ref_commands]); known_coded_ref_commands[where - known_ref_commands]);
os << "reference \"" os << "reference \""
<< convert_literate_command_inset_arg(p.verbatim_item()) << convert_command_inset_arg(p.verbatim_item())
<< "\"\n"; << "\"\n";
os << "plural \"false\"\n"; os << "plural \"false\"\n";
os << "caps \"false\"\n"; os << "caps \"false\"\n";
@ -3923,34 +3900,24 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
before.erase(); before.erase();
after.erase(); after.erase();
} }
bool literal = false;
pair<bool, string> aft;
pair<bool, string> bef;
// remove the brackets around after and before // remove the brackets around after and before
if (!after.empty()) { if (!after.empty()) {
after.erase(0, 1); after.erase(0, 1);
after.erase(after.length() - 1, 1); after.erase(after.length() - 1, 1);
aft = convert_latexed_command_inset_arg(after); after = convert_command_inset_arg(after);
literal = !aft.first;
after = literal ? subst(after, "\n", " ") : aft.second;
} }
if (!before.empty()) { if (!before.empty()) {
before.erase(0, 1); before.erase(0, 1);
before.erase(before.length() - 1, 1); before.erase(before.length() - 1, 1);
bef = convert_latexed_command_inset_arg(after); before = convert_command_inset_arg(before);
literal |= !bef.first;
before = literal ? subst(before, "\n", " ") : bef.second;
if (literal && !after.empty())
after = subst(after, "\n", " ");
} }
string lit = literal ? "\"true\"" : "\"false\"";
begin_command_inset(os, "citation", command); begin_command_inset(os, "citation", command);
os << "after " << '"' << after << '"' << "\n"; os << "after " << '"' << after << '"' << "\n";
os << "before " << '"' << before << '"' << "\n"; os << "before " << '"' << before << '"' << "\n";
os << "key \"" os << "key \""
<< convert_literate_command_inset_arg(p.verbatim_item()) << convert_command_inset_arg(p.verbatim_item())
<< "\"\n" << "\"\n"
<< "literal " << lit << "\n"; << "literal \"true\"\n";
end_inset(os); end_inset(os);
// Need to set the cite engine if natbib is loaded by // Need to set the cite engine if natbib is loaded by
// the document class directly // the document class directly
@ -4023,83 +3990,58 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
before.erase(); before.erase();
after.erase(); after.erase();
} }
bool literal = false;
pair<bool, string> aft;
pair<bool, string> bef;
// remove the brackets around after and before // remove the brackets around after and before
if (!after.empty()) { if (!after.empty()) {
after.erase(0, 1); after.erase(0, 1);
after.erase(after.length() - 1, 1); after.erase(after.length() - 1, 1);
aft = convert_latexed_command_inset_arg(after); after = convert_command_inset_arg(after);
literal = !aft.first;
after = literal ? subst(after, "\n", " ") : aft.second;
} }
if (!before.empty()) { if (!before.empty()) {
before.erase(0, 1); before.erase(0, 1);
before.erase(before.length() - 1, 1); before.erase(before.length() - 1, 1);
bef = convert_latexed_command_inset_arg(after); before = convert_command_inset_arg(before);
literal |= !bef.first;
before = literal ? subst(before, "\n", " ") : bef.second;
} }
string keys, pretextlist, posttextlist; string keys, pretextlist, posttextlist;
if (qualified) { if (qualified) {
map<string, string> pres, posts, preslit, postslit; map<string, string> pres;
map<string, string> posts;
vector<string> lkeys; vector<string> lkeys;
// text before the citation // text before the citation
string lbefore, lbeforelit; string lbefore;
// text after the citation // text after the citation
string lafter, lafterlit; string lafter;
string lkey; string lkey;
pair<bool, string> laft, lbef;
while (true) { while (true) {
get_cite_arguments(p, true, lbefore, lafter); get_cite_arguments(p, true, lbefore, lafter);
// remove the brackets around after and before // remove the brackets around after and before
if (!lafter.empty()) { if (!lafter.empty()) {
lafter.erase(0, 1); lafter.erase(0, 1);
lafter.erase(lafter.length() - 1, 1); lafter.erase(lafter.length() - 1, 1);
laft = convert_latexed_command_inset_arg(lafter); lafter = convert_command_inset_arg(lafter);
literal |= !laft.first;
lafter = laft.second;
lafterlit = subst(lbefore, "\n", " ");
} }
if (!lbefore.empty()) { if (!lbefore.empty()) {
lbefore.erase(0, 1); lbefore.erase(0, 1);
lbefore.erase(lbefore.length() - 1, 1); lbefore.erase(lbefore.length() - 1, 1);
lbef = convert_latexed_command_inset_arg(lbefore); lbefore = convert_command_inset_arg(lbefore);
literal |= !lbef.first;
lbefore = lbef.second;
lbeforelit = subst(lbefore, "\n", " ");
} }
if (lbefore.empty() && lafter == "[]") { if (lbefore.empty() && lafter == "[]")
// avoid \cite[]{a} // avoid \cite[]{a}
lafter.erase(); lafter.erase();
lafterlit.erase();
}
else if (lbefore == "[]" && lafter == "[]") { else if (lbefore == "[]" && lafter == "[]") {
// avoid \cite[][]{a} // avoid \cite[][]{a}
lbefore.erase(); lbefore.erase();
lafter.erase(); lafter.erase();
lbeforelit.erase();
lafterlit.erase();
} }
lkey = p.getArg('{', '}'); lkey = p.getArg('{', '}');
if (lkey.empty()) if (lkey.empty())
break; break;
if (!lbefore.empty()) { if (!lbefore.empty())
pres.insert(make_pair(lkey, lbefore)); pres.insert(make_pair(lkey, lbefore));
preslit.insert(make_pair(lkey, lbeforelit)); if (!lafter.empty())
}
if (!lafter.empty()) {
posts.insert(make_pair(lkey, lafter)); posts.insert(make_pair(lkey, lafter));
postslit.insert(make_pair(lkey, lafterlit));
}
lkeys.push_back(lkey); lkeys.push_back(lkey);
} }
keys = convert_literate_command_inset_arg(getStringFromVector(lkeys)); keys = convert_command_inset_arg(getStringFromVector(lkeys));
if (literal) {
pres = preslit;
posts = postslit;
}
for (auto const & ptl : pres) { for (auto const & ptl : pres) {
if (!pretextlist.empty()) if (!pretextlist.empty())
pretextlist += '\t'; pretextlist += '\t';
@ -4111,14 +4053,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
posttextlist += potl.first + " " + potl.second; posttextlist += potl.first + " " + potl.second;
} }
} else } else
keys = convert_literate_command_inset_arg(p.verbatim_item()); keys = convert_command_inset_arg(p.verbatim_item());
if (literal) {
if (!after.empty())
after = subst(after, "\n", " ");
if (!before.empty())
before = subst(after, "\n", " ");
}
string lit = literal ? "\"true\"" : "\"false\"";
begin_command_inset(os, "citation", command); begin_command_inset(os, "citation", command);
os << "after " << '"' << after << '"' << "\n"; os << "after " << '"' << after << '"' << "\n";
os << "before " << '"' << before << '"' << "\n"; os << "before " << '"' << before << '"' << "\n";
@ -4129,7 +4064,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
os << "pretextlist " << '"' << pretextlist << '"' << "\n"; os << "pretextlist " << '"' << pretextlist << '"' << "\n";
if (!posttextlist.empty()) if (!posttextlist.empty())
os << "posttextlist " << '"' << posttextlist << '"' << "\n"; os << "posttextlist " << '"' << posttextlist << '"' << "\n";
os << "literal " << lit << "\n"; os << "literal \"true\"\n";
end_inset(os); end_inset(os);
// Need to set the cite engine if biblatex is loaded by // Need to set the cite engine if biblatex is loaded by
// the document class directly // the document class directly
@ -4175,32 +4110,19 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
"package options if you used an\n" "package options if you used an\n"
"earlier jurabib version." << endl; "earlier jurabib version." << endl;
} }
bool literal = false;
pair<bool, string> aft;
pair<bool, string> bef;
// remove the brackets around after and before
if (!after.empty()) { if (!after.empty()) {
after.erase(0, 1); after.erase(0, 1);
after.erase(after.length() - 1, 1); after.erase(after.length() - 1, 1);
aft = convert_latexed_command_inset_arg(after);
literal = !aft.first;
after = literal ? subst(after, "\n", " ") : aft.second;
} }
if (!before.empty()) { if (!before.empty()) {
before.erase(0, 1); before.erase(0, 1);
before.erase(before.length() - 1, 1); before.erase(before.length() - 1, 1);
bef = convert_latexed_command_inset_arg(after);
literal |= !bef.first;
before = literal ? subst(before, "\n", " ") : bef.second;
if (literal && !after.empty())
after = subst(after, "\n", " ");
} }
string lit = literal ? "\"true\"" : "\"false\"";
begin_command_inset(os, "citation", command); begin_command_inset(os, "citation", command);
os << "after " << '"' << after << "\"\n" os << "after " << '"' << after << "\"\n"
<< "before " << '"' << before << "\"\n" << "before " << '"' << before << "\"\n"
<< "key " << '"' << citation << "\"\n" << "key " << '"' << citation << "\"\n"
<< "literal " << lit << "\n"; << "literal \"true\"\n";
end_inset(os); end_inset(os);
// Need to set the cite engine if jurabib is loaded by // Need to set the cite engine if jurabib is loaded by
// the document class directly // the document class directly
@ -4212,19 +4134,15 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
if (t.cs() == "cite" if (t.cs() == "cite"
|| t.cs() == "nocite") { || t.cs() == "nocite") {
context.check_layout(os); context.check_layout(os);
string after = p.getArg('[', ']'); string after = convert_command_inset_arg(p.getArg('[', ']'));
pair<bool, string> aft = convert_latexed_command_inset_arg(after); string key = convert_command_inset_arg(p.verbatim_item());
bool const literal = !aft.first;
after = literal ? subst(after, "\n", " ") : aft.second;
string lit = literal ? "\"true\"" : "\"false\"";
string key = convert_literate_command_inset_arg(p.verbatim_item());
// store the case that it is "\nocite{*}" to use it later for // store the case that it is "\nocite{*}" to use it later for
// the BibTeX inset // the BibTeX inset
if (key != "*") { if (key != "*") {
begin_command_inset(os, "citation", t.cs()); begin_command_inset(os, "citation", t.cs());
os << "after " << '"' << after << "\"\n" os << "after " << '"' << after << "\"\n"
<< "key " << '"' << key << "\"\n" << "key " << '"' << key << "\"\n"
<< "literal " << lit << "\n"; << "literal \"true\"\n";
end_inset(os); end_inset(os);
} else if (t.cs() == "nocite") } else if (t.cs() == "nocite")
btprint = key; btprint = key;
@ -4249,27 +4167,15 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
if (t.cs() == "nomenclature") { if (t.cs() == "nomenclature") {
context.check_layout(os); context.check_layout(os);
begin_command_inset(os, "nomenclature", "nomenclature"); begin_command_inset(os, "nomenclature", "nomenclature");
string prefix = convert_literate_command_inset_arg(p.getArg('[', ']')); string prefix = convert_command_inset_arg(p.getArg('[', ']'));
if (!prefix.empty()) if (!prefix.empty())
os << "prefix " << '"' << prefix << '"' << "\n"; os << "prefix " << '"' << prefix << '"' << "\n";
string symbol = p.verbatim_item(); os << "symbol " << '"'
pair<bool, string> sym = convert_latexed_command_inset_arg(symbol); << convert_command_inset_arg(p.verbatim_item());
bool literal = !sym.first;
string description = p.verbatim_item();
pair<bool, string> desc = convert_latexed_command_inset_arg(description);
literal |= !desc.first;
if (literal) {
symbol = subst(symbol, "\n", " ");
description = subst(description, "\n", " ");
} else {
symbol = sym.second;
description = desc.second;
}
string lit = literal ? "\"true\"" : "\"false\"";
os << "symbol " << '"' << symbol;
os << "\"\ndescription \"" os << "\"\ndescription \""
<< description << "\"\n" << convert_command_inset_arg(p.verbatim_item())
<< "literal " << lit << "\n"; << "\"\n"
<< "literal \"true\"\n";
end_inset(os); end_inset(os);
preamble.registerAutomaticallyLoadedPackage("nomencl"); preamble.registerAutomaticallyLoadedPackage("nomencl");
continue; continue;
@ -4279,7 +4185,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
context.check_layout(os); context.check_layout(os);
begin_command_inset(os, "label", "label"); begin_command_inset(os, "label", "label");
os << "name \"" os << "name \""
<< convert_literate_command_inset_arg(p.verbatim_item()) << convert_command_inset_arg(p.verbatim_item())
<< "\"\n"; << "\"\n";
end_inset(os); end_inset(os);
continue; continue;
@ -4342,7 +4248,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
// via \settowidth{\nomlabelwidth}{***} cannot be supported // via \settowidth{\nomlabelwidth}{***} cannot be supported
// because the user could have set anything, not only the width // because the user could have set anything, not only the width
// of the longest label (which would be width_type = "auto") // of the longest label (which would be width_type = "auto")
string label = convert_literate_command_inset_arg(p.getArg('{', '}')); string label = convert_command_inset_arg(p.getArg('{', '}'));
if (label.empty() && width_type.empty()) if (label.empty() && width_type.empty())
width_type = "none"; width_type = "none";
os << "set_width \"" << width_type << "\"\n"; os << "set_width \"" << width_type << "\"\n";