4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * Full author contact details are available in file CREDITS.
15 #include "DocumentClassPtr.h"
16 #include "FloatList.h"
18 #include "LayoutEnums.h"
19 #include "LayoutModuleList.h"
21 #include "insets/InsetLayout.h"
23 #include "support/docstring.h"
24 #include "support/types.h"
38 namespace support { class FileName; }
45 /// Based upon ideas in boost::noncopyable, inheriting from this
46 /// class effectively makes the copy constructor protected but the
47 /// assignment constructor private.
53 ProtectCopy(const ProtectCopy &) {}
55 const ProtectCopy & operator=(const ProtectCopy &);
59 /// A TextClass represents a collection of layout information: At the
60 /// moment, this includes Layout's and InsetLayout's.
62 /// There are two major subclasses of TextClass: LayoutFile and
63 /// DocumentClass. These subclasses are what are actually used in LyX.
64 /// Simple TextClass objects are not directly constructed in the main
65 /// LyX code---the constructor is protected. (That said, in tex2lyx
66 /// there are what amount to simple TextClass objects.)
68 /// A LayoutFile (see LayoutFile.{h,cpp}) represents a *.layout file.
69 /// These are generally static objects---though they can be reloaded
70 /// from disk via LFUN_LAYOUT_RELOAD, so one should not assume that
71 /// they will never change.
73 /// A DocumentClass (see below) represents the layout information that
74 /// is associated with a given Buffer. These are static, in the sense
75 /// that they will not themselves change, but which DocumentClass is
76 /// associated with a Buffer can change, as modules are loaded and
77 /// unloaded, for example.
79 class TextClass : protected ProtectCopy {
82 virtual ~TextClass() {}
83 ///////////////////////////////////////////////////////////////////
85 ///////////////////////////////////////////////////////////////////
86 // NOTE Do NOT try to make this a container of Layout pointers, e.g.,
87 // std::list<Layout *>. This will lead to problems. The reason is
88 // that DocumentClass objects are generally created by copying a
89 // LayoutFile, which serves as a base for the DocumentClass. If the
90 // LayoutList is a container of pointers, then every DocumentClass
91 // that derives from a given LayoutFile (e.g., article) will SHARE
92 // a basic set of layouts. So if one Buffer were to modify a layout
93 // (say, Standard), that would modify that layout for EVERY Buffer
94 // that was based upon the same DocumentClass.
96 // NOTE: Layout pointers are directly assigned to paragraphs so a
97 // container that does not invalidate these pointers after insertion
99 /// The individual paragraph layouts comprising the document class
100 typedef std::list<Layout> LayoutList;
101 /// The inset layouts available to this class
102 typedef std::map<docstring, InsetLayout> InsetLayouts;
104 typedef LayoutList::const_iterator const_iterator;
106 ///////////////////////////////////////////////////////////////////
108 ///////////////////////////////////////////////////////////////////
110 const_iterator begin() const { return layoutlist_.begin(); }
112 const_iterator end() const { return layoutlist_.end(); }
115 ///////////////////////////////////////////////////////////////////
117 ///////////////////////////////////////////////////////////////////
119 Layout const & defaultLayout() const;
121 docstring const & defaultLayoutName() const;
123 bool isDefaultLayout(Layout const &) const;
125 bool isPlainLayout(Layout const &) const;
126 /// returns a special layout for use when we don't really want one,
127 /// e.g., in table cells
128 Layout const & plainLayout() const
129 { return operator[](plain_layout_); }
130 /// the name of the plain layout
131 docstring const & plainLayoutName() const
132 { return plain_layout_; }
133 /// Enumerate the paragraph styles.
134 size_t layoutCount() const { return layoutlist_.size(); }
136 bool hasLayout(docstring const & name) const;
138 bool hasInsetLayout(docstring const & name) const;
140 Layout const & operator[](docstring const & vname) const;
141 /// Inset layouts of this doc class
142 InsetLayouts const & insetLayouts() const { return insetlayoutlist_; }
144 ///////////////////////////////////////////////////////////////////
146 ///////////////////////////////////////////////////////////////////
147 /// Enum used with TextClass::read
149 BASECLASS, //>This is a base class, i.e., top-level layout file
150 MERGE, //>This is a file included in a layout file
151 MODULE, //>This is a layout module
152 CITE_ENGINE, //>This is a cite engine
153 VALIDATION //>We're just validating
155 /// return values for read()
163 /// Performs the read of the layout file.
164 /// \return true on success.
165 // FIXME Should return ReturnValues....
166 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
168 ReturnValues read(std::string const & str, ReadType rt = MODULE);
170 ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
171 /// validates the layout information passed in str
172 static ReturnValues validate(std::string const & str);
173 /// \return the conversion of \param str to the latest layout format
174 /// compatible with the lyx format.
175 static std::string convert(std::string const & str);
177 ///////////////////////////////////////////////////////////////////
179 ///////////////////////////////////////////////////////////////////
180 /// Sees to it the textclass structure has been loaded
181 /// This function will search for $classname.layout in default directories
182 /// and an optional path, but if path points to a file, it will be loaded
184 bool load(std::string const & path = std::string()) const;
185 /// Has this layout file been loaded yet?
186 /// Overridden by DocumentClass
187 virtual bool loaded() const { return loaded_; }
189 ///////////////////////////////////////////////////////////////////
191 ///////////////////////////////////////////////////////////////////
193 std::string const & name() const { return name_; }
195 std::string const & path() const { return path_; }
197 std::string const & category() const { return category_; }
199 std::string const & description() const { return description_; }
201 std::string const & latexname() const { return latexname_; }
203 std::string const & prerequisites(std::string const & sep = "\n\t") const;
204 /// Can be LaTeX, DocBook, etc.
205 OutputType outputType() const { return outputType_; }
206 /// Can be latex, docbook ... (the name of a format)
207 std::string outputFormat() const { return outputFormat_; }
208 /// Does this class redefine the output format?
209 bool hasOutputFormat() const { return has_output_format_; }
210 /// Return the non-localised names for the toc types.
211 std::map<std::string, docstring> const &
212 outlinerNames() const { return outliner_names_; }
213 /// \returns Layout named \p name if it exists, otherwise 0
214 Layout const * getLayout(docstring const & name) const;
215 /// \returns Layout named \p name if it exists, otherwise 0
216 Layout * getLayout(docstring const & name);
219 /// Protect construction
222 Layout & operator[](docstring const & name);
223 /** Create an new, very basic layout for this textclass. This is used for
224 the Plain Layout common to all TextClass objects and also, in
225 DocumentClass, for the creation of new layouts `on the fly' when
226 previously unknown layouts are encountered.
227 \param unknown Set to true if this layout is used to represent an
230 Layout createBasicLayout(docstring const & name, bool unknown = false) const;
232 ///////////////////////////////////////////////////////////////////
233 // non-const iterators
234 ///////////////////////////////////////////////////////////////////
236 typedef LayoutList::iterator iterator;
238 iterator begin() { return layoutlist_.begin(); }
240 iterator end() { return layoutlist_.end(); }
242 ///////////////////////////////////////////////////////////////////
244 ///////////////////////////////////////////////////////////////////
245 /// Paragraph styles used in this layout
246 /// This variable is mutable because unknown layouts can be added
247 /// to const textclass.
248 mutable LayoutList layoutlist_;
251 /// Layout file path (empty for system layout files)
254 std::string category_;
255 /// document class name
256 std::string latexname_;
257 /// document class description
258 std::string description_;
259 /// available types of float, eg. figure, algorithm.
260 mutable FloatList floatlist_;
261 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
262 mutable Counters counters_;
263 /// Has this layout file been loaded yet?
264 mutable bool loaded_;
265 /// Is the TeX class available?
266 bool tex_class_avail_;
267 /// document class prerequisites
268 mutable std::string prerequisites_;
269 /// The possible cite engine types
270 std::string opt_enginetype_;
271 /// The cite framework (bibtex, biblatex)
272 std::string citeframework_;
274 std::string opt_fontsize_;
276 std::string opt_pagesize_;
278 std::string opt_pagestyle_;
279 /// Specific class options
280 std::string options_;
281 /// Format of the fontsize option
282 std::string fontsize_format_;
283 /// Default page size
284 std::string pagesize_;
285 /// Format of the papersize option
286 std::string pagesize_format_;
288 std::string pagestyle_;
290 std::string tablestyle_;
292 std::string class_header_;
294 docstring defaultlayout_;
295 /// name of plain layout
296 static const docstring plain_layout_;
297 /// preamble text to support layout styles
299 /// same, but for HTML output
300 /// this is output as is to the header
301 docstring htmlpreamble_;
302 /// same, but specifically for CSS information
303 docstring htmlstyles_;
304 /// the paragraph style to use for TOCs, bibliography, etc.
305 mutable docstring html_toc_section_;
306 /// root element when exporting as DocBook
307 std::string docbookroot_;
308 /// whether this root element does not accept text without a section (i.e. the first text that is met in LyX must
309 /// be considered as the abstract if this is true); this text must be output within <info> and <abstract>
310 bool docbookforceabstract_;
311 /// latex packages loaded by document class.
312 std::set<std::string> provides_;
313 /// latex packages requested by document class.
314 std::set<std::string> required_;
316 std::map<std::string, std::string> package_options_;
317 /// default modules wanted by document class
318 LayoutModuleList default_modules_;
319 /// modules provided by document class
320 LayoutModuleList provided_modules_;
321 /// modules excluded by document class
322 LayoutModuleList excluded_modules_;
324 unsigned int columns_;
327 /// header depth to have numbering
329 /// header depth to appear in table of contents
331 /// Can be LaTeX, DocBook, etc.
332 OutputType outputType_;
333 /// Can be latex, docbook ... (the name of a format)
334 std::string outputFormat_;
335 /// Does this class redefine the output format?
336 bool has_output_format_;
337 /** Base font. The paragraph and layout fonts are resolved against
338 this font. This has to be fully instantiated. Attributes
339 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
342 FontInfo defaultfont_;
343 /// Text that dictates how wide the left margin is on the screen
344 docstring leftmargin_;
345 /// Text that dictates how wide the right margin is on the screen
346 docstring rightmargin_;
347 /// The type of command used to produce a title
348 TitleLatexType titletype_;
349 /// The name of the title command
350 std::string titlename_;
351 /// Input layouts available to this layout
352 InsetLayouts insetlayoutlist_;
353 /// The minimal TocLevel of sectioning layouts
355 /// The maximal TocLevel of sectioning layouts
357 /// Citation formatting information
358 std::map<CiteEngineType, std::map<std::string, std::string> > cite_formats_;
360 std::map<CiteEngineType, std::map<std::string, std::string> > cite_macros_;
361 /// The default BibTeX bibliography style file
362 std::map<std::string, std::string> cite_default_biblio_style_;
363 /// Citation command aliases
364 std::map<std::string, std::string> cite_command_aliases_;
365 /// The maximum number of citations before "et al."
366 size_t maxcitenames_;
367 /// Whether full author lists are supported
368 bool cite_full_author_list_;
369 /// The possible citation styles
370 std::map<CiteEngineType, std::vector<CitationStyle> > cite_styles_;
371 /// Class-added citation styles
372 std::map<CiteEngineType, std::vector<CitationStyle> > class_cite_styles_;
374 std::map<std::string, docstring> outliner_names_;
375 /// Does this class put the bibliography to toc by itself?
378 ///////////////////////////////////////////////////////////////////
379 // helper routines for reading layout files
380 ///////////////////////////////////////////////////////////////////
382 bool deleteLayout(docstring const &);
384 bool deleteInsetLayout(docstring const &);
386 bool convertLayoutFormat(support::FileName const &, ReadType);
387 /// Reads the layout file without running layout2layout.
388 ReturnValues readWithoutConv(support::FileName const & filename, ReadType rt);
389 /// \return true for success.
390 bool readStyle(Lexer &, Layout &, ReadType) const;
392 void readOutputType(Lexer &);
394 void readTitleType(Lexer &);
396 void readMaxCounter(Lexer &);
398 void readClassOptions(Lexer &);
400 void readCharStyle(Lexer &, std::string const &);
402 bool readFloat(Lexer &);
404 std::vector<CitationStyle> const & getCiteStyles(CiteEngineType const &) const;
406 bool readCiteEngine(Lexer &, ReadType, bool const add = false);
408 int readCiteEngineType(Lexer &) const;
410 bool readCiteFormat(Lexer &, ReadType);
412 bool readOutlinerName(Lexer &);
416 /// A DocumentClass represents the layout information associated with a
417 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
420 /// In that regard, DocumentClass objects are "dynamic". But this is really
421 /// an illusion, since DocumentClass objects are not (currently) changed
422 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
423 /// discarded---actually, it will be kept around if something on the cut
424 /// stack needs it---and a new one is created from scratch.
425 class DocumentClass : public TextClass {
428 virtual ~DocumentClass() {}
430 ///////////////////////////////////////////////////////////////////
432 ///////////////////////////////////////////////////////////////////
433 /// \return true if there is a Layout with latexname lay
434 bool hasLaTeXLayout(std::string const & lay) const;
435 /// A DocumentClass nevers count as loaded, since it is dynamic
436 bool loaded() const override { return false; }
437 /// \return the layout object of an inset given by name. If the name
438 /// is not found as such, the part after the ':' is stripped off, and
439 /// searched again. In this way, an error fallback can be provided:
440 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
441 /// will invoke the layout object defined by name = 'CharStyle'.
442 /// If that doesn't work either, an empty object returns (shouldn't
443 /// happen). -- Idea JMarc, comment MV
444 InsetLayout const & insetLayout(docstring const & name) const;
445 /// a plain inset layout for use as a default
446 static InsetLayout const & plainInsetLayout();
447 /// add a new layout \c name if it does not exist in layoutlist_
448 /// \return whether we had to add one.
449 bool addLayoutIfNeeded(docstring const & name) const;
450 /// Forced layouts in layout file syntax
451 std::string forcedLayouts() const;
453 ///////////////////////////////////////////////////////////////////
455 ///////////////////////////////////////////////////////////////////
456 /// the list of floats defined in the document class
457 FloatList const & floats() const { return floatlist_; }
459 Counters & counters() const { return counters_; }
461 std::string const & opt_enginetype() const { return opt_enginetype_; }
463 std::string const & citeFramework() const { return citeframework_; }
465 std::string const & opt_fontsize() const { return opt_fontsize_; }
467 std::string const & opt_pagesize() const { return opt_pagesize_; }
469 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
471 std::string const & options() const { return options_; }
473 std::string const & class_header() const { return class_header_; }
475 std::string const & fontsizeformat() const { return fontsize_format_; }
477 std::string const & pagesize() const { return pagesize_; }
479 std::string const & pagesizeformat() const { return pagesize_format_; }
481 std::string const & pagestyle() const { return pagestyle_; }
483 std::string const & tablestyle() const { return tablestyle_; }
485 docstring const & preamble() const { return preamble_; }
487 docstring const & htmlpreamble() const { return htmlpreamble_; }
489 docstring const & htmlstyles() const { return htmlstyles_; }
491 bool docbookforceabstract() const { return docbookforceabstract_; }
493 std::string const & docbookroot() const { return docbookroot_; }
494 /// Looks for the layout of "highest level", other than Part (or other
495 /// layouts with a negative toc number), for use in constructing TOCs and
496 /// similar information.
497 Layout const & getTOCLayout() const;
498 /// the paragraph style to use for TOCs, Bibliography, etc
499 /// we will attempt to calculate this if it was not given
500 Layout const & htmlTOCLayout() const;
501 /// is this feature already provided by the class?
502 bool provides(std::string const & p) const;
503 /// features required by the class?
504 std::set<std::string> const & required() const { return required_; }
505 /// package options to write to LaTeX file
506 std::map<std::string, std::string> const & packageOptions() const
507 { return package_options_; }
509 unsigned int columns() const { return columns_; }
511 PageSides sides() const { return sides_; }
513 int secnumdepth() const { return secnumdepth_; }
515 int tocdepth() const { return tocdepth_; }
517 FontInfo const & defaultfont() const { return defaultfont_; }
518 /// Text that dictates how wide the left margin is on the screen
519 docstring const & leftmargin() const { return leftmargin_; }
520 /// Text that dictates how wide the right margin is on the screen
521 docstring const & rightmargin() const { return rightmargin_; }
522 /// The type of command used to produce a title
523 TitleLatexType titletype() const { return titletype_; }
524 /// The name of the title command
525 std::string const & titlename() const { return titlename_; }
527 int size() const { return layoutlist_.size(); }
528 /// The minimal TocLevel of sectioning layouts
529 int min_toclevel() const { return min_toclevel_; }
530 /// The maximal TocLevel of sectioning layouts
531 int max_toclevel() const { return max_toclevel_; }
532 /// returns true if the class has a ToC structure
533 bool hasTocLevels() const;
535 std::string const getCiteFormat(CiteEngineType const & type,
536 std::string const & entry, bool const punct = true,
537 std::string const & fallback = "") const;
539 std::string const & getCiteMacro(CiteEngineType const & type,
540 std::string const & macro) const;
542 std::vector<std::string> const citeCommands(CiteEngineType const &) const;
544 std::vector<CitationStyle> const & citeStyles(CiteEngineType const &) const;
546 std::map<std::string, std::string> const & defaultBiblioStyle() const
547 { return cite_default_biblio_style_; }
549 std::map<std::string, std::string> const & citeCommandAliases() const
550 { return cite_command_aliases_; }
551 /// The maximum number of citations before "et al."
552 size_t max_citenames() const { return maxcitenames_; }
554 bool fullAuthorList() const { return cite_full_author_list_; }
556 bool bibInToc() const { return bibintoc_; }
558 /// Constructs a DocumentClass based upon a LayoutFile.
559 DocumentClass(LayoutFile const & tc);
560 /// Needed in tex2lyx
563 /// The only way to make a DocumentClass is to call this function.
564 friend DocumentClassPtr
565 getDocumentClass(LayoutFile const &, LayoutModuleList const &,
571 /// The only way to make a DocumentClass is to call this function.
572 /// The shared_ptr is needed because DocumentClass objects can be kept
573 /// in memory long after their associated Buffer is destroyed, mostly
575 DocumentClassPtr getDocumentClass(LayoutFile const & baseClass,
576 LayoutModuleList const & modlist,
577 std::string const & cengine = std::string(),
578 bool const clone = false);
580 /// convert page sides option to text 1 or 2
581 std::ostream & operator<<(std::ostream & os, PageSides p);
583 /// current format of layout files
584 extern int const LAYOUT_FORMAT;
585 /// layout format for the current lyx file format (usually equal to
587 extern int const LYXFILE_LAYOUT_FORMAT;