- ofs << "\n\\the_end" << endl;
-
- // Shouldn't really be needed....
- //ofs.close();
-
- // how to check if close went ok?
- // Following is an attempt... (BE 20001011)
-
- // good() returns false if any error occured, including some
- // formatting error.
- // bad() returns true if something bad happened in the buffer,
- // which should include file system full errors.
-
- bool status = true;
- if (!ofs.good()) {
- status = false;
-#if 0
- if (ofs.bad()) {
- lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
- } else {
- lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
- << endl;
- }
-#endif
- }
-
- return status;
-}
-
-
-namespace {
-
-pair<int, string> const addDepth(int depth, int ldepth)
-{
- int d = depth * 2;
- if (ldepth > depth)
- d += (ldepth - depth) * 2;
- return make_pair(d, string(d, ' '));
-}
-
-}
-
-
-string const Buffer::asciiParagraph(Paragraph const & par,
- unsigned int linelen,
- bool noparbreak) const
-{
- ostringstream buffer;
- int ltype = 0;
- Paragraph::depth_type ltype_depth = 0;
- bool ref_printed = false;
- Paragraph::depth_type depth = par.params().depth();
-
- // First write the layout
- string const & tmp = par.layout()->name();
- if (compare_no_case(tmp, "itemize") == 0) {
- ltype = 1;
- ltype_depth = depth + 1;
- } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
- ltype = 2;
- ltype_depth = depth + 1;
- } else if (contains(ascii_lowercase(tmp), "ection")) {
- ltype = 3;
- ltype_depth = depth + 1;
- } else if (contains(ascii_lowercase(tmp), "aragraph")) {
- ltype = 4;
- ltype_depth = depth + 1;
- } else if (compare_ascii_no_case(tmp, "description") == 0) {
- ltype = 5;
- ltype_depth = depth + 1;
- } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
- ltype = 6;
- ltype_depth = 0;
- } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
- ltype = 7;
- ltype_depth = 0;
- } else {
- ltype = 0;
- ltype_depth = 0;
- }
-
- /* maybe some vertical spaces */
-
- /* the labelwidthstring used in lists */
-
- /* some lines? */
-
- /* some pagebreaks? */
-
- /* noindent ? */
-
- /* what about the alignment */
-
- // linelen <= 0 is special and means we don't have paragraph breaks
-
- string::size_type currlinelen = 0;
-
- if (!noparbreak) {
- if (linelen > 0)
- buffer << "\n\n";
-
- buffer << string(depth * 2, ' ');
- currlinelen += depth * 2;
-
- //--
- // we should probably change to the paragraph language in the
- // gettext here (if possible) so that strings are outputted in
- // the correct language! (20012712 Jug)
- //--
- switch (ltype) {
- case 0: // Standard
- case 4: // (Sub)Paragraph
- case 5: // Description
- break;
- case 6: // Abstract
- if (linelen > 0) {
- buffer << _("Abstract") << "\n\n";
- currlinelen = 0;
- } else {
- string const abst = _("Abstract: ");
- buffer << abst;
- currlinelen += abst.length();
- }
- break;
- case 7: // Bibliography
- if (!ref_printed) {
- if (linelen > 0) {
- buffer << _("References") << "\n\n";
- currlinelen = 0;
- } else {
- string const refs = _("References: ");
- buffer << refs;
- currlinelen += refs.length();
- }
-
- ref_printed = true;
- }
- break;
- default:
- {
- string const parlab = par.params().labelString();
- buffer << parlab << ' ';
- currlinelen += parlab.length() + 1;
- }
- break;
-
- }
- }
-
- if (!currlinelen) {
- pair<int, string> p = addDepth(depth, ltype_depth);
- buffer << p.second;
- currlinelen += p.first;
- }
-
- // this is to change the linebreak to do it by word a bit more
- // intelligent hopefully! (only in the case where we have a
- // max linelength!) (Jug)
-
- string word;
-
- for (pos_type i = 0; i < par.size(); ++i) {
- char c = par.getUChar(params, i);
- switch (c) {
- case Paragraph::META_INSET:
- {
- InsetOld const * inset = par.getInset(i);
- if (inset) {
- if (linelen > 0) {
- buffer << word;
- currlinelen += word.length();
- word.erase();
- }
- if (inset->ascii(this, buffer, linelen)) {
- // to be sure it breaks paragraph
- currlinelen += linelen;
- }
- }
- }
- break;
-
- default:
- if (c == ' ') {
- if (linelen > 0 &&
- currlinelen + word.length() > linelen - 10) {
- buffer << "\n";
- pair<int, string> p = addDepth(depth, ltype_depth);
- buffer << p.second;
- currlinelen = p.first;
- }
-
- buffer << word << ' ';
- currlinelen += word.length() + 1;
- word.erase();
-
- } else {
- if (c != '\0') {
- word += c;
- } else {
- lyxerr[Debug::INFO] <<
- "writeAsciiFile: NULL char in structure." << endl;
- }
- if ((linelen > 0) &&
- (currlinelen + word.length()) > linelen)
- {
- buffer << "\n";
-
- pair<int, string> p =
- addDepth(depth, ltype_depth);
- buffer << p.second;
- currlinelen = p.first;
- }
- }
- break;
- }
- }
- buffer << word;
- return STRCONV(buffer.str());
-}
-
-
-void Buffer::writeFileAscii(string const & fname, int linelen)
-{
- ofstream ofs;
- if (!::openFileWrite(ofs, fname))
- return;
- writeFileAscii(ofs, linelen);
-}
-
-
-void Buffer::writeFileAscii(ostream & os, int linelen)
-{
- ParagraphList::iterator beg = paragraphs.begin();
- ParagraphList::iterator end = paragraphs.end();
- ParagraphList::iterator it = beg;
- for (; it != end; ++it) {
- os << asciiParagraph(*it, linelen, it == beg);
- }
- os << "\n";
-}
-
-
-void Buffer::makeLaTeXFile(string const & fname,
- string const & original_path,
- LatexRunParams const & runparams,
- bool output_preamble, bool output_body)
-{
- lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
-
- ofstream ofs;
- if (!::openFileWrite(ofs, fname))
- return;
-
- makeLaTeXFile(ofs, original_path,
- runparams, output_preamble, output_body);
-
- ofs.close();
- if (ofs.fail()) {
- lyxerr << "File was not closed properly." << endl;
- }
-}
-
-
-void Buffer::makeLaTeXFile(ostream & os,
- string const & original_path,
- LatexRunParams const & runparams_in,
- bool output_preamble, bool output_body)
-{
- LatexRunParams runparams = runparams_in;
- niceFile = runparams.nice; // this will be used by Insetincludes.
-
- // validate the buffer.
- lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
- LaTeXFeatures features(params);
- validate(features);
- lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
-
- texrow.reset();
- // The starting paragraph of the coming rows is the
- // first paragraph of the document. (Asger)
- texrow.start(paragraphs.begin()->id(), 0);
-
- if (output_preamble && runparams.nice) {
- os << "%% " << lyx_docversion << " created this file. "
- "For more info, see http://www.lyx.org/.\n"
- "%% Do not edit unless you really know what "
- "you are doing.\n";
- texrow.newline();
- texrow.newline();
- }
- lyxerr[Debug::INFO] << "lyx header finished" << endl;
- // There are a few differences between nice LaTeX and usual files:
- // usual is \batchmode and has a
- // special input@path to allow the including of figures
- // with either \input or \includegraphics (what figinsets do).
- // input@path is set when the actual parameter
- // original_path is set. This is done for usual tex-file, but not
- // for nice-latex-file. (Matthias 250696)
- if (output_preamble) {
- if (!runparams.nice) {
- // code for usual, NOT nice-latex-file
- os << "\\batchmode\n"; // changed
- // from \nonstopmode
- texrow.newline();
- }
- if (!original_path.empty()) {
- string inputpath = os::external_path(original_path);
- subst(inputpath, "~", "\\string~");
- os << "\\makeatletter\n"
- << "\\def\\input@path{{"
- << inputpath << "/}}\n"
- << "\\makeatother\n";
- texrow.newline();
- texrow.newline();
- texrow.newline();
- }
-
- // Write the preamble
- runparams.use_babel = params.writeLaTeX(os, features, texrow);
-
- if (!output_body)
- return;
-
- // make the body.
- os << "\\begin{document}\n";
- texrow.newline();
- } // output_preamble
- lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
-
- if (!lyxrc.language_auto_begin) {
- os << subst(lyxrc.language_command_begin, "$$lang",
- params.language->babel())
- << endl;
- texrow.newline();
- }
-
- latexParagraphs(this, paragraphs, os, texrow, runparams);
-
- // add this just in case after all the paragraphs
- os << endl;
- texrow.newline();
-
- if (!lyxrc.language_auto_end) {
- os << subst(lyxrc.language_command_end, "$$lang",
- params.language->babel())
- << endl;
- texrow.newline();
- }
-
- if (output_preamble) {
- os << "\\end{document}\n";
- texrow.newline();
-
- lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
- } else {
- lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
- << endl;
- }
-
- // Just to be sure. (Asger)
- texrow.newline();
-
- lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
- lyxerr[Debug::INFO] << "Row count was " << texrow.rows() - 1
- << '.' << endl;
-
- // we want this to be true outside previews (for insetexternal)
- niceFile = true;
-}
-
-
-bool Buffer::isLatex() const
-{
- return params.getLyXTextClass().outputType() == LATEX;
-}
-
-
-bool Buffer::isLinuxDoc() const
-{
- return params.getLyXTextClass().outputType() == LINUXDOC;
-}
-
-
-bool Buffer::isLiterate() const
-{
- return params.getLyXTextClass().outputType() == LITERATE;
-}
-
-
-bool Buffer::isDocBook() const
-{
- return params.getLyXTextClass().outputType() == DOCBOOK;
-}
-
-
-bool Buffer::isSGML() const
-{
- LyXTextClass const & tclass = params.getLyXTextClass();
-
- return tclass.outputType() == LINUXDOC ||
- tclass.outputType() == DOCBOOK;
-}
-
-
-void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
-{
- ofstream ofs;
- if (!::openFileWrite(ofs, fname))
- return;
-
- niceFile = nice; // this will be used by included files.
-
- LaTeXFeatures features(params);
-
- validate(features);
-
- texrow.reset();
-
- LyXTextClass const & tclass = params.getLyXTextClass();
-
- string top_element = tclass.latexname();
-
- if (!body_only) {
- ofs << "<!doctype linuxdoc system";
-
- string preamble = params.preamble;
- string const name = nice ? ChangeExtension(filename_, ".sgml")
- : fname;
- preamble += features.getIncludedFiles(name);
- preamble += features.getLyXSGMLEntities();
-
- if (!preamble.empty()) {
- ofs << " [ " << preamble << " ]";
- }
- ofs << ">\n\n";
-
- if (params.options.empty())
- sgml::openTag(ofs, 0, false, top_element);
- else {
- string top = top_element;
- top += ' ';
- top += params.options;
- sgml::openTag(ofs, 0, false, top);
- }
- }
-
- ofs << "<!-- " << lyx_docversion
- << " created this file. For more info see http://www.lyx.org/"
- << " -->\n";
-
- Paragraph::depth_type depth = 0; // paragraph depth
- string item_name;
- vector<string> environment_stack(5);
-
- ParagraphList::iterator pit = paragraphs.begin();
- ParagraphList::iterator pend = paragraphs.end();
- for (; pit != pend; ++pit) {
- LyXLayout_ptr const & style = pit->layout();
- // treat <toc> as a special case for compatibility with old code
- if (pit->isInset(0)) {
- InsetOld * inset = pit->getInset(0);
- InsetOld::Code lyx_code = inset->lyxCode();
- if (lyx_code == InsetOld::TOC_CODE) {
- string const temp = "toc";
- sgml::openTag(ofs, depth, false, temp);
- continue;
- }
- }
-
- // environment tag closing
- for (; depth > pit->params().depth(); --depth) {
- sgml::closeTag(ofs, depth, false, environment_stack[depth]);
- environment_stack[depth].erase();
- }
-
- // write opening SGML tags
- switch (style->latextype) {
- case LATEX_PARAGRAPH:
- if (depth == pit->params().depth()
- && !environment_stack[depth].empty()) {
- sgml::closeTag(ofs, depth, false, environment_stack[depth]);
- environment_stack[depth].erase();
- if (depth)
- --depth;
- else
- ofs << "</p>";
- }
- sgml::openTag(ofs, depth, false, style->latexname());
- break;
-
- case LATEX_COMMAND:
- if (depth != 0)
- error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
-
- if (!environment_stack[depth].empty()) {
- sgml::closeTag(ofs, depth, false, environment_stack[depth]);
- ofs << "</p>";
- }
-
- environment_stack[depth].erase();
- sgml::openTag(ofs, depth, false, style->latexname());
- break;
-
- case LATEX_ENVIRONMENT:
- case LATEX_ITEM_ENVIRONMENT:
- case LATEX_BIB_ENVIRONMENT:
- {
- string const & latexname = style->latexname();
-
- if (depth == pit->params().depth()
- && environment_stack[depth] != latexname) {
- sgml::closeTag(ofs, depth, false,
- environment_stack[depth]);
- environment_stack[depth].erase();
- }
- if (depth < pit->params().depth()) {
- depth = pit->params().depth();
- environment_stack[depth].erase();
- }
- if (environment_stack[depth] != latexname) {
- if (depth == 0) {
- sgml::openTag(ofs, depth, false, "p");
- }
- sgml::openTag(ofs, depth, false, latexname);
-
- if (environment_stack.size() == depth + 1)
- environment_stack.push_back("!-- --");
- environment_stack[depth] = latexname;
- }
-
- if (style->latexparam() == "CDATA")
- ofs << "<![CDATA[";
-
- if (style->latextype == LATEX_ENVIRONMENT) break;
-
- if (style->labeltype == LABEL_MANUAL)
- item_name = "tag";
- else
- item_name = "item";
-
- sgml::openTag(ofs, depth + 1, false, item_name);
- }
- break;
-
- default:
- sgml::openTag(ofs, depth, false, style->latexname());
- break;
- }
-
- simpleLinuxDocOnePar(ofs, pit, depth);
-
- ofs << "\n";
- // write closing SGML tags
- switch (style->latextype) {
- case LATEX_COMMAND:
- break;
- case LATEX_ENVIRONMENT:
- case LATEX_ITEM_ENVIRONMENT:
- case LATEX_BIB_ENVIRONMENT:
- if (style->latexparam() == "CDATA")
- ofs << "]]>";
- break;
- default:
- sgml::closeTag(ofs, depth, false, style->latexname());
- break;
- }
- }
-
- // Close open tags
- for (int i = depth; i >= 0; --i)
- sgml::closeTag(ofs, depth, false, environment_stack[i]);
-
- if (!body_only) {
- ofs << "\n\n";
- sgml::closeTag(ofs, 0, false, top_element);
- }
-
- ofs.close();
- // How to check for successful close
-
- // we want this to be true outside previews (for insetexternal)
- niceFile = true;
-}
-
-
-// checks, if newcol chars should be put into this line
-// writes newline, if necessary.
-namespace {
-
-void sgmlLineBreak(ostream & os, string::size_type & colcount,
- string::size_type newcol)
-{
- colcount += newcol;
- if (colcount > lyxrc.ascii_linelen) {
- os << "\n";
- colcount = newcol; // assume write after this call
- }
-}
-
-enum PAR_TAG {
- NONE=0,
- TT = 1,
- SF = 2,
- BF = 4,
- IT = 8,
- SL = 16,
- EM = 32
-};
-
-
-string tag_name(PAR_TAG const & pt) {
- switch (pt) {
- case NONE: return "!-- --";
- case TT: return "tt";
- case SF: return "sf";
- case BF: return "bf";
- case IT: return "it";
- case SL: return "sl";
- case EM: return "em";
- }
- return "";
-}
-
-
-inline
-void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
-{
- p1 = static_cast<PAR_TAG>(p1 | p2);
-}
-
-
-inline
-void reset(PAR_TAG & p1, PAR_TAG const & p2)
-{
- p1 = static_cast<PAR_TAG>(p1 & ~p2);
-}
-
-} // anon
-
-
-// Handle internal paragraph parsing -- layout already processed.
-void Buffer::simpleLinuxDocOnePar(ostream & os,
- ParagraphList::iterator par,
- Paragraph::depth_type /*depth*/) const
-{
- LyXLayout_ptr const & style = par->layout();
-
- string::size_type char_line_count = 5; // Heuristic choice ;-)
-
- // gets paragraph main font
- LyXFont font_old;
- bool desc_on;
- if (style->labeltype == LABEL_MANUAL) {
- font_old = style->labelfont;
- desc_on = true;
- } else {
- font_old = style->font;
- desc_on = false;
- }
-
- LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
- LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
- LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
- bool is_em = false;
-
- stack<PAR_TAG> tag_state;
- // parsing main loop
- for (pos_type i = 0; i < par->size(); ++i) {
-
- PAR_TAG tag_close = NONE;
- list < PAR_TAG > tag_open;
-
- LyXFont const font = par->getFont(params, i, outerFont(par, paragraphs));
-
- if (font_old.family() != font.family()) {
- switch (family_type) {
- case LyXFont::SANS_FAMILY:
- tag_close |= SF;
- break;
- case LyXFont::TYPEWRITER_FAMILY:
- tag_close |= TT;
- break;
- default:
- break;
- }
-
- family_type = font.family();
-
- switch (family_type) {
- case LyXFont::SANS_FAMILY:
- tag_open.push_back(SF);
- break;
- case LyXFont::TYPEWRITER_FAMILY:
- tag_open.push_back(TT);
- break;
- default:
- break;
- }
- }
-
- if (font_old.series() != font.series()) {
- switch (series_type) {
- case LyXFont::BOLD_SERIES:
- tag_close |= BF;
- break;
- default:
- break;
- }
-
- series_type = font.series();
-
- switch (series_type) {
- case LyXFont::BOLD_SERIES:
- tag_open.push_back(BF);
- break;
- default:
- break;
- }
-
- }
-
- if (font_old.shape() != font.shape()) {
- switch (shape_type) {
- case LyXFont::ITALIC_SHAPE:
- tag_close |= IT;
- break;
- case LyXFont::SLANTED_SHAPE:
- tag_close |= SL;
- break;
- default:
- break;
- }
-
- shape_type = font.shape();
-
- switch (shape_type) {
- case LyXFont::ITALIC_SHAPE:
- tag_open.push_back(IT);
- break;
- case LyXFont::SLANTED_SHAPE:
- tag_open.push_back(SL);
- break;
- default:
- break;
- }
- }
- // handle <em> tag
- if (font_old.emph() != font.emph()) {
- if (font.emph() == LyXFont::ON) {
- tag_open.push_back(EM);
- is_em = true;
- }
- else if (is_em) {
- tag_close |= EM;
- is_em = false;
- }
- }
-
- list < PAR_TAG > temp;
- while (!tag_state.empty() && tag_close) {
- PAR_TAG k = tag_state.top();
- tag_state.pop();
- os << "</" << tag_name(k) << '>';
- if (tag_close & k)
- reset(tag_close,k);
- else
- temp.push_back(k);
- }
-
- for(list< PAR_TAG >::const_iterator j = temp.begin();
- j != temp.end(); ++j) {
- tag_state.push(*j);
- os << '<' << tag_name(*j) << '>';
- }
-
- for(list< PAR_TAG >::const_iterator j = tag_open.begin();
- j != tag_open.end(); ++j) {
- tag_state.push(*j);
- os << '<' << tag_name(*j) << '>';
- }