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"
|
2009-11-19 16:29:57 +00:00
|
|
|
#include "support/docstream.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 {
|
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.
|
2009-11-19 16:29:57 +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) {}
|
2009-11-19 17:51:06 +00:00
|
|
|
/// <tag_ attr_>
|
|
|
|
docstring asTag() const;
|
|
|
|
/// </tag_>
|
|
|
|
docstring asEndTag() 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_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct EndTag {
|
|
|
|
///
|
2009-11-28 21:37:47 +00:00
|
|
|
explicit EndTag(std::string tag) : tag_(tag) {}
|
2009-11-19 17:51:06 +00:00
|
|
|
/// </tag_>
|
|
|
|
docstring asEndTag() const;
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
|
|
|
std::string tag_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-01-12 21:06:55 +00:00
|
|
|
// FIXME XHTML
|
|
|
|
// We need to allow these to be deferrable, which means it should
|
|
|
|
// inherit from StartTag. This is probably better, anyway, but we'll
|
|
|
|
// need to re-work a bit of code....
|
2009-11-25 22:18:47 +00:00
|
|
|
/// Tags like <img />
|
|
|
|
/// 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.
|
2009-11-19 16:29:57 +00:00
|
|
|
struct CompTag {
|
|
|
|
///
|
2009-11-28 21:37:47 +00:00
|
|
|
explicit CompTag(std::string const & tag)
|
2009-11-19 23:04:10 +00:00
|
|
|
: tag_(tag) {}
|
|
|
|
///
|
2009-11-28 21:37:47 +00:00
|
|
|
explicit CompTag(std::string const & tag, std::string const & attr)
|
2009-11-19 16:29:57 +00:00
|
|
|
: tag_(tag), attr_(attr) {}
|
2009-11-19 17:51:06 +00:00
|
|
|
/// <tag_ attr_ />
|
|
|
|
docstring asTag() const;
|
2009-11-19 16:29:57 +00:00
|
|
|
///
|
|
|
|
std::string tag_;
|
|
|
|
///
|
|
|
|
std::string attr_;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
///
|
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);
|
2009-11-19 16:29:57 +00:00
|
|
|
private:
|
|
|
|
///
|
|
|
|
void clearTagDeque();
|
|
|
|
///
|
|
|
|
bool isTagOpen(std::string const &);
|
|
|
|
///
|
2009-11-19 21:50:02 +00:00
|
|
|
void writeError(std::string const &);
|
|
|
|
///
|
2009-11-19 16:29:57 +00:00
|
|
|
odocstream & os_;
|
|
|
|
///
|
2010-12-02 22:11:52 +00:00
|
|
|
typedef std::deque<html::StartTag> TagStack;
|
2009-11-19 16:29:57 +00:00
|
|
|
/// holds start tags until we know there is content in them.
|
2010-12-02 22:11:52 +00:00
|
|
|
TagStack pending_tags_;
|
2009-11-19 16:29:57 +00:00
|
|
|
/// remembers the history, so we can make sure we nest properly.
|
|
|
|
TagStack tag_stack_;
|
2009-11-19 23:29:07 +00:00
|
|
|
///
|
2010-11-24 15:27:36 +00:00
|
|
|
EscapeSettings escape_;
|
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
|