-
- Paragraph::depth_type depth = 0; // paragraph depth
- string item_name;
- vector<string> environment_stack(5);
-
- ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
- ParagraphList::iterator pend = const_cast<ParagraphList&>(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(os, depth, false, temp);
- continue;
- }
- }
-
- // environment tag closing
- for (; depth > pit->params().depth(); --depth) {
- sgml::closeTag(os, 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(os, depth, false, environment_stack[depth]);
- environment_stack[depth].erase();
- if (depth)
- --depth;
- else
- os << "</p>";
- }
- sgml::openTag(os, 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(os, depth, false, environment_stack[depth]);
- os << "</p>";
- }
-
- environment_stack[depth].erase();
- sgml::openTag(os, 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(os, 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(os, depth, false, "p");
- }
- sgml::openTag(os, depth, false, latexname);
-
- if (environment_stack.size() == depth + 1)
- environment_stack.push_back("!-- --");
- environment_stack[depth] = latexname;
- }
-
- if (style->latexparam() == "CDATA")
- os << "<![CDATA[";
-
- if (style->latextype == LATEX_ENVIRONMENT) break;
-
- if (style->labeltype == LABEL_MANUAL)
- item_name = "tag";
- else
- item_name = "item";
-
- sgml::openTag(os, depth + 1, false, item_name);
- }
- break;
-
- default:
- sgml::openTag(os, depth, false, style->latexname());
- break;
- }
-
- pit->simpleLinuxDocOnePar(buf, os, outerFont(pit, paragraphs), depth);
-
- os << "\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")
- os << "]]>";
- break;
- default:
- sgml::closeTag(os, depth, false, style->latexname());
- break;
- }
- }
-
- // Close open tags
- for (int i = depth; i >= 0; --i)
- sgml::closeTag(os, depth, false, environment_stack[i]);
-}
-
-
-void docbookParagraphs(Buffer const & buf,
- ParagraphList const & paragraphs,
- ostream & os)
-{
- vector<string> environment_stack(10);
- vector<string> environment_inner(10);
- vector<string> command_stack(10);
-
- bool command_flag = false;
- Paragraph::depth_type command_depth = 0;
- Paragraph::depth_type command_base = 0;
- Paragraph::depth_type cmd_depth = 0;
- Paragraph::depth_type depth = 0; // paragraph depth
-
- string item_name;
- string command_name;
-
- ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
- ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
-
- for (; par != pend; ++par) {
- string sgmlparam;
- string c_depth;
- string c_params;
- int desc_on = 0; // description mode
-
- LyXLayout_ptr const & style = par->layout();
-
- // environment tag closing
- for (; depth > par->params().depth(); --depth) {
- if (!environment_inner[depth].empty())
- sgml::closeEnvTags(os, false, environment_inner[depth],
- command_depth + depth);
- sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
- environment_stack[depth].erase();
- environment_inner[depth].erase();
- }
-
- if (depth == par->params().depth()
- && environment_stack[depth] != style->latexname()
- && !environment_stack[depth].empty()) {
- sgml::closeEnvTags(os, false, environment_inner[depth],
- command_depth + depth);
- sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
-
- environment_stack[depth].erase();
- environment_inner[depth].erase();
- }
-
- // Write opening SGML tags.
- switch (style->latextype) {
- case LATEX_PARAGRAPH:
- sgml::openTag(os, depth + command_depth,
- false, style->latexname());
- break;
-
- case LATEX_COMMAND:
- if (depth != 0)
- //error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
- ;
-
- command_name = style->latexname();
-
- sgmlparam = style->latexparam();
- c_params = split(sgmlparam, c_depth,'|');
-
- cmd_depth = atoi(c_depth);
-
- if (command_flag) {
- if (cmd_depth < command_base) {
- for (Paragraph::depth_type j = command_depth;
- j >= command_base; --j) {
- sgml::closeTag(os, j, false, command_stack[j]);
- os << endl;
- }
- command_depth = command_base = cmd_depth;
- } else if (cmd_depth <= command_depth) {
- for (int j = command_depth;
- j >= int(cmd_depth); --j) {
- sgml::closeTag(os, j, false, command_stack[j]);
- os << endl;
- }
- command_depth = cmd_depth;
- } else
- command_depth = cmd_depth;
- } else {
- command_depth = command_base = cmd_depth;
- command_flag = true;
- }
- if (command_stack.size() == command_depth + 1)
- command_stack.push_back(string());
- command_stack[command_depth] = command_name;
-
- // treat label as a special case for
- // more WYSIWYM handling.
- // This is a hack while paragraphs can't have
- // attributes, like id in this case.
- if (par->isInset(0)) {
- InsetOld * inset = par->getInset(0);
- InsetOld::Code lyx_code = inset->lyxCode();
- if (lyx_code == InsetOld::LABEL_CODE) {
- command_name += " id=\"";
- command_name += (static_cast<InsetCommand *>(inset))->getContents();
- command_name += '"';
- desc_on = 3;
- }
- }
-
- sgml::openTag(os, depth + command_depth, false, command_name);
-
- item_name = c_params.empty() ? "title" : c_params;
- sgml::openTag(os, depth + 1 + command_depth, false, item_name);
- break;
-
- case LATEX_ENVIRONMENT:
- case LATEX_ITEM_ENVIRONMENT:
- if (depth < par->params().depth()) {
- depth = par->params().depth();
- environment_stack[depth].erase();
- }
-
- if (environment_stack[depth] != style->latexname()) {
- if (environment_stack.size() == depth + 1) {
- environment_stack.push_back("!-- --");
- environment_inner.push_back("!-- --");
- }
- environment_stack[depth] = style->latexname();
- environment_inner[depth] = "!-- --";
- sgml::openTag(os, depth + command_depth, false, environment_stack[depth]);
- } else {
- sgml::closeEnvTags(os, false, environment_inner[depth],
- command_depth + depth);
- }
-
- if (style->latextype == LATEX_ENVIRONMENT) {
- if (!style->latexparam().empty()) {
- if (style->latexparam() == "CDATA")
- os << "<![CDATA[";
- else
- sgml::openTag(os, depth + command_depth, false, style->latexparam());
- }
- break;
- }
-
- desc_on = (style->labeltype == LABEL_MANUAL);
-
- environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
- sgml::openTag(os, depth + 1 + command_depth,
- false, environment_inner[depth]);
-
- item_name = desc_on ? "term" : "para";
- sgml::openTag(os, depth + 1 + command_depth,
- false, item_name);
- break;
- default:
- sgml::openTag(os, depth + command_depth,
- false, style->latexname());
- break;
- }
-
- par->simpleDocBookOnePar(buf, os, outerFont(par, paragraphs), desc_on,
- depth + 1 + command_depth);
-
- string end_tag;
- // write closing SGML tags
- switch (style->latextype) {
- case LATEX_COMMAND:
- end_tag = c_params.empty() ? "title" : c_params;
- sgml::closeTag(os, depth + command_depth,
- false, end_tag);
- break;
- case LATEX_ENVIRONMENT:
- if (!style->latexparam().empty()) {
- if (style->latexparam() == "CDATA")
- os << "]]>";
- else
- sgml::closeTag(os, depth + command_depth, false, style->latexparam());
- }
- break;
- case LATEX_ITEM_ENVIRONMENT:
- if (desc_on == 1) break;
- end_tag = "para";
- sgml::closeTag(os, depth + 1 + command_depth, false, end_tag);
- break;
- case LATEX_PARAGRAPH:
- sgml::closeTag(os, depth + command_depth, false, style->latexname());
- break;
- default:
- sgml::closeTag(os, depth + command_depth, false, style->latexname());
- break;
- }
- }
-
- // Close open tags
- for (int d = depth; d >= 0; --d) {
- if (!environment_stack[depth].empty()) {
- sgml::closeEnvTags(os, false, environment_inner[depth],
- command_depth + depth);
- }
- }
-
- for (int j = command_depth; j >= 0 ; --j)
- if (!command_stack[j].empty()) {
- sgml::closeTag(os, j, false, command_stack[j]);
- os << endl;
- }
-}
-
-namespace {
-
-int readParToken(Buffer & buf, Paragraph & par, LyXLex & lex, string const & token)
-{
- static LyXFont font;
- static Change change;
-
- BufferParams const & bp = buf.params();
-
- if (token[0] != '\\') {
- string::const_iterator cit = token.begin();
- for (; cit != token.end(); ++cit) {
- par.insertChar(par.size(), (*cit), font, change);
- }
- } else if (token == "\\begin_layout") {
- lex.eatLine();
- string layoutname = lex.getString();
-
- font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
- change = Change();
-
- LyXTextClass const & tclass = bp.getLyXTextClass();
-
- if (layoutname.empty()) {
- layoutname = tclass.defaultLayoutName();
- }
-
- bool hasLayout = tclass.hasLayout(layoutname);
-
- if (!hasLayout) {
- lyxerr << "Layout '" << layoutname << "' does not"
- << " exist in textclass '" << tclass.name()
- << "'." << endl;
- lyxerr << "Trying to use default layout instead."
- << endl;
- layoutname = tclass.defaultLayoutName();
- }
-
- par.layout(bp.getLyXTextClass()[layoutname]);
-
- // Test whether the layout is obsolete.
- LyXLayout_ptr const & layout = par.layout();
- if (!layout->obsoleted_by().empty())
- par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
-
- par.params().read(lex);
-
- } else if (token == "\\end_layout") {
- lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
- << "Missing \\begin_layout?.\n";
- } else if (token == "\\end_inset") {
- lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
- << "Missing \\begin_inset?.\n";
- } else if (token == "\\begin_inset") {
- InsetOld * inset = readInset(lex, buf);
- if (inset)
- par.insertInset(par.size(), inset, font, change);
- else {
- lex.eatLine();
- string line = lex.getString();
- buf.error(ErrorItem(_("Unknown Inset"), line,
- buf.paragraphs().back().id(),
- 0, par.size()));
- return 1;
- }
- } else if (token == "\\family") {
- lex.next();
- font.setLyXFamily(lex.getString());
- } else if (token == "\\series") {
- lex.next();
- font.setLyXSeries(lex.getString());
- } else if (token == "\\shape") {
- lex.next();
- font.setLyXShape(lex.getString());
- } else if (token == "\\size") {
- lex.next();
- font.setLyXSize(lex.getString());
- } else if (token == "\\lang") {
- lex.next();
- string const tok = lex.getString();
- Language const * lang = languages.getLanguage(tok);
- if (lang) {
- font.setLanguage(lang);
- } else {
- font.setLanguage(bp.language);
- lex.printError("Unknown language `$$Token'");
- }
- } else if (token == "\\numeric") {
- lex.next();
- font.setNumber(font.setLyXMisc(lex.getString()));
- } else if (token == "\\emph") {
- lex.next();
- font.setEmph(font.setLyXMisc(lex.getString()));
- } else if (token == "\\bar") {
- lex.next();
- string const tok = lex.getString();
-
- if (tok == "under")
- font.setUnderbar(LyXFont::ON);
- else if (tok == "no")
- font.setUnderbar(LyXFont::OFF);
- else if (tok == "default")
- font.setUnderbar(LyXFont::INHERIT);
- else
- lex.printError("Unknown bar font flag "
- "`$$Token'");
- } else if (token == "\\noun") {
- lex.next();
- font.setNoun(font.setLyXMisc(lex.getString()));
- } else if (token == "\\color") {
- lex.next();
- font.setLyXColor(lex.getString());
- } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
-
- // Insets don't make sense in a free-spacing context! ---Kayvan
- if (par.isFreeSpacing()) {
- if (token == "\\InsetSpace")
- par.insertChar(par.size(), ' ', font, change);
- else if (lex.isOK()) {
- lex.next();
- string const next_token = lex.getString();
- if (next_token == "\\-")
- par.insertChar(par.size(), '-', font, change);
- else {
- lex.printError("Token `$$Token' "
- "is in free space "
- "paragraph layout!");
- }
- }
- } else {
- InsetOld * inset = 0;
- if (token == "\\SpecialChar" )
- inset = new InsetSpecialChar;
- else
- inset = new InsetSpace;
- inset->read(buf, lex);
- par.insertInset(par.size(), inset, font, change);
- }
- } else if (token == "\\i") {
- InsetOld * inset = new InsetLatexAccent;
- inset->read(buf, lex);
- par.insertInset(par.size(), inset, font, change);
- } else if (token == "\\backslash") {
- par.insertChar(par.size(), '\\', font, change);
- } else if (token == "\\newline") {
- InsetOld * inset = new InsetNewline;
- inset->read(buf, lex);
- par.insertInset(par.size(), inset, font, change);
- } else if (token == "\\LyXTable") {
- InsetOld * inset = new InsetTabular(buf);
- inset->read(buf, lex);
- par.insertInset(par.size(), inset, font, change);
- } else if (token == "\\bibitem") {
- InsetCommandParams p("bibitem", "dummy");
- InsetBibitem * inset = new InsetBibitem(p);
- inset->read(buf, lex);
- par.insertInset(par.size(), inset, font, change);
- } else if (token == "\\hfill") {
- par.insertInset(par.size(), new InsetHFill, font, change);
- } else if (token == "\\lyxline") {
- par.insertInset(par.size(), new InsetLine, font, change);
- } else if (token == "\\newpage") {
- par.insertInset(par.size(), new InsetPagebreak, font, change);
- } else if (token == "\\change_unchanged") {
- // Hack ! Needed for empty paragraphs :/
- // FIXME: is it still ??
- if (!par.size())
- par.cleanChanges();
- change = Change(Change::UNCHANGED);
- } else if (token == "\\change_inserted") {
- lex.nextToken();
- istringstream is(lex.getString());
- int aid;
- lyx::time_type ct;
- is >> aid >> ct;
- change = Change(Change::INSERTED, bp.author_map[aid], ct);
- } else if (token == "\\change_deleted") {
- lex.nextToken();
- istringstream is(lex.getString());
- int aid;
- lyx::time_type ct;
- is >> aid >> ct;
- change = Change(Change::DELETED, bp.author_map[aid], ct);
- } else {
- lex.eatLine();
- string const s = bformat(_("Unknown token: %1$s %2$s\n"),
- token, lex.getString());
-
- buf.error(ErrorItem(_("Unknown token"), s,
- buf.paragraphs().back().id(),
- 0, par.size()));
- return 1;
- }
- return 0;
-}
-
-}
-
-
-int readParagraph(Buffer & buf, Paragraph & par, LyXLex & lex)
-{
- int unknown = 0;
-
- lex.nextToken();
- string token = lex.getString();
-
- while (lex.isOK()) {
-
- unknown += readParToken(buf, par, lex, token);
-
- lex.nextToken();
- token = lex.getString();
-
- if (token.empty())
- continue;
-
- if (token == "\\end_layout") {
- //Ok, paragraph finished
- break;
- }
-
- lyxerr[Debug::PARSER] << "Handling paragraph token: `"
- << token << '\'' << endl;
- if (token == "\\begin_layout" || token == "\\end_document"
- || token == "\\end_inset" || token == "\\begin_deeper"
- || token == "\\end_deeper") {
- lex.pushToken(token);
- lyxerr << "Paragraph ended in line "
- << lex.getLineNo() << "\n"
- << "Missing \\end_layout.\n";
- break;
- }
- }
-
- return unknown;
-}
-
-
-LyXFont const outerFont(ParagraphList::iterator pit,
- ParagraphList const & plist)
-{
- Paragraph::depth_type par_depth = pit->getDepth();