]> git.lyx.org Git - features.git/commitdiff
Embedding: move manifest to .lyx file, as a separate section between header and body
authorBo Peng <bpeng@lyx.org>
Tue, 11 Sep 2007 14:23:12 +0000 (14:23 +0000)
committerBo Peng <bpeng@lyx.org>
Tue, 11 Sep 2007 14:23:12 +0000 (14:23 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20212 a592a061-630c-0410-9148-cb99ea01b6c8

lib/lyx2lyx/LyX.py
lib/lyx2lyx/lyx_1_6.py
src/Buffer.cpp
src/EmbeddedFiles.cpp
src/EmbeddedFiles.h

index 8b8fddb38eb538b194856524d025769ad1103261..4bb101fda83e52602174c597360baf015b986167 100644 (file)
@@ -78,7 +78,7 @@ format_relation = [("0_06",    [200], generate_minor_versions("0.6" , 4)),
                    ("1_3",     [221], generate_minor_versions("1.3" , 7)),
                    ("1_4", range(222,246), generate_minor_versions("1.4" , 5)),
                    ("1_5", range(246,277), generate_minor_versions("1.5" , 1)),
-                   ("1_6", range(277,285), generate_minor_versions("1.6" , 0))]
+                   ("1_6", range(277,286), generate_minor_versions("1.6" , 0))]
 
 
 def formats_list():
@@ -175,6 +175,7 @@ class LyX_Base:
         self.default_layout = ''
         self.header = []
         self.preamble = []
+        self.manifest = []
         self.body = []
         self.status = 0
         self.encoding = encoding
@@ -198,7 +199,7 @@ class LyX_Base:
 
 
     def read(self):
-        """Reads a file into the self.header and self.body parts, from self.input."""
+        """Reads a file into the self.header, self.manifest and self.body parts, from self.input."""
 
         while 1:
             line = self.input.readline()
@@ -225,6 +226,25 @@ class LyX_Base:
             if check_token(line, '\\end_preamble'):
                 continue
 
+            if check_token(line, '\\begin_manifest'):
+                while 1:
+                    line = self.input.readline()
+                    if not line:
+                        self.error("Invalid LyX file.")
+
+                    line = trim_eol(line)
+                    if check_token(line, "\\end_manifest"):
+                        break
+
+                    if not line.startswith('\\filename') and not line.startswith('\\inzipName') \
+                            and not line.startswith('\\embed'):
+                        self.warning("Malformed LyX file: Missing '\\end_manifest'.")
+
+                    self.manifest.append(line)
+            
+            if check_token(line, '\\end_manifest'):
+                continue
+
             line = line.strip()
             if not line:
                 continue
@@ -275,7 +295,14 @@ class LyX_Base:
         else:
             header = self.header
 
-        for line in header + [''] + self.body:
+        # LyX file format <= 284 does not have a manifest section
+        # so this section is set to None
+        if self.manifest is None:
+            manifest = []
+        else:
+            manifest = ['\\begin_manifest'] + self.manifest + ['\\end_manifest', '']
+
+        for line in header + [''] + manifest + self.body:
             self.output.write(line.encode(self.encoding)+"\n")
 
 
index 93e275b478881bc7d3c7bb0db545320fe89c24ad..30c3421942f30d16e5d42ab181b6a5da68a918cc 100644 (file)
@@ -174,6 +174,10 @@ def revert_flex(document):
         document.body[i] = document.body[i].replace('\\begin_inset Flex', '\\begin_inset CharStyle')
 
 
+def remove_manifest(document):
+    "Remove the manifest section"
+    document.manifest = None
+
 
 ##
 # Conversion hub
@@ -188,10 +192,12 @@ convert = [
            [281, []],
            [282, []],
            [283, [convert_flex]],
-           [284, []]
+           [284, []],
+           [285, []], # an empty manifest is automatically added
           ]
 
 revert =  [
+           [284, [remove_manifest]],
            [283, []],
            [282, [revert_flex]],
            [281, []],
index 76cce2c544b72210b4288115fcad4822816c5a75..91158770d57e6c7489f569db22e9cca7faec313d 100644 (file)
@@ -142,7 +142,7 @@ namespace fs = boost::filesystem;
 
 namespace {
 
-int const LYX_FORMAT = 284;
+int const LYX_FORMAT = 285;
 
 } // namespace anon
 
@@ -559,7 +559,10 @@ bool Buffer::readDocument(Lexer & lex)
                                         "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
                }
        }
+       // read manifest after header
+       embeddedFiles().readManifest(lex, errorList);   
 
+       // read main text
        bool const res = text().read(*this, lex, errorList);
        for_each(text().paragraphs().begin(),
                 text().paragraphs().end(),
@@ -661,12 +664,9 @@ bool Buffer::readFile(FileName const & filename)
                LYXERR(Debug::FILES) << filename << " is in zip format. Unzip to " << temppath() << endl;
                ::unzipToDir(filename.toFilesystemEncoding(), temppath());
                //
-               FileName manifest(addName(temppath(), "manifest.txt"));
-               FileName lyxfile(addName(temppath(), 
-                       onlyFilename(filename.toFilesystemEncoding())));
+               FileName lyxfile(addName(temppath(), "content.lyx"));
                // if both manifest.txt and file.lyx exist, this is am embedded file
-               if (fs::exists(manifest.toFilesystemEncoding()) &&
-                       fs::exists(lyxfile.toFilesystemEncoding())) {
+               if (fs::exists(lyxfile.toFilesystemEncoding())) {
                        params().embedded = true;
                        fname = lyxfile;
                }
@@ -889,8 +889,7 @@ bool Buffer::writeFile(FileName const & fname) const
        FileName content;
        if (params().embedded)
                // first write the .lyx file to the temporary directory
-               content = FileName(addName(temppath(), 
-                       onlyFilename(fname.toFilesystemEncoding())));
+               content = FileName(addName(temppath(), "content.lyx"));
        else
                content = fname;
        
@@ -910,9 +909,8 @@ bool Buffer::writeFile(FileName const & fname) const
 
        if (retval && params().embedded) {
                // write file.lyx and all the embedded files to the zip file fname
-               // if embedding is enabled, and there is any embedded file
-               pimpl_->embedded_files.update();
-               return pimpl_->embedded_files.write(fname);
+               // if embedding is enabled
+               return pimpl_->embedded_files.writeFile(fname);
        }
        return retval;
 }
@@ -951,6 +949,12 @@ bool Buffer::write(ostream & ofs) const
        params().writeFile(ofs);
        ofs << "\\end_header\n";
 
+       // write the manifest after header
+       ofs << "\n\\begin_manifest\n";
+       pimpl_->embedded_files.update();
+       embeddedFiles().writeManifest(ofs);
+       ofs << "\\end_manifest\n";
+
        // write the text
        ofs << "\n\\begin_body\n";
        text().write(*this, ofs);
index 155ed86ac787c1b57f58d669b59c9bc71f0bf482..28add45c67de952b707a1bd272077ee6096421a2 100644 (file)
@@ -286,7 +286,7 @@ void EmbeddedFiles::update()
 }
 
 
-bool EmbeddedFiles::write(DocFileName const & filename)
+bool EmbeddedFiles::writeFile(DocFileName const & filename)
 {
        // file in the temporary path has the content
        string const content = FileName(addName(buffer_->temppath(),
@@ -404,9 +404,9 @@ bool EmbeddedFiles::readManifest(Lexer & lex, ErrorList & errorList)
        int begin_manifest_line = -1;
 
        file_list_.clear();
-       string filename = "";
-       string inzipName = "";
-       bool status = "";
+       string filename;
+       string inzipName;
+       bool embedded = false;
 
        while (lex.isOK()) {
                lex.next();
@@ -431,9 +431,9 @@ bool EmbeddedFiles::readManifest(Lexer & lex, ErrorList & errorList)
                        lex >> filename;
                else if (token == "\\inzipName")
                        lex >> inzipName;
-               else if (token == "\\status") {
-                       lex >> status;
-                       registerFile(filename, status, NULL, inzipName);
+               else if (token == "\\embed") {
+                       lex >> embedded;
+                       registerFile(filename, embedded, NULL, inzipName);
                        filename = "";
                        inzipName = "";
                } else {
@@ -463,7 +463,7 @@ void EmbeddedFiles::writeManifest(ostream & os) const
                        << to_utf8(makeRelPath(from_utf8(it->absFilename()),
                                from_utf8(buffer_->filePath()))) << '\n'
                        << "\\inzipName " << it->inzipName() << '\n'
-                       << "\\status " << (it->embedded() ? "true" : "false") << '\n';
+                       << "\\embed " << (it->embedded() ? "true" : "false") << '\n';
        }
 }
 
index 58b1a08b1a9501264431267f153c077299e679f3..a8f570b730e453012dea6a201186840c408b06e4 100644 (file)
@@ -77,7 +77,7 @@ EmbeddedFiles::update(), Inset::registerEmbeddedFiles()).
 3. When a lyx file file.lyx is saved, it is save to tmppath() first.
 Embedded files are compressed along with file.lyx and a manifest.txt. 
 If embedding is disabled, file.lyx is saved in the usual pure-text form.
-(c.f. Buffer::writeFile(), EmbeddedFiles::write())
+(c.f. Buffer::writeFile(), EmbeddedFiles::writeFile())
 
 4. When a lyx file.lyx file is opened, if it is a zip file, it is
 decompressed to tmppath(). If manifest.txt and file.lyx exists in
@@ -199,7 +199,7 @@ public:
        void update();
 
        /// write a zip file
-       bool write(support::DocFileName const & filename);
+       bool writeFile(support::DocFileName const & filename);
 
        void clear() { file_list_.clear(); }