From 18d18f1b8c1506c4d8c8528ea53545e2c15948dc Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Sun, 30 Oct 2011 12:47:45 +0000 Subject: [PATCH] First step towards proper handling of partially supported packages: Convert the preamble code into a class. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@40090 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/tex2lyx/Makefile.am | 3 +- src/tex2lyx/{preamble.cpp => Preamble.cpp} | 214 +++++++++++---------- src/tex2lyx/Preamble.h | 154 +++++++++++++++ src/tex2lyx/tex2lyx.cpp | 9 +- src/tex2lyx/tex2lyx.h | 9 - src/tex2lyx/text.cpp | 10 +- 6 files changed, 282 insertions(+), 117 deletions(-) rename src/tex2lyx/{preamble.cpp => Preamble.cpp} (91%) create mode 100644 src/tex2lyx/Preamble.h diff --git a/src/tex2lyx/Makefile.am b/src/tex2lyx/Makefile.am index fc39503b90..531c92a15b 100644 --- a/src/tex2lyx/Makefile.am +++ b/src/tex2lyx/Makefile.am @@ -57,7 +57,8 @@ tex2lyx_SOURCES = \ math.cpp \ Parser.cpp \ Parser.h \ - preamble.cpp \ + Preamble.cpp \ + Preamble.h \ table.cpp \ tex2lyx.cpp \ tex2lyx.h \ diff --git a/src/tex2lyx/preamble.cpp b/src/tex2lyx/Preamble.cpp similarity index 91% rename from src/tex2lyx/preamble.cpp rename to src/tex2lyx/Preamble.cpp index 801b1b747d..e381af444d 100644 --- a/src/tex2lyx/preamble.cpp +++ b/src/tex2lyx/Preamble.cpp @@ -1,5 +1,5 @@ /** - * \file preamble.cpp + * \file Preamble.cpp * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * @@ -13,6 +13,7 @@ #include +#include "Preamble.h" #include "tex2lyx.h" #include "LayoutFile.h" @@ -29,10 +30,6 @@ #include #include -#include -#include -#include -#include using namespace std; using namespace lyx::support; @@ -43,19 +40,9 @@ namespace lyx { // special columntypes extern map special_columns; -map > used_packages; const char * const modules_placeholder = "\001modules\001"; -// needed to handle encodings with babel -bool one_language = true; -string h_inputencoding = "auto"; - -// necessary to set the separation when \setlength is parsed -string h_paragraph_separation = "indent"; - -// necessary to avoid that our preamble stuff is added at each tex2lyx run -// which would pollute the preamble when doing roundtrips -bool ifundefined_color_set = false; +Preamble preamble; namespace { @@ -180,74 +167,6 @@ const char * const known_basic_color_codes[] = {"#0000ff", "#000000", "#00ffff", const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists", 0}; -// default settings -ostringstream h_preamble; -string h_backgroundcolor; -string h_boxbgcolor; -string h_cite_engine = "basic"; -string h_defskip = "medskip"; -string h_float_placement; -string h_fontcolor; -string h_fontencoding = "default"; -string h_font_roman = "default"; -string h_font_sans = "default"; -string h_font_typewriter = "default"; -string h_font_default_family = "default"; -string h_font_sc = "false"; -string h_font_osf = "false"; -string h_font_sf_scale = "100"; -string h_font_tt_scale = "100"; -string h_graphics = "default"; -string h_html_be_strict = "false"; -string h_html_css_as_file = "0"; -string h_html_math_output = "0"; -string h_language = "english"; -string h_language_package = "none"; -string h_listings_params; -string h_margins; -string h_notefontcolor; -string h_options; -string h_output_changes = "false"; -string h_papercolumns = "1"; -string h_paperfontsize = "default"; -string h_paperorientation = "portrait"; -string h_paperpagestyle = "default"; -string h_papersides; -string h_papersize = "default"; -string h_paragraph_indentation = "default"; -string h_pdf_title; -string h_pdf_author; -string h_pdf_subject; -string h_pdf_keywords; -string h_pdf_bookmarks = "1"; -string h_pdf_bookmarksnumbered = "0"; -string h_pdf_bookmarksopen = "0"; -string h_pdf_bookmarksopenlevel = "1"; -string h_pdf_breaklinks = "0"; -string h_pdf_pdfborder = "0"; -string h_pdf_colorlinks = "0"; -string h_pdf_backref = "section"; -string h_pdf_pdfusetitle = "1"; -string h_pdf_pagemode; -string h_pdf_quoted_options; -string h_quotes_language = "english"; -string h_secnumdepth = "3"; -string h_spacing = "single"; -string h_suppress_date = "false"; -string h_textclass = "article"; -string h_tocdepth = "3"; -string h_tracking_changes = "false"; -string h_use_bibtopic = "false"; -string h_use_geometry = "false"; -string h_use_amsmath = "1"; -string h_use_default_options = "false"; -string h_use_esint = "1"; -string h_use_hyperref = "0"; -string h_use_mhchem = "0"; -string h_use_mathdots = "0"; -string h_use_refstyle = "0"; -string h_use_undertilde = "0"; - // returns true if at least one of the options in what has been found bool handle_opt(vector & opts, char const * const * what, string & target) @@ -345,12 +264,37 @@ string process_keyval_opt(vector & options, string name) return ""; } +} // anonymous namespace -/*! - * Add package \p name with options \p options to used_packages. - * Remove options from \p options that we don't want to output. - */ -void add_package(string const & name, vector & options) + +bool Preamble::indentParagraphs() const +{ + return h_paragraph_separation == "indent"; +} + + +bool Preamble::isPackageUsed(string const & package) const +{ + return used_packages.find(package) != used_packages.end(); +} + + +vector Preamble::getPackageOptions(string const & package) const +{ + map >::const_iterator it = used_packages.find(package); + if (it != used_packages.end()) + return it->second; + return vector(); +} + + +string Preamble::addModules(string const & lyxpreamble, string const & modules) +{ + return subst(lyxpreamble, modules_placeholder, modules); +} + + +void Preamble::add_package(string const & name, vector & options) { // every package inherits the global options if (used_packages.find(name) == used_packages.end()) @@ -369,6 +313,8 @@ void add_package(string const & name, vector & options) } +namespace { + // Given is a string like "scaled=0.9", return 0.9 * 100 string const scale_as_percentage(string const & scale) { @@ -393,8 +339,82 @@ string remove_braces(string const & value) return value; } +} // anonymous namespace -void handle_hyperref(vector & options) + +Preamble::Preamble() : one_language(true), ifundefined_color_set(false) +{ + //h_backgroundcolor; + //h_boxbgcolor; + h_cite_engine = "basic"; + h_defskip = "medskip"; + //h_float_placement; + //h_fontcolor; + h_fontencoding = "default"; + h_font_roman = "default"; + h_font_sans = "default"; + h_font_typewriter = "default"; + h_font_default_family = "default"; + h_font_sc = "false"; + h_font_osf = "false"; + h_font_sf_scale = "100"; + h_font_tt_scale = "100"; + h_graphics = "default"; + h_html_be_strict = "false"; + h_html_css_as_file = "0"; + h_html_math_output = "0"; + h_inputencoding = "auto"; + h_language = "english"; + h_language_package = "none"; + //h_listings_params; + //h_margins; + //h_notefontcolor; + //h_options; + h_output_changes = "false"; + h_papercolumns = "1"; + h_paperfontsize = "default"; + h_paperorientation = "portrait"; + h_paperpagestyle = "default"; + //h_papersides; + h_papersize = "default"; + h_paragraph_indentation = "default"; + h_paragraph_separation = "indent"; + //h_pdf_title; + //h_pdf_author; + //h_pdf_subject; + //h_pdf_keywords; + h_pdf_bookmarks = "1"; + h_pdf_bookmarksnumbered = "0"; + h_pdf_bookmarksopen = "0"; + h_pdf_bookmarksopenlevel = "1"; + h_pdf_breaklinks = "0"; + h_pdf_pdfborder = "0"; + h_pdf_colorlinks = "0"; + h_pdf_backref = "section"; + h_pdf_pdfusetitle = "1"; + //h_pdf_pagemode; + //h_pdf_quoted_options; + h_quotes_language = "english"; + h_secnumdepth = "3"; + h_spacing = "single"; + h_suppress_date = "false"; + h_textclass = "article"; + h_tocdepth = "3"; + h_tracking_changes = "false"; + h_use_bibtopic = "false"; + h_use_geometry = "false"; + h_use_amsmath = "1"; + h_use_default_options = "false"; + h_use_esint = "1"; + h_use_hyperref = "0"; + h_use_mhchem = "0"; + h_use_mathdots = "0"; + h_use_refstyle = "0"; + h_use_undertilde = "0"; +} + + +void Preamble::handle_hyperref(vector & options) { // FIXME swallow inputencoding changes that might surround the // hyperref setup if it was written by LyX @@ -478,8 +498,8 @@ void handle_hyperref(vector & options) } -void handle_package(Parser &p, string const & name, string const & opts, - bool in_lyx_preamble) +void Preamble::handle_package(Parser &p, string const & name, + string const & opts, bool in_lyx_preamble) { vector options = split_options(opts); add_package(name, options); @@ -713,7 +733,7 @@ void handle_package(Parser &p, string const & name, string const & opts, } -void handle_if(Parser & p, bool in_lyx_preamble) +void Preamble::handle_if(Parser & p, bool in_lyx_preamble) { while (p.good()) { Token t = p.get_token(); @@ -730,7 +750,7 @@ void handle_if(Parser & p, bool in_lyx_preamble) } -void end_preamble(ostream & os, TextClass const & /*textclass*/) +void Preamble::end_preamble(ostream & os, TeX2LyXDocClass const & /*tc*/) { // translate from babel to LyX names h_language = babel2lyx(h_language); @@ -860,10 +880,8 @@ void end_preamble(ostream & os, TextClass const & /*textclass*/) h_preamble.str(""); } -} // anonymous namespace - -void parse_preamble(Parser & p, ostream & os, +void Preamble::parse(Parser & p, ostream & os, string const & forceclass, TeX2LyXDocClass & tc) { // initialize fixed types @@ -1344,7 +1362,6 @@ void parse_preamble(Parser & p, ostream & os, } -/// translates a babel language name to a LyX language name string babel2lyx(string const & language) { char const * const * where = is_known(language, known_languages); @@ -1354,7 +1371,6 @@ string babel2lyx(string const & language) } -/// translates a color name to a LyX color code string color2code(string const & name) { char const * const * where = is_known(name, known_basic_colors); diff --git a/src/tex2lyx/Preamble.h b/src/tex2lyx/Preamble.h new file mode 100644 index 0000000000..9aad4ecfc8 --- /dev/null +++ b/src/tex2lyx/Preamble.h @@ -0,0 +1,154 @@ +/** + * \file Preamble.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author André Pönitz + * \author Uwe Stöhr + * + * Full author contact details are available in file CREDITS. + */ + +// {[( + +#include + +#include +#include +#include +#include +#include + + +namespace lyx { + +class Parser; +class TeX2LyXDocClass; + +class Preamble +{ +public: + Preamble(); + + /// + std::string inputencoding() const { return h_inputencoding; } + /// + bool indentParagraphs() const; + /// + bool isPackageUsed(std::string const & package) const; + /// + std::vector + getPackageOptions(std::string const & package) const; + /// + std::string addModules(std::string const & lyxpreamble, + std::string const & modules); + + /// + void parse(Parser & p, std::ostream & os, + std::string const & forceclass, TeX2LyXDocClass & tc); + +private: + /// + std::map > used_packages; + + /// needed to handle encodings with babel + bool one_language; + + /// necessary to avoid that our preamble stuff is added at each + /// tex2lyx run which would pollute the preamble when doing roundtrips + bool ifundefined_color_set; + + std::ostringstream h_preamble; + std::string h_backgroundcolor; + std::string h_boxbgcolor; + std::string h_cite_engine; + std::string h_defskip; + std::string h_float_placement; + std::string h_fontcolor; + std::string h_fontencoding; + std::string h_font_roman; + std::string h_font_sans; + std::string h_font_typewriter; + std::string h_font_default_family; + std::string h_font_sc; + std::string h_font_osf; + std::string h_font_sf_scale; + std::string h_font_tt_scale; + std::string h_graphics; + std::string h_html_be_strict; + std::string h_html_css_as_file; + std::string h_html_math_output; + std::string h_inputencoding; + std::string h_language; + std::string h_language_package; + std::string h_listings_params; + std::string h_margins; + std::string h_notefontcolor; + std::string h_options; + std::string h_output_changes; + std::string h_papercolumns; + std::string h_paperfontsize; + std::string h_paperorientation; + std::string h_paperpagestyle; + std::string h_papersides; + std::string h_papersize; + std::string h_paragraph_indentation; + /// necessary to set the separation when \setlength is parsed + std::string h_paragraph_separation; + std::string h_pdf_title; + std::string h_pdf_author; + std::string h_pdf_subject; + std::string h_pdf_keywords; + std::string h_pdf_bookmarks; + std::string h_pdf_bookmarksnumbered; + std::string h_pdf_bookmarksopen; + std::string h_pdf_bookmarksopenlevel; + std::string h_pdf_breaklinks; + std::string h_pdf_pdfborder; + std::string h_pdf_colorlinks; + std::string h_pdf_backref; + std::string h_pdf_pdfusetitle; + std::string h_pdf_pagemode; + std::string h_pdf_quoted_options; + std::string h_quotes_language; + std::string h_secnumdepth; + std::string h_spacing; + std::string h_suppress_date; + std::string h_textclass; + std::string h_tocdepth; + std::string h_tracking_changes; + std::string h_use_bibtopic; + std::string h_use_geometry; + std::string h_use_amsmath; + std::string h_use_default_options; + std::string h_use_esint; + std::string h_use_hyperref; + std::string h_use_mhchem; + std::string h_use_mathdots; + std::string h_use_refstyle; + std::string h_use_undertilde; + + /*! + * Add package \p name with options \p options to used_packages. + * Remove options from \p options that we don't want to output. + */ + void add_package(std::string const & name, + std::vector & options); + /// + void handle_hyperref(std::vector & options); + /// + void handle_package(Parser &p, std::string const & name, + std::string const & opts, bool in_lyx_preamble); + /// + void handle_if(Parser & p, bool in_lyx_preamble); + /// + void end_preamble(std::ostream & os, TeX2LyXDocClass const & tc); +}; + + +extern Preamble preamble; + +// }]) + + +} // namespace lyx diff --git a/src/tex2lyx/tex2lyx.cpp b/src/tex2lyx/tex2lyx.cpp index 8e3809cadc..36856c0f96 100644 --- a/src/tex2lyx/tex2lyx.cpp +++ b/src/tex2lyx/tex2lyx.cpp @@ -21,6 +21,7 @@ #include "LayoutFile.h" #include "LayoutModuleList.h" #include "ModuleList.h" +#include "Preamble.h" #include "TextClass.h" #include "support/convert.h" @@ -646,10 +647,10 @@ void tex2lyx(idocstream & is, ostream & os, string encoding) // since latin1 does not cause an iconv error if the actual encoding // is different (bug 7509). if (encoding.empty()) { - if (h_inputencoding == "auto") + if (preamble.inputencoding() == "auto") encoding = "latin1"; else - encoding = h_inputencoding; + encoding = preamble.inputencoding(); } Parser p(is); @@ -657,7 +658,7 @@ void tex2lyx(idocstream & is, ostream & os, string encoding) //p.dump(); ostringstream ps; - parse_preamble(p, ps, documentclass, textclass); + preamble.parse(p, ps, documentclass, textclass); active_environments.push_back("document"); Context context(true, textclass); @@ -680,7 +681,7 @@ void tex2lyx(idocstream & is, ostream & os, string encoding) ms << *it << '\n'; ms << "\\end_modules\n"; } - os << subst(ps.str(), modules_placeholder, ms.str()); + os << preamble.addModules(ps.str(), ms.str()); ss.seekg(0); os << ss.str(); diff --git a/src/tex2lyx/tex2lyx.h b/src/tex2lyx/tex2lyx.h index 7ccc303a01..9bb47b56c3 100644 --- a/src/tex2lyx/tex2lyx.h +++ b/src/tex2lyx/tex2lyx.h @@ -43,20 +43,11 @@ public: void setName(std::string const & name) { name_ = name; } }; -/// in preamble.cpp -void parse_preamble(Parser & p, std::ostream & os, - std::string const & forceclass, TeX2LyXDocClass & tc); /// Translate babel language name to LyX language name extern std::string babel2lyx(std::string const & language); /// translate color name to LyX color code extern std::string color2code(std::string const & name); -/// used packages with options -extern std::map > used_packages; -extern const char * const modules_placeholder; -extern std::string h_inputencoding; -extern std::string h_paragraph_separation; - /// in text.cpp std::string translate_len(std::string const &); diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index 363816991b..bd8f3e9f67 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -21,6 +21,7 @@ #include "FloatList.h" #include "Layout.h" #include "Length.h" +#include "Preamble.h" #include "support/lassert.h" #include "support/convert.h" @@ -1713,8 +1714,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, // (needed for bibtex inset) string btprint; string bibliographystyle; - bool const use_natbib = used_packages.find("natbib") != used_packages.end(); - bool const use_jurabib = used_packages.find("jurabib") != used_packages.end(); + bool const use_natbib = preamble.isPackageUsed("natbib"); + bool const use_jurabib = preamble.isPackageUsed("jurabib"); string last_env; while (p.good()) { Token const & t = p.get_token(); @@ -2625,7 +2626,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, // about the empty paragraph. context.new_paragraph(os); } - if (h_paragraph_separation == "indent") { + if (preamble.indentParagraphs()) { // we need to unindent, lest the line be too long context.add_par_extra_stuff("\\noindent\n"); } @@ -2752,7 +2753,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, p.get_token(); } char argumentOrder = '\0'; - vector const & options = used_packages["jurabib"]; + vector const options = + preamble.getPackageOptions("jurabib"); if (find(options.begin(), options.end(), "natbiborder") != options.end()) argumentOrder = 'n';