-}
-
-
-void Buffer::makeLaTeXFile(ostream & os,
- string const & original_path,
- LatexRunParams const & runparams_in,
- bool only_body, bool only_preamble)
-{
- 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 (!only_body && 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 (!only_body) {
- 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 (only_preamble)
- return;
-
- // make the body.
- os << "\\begin{document}\n";
- texrow.newline();
- } // only_body
- 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 (!only_body) {
- 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(fname.c_str());
-
- if (!ofs) {
- string const file = MakeDisplayPath(fname, 50);
- string text = bformat(_("Could not save the specified document\n%1$s.\n"),
- file);
- Alert::error(_("Could not save document"), text);
- 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;
- const string 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);
-
- users->resetErrorList();
-
- 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)) {
- Inset * inset = pit->getInset(0);
- Inset::Code lyx_code = inset->lyxCode();
- if (lyx_code == Inset::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)
- parseError(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;
-
- users->showErrorList(_("LinuxDoc"));
-}
-
-
-// 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) << '>';
- }
-
- char c = par->getChar(i);
-
- if (c == Paragraph::META_INSET) {
- Inset * inset = par->getInset(i);
- inset->linuxdoc(this, os);
- font_old = font;
- continue;
- }
-
- if (style->latexparam() == "CDATA") {
- // "TeX"-Mode on == > SGML-Mode on.
- if (c != '\0')
- os << c;
- ++char_line_count;
- } else {
- bool ws;
- string str;
- boost::tie(ws, str) = sgml::escapeChar(c);
- if (ws && !par->isFreeSpacing()) {
- // in freespacing mode, spaces are
- // non-breaking characters
- if (desc_on) {// if char is ' ' then...
-
- ++char_line_count;
- sgmlLineBreak(os, char_line_count, 6);
- os << "</tag>";
- desc_on = false;
- } else {
- sgmlLineBreak(os, char_line_count, 1);
- os << c;
- }
- } else {
- os << str;
- char_line_count += str.length();
- }
- }
- font_old = font;
- }
-
- while (!tag_state.empty()) {
- os << "</" << tag_name(tag_state.top()) << '>';
- tag_state.pop();
- }