From: Bo Peng Date: Tue, 11 Sep 2007 14:23:12 +0000 (+0000) Subject: Embedding: move manifest to .lyx file, as a separate section between header and body X-Git-Tag: 1.6.10~8392 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=e2d5893eabac24eb8827ad78a35c6e092db0087e;p=features.git Embedding: move manifest to .lyx file, as a separate section between header and body git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20212 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/lib/lyx2lyx/LyX.py b/lib/lyx2lyx/LyX.py index 8b8fddb38e..4bb101fda8 100644 --- a/lib/lyx2lyx/LyX.py +++ b/lib/lyx2lyx/LyX.py @@ -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") diff --git a/lib/lyx2lyx/lyx_1_6.py b/lib/lyx2lyx/lyx_1_6.py index 93e275b478..30c3421942 100644 --- a/lib/lyx2lyx/lyx_1_6.py +++ b/lib/lyx2lyx/lyx_1_6.py @@ -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, []], diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 76cce2c544..91158770d5 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -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); diff --git a/src/EmbeddedFiles.cpp b/src/EmbeddedFiles.cpp index 155ed86ac7..28add45c67 100644 --- a/src/EmbeddedFiles.cpp +++ b/src/EmbeddedFiles.cpp @@ -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'; } } diff --git a/src/EmbeddedFiles.h b/src/EmbeddedFiles.h index 58b1a08b1a..a8f570b730 100644 --- a/src/EmbeddedFiles.h +++ b/src/EmbeddedFiles.h @@ -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(); }