2009-06-05 17:49:10 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
|
|
|
* \file output_xhtml.h
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Richard Heck
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OUTPUT_XHTML_H
|
|
|
|
#define OUTPUT_XHTML_H
|
|
|
|
|
2010-09-15 14:06:36 +00:00
|
|
|
#include "LayoutEnums.h"
|
2013-05-07 04:19:34 +00:00
|
|
|
|
2009-11-19 16:29:57 +00:00
|
|
|
#include "support/docstream.h"
|
2013-05-07 04:19:34 +00:00
|
|
|
#include "support/shared_ptr.h"
|
2009-11-25 22:18:47 +00:00
|
|
|
#include "support/strfwd.h"
|
2009-11-19 16:29:57 +00:00
|
|
|
|
|
|
|
#include <deque>
|
2009-06-05 17:49:10 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
class Buffer;
|
|
|
|
class OutputParams;
|
2009-08-09 18:35:39 +00:00
|
|
|
class Text;
|
2009-06-05 17:49:10 +00:00
|
|
|
|
2009-11-19 18:28:36 +00:00
|
|
|
// Inspiration for the *Tag structs and for XHTMLStream
|
|
|
|
// came from MathStream and its cousins.
|
|
|
|
|
2010-01-19 22:08:04 +00:00
|
|
|
namespace html {
|
2013-05-07 06:30:26 +00:00
|
|
|
|
2013-05-14 11:56:31 +00:00
|
|
|
struct FontTag;
|
|
|
|
struct EndFontTag;
|
2013-05-07 06:34:15 +00:00
|
|
|
|
2009-11-25 22:18:47 +00:00
|
|
|
/// Attributes will be escaped automatically and so should NOT
|
2010-01-19 22:24:46 +00:00
|
|
|
/// be escaped before being passed to the constructor.
|
2013-05-07 05:16:14 +00:00
|
|
|
struct StartTag
|
|
|
|
{
|
2009-11-19 18:24:19 +00:00
|
|
|
///
|
2010-07-02 11:04:36 +00:00
|
|
|
explicit StartTag(std::string const & tag) : tag_(tag), keepempty_(false) {}
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
2009-11-28 21:37:47 +00:00
|
|
|
explicit StartTag(std::string const & tag, std::string const & attr,
|
2009-11-19 16:29:57 +00:00
|
|
|
bool keepempty = false)
|
|
|
|
: tag_(tag), attr_(attr), keepempty_(keepempty) {}
|
2013-05-07 05:16:14 +00:00
|
|
|
///
|
2014-04-30 21:12:41 +00:00
|
|
|
virtual ~StartTag() {}
|
2009-11-19 17:51:06 +00:00
|
|
|
/// <tag_ attr_>
|
2013-05-07 06:34:15 +00:00
|
|
|
virtual docstring writeTag() const;
|
2009-11-19 17:51:06 +00:00
|
|
|
/// </tag_>
|
2013-05-07 06:34:15 +00:00
|
|
|
virtual docstring writeEndTag() const;
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
virtual FontTag const * asFontTag() const { return 0; }
|
|
|
|
///
|
|
|
|
virtual bool operator==(StartTag const & rhs) const
|
2013-05-07 06:30:26 +00:00
|
|
|
{ return tag_ == rhs.tag_; }
|
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
virtual bool operator!=(StartTag const & rhs) const
|
2013-05-07 06:30:26 +00:00
|
|
|
{ return !(*this == rhs); }
|
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
virtual bool operator==(FontTag const & rhs) const;
|
|
|
|
///
|
2009-11-19 16:29:57 +00:00
|
|
|
std::string tag_;
|
|
|
|
///
|
|
|
|
std::string attr_;
|
|
|
|
/// whether to keep things like "<tag></tag>" or discard them
|
|
|
|
/// you would want this for td, e.g, but maybe not for a div
|
|
|
|
bool keepempty_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-07 06:34:15 +00:00
|
|
|
///
|
|
|
|
struct EndTag
|
|
|
|
{
|
|
|
|
///
|
|
|
|
explicit EndTag(std::string tag) : tag_(tag) {}
|
2015-09-11 21:04:59 +00:00
|
|
|
///
|
|
|
|
virtual ~EndTag() {}
|
2013-05-07 06:34:15 +00:00
|
|
|
/// </tag_>
|
|
|
|
virtual docstring writeEndTag() const;
|
|
|
|
///
|
|
|
|
bool operator==(StartTag const & rhs) const
|
|
|
|
{ return tag_ == rhs.tag_; }
|
|
|
|
///
|
|
|
|
bool operator!=(StartTag const & rhs) const
|
|
|
|
{ return !(*this == rhs); }
|
|
|
|
///
|
|
|
|
virtual EndFontTag const * asFontTag() const { return 0; }
|
|
|
|
///
|
|
|
|
std::string tag_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Tags like <img />
|
|
|
|
/// Attributes will be escaped automatically and so should NOT
|
|
|
|
/// be escaped before being passed to the constructor.
|
|
|
|
struct CompTag
|
|
|
|
{
|
|
|
|
///
|
|
|
|
explicit CompTag(std::string const & tag)
|
|
|
|
: tag_(tag) {}
|
|
|
|
///
|
|
|
|
explicit CompTag(std::string const & tag, std::string const & attr)
|
|
|
|
: tag_(tag), attr_(attr) {}
|
|
|
|
/// <tag_ attr_ />
|
|
|
|
docstring writeTag() const;
|
|
|
|
///
|
|
|
|
std::string tag_;
|
|
|
|
///
|
|
|
|
std::string attr_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-07 05:16:14 +00:00
|
|
|
/// A special case of StartTag, used exclusively for tags that wrap paragraphs.
|
|
|
|
struct ParTag : public StartTag
|
|
|
|
{
|
|
|
|
///
|
2013-05-07 06:30:26 +00:00
|
|
|
explicit ParTag(std::string const & tag, std::string const & attr,
|
2013-05-07 05:16:14 +00:00
|
|
|
std::string const & parid)
|
|
|
|
: StartTag(tag, attr), parid_(parid)
|
|
|
|
{}
|
|
|
|
///
|
|
|
|
~ParTag() {}
|
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
docstring writeTag() const;
|
2013-05-07 05:16:14 +00:00
|
|
|
/// the "magic par label" for this paragraph
|
|
|
|
std::string parid_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-07 06:30:26 +00:00
|
|
|
///
|
|
|
|
enum FontTypes {
|
2013-05-10 20:58:38 +00:00
|
|
|
// ranges
|
2013-05-07 06:30:26 +00:00
|
|
|
FT_EMPH,
|
|
|
|
FT_NOUN,
|
|
|
|
FT_UBAR,
|
|
|
|
FT_DBAR,
|
|
|
|
FT_WAVE,
|
2013-05-10 20:58:38 +00:00
|
|
|
FT_SOUT,
|
|
|
|
// bold
|
|
|
|
FT_BOLD,
|
|
|
|
// shapes
|
|
|
|
FT_UPRIGHT,
|
2013-05-07 06:30:26 +00:00
|
|
|
FT_ITALIC,
|
|
|
|
FT_SLANTED,
|
|
|
|
FT_SMALLCAPS,
|
2013-05-10 20:58:38 +00:00
|
|
|
// families
|
2013-05-07 06:30:26 +00:00
|
|
|
FT_ROMAN,
|
|
|
|
FT_SANS,
|
2013-05-13 16:22:41 +00:00
|
|
|
FT_TYPE,
|
|
|
|
// sizes
|
|
|
|
FT_SIZE_TINY,
|
|
|
|
FT_SIZE_SCRIPT,
|
|
|
|
FT_SIZE_FOOTNOTE,
|
|
|
|
FT_SIZE_SMALL,
|
|
|
|
FT_SIZE_NORMAL,
|
|
|
|
FT_SIZE_LARGE,
|
|
|
|
FT_SIZE_LARGER,
|
|
|
|
FT_SIZE_LARGEST,
|
|
|
|
FT_SIZE_HUGE,
|
|
|
|
FT_SIZE_HUGER,
|
|
|
|
FT_SIZE_INCREASE,
|
|
|
|
FT_SIZE_DECREASE
|
2013-05-07 06:30:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
struct FontTag : public StartTag
|
|
|
|
{
|
|
|
|
///
|
|
|
|
explicit FontTag(FontTypes type);
|
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
FontTag const * asFontTag() const { return this; }
|
2013-05-07 06:30:26 +00:00
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
bool operator==(StartTag const &) const;
|
2013-05-07 06:30:26 +00:00
|
|
|
///
|
|
|
|
FontTypes font_type_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
struct EndFontTag : public EndTag
|
2013-05-07 05:16:14 +00:00
|
|
|
{
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
explicit EndFontTag(FontTypes type);
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
EndFontTag const * asFontTag() const { return this; }
|
2013-05-07 06:30:26 +00:00
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
FontTypes font_type_;
|
2009-11-19 16:29:57 +00:00
|
|
|
};
|
|
|
|
|
2013-05-07 04:19:34 +00:00
|
|
|
|
2011-04-01 19:18:25 +00:00
|
|
|
// trivial struct for output of newlines
|
|
|
|
struct CR{};
|
|
|
|
|
2010-01-19 22:24:46 +00:00
|
|
|
} // namespace html
|
2009-11-19 16:29:57 +00:00
|
|
|
|
|
|
|
class XHTMLStream {
|
|
|
|
public:
|
|
|
|
///
|
|
|
|
explicit XHTMLStream(odocstream & os);
|
2009-06-05 18:41:28 +00:00
|
|
|
///
|
2009-11-19 16:29:57 +00:00
|
|
|
odocstream & os() { return os_; }
|
|
|
|
///
|
|
|
|
// int & tab() { return tab_; }
|
|
|
|
/// closes any font tags that are eligible to be closed,
|
|
|
|
/// i.e., last on the tag_stack_.
|
|
|
|
/// \return false if there are open font tags we could not close.
|
|
|
|
/// because they are "blocked" by open non-font tags on the stack.
|
|
|
|
bool closeFontTags();
|
2011-04-03 01:56:20 +00:00
|
|
|
/// call at start of paragraph. sets a mark so we know what tags
|
|
|
|
/// to close at the end.
|
2011-06-20 15:55:41 +00:00
|
|
|
void startParagraph(bool keep_empty);
|
2011-04-03 01:56:20 +00:00
|
|
|
/// call at end of paragraph to clear that mark. note that this
|
|
|
|
/// will also close any tags still open.
|
|
|
|
void endParagraph();
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
|
|
|
XHTMLStream & operator<<(docstring const &);
|
|
|
|
///
|
2009-11-19 20:22:04 +00:00
|
|
|
XHTMLStream & operator<<(const char *);
|
|
|
|
///
|
2009-11-19 17:51:06 +00:00
|
|
|
XHTMLStream & operator<<(char_type);
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
2010-01-12 21:06:55 +00:00
|
|
|
XHTMLStream & operator<<(int);
|
|
|
|
///
|
2010-09-15 17:19:29 +00:00
|
|
|
XHTMLStream & operator<<(char);
|
|
|
|
///
|
2010-01-19 22:08:04 +00:00
|
|
|
XHTMLStream & operator<<(html::StartTag const &);
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
2010-01-19 22:08:04 +00:00
|
|
|
XHTMLStream & operator<<(html::EndTag const &);
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
2010-01-19 22:08:04 +00:00
|
|
|
XHTMLStream & operator<<(html::CompTag const &);
|
2009-11-19 23:29:07 +00:00
|
|
|
///
|
2013-05-07 05:16:14 +00:00
|
|
|
XHTMLStream & operator<<(html::ParTag const &);
|
|
|
|
///
|
2013-05-07 06:34:15 +00:00
|
|
|
XHTMLStream & operator<<(html::FontTag const &);
|
|
|
|
///
|
2011-04-01 19:18:25 +00:00
|
|
|
XHTMLStream & operator<<(html::CR const &);
|
|
|
|
///
|
2010-11-24 15:27:36 +00:00
|
|
|
enum EscapeSettings {
|
|
|
|
ESCAPE_NONE,
|
|
|
|
ESCAPE_AND, // meaning &
|
|
|
|
ESCAPE_ALL // meaning <, >, &, at present
|
|
|
|
};
|
|
|
|
/// Sets what we are going to escape on the NEXT write.
|
|
|
|
/// Everything is reset for the next time.
|
|
|
|
XHTMLStream & operator<<(EscapeSettings);
|
2012-04-08 13:59:57 +00:00
|
|
|
#if 0
|
|
|
|
/// This routine is for debugging the tag stack, etc. Code
|
|
|
|
/// for it is disabled by default, however, so you will need
|
|
|
|
/// to enable it if you want to use it.
|
|
|
|
void dumpTagStack(std::string const & msg) const;
|
|
|
|
#endif
|
2009-11-19 16:29:57 +00:00
|
|
|
private:
|
|
|
|
///
|
|
|
|
void clearTagDeque();
|
|
|
|
///
|
2013-05-07 06:30:26 +00:00
|
|
|
bool isTagOpen(html::StartTag const &) const;
|
|
|
|
///
|
|
|
|
bool isTagOpen(html::EndTag const &) const;
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
2013-05-07 06:30:26 +00:00
|
|
|
bool isTagPending(html::StartTag const &) const;
|
2012-04-08 14:02:40 +00:00
|
|
|
///
|
2012-04-08 13:59:57 +00:00
|
|
|
void writeError(std::string const &) const;
|
2009-11-19 21:50:02 +00:00
|
|
|
///
|
2009-11-19 16:29:57 +00:00
|
|
|
odocstream & os_;
|
2009-11-19 23:29:07 +00:00
|
|
|
///
|
2010-11-24 15:27:36 +00:00
|
|
|
EscapeSettings escape_;
|
2013-05-07 04:19:34 +00:00
|
|
|
// What we would really like to do here is simply use a
|
|
|
|
// deque<StartTag>. But we want to store both StartTags and
|
|
|
|
// sub-classes thereof on this stack, which means we run into the
|
|
|
|
// so-called polymorphic class problem with the STL. We therefore have
|
|
|
|
// to use a deque<StartTag *>, which leads to the question who will
|
|
|
|
// own these pointers and how they will be deleted, so we use shared
|
|
|
|
// pointers.
|
|
|
|
///
|
|
|
|
typedef shared_ptr<html::StartTag> TagPtr;
|
|
|
|
typedef std::deque<TagPtr> TagDeque;
|
|
|
|
///
|
|
|
|
template <typename T>
|
|
|
|
shared_ptr<T> makeTagPtr(T const & tag)
|
|
|
|
{ return shared_ptr<T>(new T(tag)); }
|
|
|
|
///
|
|
|
|
TagDeque pending_tags_;
|
|
|
|
///
|
|
|
|
TagDeque tag_stack_;
|
2009-11-19 16:29:57 +00:00
|
|
|
};
|
|
|
|
|
2009-11-19 16:35:58 +00:00
|
|
|
///
|
|
|
|
void xhtmlParagraphs(Text const & text,
|
|
|
|
Buffer const & buf,
|
2009-11-19 17:51:06 +00:00
|
|
|
XHTMLStream & xs,
|
2009-11-19 16:35:58 +00:00
|
|
|
OutputParams const & runparams);
|
2009-11-19 16:29:57 +00:00
|
|
|
|
2010-09-15 14:06:36 +00:00
|
|
|
/// \return a string appropriate for setting alignment in CSS
|
|
|
|
/// Does NOT return "justify" for "block"
|
|
|
|
std::string alignmentToCSS(LyXAlignment align);
|
|
|
|
|
2009-11-19 16:35:58 +00:00
|
|
|
namespace html {
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
2010-11-24 15:27:36 +00:00
|
|
|
docstring escapeChar(char_type c, XHTMLStream::EscapeSettings e);
|
2009-11-19 16:29:57 +00:00
|
|
|
/// converts a string to a form safe for links, etc
|
2010-11-24 15:27:36 +00:00
|
|
|
docstring htmlize(docstring const & str, XHTMLStream::EscapeSettings e);
|
2009-11-28 21:37:47 +00:00
|
|
|
/// cleans \param str for use as an atttribute by replacing
|
|
|
|
/// all non-alnum by "_"
|
|
|
|
docstring cleanAttr(docstring const & str);
|
2009-11-25 22:18:47 +00:00
|
|
|
///
|
2010-11-24 15:27:36 +00:00
|
|
|
std::string escapeChar(char c, XHTMLStream::EscapeSettings e);
|
2009-11-28 21:37:47 +00:00
|
|
|
///
|
2010-11-24 15:27:36 +00:00
|
|
|
std::string htmlize(std::string const & str, XHTMLStream::EscapeSettings e);
|
2009-11-28 21:37:47 +00:00
|
|
|
///
|
|
|
|
std::string cleanAttr(std::string const & str);
|
2009-11-19 17:51:06 +00:00
|
|
|
|
2010-01-19 22:24:46 +00:00
|
|
|
} // namespace html
|
2009-06-05 17:49:10 +00:00
|
|
|
} // namespace lyx
|
|
|
|
|
|
|
|
#endif
|