From 821fc8dfb8ac68aa1de8f82560c697ed0888f6fe Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Mon, 18 Apr 2005 17:43:11 +0000 Subject: [PATCH] Introduce and use latex_path(). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9824 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 5 +++ src/buffer.C | 6 +-- src/insets/ChangeLog | 16 ++++++++ src/insets/ExternalSupport.C | 74 +++++++++++++++++++++++------------- src/insets/ExternalSupport.h | 3 +- src/insets/insetbibtex.C | 7 ++-- src/insets/insetexternal.C | 2 +- src/insets/insetgraphics.C | 4 +- src/insets/insetinclude.C | 11 +++++- src/support/ChangeLog | 11 ++++++ src/support/filetools.C | 14 ++++++- src/support/filetools.h | 13 +++++++ src/support/os_win32.C | 7 +--- 13 files changed, 125 insertions(+), 48 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index eab081db17..a0bfb047a5 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2005-04-17 Angus Leeming + + * buffer.C (makeLaTeXFile): replace code to manipulate a path + containing space and '~' characters with a call to latex_path(). + 2005-04-17 Angus Leeming * converter.C (convert): protect all args of convertDefault.sh diff --git a/src/buffer.C b/src/buffer.C index f33c70bbf6..8f698f5196 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -98,6 +98,7 @@ using lyx::support::destroyDir; using lyx::support::getFormatFromContents; using lyx::support::IsDirWriteable; using lyx::support::LibFileSearch; +using lyx::support::latex_path; using lyx::support::ltrim; using lyx::support::MakeAbsPath; using lyx::support::MakeDisplayPath; @@ -870,10 +871,7 @@ void Buffer::makeLaTeXFile(ostream & os, texrow().newline(); } if (!original_path.empty()) { - string inputpath = os::external_path(original_path); - subst(inputpath, "~", "\\string~"); - if (inputpath.find(' ') != string::npos) - inputpath = '"' + inputpath + '"'; + string const inputpath = latex_path(original_path); os << "\\makeatletter\n" << "\\def\\input@path{{" << inputpath << "/}}\n" diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 65286e6f27..323bbac386 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,19 @@ +2005-04-18 Angus Leeming + + * ExternalSupport.[Ch] (doSubstitution): passed an extra boolean + 'use_latex_path' argument. In turn, passed to the new subst_path + function when replacing placeholders with paths. + + * insetexternal.C (getScreenLabel): + * ExternalSupport.C (lots of places): change invocation of + doSubstitution in lots of places, specifying the appropriate values + for this 'use_latex_path' boolean. + + * insetbib.C (latex): + * insetgraphics.C (latex): + * insetinclude.C (latex): pass path to latex_path rather than to + os::external_path. + 2005-04-18 Martin Vermeer * insetcharstyle.C (validate): allow nested charstyle insets diff --git a/src/insets/ExternalSupport.C b/src/insets/ExternalSupport.C index 5896bdbd2e..a03cb901e5 100644 --- a/src/insets/ExternalSupport.C +++ b/src/insets/ExternalSupport.C @@ -28,6 +28,7 @@ #include "support/lstrings.h" #include "support/lyxalgo.h" #include "support/lyxlib.h" +#include "support/os.h" #include "support/package.h" #include "support/path.h" @@ -60,9 +61,27 @@ void editExternal(InsetExternalParams const & params, Buffer const & buffer) } +namespace { + +string const subst_path(string const & input, + string const & placeholder, + string const & path, + bool use_latex_path) +{ + if (input.find(placeholder) == string::npos) + return input; + string const path2 = use_latex_path ? + support::latex_path(path) : support::os::external_path(path); + return support::subst(input, placeholder, path2); +} + +} // namespace anon + + string const doSubstitution(InsetExternalParams const & params, Buffer const & buffer, string const & s, - bool external_in_tmpdir, + bool use_latex_path, + bool external_in_tmpdir, Substitute what) { Buffer const * m_buffer = buffer.getMasterBuffer(); @@ -92,35 +111,35 @@ string const doSubstitution(InsetExternalParams const & params, if (relToParentPath == "./") relToParentPath.clear(); - result = support::subst(result, "$$FPath", filepath); - result = support::subst(result, "$$AbsPath", abspath); - result = support::subst(result, "$$RelPathMaster", - relToMasterPath); - result = support::subst(result, "$$RelPathParent", - relToParentPath); + result = subst_path(result, "$$FPath", filepath, use_latex_path); + result = subst_path(result, "$$AbsPath", abspath, use_latex_path); + result = subst_path(result, "$$RelPathMaster", + relToMasterPath, use_latex_path); + result = subst_path(result, "$$RelPathParent", + relToParentPath, use_latex_path); if (support::AbsolutePath(filename)) { - result = support::subst(result, "$$AbsOrRelPathMaster", - abspath); - result = support::subst(result, "$$AbsOrRelPathParent", - abspath); + result = subst_path(result, "$$AbsOrRelPathMaster", + abspath, use_latex_path); + result = subst_path(result, "$$AbsOrRelPathParent", + abspath, use_latex_path); } else { - result = support::subst(result, "$$AbsOrRelPathMaster", - relToMasterPath); - result = support::subst(result, "$$AbsOrRelPathParent", - relToParentPath); + result = subst_path(result, "$$AbsOrRelPathMaster", + relToMasterPath, use_latex_path); + result = subst_path(result, "$$AbsOrRelPathParent", + relToParentPath, use_latex_path); } } if (what == PATHS) return result; - result = support::subst(result, "$$FName", filename); - result = support::subst(result, "$$Basename", basename); - result = support::subst(result, "$$Extension", - '.' + support::GetExtension(filename)); - result = support::subst(result, "$$Tempname", params.tempname()); - result = support::subst(result, "$$Sysdir", - support::package().system_support()); + result = subst_path(result, "$$FName", filename, use_latex_path); + result = subst_path(result, "$$Basename", basename, use_latex_path); + result = subst_path(result, "$$Extension", + '.' + support::GetExtension(filename), use_latex_path); + result = subst_path(result, "$$Tempname", params.tempname(), use_latex_path); + result = subst_path(result, "$$Sysdir", + support::package().system_support(), use_latex_path); // Handle the $$Contents(filename) syntax if (support::contains(result, "$$Contents(\"")) { @@ -231,7 +250,7 @@ void updateExternal(InsetExternalParams const & params, // the generated file (always in the temp dir) string const to_file = doSubstitution(params, buffer, outputFormat.updateResult, - true); + false, true); string const abs_to_file = support::MakeAbsPath(to_file, m_buffer->temppath()); @@ -246,7 +265,7 @@ void updateExternal(InsetExternalParams const & params, for (; fit != fend; ++fit) { string const source = support::MakeAbsPath( doSubstitution(params, buffer, *fit, - true), + false, true), m_buffer->temppath()); // The path of the referenced file is never the // temp path, but the filename may be the mangled @@ -254,10 +273,10 @@ void updateExternal(InsetExternalParams const & params, // paths and names separately. string file = support::subst(*fit, "$$FName", "$$FPath$$Basename$$Extension"); - file = doSubstitution(params, buffer, file, false, + file = doSubstitution(params, buffer, file, false, false, PATHS); file = doSubstitution(params, buffer, file, - external_in_tmpdir, + false, external_in_tmpdir, ALL_BUT_PATHS); // if file is a relative name, it is interpreted // relative to the master document. @@ -309,8 +328,9 @@ int writeExternal(InsetExternalParams const & params, updateExternal(params, format, buffer, exportdata, external_in_tmpdir); + bool const use_latex_path = format == "LaTeX"; string str = doSubstitution(params, buffer, cit->second.product, - external_in_tmpdir); + use_latex_path, external_in_tmpdir); str = substituteCommands(params, str, format); str = substituteOptions(params, str, format); os << str; diff --git a/src/insets/ExternalSupport.h b/src/insets/ExternalSupport.h index aa6c9894be..9967c5dbad 100644 --- a/src/insets/ExternalSupport.h +++ b/src/insets/ExternalSupport.h @@ -50,7 +50,8 @@ enum Substitute { std::string const doSubstitution(InsetExternalParams const & params, Buffer const & buffer, std::string const & s, - bool external_in_tmpdir = false, + bool use_latex_path, + bool external_in_tmpdir = false, Substitute what = ALL); diff --git a/src/insets/insetbibtex.C b/src/insets/insetbibtex.C index 73ca790e50..7cb57e6686 100644 --- a/src/insets/insetbibtex.C +++ b/src/insets/insetbibtex.C @@ -35,6 +35,7 @@ using lyx::support::ChangeExtension; using lyx::support::contains; using lyx::support::findtexfile; using lyx::support::IsFileReadable; +using lyx::support::latex_path; using lyx::support::ltrim; using lyx::support::MakeAbsPath; using lyx::support::MakeRelPath; @@ -132,8 +133,7 @@ int InsetBibtex::latex(Buffer const & buffer, ostream & os, // have a comma-separated list of bibliographies string db_out; while (!adb.empty()) { - db_out += os::external_path(normalize_name(buffer, runparams, - adb, ".bib")); + db_out += latex_path(normalize_name(buffer, runparams, adb, ".bib")); db_out += ','; db_in = split(db_in, adb,','); } @@ -154,8 +154,7 @@ int InsetBibtex::latex(Buffer const & buffer, ostream & os, if (!style.empty()) { os << "\\bibliographystyle{" - << os::external_path(normalize_name(buffer, runparams, - style, ".bst")) + << latex_path(normalize_name(buffer, runparams, style, ".bst")) << "}\n"; i += 1; } diff --git a/src/insets/insetexternal.C b/src/insets/insetexternal.C index e985ae175e..24885b5178 100644 --- a/src/insets/insetexternal.C +++ b/src/insets/insetexternal.C @@ -562,7 +562,7 @@ string const getScreenLabel(InsetExternalParams const & params, if (!ptr) return support::bformat(_("External template %1$s is not installed"), params.templatename()); - return external::doSubstitution(params, buffer, ptr->guiName); + return external::doSubstitution(params, buffer, ptr->guiName, false); } void add_preview_and_start_loading(RenderMonitoredPreview &, diff --git a/src/insets/insetgraphics.C b/src/insets/insetgraphics.C index 10040ab8a4..f203b75cf3 100644 --- a/src/insets/insetgraphics.C +++ b/src/insets/insetgraphics.C @@ -98,6 +98,7 @@ using lyx::support::FileName; using lyx::support::float_equal; using lyx::support::GetExtension; using lyx::support::IsFileReadable; +using lyx::support::latex_path; using lyx::support::OnlyFilename; using lyx::support::rtrim; using lyx::support::subst; @@ -738,12 +739,11 @@ int InsetGraphics::latex(Buffer const & buf, ostream & os, << "\tBefore = " << before << "\n\tafter = " << after << endl; - string latex_str = before + '{'; // Convert the file if necessary. // Remove the extension so LaTeX will use whatever is appropriate // (when there are several versions in different formats) - latex_str += prepareFile(buf, runparams); + latex_str += latex_path(prepareFile(buf, runparams)); latex_str += '}' + after; os << latex_str; diff --git a/src/insets/insetinclude.C b/src/insets/insetinclude.C index 14f998ad62..ca4a6af20b 100644 --- a/src/insets/insetinclude.C +++ b/src/insets/insetinclude.C @@ -62,6 +62,7 @@ using lyx::support::FileName; using lyx::support::GetFileContents; using lyx::support::IsFileReadable; using lyx::support::IsLyXFilename; +using lyx::support::latex_path; using lyx::support::MakeAbsPath; using lyx::support::MakeDisplayPath; using lyx::support::MakeRelPath; @@ -385,6 +386,7 @@ int InsetInclude::latex(Buffer const & buffer, ostream & os, } if (isVerbatim(params_)) { + incfile = latex_path(incfile); os << '\\' << params_.getCmdName() << '{' << incfile << '}'; } else if (type(params_) == INPUT) { runparams.exportdata->addExternalFile("latex", writefile, @@ -392,10 +394,13 @@ int InsetInclude::latex(Buffer const & buffer, ostream & os, // \input wants file with extension (default is .tex) if (!IsLyXFilename(included_file)) { + incfile = latex_path(incfile); os << '\\' << params_.getCmdName() << '{' << incfile << '}'; } else { + incfile = ChangeExtension(incfile, ".tex"); + incfile = latex_path(incfile); os << '\\' << params_.getCmdName() << '{' - << ChangeExtension(incfile, ".tex") + << incfile << '}'; } } else { @@ -404,8 +409,10 @@ int InsetInclude::latex(Buffer const & buffer, ostream & os, // \include don't want extension and demands that the // file really have .tex + incfile = ChangeExtension(incfile, string()); + incfile = latex_path(incfile); os << '\\' << params_.getCmdName() << '{' - << ChangeExtension(incfile, string()) + << incfile << '}'; } diff --git a/src/support/ChangeLog b/src/support/ChangeLog index a4a27f6dc1..07f36f8039 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,14 @@ + 2005-04-17 Angus Leeming + + * filetools.C (MakeDisplayPath): invoke os::external_path before + returning path. + + * os_win32.C (external_path): convert '/' chars to '\'. + + * filetools.[Ch] (latex_path): new function which modifies + an input path containing space and '~' characters into something that + LaTeX can understand. + 2005-04-17 Angus Leeming * forkedcall.C (generateChild): do not strip quotes from args on diff --git a/src/support/filetools.C b/src/support/filetools.C index 4671f28960..ab7a8d34e1 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -83,6 +83,16 @@ bool IsSGMLFilename(string const & filename) } +string const latex_path(string const & original_path) +{ + string path = subst(original_path, "\\", "/"); + path = subst(path, "~", "\\string~"); + if (path.find(' ') != string::npos) + path = '"' + path + '"'; + return path; +} + + // Substitutes spaces with underscores in filename (and path) string const MakeLatexName(string const & file) { @@ -994,7 +1004,7 @@ string const MakeDisplayPath(string const & path, unsigned int threshold) str = subst(str, home, "~"); if (str.length() <= threshold) - return str; + return os::external_path(str); string const prefix = ".../"; string temp; @@ -1015,7 +1025,7 @@ string const MakeDisplayPath(string const & path, unsigned int threshold) str = head + "..." + tail; } - return prefix + str; + return os::external_path(prefix + str); } diff --git a/src/support/filetools.h b/src/support/filetools.h index bdba4bca59..092aa70a45 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -104,6 +104,19 @@ i18nLibFileSearch(std::string const & dir, std::string const & name, */ std::string const LibScriptSearch(std::string const & command); +/** @param path a file path in internal_path format. Ie, directories + * are indicated by '/', not by '\'. + * + * Manipulates @c path into a form suitable for inclusion in a LaTeX + * document. + * If @c path contains LaTeX special characters, these are escaped. + * Eg, '~' -> '\string~' + * If @c path contains spaces, then the returned path is enclosed in + * "-quotes. This last fix will lead to successful compiliation of the + * LaTeX file only if a sufficiently modern LaTeX compiler is used. + */ +std::string const latex_path(std::string const & path); + /// Substitutes active latex characters with underscores in filename std::string const MakeLatexName(std::string const & file); diff --git a/src/support/os_win32.C b/src/support/os_win32.C index 1559d61e36..711eeb3b94 100644 --- a/src/support/os_win32.C +++ b/src/support/os_win32.C @@ -140,10 +140,7 @@ string::size_type common_path(string const & p1, string const & p2) string external_path(string const & p) { - string dos_path = p; - - //No backslashes in LaTeX files - dos_path = subst(dos_path,'\\','/'); + string const dos_path = subst(p, "/", "\\"); lyxerr[Debug::LATEX] << " [" @@ -159,7 +156,7 @@ string external_path(string const & p) // the Win32/DOS pathnames into Cygwin pathnames. string internal_path(string const & p) { - return subst(p,"\\","/"); + return subst(p, "\\", "/"); } -- 2.39.2