From c51449cac6e49b02be98be8b2d72318888cd7c0a Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Sat, 29 Dec 2012 13:16:22 +0100 Subject: [PATCH] Fix tex2lyx bug #8466: special file names. Special characters as created by latex_path() where not converted corectly from LaTeX macros by tex2lyx. Now this is done, even for file names containing double quotes which are not used for quoting spaces. These file names are not legal on windows, and will causes probelms in DVI files, but if they occur tex2lyx does not produce invalid .lyx files. --- src/tex2lyx/Makefile.am | 2 + src/tex2lyx/test/Dummy Document.lyx | 89 ++++++++++++++++++++++++++++ src/tex2lyx/test/Dummy Document.tex | 2 + src/tex2lyx/test/Dummy~Document.lyx | 89 ++++++++++++++++++++++++++++ src/tex2lyx/test/Dummy~Document.tex | 2 + src/tex2lyx/test/test-insets.lyx.lyx | 36 ++++++++--- src/tex2lyx/test/test-insets.tex | 11 +++- src/tex2lyx/text.cpp | 24 +++++++- 8 files changed, 243 insertions(+), 12 deletions(-) create mode 100644 src/tex2lyx/test/Dummy Document.lyx create mode 100644 src/tex2lyx/test/Dummy Document.tex create mode 100644 src/tex2lyx/test/Dummy~Document.lyx create mode 100644 src/tex2lyx/test/Dummy~Document.tex diff --git a/src/tex2lyx/Makefile.am b/src/tex2lyx/Makefile.am index 1c296a725f..44143e26fe 100644 --- a/src/tex2lyx/Makefile.am +++ b/src/tex2lyx/Makefile.am @@ -24,6 +24,8 @@ TEST_FILES = \ test/box-color-size-space-align.tex \ test/CJK.tex \ test/DummyDocument.tex \ + test/Dummy~Document.tex \ + test/Dummy\ Document.tex \ test/foo.eps \ test/foo.png \ test/test-insets.tex \ diff --git a/src/tex2lyx/test/Dummy Document.lyx b/src/tex2lyx/test/Dummy Document.lyx new file mode 100644 index 0000000000..b610a889e7 --- /dev/null +++ b/src/tex2lyx/test/Dummy Document.lyx @@ -0,0 +1,89 @@ +#LyX file created by tex2lyx 2.1.0dev +\lyxformat 457 +\begin_document +\begin_header +\textclass article +\use_default_options false +\maintain_unincluded_children false +\language english +\language_package none +\inputencoding latin9 +\fontencoding T1 +\font_roman default +\font_sans default +\font_typewriter default +\font_math auto +\font_default_family default +\use_non_tex_fonts false +\font_sc false +\font_osf false +\font_sf_scale 100 +\font_tt_scale 100 +\graphics default +\default_output_format default +\output_sync 0 +\bibtex_command default +\index_command default +\paperfontsize 12 +\spacing onehalf +\use_hyperref true +\pdf_bookmarks 1 +\pdf_bookmarksnumbered 0 +\pdf_bookmarksopen 0 +\pdf_bookmarksopenlevel 1 +\pdf_breaklinks 0 +\pdf_pdfborder 0 +\pdf_colorlinks 0 +\pdf_backref section +\pdf_pdfusetitle 1 +\papersize a4paper +\use_geometry false +\use_package amsmath 2 +\use_package amssymb 2 +\use_package esint 1 +\use_package mathdots 0 +\use_package mathtools 0 +\use_package mhchem 0 +\use_package stackrel 0 +\use_package stmaryrd 0 +\use_package undertilde 0 +\cite_engine natbib +\cite_engine_type numerical +\biblio_style plainnat +\use_bibtopic false +\use_indices true +\paperorientation portrait +\suppress_date false +\justification true +\use_refstyle 0 +\index Index +\shortcut idx +\color #008000 +\end_index +\secnumdepth 3 +\tocdepth 3 +\paragraph_separation indent +\paragraph_indentation 3mm +\quotes_language english +\papercolumns 1 +\papersides 1 +\paperpagestyle default +\tracking_changes false +\output_changes false +\html_math_output 0 +\html_css_as_file 0 +\html_be_strict false +\end_header + +\begin_body + +\begin_layout Subsection +Space Test +\end_layout + +\begin_layout Standard +The file name contains a space. +\end_layout + +\end_body +\end_document diff --git a/src/tex2lyx/test/Dummy Document.tex b/src/tex2lyx/test/Dummy Document.tex new file mode 100644 index 0000000000..dff06d3b1a --- /dev/null +++ b/src/tex2lyx/test/Dummy Document.tex @@ -0,0 +1,2 @@ +\subsection{Space Test} +The file name contains a space. diff --git a/src/tex2lyx/test/Dummy~Document.lyx b/src/tex2lyx/test/Dummy~Document.lyx new file mode 100644 index 0000000000..51ea560b48 --- /dev/null +++ b/src/tex2lyx/test/Dummy~Document.lyx @@ -0,0 +1,89 @@ +#LyX file created by tex2lyx 2.1.0dev +\lyxformat 457 +\begin_document +\begin_header +\textclass article +\use_default_options false +\maintain_unincluded_children false +\language english +\language_package none +\inputencoding latin9 +\fontencoding T1 +\font_roman default +\font_sans default +\font_typewriter default +\font_math auto +\font_default_family default +\use_non_tex_fonts false +\font_sc false +\font_osf false +\font_sf_scale 100 +\font_tt_scale 100 +\graphics default +\default_output_format default +\output_sync 0 +\bibtex_command default +\index_command default +\paperfontsize 12 +\spacing onehalf +\use_hyperref true +\pdf_bookmarks 1 +\pdf_bookmarksnumbered 0 +\pdf_bookmarksopen 0 +\pdf_bookmarksopenlevel 1 +\pdf_breaklinks 0 +\pdf_pdfborder 0 +\pdf_colorlinks 0 +\pdf_backref section +\pdf_pdfusetitle 1 +\papersize a4paper +\use_geometry false +\use_package amsmath 2 +\use_package amssymb 2 +\use_package esint 1 +\use_package mathdots 0 +\use_package mathtools 0 +\use_package mhchem 0 +\use_package stackrel 0 +\use_package stmaryrd 0 +\use_package undertilde 0 +\cite_engine natbib +\cite_engine_type numerical +\biblio_style plainnat +\use_bibtopic false +\use_indices true +\paperorientation portrait +\suppress_date false +\justification true +\use_refstyle 0 +\index Index +\shortcut idx +\color #008000 +\end_index +\secnumdepth 3 +\tocdepth 3 +\paragraph_separation indent +\paragraph_indentation 3mm +\quotes_language english +\papercolumns 1 +\papersides 1 +\paperpagestyle default +\tracking_changes false +\output_changes false +\html_math_output 0 +\html_css_as_file 0 +\html_be_strict false +\end_header + +\begin_body + +\begin_layout Subsection +Tilde Test +\end_layout + +\begin_layout Standard +The file name contains a tilde. +\end_layout + +\end_body +\end_document diff --git a/src/tex2lyx/test/Dummy~Document.tex b/src/tex2lyx/test/Dummy~Document.tex new file mode 100644 index 0000000000..62216863b9 --- /dev/null +++ b/src/tex2lyx/test/Dummy~Document.tex @@ -0,0 +1,2 @@ +\subsection{Tilde Test} +The file name contains a tilde. diff --git a/src/tex2lyx/test/test-insets.lyx.lyx b/src/tex2lyx/test/test-insets.lyx.lyx index df5c9cc894..bb1574ac6d 100644 --- a/src/tex2lyx/test/test-insets.lyx.lyx +++ b/src/tex2lyx/test/test-insets.lyx.lyx @@ -697,25 +697,43 @@ filename "DummyDocument.lyx" \end_inset -, or with the include variant +, or with the include variant (tests quoting as well): \begin_inset CommandInset include LatexCommand include preview false -filename "DummyDocument.lyx" +filename "Dummy Document.lyx" \end_inset -\begin_inset ERT -status collapsed +\begin_inset CommandInset include +LatexCommand include +preview false +filename "Dummy Document.lyx" -\begin_layout Standard -% unfortunately, including the doc twice generates a multiply defined label -\end_layout +\end_inset -\begin_layout Standard + +\begin_inset CommandInset include +LatexCommand include +preview false +filename "Dummy~Document.lyx" -\end_layout +\end_inset + + +\begin_inset CommandInset include +LatexCommand include +preview false +filename "Dummy~Document.lyx" + +\end_inset + + File names containg double quotes are not legal on windows, so no test file is provided. Nevertheless, they are legal on unix, so we test at least the paring. Since the file does not exist, the variant without extension would not work (tex2lyx can't deduce the correct extension of nonexisting files). +\begin_inset CommandInset include +LatexCommand include +preview false +filename "Dummy\"Document.tex" \end_inset diff --git a/src/tex2lyx/test/test-insets.tex b/src/tex2lyx/test/test-insets.tex index e5596bbb84..2a476a2601 100644 --- a/src/tex2lyx/test/test-insets.tex +++ b/src/tex2lyx/test/test-insets.tex @@ -134,7 +134,16 @@ M., \& Rasio, F.~A. 2004, ApJ, 604, 632\end{thebibliography} \section{Input files\index{Input files}} We can input files too, like this \input{DummyDocument}, or with the include -variant \include{DummyDocument} % unfortunately, including the doc twice generates a multiply defined label +variant (tests quoting as well): +\include{\string"Dummy\space Document\string"} +\include{\string"Dummy Document\string".tex} +\include{\string"Dummy\string~Document\string"} +\include{\string"Dummy\string~Document\string".tex} +File names containg double quotes are not legal on windows, so no test file +is provided. Nevertheless, they are legal on unix, so we test at least the +paring. Since the file does not exist, the variant without extension would +not work (tex2lyx can't deduce the correct extension of nonexisting files). +\include{\string"Dummy\string"Document\string".tex} We can also import chess diagrams: diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index c90b3cd25d..6f15a7ce45 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -1839,7 +1839,7 @@ void get_cite_arguments(Parser & p, bool natbibOrder, /// can understand string const normalize_filename(string const & name) { - Parser p(trim(name, "\"")); + Parser p(name); ostringstream os; while (p.good()) { Token const & t = p.get_token(); @@ -1853,10 +1853,29 @@ string const normalize_filename(string const & name) } else if (t.cs() == "space") { os << ' '; p.skip_spaces(); + } else if (t.cs() == "string") { + // Convert \string" to " and \string~ to ~ + Token const & n = p.next_token(); + if (n.asInput() != "\"" && n.asInput() != "~") + os << t.asInput(); } else os << t.asInput(); } - return os.str(); + // Strip quotes. This is a bit complicated (see latex_path()). + string full = os.str(); + if (!full.empty() && full[0] == '"') { + string base = removeExtension(full); + string ext = getExtension(full); + if (!base.empty() && base[base.length()-1] == '"') + // "a b" + // "a b".tex + return addExtension(trim(base, "\""), ext); + if (full[full.length()-1] == '"') + // "a b.c" + // "a b.c".tex + return trim(full, "\""); + } + return full; } @@ -3925,6 +3944,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, registerExternalTemplatePackages("XFig"); } else { begin_command_inset(os, "include", name); + outname = subst(outname, "\"", "\\\""); os << "preview false\n" "filename \"" << outname << "\"\n"; if (t.cs() == "verbatiminput") -- 2.39.2