Enable tex2lyx to read the LyX textclass and to use this info a little.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7362 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Angus Leeming 2003-07-26 00:15:38 +00:00
parent c7f1fcdc65
commit 812b659d89
8 changed files with 168 additions and 47 deletions

View File

@ -5,3 +5,11 @@ Makefile.in
*.lo *.lo
libtexparser.la libtexparser.la
tex2lyx tex2lyx
FloatList.C
Floating.C
boost.C
counters.C
lyxlayout.[Ch]
lyxtextclass.[Ch]
lyxlex.C
lyxlex_pimpl.C

View File

@ -1,3 +1,34 @@
2003-07-26 Angus Leeming <leeming@lyx.org>
* Spacing.h:
* lyxfont.[Ch]:
* gettext.[Ch]: minimal (near empty) analogues of the real LyX
files; enough to get LyXTextClass and LyXLayout to compile without
needing to store any real information about Spacing or Font.
* Makefile.am: add these files above and also include a minimal
chunk of the main lyx source tree as soft links in order to add in
LyXTextClass and LyXLayout.
* .cvsignore: add all these soft links.
* preamble.C (end_preamble): passed a LyXTextClass arg.
(parse_preamble): returns the document LyXTextClass.
* tabular.C (handle_tabular): passed a LyXTextClass arg. In turn
pass it to parse_text.
* tex2lyx.C: add some global vars, build_lyxdir et al. For now,
give them some horrible hard-coded value.
(main): receive a LyXTextClass var from parse_preamble and pass it
on to parse_text.
* text.C (parse_text): receives a LyXTextClass arg. Use it to
remove the hard-coded handling of floats and instead check the
textclass for its existence. Also handle wide floats naturally.
* tex2lyx.h: associated changes to function declarations.
2003-07-18 Lars Gullik Bjønnes <larsbj@gullik.net> 2003-07-18 Lars Gullik Bjønnes <larsbj@gullik.net>
* texparser.C (catInit): same warning avoidance as in math_parser.C * texparser.C (catInit): same warning avoidance as in math_parser.C

View File

@ -12,7 +12,27 @@ INCLUDES = -I$(srcdir)/../ $(BOOST_INCLUDES)
bin_PROGRAMS = tex2lyx bin_PROGRAMS = tex2lyx
linked_files = \
FloatList.C \
Floating.C \
boost.C \
counters.C \
lyxlayout.h \
lyxlayout.C \
lyxtextclass.C \
lyxtextclass.h \
lyxlex.C \
lyxlex_pimpl.C
# debug.C
tex2lyx_SOURCES = \ tex2lyx_SOURCES = \
$(linked_files) \
Spacing.h \
gettext.C \
gettext.h \
lyxfont.C \
lyxfont.h \
texparser.C \ texparser.C \
texparser.h \ texparser.h \
tex2lyx.C \ tex2lyx.C \
@ -21,3 +41,20 @@ tex2lyx_SOURCES = \
math.C \ math.C \
table.C \ table.C \
text.C text.C
tex2lyx_LDADD = \
../support/libsupport.la \
../../boost/libs/regex/src/libboostregex.la
FloatList.C: link_files
link_files:
for i in $(linked_files); do \
ln -sf "$(top_srcdir)/src/$$i" . ; \
done ; \
for i in $(graphics_linked_files); do \
ln -sf "$(top_srcdir)/src/graphics/$$i" . ; \
done
rm_link_files:
rm -f $(linked_files) $(graphics_linked_files)

View File

@ -8,6 +8,11 @@
#include "tex2lyx.h" #include "tex2lyx.h"
#include "layout.h"
#include "lyxtextclass.h"
#include "lyxlex.h"
#include "support/filetools.h"
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -25,6 +30,8 @@ using std::ostringstream;
using std::string; using std::string;
using std::vector; using std::vector;
using lyx::support::LibFileSearch;
// special columntypes // special columntypes
extern std::map<char, int> special_columns; extern std::map<char, int> special_columns;
@ -128,7 +135,7 @@ void handle_package(string const & name, string const & options)
void end_preamble(ostream & os) void end_preamble(ostream & os, LyXTextClass const & textclass)
{ {
os << "# tex2lyx 0.0.3 created this file\n" os << "# tex2lyx 0.0.3 created this file\n"
<< "\\lyxformat 224\n" << "\\lyxformat 224\n"
@ -165,7 +172,7 @@ void end_preamble(ostream & os)
} // anonymous namespace } // anonymous namespace
void parse_preamble(Parser & p, ostream & os) LyXTextClass const parse_preamble(Parser & p, ostream & os)
{ {
// initialize fixed types // initialize fixed types
special_columns['D'] = 3; special_columns['D'] = 3;
@ -334,17 +341,19 @@ void parse_preamble(Parser & p, ostream & os)
else if (t.cs() == "begin") { else if (t.cs() == "begin") {
string const name = p.getArg('{', '}'); string const name = p.getArg('{', '}');
if (name == "document") { if (name == "document")
end_preamble(os); break;
return;
}
h_preamble << "\\begin{" << name << "}"; h_preamble << "\\begin{" << name << "}";
} }
else if (t.cs().size()) else if (t.cs().size())
h_preamble << '\\' << t.cs() << ' '; h_preamble << '\\' << t.cs() << ' ';
} }
LyXTextClass textclass;
textclass.Read(LibFileSearch("layouts", h_textclass, "layout"));
end_preamble(os, textclass);
return textclass;
} }
// }]) // }])

View File

@ -282,7 +282,8 @@ void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
} }
void handle_tabular(Parser & p, ostream & os) void handle_tabular(Parser & p, ostream & os,
LyXTextClass const & textclass)
{ {
string posopts = p.getOpt(); string posopts = p.getOpt();
if (posopts.size()) if (posopts.size())
@ -397,7 +398,7 @@ void handle_tabular(Parser & p, ostream & os)
handle_colalign(p, t); handle_colalign(p, t);
cellinfo[row][col].multi = 1; cellinfo[row][col].multi = 1;
cellinfo[row][col].align = t.front().align; cellinfo[row][col].align = t.front().align;
cellinfo[row][col].content = parse_text(p, FLAG_ITEM, false); cellinfo[row][col].content = parse_text(p, FLAG_ITEM, false, textclass);
cellinfo[row][col].leftline |= t.front().leftline; cellinfo[row][col].leftline |= t.front().leftline;
cellinfo[row][col].rightline |= t.front().rightline; cellinfo[row][col].rightline |= t.front().rightline;
@ -414,7 +415,7 @@ void handle_tabular(Parser & p, ostream & os)
} else { } else {
// FLAG_END is a hack, we need to read all of it // FLAG_END is a hack, we need to read all of it
cellinfo[row][col].content = parse_text(p, FLAG_END, false); cellinfo[row][col].content = parse_text(p, FLAG_END, false, textclass);
} }
} }

View File

@ -6,6 +6,10 @@
#include "tex2lyx.h" #include "tex2lyx.h"
#include "lyx_main.h"
#include "debug.h"
#include "lyxtextclass.h"
#include <cctype> #include <cctype>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@ -26,6 +30,14 @@ using std::stringstream;
using std::string; using std::string;
using std::vector; using std::vector;
// A hack to allow the thing to link in the lyxlayout stuff
string system_lyxdir = "../../../lib";
string build_lyxdir = "../../lib";
string user_lyxdir = ".";
DebugStream lyxerr;
void LyX::emergencyCleanup() {}
void handle_comment(Parser & p) void handle_comment(Parser & p)
{ {
@ -142,9 +154,9 @@ int main(int argc, char * argv[])
//p.dump(); //p.dump();
stringstream ss; stringstream ss;
parse_preamble(p, ss); LyXTextClass textclass = parse_preamble(p, ss);
active_environments.push_back("document"); active_environments.push_back("document");
parse_text(p, ss, FLAG_END, true); parse_text(p, ss, FLAG_END, true, textclass);
ss << "\n\\the_end\n"; ss << "\n\\the_end\n";
ss.seekg(0); ss.seekg(0);

View File

@ -7,19 +7,23 @@
#include <string> #include <string>
#include <vector> #include <vector>
class LyXTextClass;
void parse_preamble(Parser & p, std::ostream & os); LyXTextClass const parse_preamble(Parser & p, std::ostream & os);
void parse_text(Parser & p, std::ostream & os, unsigned flags, bool outer); void parse_text(Parser & p, std::ostream & os, unsigned flags,
bool outer, LyXTextClass const & textclass);
void parse_table(Parser & p, std::ostream & os, unsigned flags); void parse_table(Parser & p, std::ostream & os, unsigned flags);
void parse_math(Parser & p, std::ostream & os, unsigned flags, mode_type mode); void parse_math(Parser & p, std::ostream & os, unsigned flags, mode_type mode);
void handle_tabular(Parser & p, std::ostream & os); void handle_tabular(Parser & p, std::ostream & os,
LyXTextClass const & textclass);
// Helper // Helper
std::string parse_text(Parser & p, unsigned flags, const bool outer); std::string parse_text(Parser & p, unsigned flags, const bool outer,
LyXTextClass const & textclass);
void handle_comment(Parser & p); void handle_comment(Parser & p);
std::string const trim(std::string const & a, char const * p = " \t\n\r"); std::string const trim(std::string const & a, char const * p = " \t\n\r");

View File

@ -7,6 +7,10 @@
#include <config.h> #include <config.h>
#include "tex2lyx.h" #include "tex2lyx.h"
#include "FloatList.h"
#include "lyxtextclass.h"
#include "support/lstrings.h"
#include "support/tostr.h"
#include <iostream> #include <iostream>
#include <map> #include <map>
@ -21,6 +25,8 @@ using std::ostringstream;
using std::string; using std::string;
using std::vector; using std::vector;
using lyx::support::rtrim;
using lyx::support::suffixIs;
namespace { namespace {
@ -132,7 +138,8 @@ void handle_par(ostream & os)
} // anonymous namespace } // anonymous namespace
void parse_text(Parser & p, ostream & os, unsigned flags, bool outer) void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
LyXTextClass const & textclass)
{ {
while (p.good()) { while (p.good()) {
Token const & t = p.get_token(); Token const & t = p.get_token();
@ -215,7 +222,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
else if (t.cat() == catBegin) { else if (t.cat() == catBegin) {
// special handling of size changes // special handling of size changes
bool const is_size = is_known(p.next_token().cs(), known_sizes); bool const is_size = is_known(p.next_token().cs(), known_sizes);
string const s = parse_text(p, FLAG_BRACE_LAST, outer); string const s = parse_text(p, FLAG_BRACE_LAST, outer, textclass);
if (s.empty() && p.next_token().character() == '`') if (s.empty() && p.next_token().character() == '`')
; // ignore it in {}`` ; // ignore it in {}``
else if (is_size || s == "[" || s == "]" || s == "*") else if (is_size || s == "[" || s == "]" || s == "*")
@ -274,22 +281,33 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
parse_math(p, os, FLAG_END, MATH_MODE); parse_math(p, os, FLAG_END, MATH_MODE);
os << "\\end{" << name << "}"; os << "\\end{" << name << "}";
end_inset(os); end_inset(os);
} else if (name == "tabular") { continue;
}
if (name == "tabular") {
begin_inset(os, "Tabular "); begin_inset(os, "Tabular ");
handle_tabular(p, os); handle_tabular(p, os, textclass);
end_inset(os); end_inset(os);
} else if (name == "table" || name == "figure") { continue;
}
bool is_starred = suffixIs(name, '*');
string unstarred_name = rtrim(name, "*");
if (textclass.floats().typeExist(unstarred_name)) {
string opts = p.getOpt(); string opts = p.getOpt();
begin_inset(os, "Float " + name + "\n"); begin_inset(os, "Float " + unstarred_name + "\n");
if (opts.size()) if (opts.size())
os << "placement " << opts << '\n'; os << "placement " << opts << '\n';
os << "wide false\ncollapsed false\n\n" os << "wide " << tostr(is_starred)
<< "\ncollapsed false\n\n"
<< "\\layout Standard\n"; << "\\layout Standard\n";
parse_text(p, os, FLAG_END, outer); parse_text(p, os, FLAG_END, outer,
textclass);
end_inset(os); end_inset(os);
} else if (name == "center") { } else if (name == "center") {
handle_par(os); handle_par(os);
parse_text(p, os, FLAG_END, outer); parse_text(p, os, FLAG_END, outer,
textclass);
} else if (name == "enumerate" || name == "itemize" } else if (name == "enumerate" || name == "itemize"
|| name == "lyxlist") { || name == "lyxlist") {
size_t const n = active_environments.size(); size_t const n = active_environments.size();
@ -301,17 +319,17 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
os << "\n\\layout " << cap(name) << "\n\n"; os << "\n\\layout " << cap(name) << "\n\n";
if (name == "lyxlist") if (name == "lyxlist")
p.verbatim_item(); // swallow next arg p.verbatim_item(); // swallow next arg
parse_text(p, os, FLAG_END, outer); parse_text(p, os, FLAG_END, outer, textclass);
if (deeper) if (deeper)
os << "\n\\end_deeper\n"; os << "\n\\end_deeper\n";
handle_par(os); handle_par(os);
} else if (name == "thebibliography") { } else if (name == "thebibliography") {
p.verbatim_item(); // swallow next arg p.verbatim_item(); // swallow next arg
parse_text(p, os, FLAG_END, outer); parse_text(p, os, FLAG_END, outer, textclass);
os << "\n\\layout Bibliography\n\n"; os << "\n\\layout Bibliography\n\n";
} else { } else {
handle_par(os); handle_par(os);
parse_text(p, os, FLAG_END, outer); parse_text(p, os, FLAG_END, outer, textclass);
} }
} }
@ -334,7 +352,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
string s; string s;
if (p.next_token().character() == '[') { if (p.next_token().character() == '[') {
p.get_token(); // eat '[' p.get_token(); // eat '['
s = parse_text(p, FLAG_BRACK_LAST, outer); s = parse_text(p, FLAG_BRACK_LAST, outer, textclass);
} }
handle_par(os); handle_par(os);
os << s << ' '; os << s << ' ';
@ -367,7 +385,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
os << "collapsed true\n\n\\layout Standard\n\n" << opt; os << "collapsed true\n\n\\layout Standard\n\n" << opt;
end_inset(os); end_inset(os);
} }
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\n\\layout Standard\n\n"; os << "\n\n\\layout Standard\n\n";
} }
@ -386,12 +404,12 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
else if (t.cs() == "footnote") { else if (t.cs() == "footnote") {
begin_inset(os, "Foot\n"); begin_inset(os, "Foot\n");
os << "collapsed true\n\n\\layout Standard\n\n"; os << "collapsed true\n\n\\layout Standard\n\n";
parse_text(p, os, FLAG_ITEM, false); parse_text(p, os, FLAG_ITEM, false, textclass);
end_inset(os); end_inset(os);
} }
else if (t.cs() == "ensuremath") { else if (t.cs() == "ensuremath") {
string s = parse_text(p, FLAG_ITEM, false); string s = parse_text(p, FLAG_ITEM, false, textclass);
if (s == "±" || s == "³" || s == "²" || s == "µ") if (s == "±" || s == "³" || s == "²" || s == "µ")
os << s; os << s;
else else
@ -401,7 +419,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
else if (t.cs() == "marginpar") { else if (t.cs() == "marginpar") {
begin_inset(os, "Marginal\n"); begin_inset(os, "Marginal\n");
os << "collapsed true\n\n\\layout Standard\n\n"; os << "collapsed true\n\n\\layout Standard\n\n";
parse_text(p, os, FLAG_ITEM, false); parse_text(p, os, FLAG_ITEM, false, textclass);
end_inset(os); end_inset(os);
} }
@ -418,49 +436,49 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
else if (t.cs() == "textrm") { else if (t.cs() == "textrm") {
os << "\n\\family roman \n"; os << "\n\\family roman \n";
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\\family default \n"; os << "\n\\family default \n";
} }
else if (t.cs() == "textsf") { else if (t.cs() == "textsf") {
os << "\n\\family sans \n"; os << "\n\\family sans \n";
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\\family default \n"; os << "\n\\family default \n";
} }
else if (t.cs() == "texttt") { else if (t.cs() == "texttt") {
os << "\n\\family typewriter \n"; os << "\n\\family typewriter \n";
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\\family default \n"; os << "\n\\family default \n";
} }
else if (t.cs() == "textit") { else if (t.cs() == "textit") {
os << "\n\\shape italic \n"; os << "\n\\shape italic \n";
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\\shape default \n"; os << "\n\\shape default \n";
} }
else if (t.cs() == "textsc") { else if (t.cs() == "textsc") {
os << "\n\\noun on \n"; os << "\n\\noun on \n";
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\\noun default \n"; os << "\n\\noun default \n";
} }
else if (t.cs() == "textbf") { else if (t.cs() == "textbf") {
os << "\n\\series bold \n"; os << "\n\\series bold \n";
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\\series default \n"; os << "\n\\series default \n";
} }
else if (t.cs() == "underbar") { else if (t.cs() == "underbar") {
os << "\n\\bar under \n"; os << "\n\\bar under \n";
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\\bar default \n"; os << "\n\\bar default \n";
} }
else if (t.cs() == "emph" || t.cs() == "noun") { else if (t.cs() == "emph" || t.cs() == "noun") {
os << "\n\\" << t.cs() << " on \n"; os << "\n\\" << t.cs() << " on \n";
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\\" << t.cs() << " default \n"; os << "\n\\" << t.cs() << " default \n";
} }
@ -562,7 +580,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
|| t.cs() == "^" || t.cs() == "'" || t.cs() == "~") { || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
// we need the trim as the LyX parser chokes on such spaces // we need the trim as the LyX parser chokes on such spaces
os << "\n\\i \\" << t.cs() << "{" os << "\n\\i \\" << t.cs() << "{"
<< trim(parse_text(p, FLAG_ITEM, outer), " ") << "}\n"; << trim(parse_text(p, FLAG_ITEM, outer, textclass), " ") << "}\n";
} }
else if (t.cs() == "ss") else if (t.cs() == "ss")
@ -579,13 +597,13 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
else if (t.cs() == "lyxrightaddress") { else if (t.cs() == "lyxrightaddress") {
os << "\n\\layout Right Address\n"; os << "\n\\layout Right Address\n";
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\\layout Standard\n"; os << "\n\\layout Standard\n";
} }
else if (t.cs() == "lyxaddress") { else if (t.cs() == "lyxaddress") {
os << "\n\\layout Address\n"; os << "\n\\layout Address\n";
parse_text(p, os, FLAG_ITEM, outer); parse_text(p, os, FLAG_ITEM, outer, textclass);
os << "\n\\layout Standard\n"; os << "\n\\layout Standard\n";
} }
@ -625,10 +643,11 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
} }
string parse_text(Parser & p, unsigned flags, const bool outer) string parse_text(Parser & p, unsigned flags, const bool outer,
LyXTextClass const & textclass)
{ {
ostringstream os; ostringstream os;
parse_text(p, os, flags, outer); parse_text(p, os, flags, outer, textclass);
return os.str(); return os.str();
} }