Georg Baum wrote:

> I did, and I could not break it, but I found another reason to use this
> mover stuff: Bug 605 is still not completely fixed. Consider the
> following situation:
> 
> master.lyx includes sub/child.lyx
> sub/child.lyx includes sub/pic.fig (external material, relative file name)
> sub/pic.fig
> 
> You will get the following files for a latex export:
> 
> master.tex
> sub/child.tex
> sub/pic.pstex_t
> sub/pic.eps
> 
> sub/child.tex contains "\input{sub/pic.pstex_t}"
> sub/pic.pstex_t contains "\includegraphics{pic}" -> BANG! It should be
> "\includegraphics{sub/pic}".
> 
> This can be fixed if the external inset runs the conversion in the temp
> dir, too, and a copier is used to copy the resulting .pstex_t file. The
> attached patch builds on top of the mover patch and implements that. I
> did create copy and rename functions with a third argument. This argument
> is the "latex name" of the file, that means it is either absolute or
> relative to the master document.

Here comes an updated patch. It is basically the same plus Changelogs and
documentation. Does anybody have objections?

It is really necessary to document the new external template fetaures in
Customization.lyx now. I started to do this, but it is not finished yet.


Georg
Index: lib/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/ChangeLog,v
retrieving revision 1.644
diff -u -p -r1.644 ChangeLog
--- lib/ChangeLog	28 Oct 2004 14:56:08 -0000	1.644
+++ lib/ChangeLog	29 Oct 2004 15:51:28 -0000
@@ -1,3 +1,10 @@
+2004-10-29  Georg Baum  <[EMAIL PROTECTED]>
+
+	* configure.m4: add copier for pstex_t and pdftex_t files
+	* external_templates: (Template XFig): correct referenced files
+	* scripts/tex_copy.py: new
+	* Makefile.am: add scripts/tex_copy.py
+
 2004-10-28  José Matos  <[EMAIL PROTECTED]>
 
 	* Makefile.am: add entry to layouts/db_stdcounters.inc
Index: lib/Makefile.am
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/Makefile.am,v
retrieving revision 1.64
diff -u -p -r1.64 Makefile.am
--- lib/Makefile.am	28 Oct 2004 14:56:08 -0000	1.64
+++ lib/Makefile.am	29 Oct 2004 15:58:59 -0000
@@ -860,7 +861,8 @@ dist_scripts_SCRIPTS = \
 	scripts/fig2pstex.sh \
 	scripts/listerrors \
 	scripts/legacy_lyxpreview2ppm.py \
-	scripts/lyxpreview2bitmap.py
+	scripts/lyxpreview2bitmap.py \
+	scripts/tex_copy.py
 
 templatesdir = $(pkgdatadir)/templates
 dist_templates_DATA = \
Index: lib/configure.m4
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/configure.m4,v
retrieving revision 1.83
diff -u -p -r1.83 configure.m4
--- lib/configure.m4	26 Oct 2004 18:39:08 -0000	1.83
+++ lib/configure.m4	29 Oct 2004 16:03:56 -0000
@@ -587,8 +587,6 @@ cat >$outfile <<EOF
 \\converter ps         fax        "$fax_command"	""
 \\converter ps         pdf        "$ps_to_pdf_command"	""
 \\converter word       latex      "$word_to_latex_command"	""
-
-\\copier    fig        "sh \$\$s/fig_copy.sh \$\$i \$\$o"
 EOF
 
 ### the graphic converter part with the predefined ones
@@ -637,6 +635,10 @@ EOF
 fi
 
 cat >>$outfile <<EOF
+
+\\copier    fig        "sh \$\$s/fig_copy.sh \$\$i \$\$o"
+\\copier    pstex      "python \$\$s/tex_copy.py \$\$i \$\$o \$\$l"
+\\copier    pdftex     "python \$\$s/tex_copy.py \$\$i \$\$o \$\$l"
 
 $rc_entries
 \\font_encoding "$chk_fontenc"
Index: lib/external_templates
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/external_templates,v
retrieving revision 1.27
diff -u -p -r1.27 external_templates
--- lib/external_templates	1 Jun 2004 13:39:31 -0000	1.27
+++ lib/external_templates	29 Oct 2004 15:51:28 -0000
@@ -121,9 +121,9 @@ Template XFig
 		Requirement "graphicx"
 		# Preamble WarnNotFound
 		# Preamble InputOrWarn
-		ReferencedFile latex "$$AbsPath$$Basename.pstex_t"
-		ReferencedFile latex "$$AbsPath$$Basename.pstex"
-		ReferencedFile dvi   "$$AbsPath$$Basename.pstex"
+		ReferencedFile latex "$$AbsOrRelPathMaster$$Basename.pstex_t"
+		ReferencedFile latex "$$AbsPath$$Basename.eps"
+		ReferencedFile dvi   "$$AbsPath$$Basename.eps"
 	FormatEnd
 	Format PDFLaTeX
 		TransformCommand Rotate RotationLatexCommand
@@ -134,8 +134,8 @@ Template XFig
 		Requirement "graphicx"
 		# Preamble WarnNotFound
 		# Preamble InputOrWarn
-		ReferencedFile latex "$$AbsPath$$Basename.pdftex_t"
-		ReferencedFile latex "$$AbsPath$$Basename.pdftex"
+		ReferencedFile latex "$$AbsOrRelPathMaster$$Basename.pdftex_t"
+		ReferencedFile latex "$$AbsPath$$Basename.pdf"
 	FormatEnd
 	Format Ascii
 		Product "$$Contents(\"$$AbsPath$$Basename.asc\")"
Index: lib/scripts/tex_copy.py
===================================================================
RCS file: lib/scripts/tex_copy.py
diff -N lib/scripts/tex_copy.py
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ lib/scripts/tex_copy.py	29 Oct 2004 15:56:07 -0000
@@ -0,0 +1,69 @@
+#! /usr/bin/env python
+# -*- coding: iso-8859-1 -*-
+
+# file tex_copy.py
+# This file is part of LyX, the document processor.
+# Licence details can be found in the file COPYING.
+
+# author Angus Leeming
+# author Georg Baum
+
+# Full author contact details are available in file CREDITS
+
+# Usage:
+# tex_copy.py from file> <to file> <latex name>
+
+# This script will copy a file <from file> to <to file>.
+# <to file> is no exact copy of <from file>, but any occurence of <basename>
+# where <basename> is <from file> without directory and extension parts is
+# replaced by <latex name> without extension.
+
+
+import os, string, sys
+
+from lyxpreview_tools import error
+
+
+def usage(prog_name):
+    return "Usage: %s <from file> <to file> <latex name>" % prog_name
+
+
+def main(argv):
+    # Parse and manipulate the command line arguments.
+    if len(argv) != 4:
+        error(usage(argv[0]))
+
+    # input file
+    abs_from_file = argv[1]
+    if not os.path.isabs(abs_from_file):
+        error("%s is no absolute file name.\n%s"\
+              % abs_from_file, usage(argv[0]))
+    from_dir, rel_from_file = os.path.split(abs_from_file)
+    from_base, from_ext = os.path.splitext(rel_from_file)
+
+    # output file
+    abs_to_file = argv[2]
+    if not os.path.isabs(abs_to_file):
+        error("%s is no absolute file name.\n%s"\
+              % abs_to_file, usage(argv[0]))
+    to_dir, rel_to_file = os.path.split(abs_to_file)
+    to_base, to_ext = os.path.splitext(rel_to_file)
+
+    # latex file name
+    latex_file = argv[3]
+    latex_base, latex_ext = os.path.splitext(latex_file)
+
+    # Read the input file and write the output file
+    from_file = open(abs_from_file, 'rb')
+    to_file = open(abs_to_file, 'wb')
+    lines = from_file.readlines()
+    for line in lines:
+        to_file.write(line.replace(from_base, latex_base))
+    from_file.close()
+    to_file.close()
+
+    return 0
+
+
+if __name__ == "__main__":
+    main(sys.argv)
Index: src/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/ChangeLog,v
retrieving revision 1.2011
diff -u -p -r1.2011 ChangeLog
--- src/ChangeLog	29 Oct 2004 15:47:52 -0000	1.2011
+++ src/ChangeLog	29 Oct 2004 15:51:33 -0000
@@ -1,3 +1,10 @@
+2004-10-29  Georg Baum  <[EMAIL PROTECTED]>
+
+	* exporter.C (copyFile): use the mover instead of support::copy()
+	* exporter.C (Export): pass format and latex name to copyFile()
+	* exporter.h (addExternalFile): document
+	* mover.[Ch] (do_copy, do_rename): new methods with 3 arguments
+
 2004-10-29  Georg Baum  <[EMAIL PROTECTED]>
 
 	* buffer.C, lyxlex_pimpl.C:
Index: src/exporter.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/exporter.C,v
retrieving revision 1.54
diff -u -p -r1.54 exporter.C
--- src/exporter.C	5 Oct 2004 10:11:27 -0000	1.54
+++ src/exporter.C	29 Oct 2004 15:51:34 -0000
@@ -25,6 +25,7 @@
 #include "format.h"
 #include "gettext.h"
 #include "lyxrc.h"
+#include "mover.h"
 #include "output_plaintext.h"
 #include "outputparams.h"
 #include "frontends/Alert.h"
@@ -38,6 +39,7 @@ using lyx::support::AddName;
 using lyx::support::bformat;
 using lyx::support::ChangeExtension;
 using lyx::support::contains;
+using lyx::support::getFormatFromContents;
 using lyx::support::MakeAbsPath;
 using lyx::support::MakeDisplayPath;
 using lyx::support::OnlyFilename;
@@ -92,8 +94,9 @@ enum CopyStatus {
  *            overwriting files anymore.
  *  - CANCEL  if the export should be cancelled
  */
-CopyStatus copyFile(string const & sourceFile, string const & destFile,
-		    bool force)
+CopyStatus copyFile(string const & format,
+                    string const & sourceFile, string const & destFile,
+                    string const & latexFile, bool force)
 {
 	CopyStatus ret = force ? FORCE : SUCCESS;
 
@@ -117,7 +120,8 @@ CopyStatus copyFile(string const & sourc
 		}
 	}
 
-	if (!lyx::support::copy(sourceFile, destFile))
+	Mover const & mover = movers(format);
+	if (!mover.copy(sourceFile, destFile, latexFile))
 		Alert::error(_("Couldn't copy file"),
 		             bformat(_("Copying %1$s to %2$s failed."),
 		                     MakeDisplayPath(sourceFile),
@@ -203,15 +210,18 @@ bool Exporter::Export(Buffer * buffer, s
 		string const dest = OnlyPath(result_file);
 		CopyStatus status = SUCCESS;
 		for (vector<ExportedFile>::const_iterator it = files.begin();
-				it != files.end() && status != CANCEL; ++it)
-			status = copyFile(it->sourceName,
+				it != files.end() && status != CANCEL; ++it) {
+			string const fmt = getFormatFromContents(it->sourceName);
+			status = copyFile(fmt, it->sourceName,
 			                  MakeAbsPath(it->exportName, dest),
-			                  status == FORCE);
+			                  it->exportName, status == FORCE);
+		}
 		if (status == CANCEL) {
 			buffer->message(_("Document export cancelled."));
 		} else {
 			// Finally copy the main file
-			status = copyFile(tmp_result_file, result_file,
+			status = copyFile(format, tmp_result_file,
+			                  result_file, result_file,
 			                  status == FORCE);
 			buffer->message(bformat(_("Document exported as %1$s"
 			                          "to file `%2$s'"),
Index: src/exporter.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/exporter.h,v
retrieving revision 1.20
diff -u -p -r1.20 exporter.h
--- src/exporter.h	1 Jun 2004 13:39:33 -0000	1.20
+++ src/exporter.h	29 Oct 2004 15:51:34 -0000
@@ -66,15 +66,24 @@ public:
 	 *  with this method.
 	 *  Then the exporter mechanism copies them to the right place, asks
 	 *  for confirmation before overwriting existing files etc.
+	 * \param format     format that references the given file
+	 * \param sourceName source file name. Needs to be absolute
+	 * \param exportName resulting file name. Can be either absolute
+	 *                   or relative to the exported document.
 	 */
-	void addExternalFile(std::string const &, std::string const &,
-	                     std::string const &);
-	/// add a referenced file for one format.
-	/// The final name is the source file name without path
-	void addExternalFile(std::string const &, std::string const &);
-	/// get referenced files for one format
+	void addExternalFile(std::string const & format,
+	                     std::string const & sourceName,
+	                     std::string const & exportName);
+	/** add a referenced file for one format.
+	 *  The final name is the source file name without path.
+	 * \param format     format that references the given file
+	 * \param sourceName source file name. Needs to be absolute
+	 */
+	void addExternalFile(std::string const & format,
+	                     std::string const & sourceName);
+	/// get referenced files for \p format
 	std::vector<ExportedFile> const
-	externalFiles(std::string const &) const;
+	externalFiles(std::string const & format) const;
 private:
 	typedef std::map<std::string, std::vector<ExportedFile> > FileMap;
 	/** Files that are referenced by the export result in the
Index: src/mover.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mover.C,v
retrieving revision 1.1
diff -u -p -r1.1 mover.C
--- src/mover.C	26 Oct 2004 18:39:10 -0000	1.1
+++ src/mover.C	29 Oct 2004 15:51:34 -0000
@@ -26,38 +26,43 @@ Movers movers;
 Movers system_movers;
 
 
-bool Mover::do_copy(string const & from, string const & to) const
+bool Mover::do_copy(string const & from, string const & to,
+                    string const &) const
 {
 	return support::copy(from, to);
 }
 
 
-bool Mover::do_rename(string const & from, string const & to) const
+bool Mover::do_rename(string const & from, string const & to,
+                      string const &) const
 {
 	return support::rename(from, to);
 }
 
 
-bool SpecialisedMover::do_copy(string const & from, string const & to) const
+bool SpecialisedMover::do_copy(string const & from, string const & to,
+                               string const & latex) const
 {
 	if (command_.empty())
-		return Mover::do_copy(from, to);
+		return Mover::do_copy(from, to, latex);
 
 	string command = support::LibScriptSearch(command_);
 	command = support::subst(command, "$$i", from);
 	command = support::subst(command, "$$o", to);
+	command = support::subst(command, "$$l", latex);
 
 	support::Systemcall one;
 	return one.startscript(support::Systemcall::Wait, command) == 0;
 }
 
 
-bool SpecialisedMover::do_rename(string const & from, string const & to) const
+bool SpecialisedMover::do_rename(string const & from, string const & to,
+                                 string const & latex) const
 {
 	if (command_.empty())
-		return Mover::do_rename(from, to);
+		return Mover::do_rename(from, to, latex);
 
-	if (!do_copy(from, to))
+	if (!do_copy(from, to, latex))
 		return false;
 	return support::unlink(from) == 0;
 }
Index: src/mover.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mover.h,v
retrieving revision 1.1
diff -u -p -r1.1 mover.h
--- src/mover.h	26 Oct 2004 18:39:10 -0000	1.1
+++ src/mover.h	29 Oct 2004 16:39:05 -0000
@@ -25,29 +25,67 @@ public:
 	virtual ~Mover() {}
 
 	/** Copy file @c from to @c to.
+	 *  This version should be used to copy files from the original
+	 *  location to the temporary directory, since @c to and @c latex
+	 *  would be equal in this case.
 	 *  \returns true if successful.
 	 */
 	bool
 	copy(std::string const & from, std::string const & to) const
 	{
-		return do_copy(from, to);
+		return do_copy(from, to, to);
+	}
+
+	/** Copy file @c from to @c to.
+	 *  \see SpecialisedMover::SpecialisedMover() for an explanation of
+	 *  @c latex.
+	 *  This version should be used to copy files from the temporary
+	 *  directory to the export location, since @c to and @c latex may
+	 *  not be equal in this case.
+	 *  \returns true if successful.
+	 */
+	bool
+	copy(std::string const & from, std::string const & to,
+	     std::string const & latex) const
+	{
+		return do_copy(from, to, latex);
 	}
 
 	/** Rename file @c from as @c to.
+	 *  This version should be used to move files from the original
+	 *  location to the temporary directory, since @c to and @c latex
+	 *  would be equal in this case.
 	 *  \returns true if successful.
 	 */
 	bool
 	rename(std::string const & from, std::string const & to) const
 	{
-		return do_rename(from, to);
+		return do_rename(from, to, to);
+	}
+
+	/** Rename file @c from as @c to.
+	 *  \see SpecialisedMover::SpecialisedMover() for an explanation of
+	 *  @c latex.
+	 *  This version should be used to move files from the temporary
+	 *  directory to the export location, since @c to and @c latex may
+	 *  not be equal in this case.
+	 *  \returns true if successful.
+	 */
+	bool
+	rename(std::string const & from, std::string const & to,
+	       std::string const & latex) const
+	{
+		return do_rename(from, to, latex);
 	}
 
 protected:
 	virtual bool
-	do_copy(std::string const & from, std::string const & to) const;
+	do_copy(std::string const & from, std::string const & to,
+	        std::string const &) const;
 
 	virtual bool
-	do_rename(std::string const & from, std::string const & to) const;
+	do_rename(std::string const & from, std::string const & to,
+	          std::string const &) const;
 };
 
 
@@ -66,11 +104,19 @@ struct SpecialisedMover : public Mover
 
 	/** @c command should be of the form
 	 *  <code>
-	 *      sh $$s/copy_fig.sh $$i $$o
+	 *      sh $$s/copy_fig.sh $$i $$o $$l
 	 *  </code>
 	 *  where $$s is a placeholder for the lyx script directory,
 	 *        $$i is a placeholder for the name of the file to be moved,
-	 *        $$o is a placeholder for the name of the file after moving.
+	 *        $$o is a placeholder for the name of the file after moving,
+	 *        $$l is a placeholder for the name of the file after moving,
+	 *        suitable as argument to a latex include command. This is
+	 *        either an absolute filename or relative to the master
+	 *        document.
+	 *        $$o and $$l can only differ if the file is copied from the
+	 *        temporary directory to the export location. If it is copied
+	 *        from the original location to the temporary directory, they
+	 *        are the same, so $$l may be ommitted in this case.
 	 */
 	SpecialisedMover(std::string const & command)
 		: command_(command) {}
@@ -80,10 +126,12 @@ struct SpecialisedMover : public Mover
 
 private:
 	virtual bool
-	do_copy(std::string const & from, std::string const & to) const;
+	do_copy(std::string const & from, std::string const & to,
+	        std::string const & latex) const;
 
 	virtual bool
-	do_rename(std::string const & from, std::string const & to) const;
+	do_rename(std::string const & from, std::string const & to,
+	          std::string const & latex) const;
 
 	std::string command_;
 };
Index: src/insets/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/ChangeLog,v
retrieving revision 1.1062
diff -u -p -r1.1062 ChangeLog
--- src/insets/ChangeLog	29 Oct 2004 15:47:54 -0000	1.1062
+++ src/insets/ChangeLog	29 Oct 2004 16:13:37 -0000
@@ -1,3 +1,7 @@
+2004-10-30  Georg Baum  <[EMAIL PROTECTED]>
+
+	* ExternalSupport.C (updateExternal): convert files in the temp dir
+
 2004-10-29  Georg Baum  <[EMAIL PROTECTED]>
 
 	* ExternalSupport.C: s/getExtFromContents/getFormatFromContents/
Index: src/insets/ExternalSupport.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/ExternalSupport.C,v
retrieving revision 1.11
diff -u -p -r1.11 ExternalSupport.C
--- src/insets/ExternalSupport.C	29 Oct 2004 15:47:54 -0000	1.11
+++ src/insets/ExternalSupport.C	29 Oct 2004 15:51:38 -0000
@@ -196,11 +196,12 @@ void updateExternal(InsetExternalParams 
 	// of include files
 	Buffer const * m_buffer = buffer.getMasterBuffer();
 
-	if (external_in_tmpdir && !abs_from_file.empty()) {
-		// We are running stuff through LaTeX
-		string const temp_file =
-			support::MakeAbsPath(params.filename.mangledFilename(),
-					     m_buffer->temppath());
+	// We copy the source file to the temp dir and do the conversion
+	// there if necessary
+	string const temp_file =
+		support::MakeAbsPath(params.filename.mangledFilename(),
+				     m_buffer->temppath());
+	if (!abs_from_file.empty()) {
 		unsigned long const from_checksum = support::sum(abs_from_file);
 		unsigned long const temp_checksum = support::sum(temp_file);
 
@@ -214,20 +215,17 @@ void updateExternal(InsetExternalParams 
 				return; // FAILURE
 			}
 		}
-
-		abs_from_file = temp_file;
 	}
 
+	// the generated file (always in the temp dir)
 	string const to_file = doSubstitution(params, buffer,
 					      outputFormat.updateResult,
-	                                      external_in_tmpdir);
-
+	                                      true);
 	string const abs_to_file =
-		support::MakeAbsPath(to_file, external_in_tmpdir
-			? m_buffer->temppath()
-			: buffer.filePath());
+		support::MakeAbsPath(to_file, m_buffer->temppath());
 
-	// record the referenced files for the exporter
+	// Record the referenced files for the exporter.
+	// The exporter will copy them to the export dir.
 	typedef Template::Format::FileMap FileMap;
 	FileMap::const_iterator rit  = outputFormat.referencedFiles.begin();
 	FileMap::const_iterator rend = outputFormat.referencedFiles.end();
@@ -235,22 +233,27 @@ void updateExternal(InsetExternalParams 
 		vector<string>::const_iterator fit  = rit->second.begin();
 		vector<string>::const_iterator fend = rit->second.end();
 		for (; fit != fend; ++fit) {
+			string const source = support::MakeAbsPath(
+					doSubstitution(params, buffer, *fit,
+					               true),
+					m_buffer->temppath());
 			string const file = doSubstitution(params, buffer,
 			                                   *fit,
 			                                   external_in_tmpdir);
-			exportdata.addExternalFile(rit->first, file);
+			// if file is a relative name, it is interpreted
+			// relative to the master document.
+			exportdata.addExternalFile(rit->first, source, file);
 		}
 	}
 
 	// Do we need to perform the conversion?
 	// Yes if to_file does not exist or if from_file is newer than to_file
-	if (support::compare_timestamps(abs_from_file, abs_to_file) < 0)
+	if (support::compare_timestamps(temp_file, abs_to_file) < 0)
 		return; // SUCCESS
-
 	string const to_file_base =
 		support::ChangeExtension(to_file, string());
 	/* bool const success = */
-		converters.convert(&buffer, abs_from_file, to_file_base,
+		converters.convert(&buffer, temp_file, to_file_base,
 				   from_format, to_format);
 	// return success
 }

Reply via email to