From e3671722567785da470c34d76a77ba0968e0cef9 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Mon, 1 Nov 2004 08:50:42 +0000 Subject: [PATCH] use the movers also for copying from temp dir -> export dir git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9153 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/ChangeLog | 7 ++++ lib/Makefile.am | 3 +- lib/configure.m4 | 6 ++-- lib/external_templates | 10 +++--- lib/scripts/tex_copy.py | 69 ++++++++++++++++++++++++++++++++++++ src/ChangeLog | 7 ++++ src/exporter.C | 21 +++++++---- src/exporter.h | 23 ++++++++---- src/insets/ChangeLog | 4 +++ src/insets/ExternalSupport.C | 37 ++++++++++--------- src/mover.C | 19 ++++++---- src/mover.h | 64 ++++++++++++++++++++++++++++----- 12 files changed, 216 insertions(+), 54 deletions(-) create mode 100644 lib/scripts/tex_copy.py diff --git a/lib/ChangeLog b/lib/ChangeLog index a933f48ad6..8bd9afe3f2 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,10 @@ +2004-11-01 Georg Baum + + * 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-30 José Matos * db_stdclasses.inc: diff --git a/lib/Makefile.am b/lib/Makefile.am index cc2acbeab7..c0387616bb 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -861,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 = \ diff --git a/lib/configure.m4 b/lib/configure.m4 index 232b829bfb..ad2af1dae8 100644 --- a/lib/configure.m4 +++ b/lib/configure.m4 @@ -587,8 +587,6 @@ cat >$outfile <>$outfile < + +# This script will copy a file to . +# is no exact copy of , but any occurence of +# where is without directory and extension parts is +# replaced by without extension. + + +import os, string, sys + +from lyxpreview_tools import error + + +def usage(prog_name): + return "Usage: %s " % 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) diff --git a/src/ChangeLog b/src/ChangeLog index fa4973e950..543c5d7a85 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2004-11-01 Georg Baum + + * 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-31 Jürgen Spitzmüller * text.C (leftMargin): do not indent paragraphs in charstyle insets. diff --git a/src/exporter.C b/src/exporter.C index bf270b5ba7..1c12aa2d2f 100644 --- a/src/exporter.C +++ b/src/exporter.C @@ -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 & sourceFile, string const & destFile, } } - 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 +207,18 @@ bool Exporter::Export(Buffer * buffer, string const & format, string const dest = OnlyPath(result_file); CopyStatus status = SUCCESS; for (vector::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'"), diff --git a/src/exporter.h b/src/exporter.h index 0cdbd4e96e..1f912a6f5b 100644 --- a/src/exporter.h +++ b/src/exporter.h @@ -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 const - externalFiles(std::string const &) const; + externalFiles(std::string const & format) const; private: typedef std::map > FileMap; /** Files that are referenced by the export result in the diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index a9bcff86e4..f1e5193cc3 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,7 @@ +2004-11-01 Georg Baum + + * ExternalSupport.C (updateExternal): convert files in the temp dir + 2004-10-31 Jürgen Spitzmüller * insetcharstyle.C: drawing cosmetics. diff --git a/src/insets/ExternalSupport.C b/src/insets/ExternalSupport.C index b60fee1f02..c2ea3fa767 100644 --- a/src/insets/ExternalSupport.C +++ b/src/insets/ExternalSupport.C @@ -196,11 +196,12 @@ void updateExternal(InsetExternalParams const & params, // 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 const & params, 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 const & params, vector::const_iterator fit = rit->second.begin(); vector::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 } diff --git a/src/mover.C b/src/mover.C index a646f49478..4ffb3458fd 100644 --- a/src/mover.C +++ b/src/mover.C @@ -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; } diff --git a/src/mover.h b/src/mover.h index 3e80753601..153a20ba89 100644 --- a/src/mover.h +++ b/src/mover.h @@ -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 * - * sh $$s/copy_fig.sh $$i $$o + * sh $$s/copy_fig.sh $$i $$o $$l * * 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_; }; -- 2.39.2