Improve LaTeX format detection

libmime is a bit lacking here.
This commit is contained in:
Georg Baum 2013-04-12 22:31:48 +02:00
parent 0613a218aa
commit f4eae12d60
3 changed files with 42 additions and 5 deletions

View File

@ -253,12 +253,11 @@ string guessFormatFromContents(FileName const & fn)
string str; string str;
string format; string format;
bool firstLine = true; bool firstLine = true;
bool backslash = false;
int dollars = 0;
while ((count++ < max_count) && format.empty()) { while ((count++ < max_count) && format.empty()) {
if (ifs.eof()) { if (ifs.eof())
LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
<< "\tFile type not recognised before EOF!");
break; break;
}
getline(ifs, str); getline(ifs, str);
string const stamp = str.substr(0, 2); string const stamp = str.substr(0, 2);
@ -363,9 +362,32 @@ string guessFormatFromContents(FileName const & fn)
else if (contains(str, "BITPIX")) else if (contains(str, "BITPIX"))
format = "fits"; format = "fits";
else if (contains(str, "\\documentclass") ||
contains(str, "\\chapter") ||
contains(str, "\\section") ||
contains(str, "\\begin") ||
contains(str, "\\end") ||
contains(str, "$$") ||
contains(str, "\\[") ||
contains(str, "\\]"))
format = "latex";
else {
if (contains(str, '\\'))
backslash = true;
dollars += count_char(str, '$');
}
} }
if (!format.empty()) { if (format.empty() && backslash && dollars > 1)
// inline equation
format = "latex";
if (format.empty()) {
if (ifs.eof())
LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
"\tFile type not recognised before EOF!");
} else {
LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format); LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
return format; return format;
} }

View File

@ -882,6 +882,18 @@ docstring const subst(docstring const & a,
} }
int count_char(string const & str, char chr)
{
int count = 0;
string::const_iterator lit = str.begin();
string::const_iterator end = str.end();
for (; lit != end; ++lit)
if ((*lit) == chr)
count++;
return count;
}
/// Count all occurences of char \a chr inside \a str /// Count all occurences of char \a chr inside \a str
int count_char(docstring const & str, docstring::value_type chr) int count_char(docstring const & str, docstring::value_type chr)
{ {

View File

@ -188,6 +188,9 @@ std::string const subst(std::string const & a,
docstring const subst(docstring const & a, docstring const subst(docstring const & a,
docstring const & oldstr, docstring const & newstr); docstring const & oldstr, docstring const & newstr);
/// Count all occurences of char \a chr inside \a str
int count_char(std::string const & str, char chr);
/// Count all occurences of char \a chr inside \a str /// Count all occurences of char \a chr inside \a str
int count_char(docstring const & str, docstring::value_type chr); int count_char(docstring const & str, docstring::value_type chr);