Rework the way the parsep tag is handled, and introduce comparison

operators for these tags.
This commit is contained in:
Richard Heck 2013-05-07 02:30:26 -04:00
parent ad56bb7286
commit 64f0cffbba
2 changed files with 96 additions and 30 deletions

View File

@ -173,6 +173,13 @@ docstring StartTag::asEndTag() const
} }
docstring EndTag::asEndTag() const
{
string output = "</" + tag_ + ">";
return from_utf8(output);
}
docstring ParTag::asTag() const docstring ParTag::asTag() const
{ {
docstring output = StartTag::asTag(); docstring output = StartTag::asTag();
@ -186,13 +193,6 @@ docstring ParTag::asTag() const
} }
docstring EndTag::asEndTag() const
{
string output = "</" + tag_ + ">";
return from_utf8(output);
}
docstring CompTag::asTag() const docstring CompTag::asTag() const
{ {
string output = "<" + tag_; string output = "<" + tag_;
@ -246,7 +246,7 @@ void XHTMLStream::writeError(std::string const & s) const
namespace { namespace {
// an illegal tag for internal use // an illegal tag for internal use
static string const parsep_tag = "&LyX_parsep_tag&"; static html::StartTag const parsep_tag("&LyX_parsep_tag&");
} }
@ -272,7 +272,7 @@ bool XHTMLStream::closeFontTags()
curtag = tag_stack_.back(); curtag = tag_stack_.back();
} }
if (curtag->tag_ == parsep_tag) if (*curtag == parsep_tag)
return true; return true;
// so we've hit a non-font tag. // so we've hit a non-font tag.
@ -281,8 +281,7 @@ bool XHTMLStream::closeFontTags()
TagDeque::const_reverse_iterator it = tag_stack_.rbegin(); TagDeque::const_reverse_iterator it = tag_stack_.rbegin();
TagDeque::const_reverse_iterator const en = tag_stack_.rend(); TagDeque::const_reverse_iterator const en = tag_stack_.rend();
for (; it != en; ++it) { for (; it != en; ++it) {
string const tagname = (*it)->tag_; if (**it == parsep_tag)
if (tagname == parsep_tag)
break; break;
writeError((*it)->tag_); writeError((*it)->tag_);
} }
@ -309,7 +308,7 @@ void XHTMLStream::endParagraph()
// of everything that hasn't been used. // of everything that hasn't been used.
TagPtr const cur_tag = pending_tags_.back(); TagPtr const cur_tag = pending_tags_.back();
pending_tags_.pop_back(); pending_tags_.pop_back();
if (cur_tag->tag_ == parsep_tag) if (*cur_tag == parsep_tag)
break; break;
} }
return; return;
@ -325,7 +324,7 @@ void XHTMLStream::endParagraph()
while (!tag_stack_.empty()) { while (!tag_stack_.empty()) {
TagPtr const cur_tag = tag_stack_.back(); TagPtr const cur_tag = tag_stack_.back();
tag_stack_.pop_back(); tag_stack_.pop_back();
if (cur_tag->tag_ == parsep_tag) if (*cur_tag == parsep_tag)
break; break;
writeError("Tag `" + cur_tag->tag_ + "' still open at end of paragraph. Closing."); writeError("Tag `" + cur_tag->tag_ + "' still open at end of paragraph. Closing.");
os_ << cur_tag->asEndTag(); os_ << cur_tag->asEndTag();
@ -337,7 +336,7 @@ void XHTMLStream::clearTagDeque()
{ {
while (!pending_tags_.empty()) { while (!pending_tags_.empty()) {
TagPtr const tag = pending_tags_.front(); TagPtr const tag = pending_tags_.front();
if (tag->tag_ != parsep_tag) if (*tag != parsep_tag)
// tabs? // tabs?
os_ << tag->asTag(); os_ << tag->asTag();
tag_stack_.push_back(tag); tag_stack_.push_back(tag);
@ -438,23 +437,34 @@ XHTMLStream & XHTMLStream::operator<<(html::CR const &)
} }
bool XHTMLStream::isTagOpen(string const & stag) const bool XHTMLStream::isTagOpen(html::StartTag const & stag) const
{ {
TagDeque::const_iterator sit = tag_stack_.begin(); TagDeque::const_iterator sit = tag_stack_.begin();
TagDeque::const_iterator const sen = tag_stack_.end(); TagDeque::const_iterator const sen = tag_stack_.end();
for (; sit != sen; ++sit) for (; sit != sen; ++sit)
if ((*sit)->tag_ == stag) if (**sit == stag)
return true; return true;
return false; return false;
} }
bool XHTMLStream::isTagPending(string const & stag) const bool XHTMLStream::isTagOpen(html::EndTag const & etag) const
{
TagDeque::const_iterator sit = tag_stack_.begin();
TagDeque::const_iterator const sen = tag_stack_.end();
for (; sit != sen; ++sit)
if (etag == **sit)
return true;
return false;
}
bool XHTMLStream::isTagPending(html::StartTag const & stag) const
{ {
TagDeque::const_iterator sit = pending_tags_.begin(); TagDeque::const_iterator sit = pending_tags_.begin();
TagDeque::const_iterator const sen = pending_tags_.end(); TagDeque::const_iterator const sen = pending_tags_.end();
for (; sit != sen; ++sit) for (; sit != sen; ++sit)
if ((*sit)->tag_ == stag) if (**sit == stag)
return true; return true;
return false; return false;
} }
@ -473,7 +483,7 @@ XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
// if this tag is pending, we can simply discard it. // if this tag is pending, we can simply discard it.
if (!pending_tags_.empty()) { if (!pending_tags_.empty()) {
if (etag.tag_ == pending_tags_.back()->tag_) { if (etag == *pending_tags_.back()) {
// we have <tag></tag>, so we discard it and remove it // we have <tag></tag>, so we discard it and remove it
// from the pending_tags_. // from the pending_tags_.
pending_tags_.pop_back(); pending_tags_.pop_back();
@ -488,7 +498,7 @@ XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
TagDeque::iterator dit = pending_tags_.begin(); TagDeque::iterator dit = pending_tags_.begin();
TagDeque::iterator const den = pending_tags_.end(); TagDeque::iterator const den = pending_tags_.end();
for (; dit != den; ++dit) { for (; dit != den; ++dit) {
if ((*dit)->tag_ == etag.tag_) { if (etag == **dit) {
// it was pending, so we just erase it // it was pending, so we just erase it
writeError("Tried to close pending tag `" + etag.tag_ writeError("Tried to close pending tag `" + etag.tag_
+ "' when other tags were pending. Last pending tag is `" + "' when other tags were pending. Last pending tag is `"
@ -498,7 +508,7 @@ XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
} }
} }
// so etag isn't itself pending. is it even open? // so etag isn't itself pending. is it even open?
if (!isTagOpen(etag.tag_)) { if (!isTagOpen(etag)) {
writeError("Tried to close `" + etag.tag_ writeError("Tried to close `" + etag.tag_
+ "' when tag was not open. Tag discarded."); + "' when tag was not open. Tag discarded.");
return *this; return *this;
@ -524,7 +534,7 @@ XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
} }
// is the tag we are closing the last one we opened? // is the tag we are closing the last one we opened?
if (etag.tag_ == tag_stack_.back()->tag_) { if (etag == *tag_stack_.back()) {
// output it... // output it...
os_ << etag.asEndTag(); os_ << etag.asEndTag();
// ...and forget about it // ...and forget about it
@ -534,7 +544,7 @@ XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
// we are trying to close a tag other than the one last opened. // we are trying to close a tag other than the one last opened.
// let's first see if this particular tag is still open somehow. // let's first see if this particular tag is still open somehow.
if (!isTagOpen(etag.tag_)) { if (!isTagOpen(etag)) {
writeError("Tried to close `" + etag.tag_ writeError("Tried to close `" + etag.tag_
+ "' when tag was not open. Tag discarded."); + "' when tag was not open. Tag discarded.");
return *this; return *this;
@ -549,7 +559,7 @@ XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
TagDeque::const_reverse_iterator rit = tag_stack_.rbegin(); TagDeque::const_reverse_iterator rit = tag_stack_.rbegin();
TagDeque::const_reverse_iterator ren = tag_stack_.rend(); TagDeque::const_reverse_iterator ren = tag_stack_.rend();
for (; rit != ren; ++rit) { for (; rit != ren; ++rit) {
if ((*rit)->tag_ == etag.tag_) if (etag == **rit)
break; break;
if (!html::isFontTag((*rit)->tag_)) { if (!html::isFontTag((*rit)->tag_)) {
// we'll just leave it and, presumably, have to close it later. // we'll just leave it and, presumably, have to close it later.
@ -567,7 +577,7 @@ XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
TagPtr curtag = tag_stack_.back(); TagPtr curtag = tag_stack_.back();
// ...remembering them in a stack. // ...remembering them in a stack.
TagDeque fontstack; TagDeque fontstack;
while (etag.tag_ != curtag->tag_) { while (etag != *curtag) {
os_ << curtag->asEndTag(); os_ << curtag->asEndTag();
fontstack.push_back(curtag); fontstack.push_back(curtag);
tag_stack_.pop_back(); tag_stack_.pop_back();
@ -592,9 +602,9 @@ XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
writeError("Closing tag `" + etag.tag_ writeError("Closing tag `" + etag.tag_
+ "' when other tags are open, namely:"); + "' when other tags are open, namely:");
TagPtr curtag = tag_stack_.back(); TagPtr curtag = tag_stack_.back();
while (etag.tag_ != curtag->tag_) { while (etag != *curtag) {
writeError(curtag->tag_); writeError(curtag->tag_);
if (curtag->tag_ != parsep_tag) if (*curtag != parsep_tag)
os_ << curtag->asEndTag(); os_ << curtag->asEndTag();
tag_stack_.pop_back(); tag_stack_.pop_back();
curtag = tag_stack_.back(); curtag = tag_stack_.back();

View File

@ -30,6 +30,7 @@ class Text;
// came from MathStream and its cousins. // came from MathStream and its cousins.
namespace html { namespace html {
/// Attributes will be escaped automatically and so should NOT /// Attributes will be escaped automatically and so should NOT
/// be escaped before being passed to the constructor. /// be escaped before being passed to the constructor.
struct StartTag struct StartTag
@ -47,6 +48,12 @@ struct StartTag
/// </tag_> /// </tag_>
virtual docstring asEndTag() const; virtual docstring asEndTag() const;
/// ///
bool operator==(StartTag const & rhs) const
{ return tag_ == rhs.tag_; }
///
bool operator!=(StartTag const & rhs) const
{ return !(*this == rhs); }
///
std::string tag_; std::string tag_;
/// ///
std::string attr_; std::string attr_;
@ -60,7 +67,7 @@ struct StartTag
struct ParTag : public StartTag struct ParTag : public StartTag
{ {
/// ///
ParTag(std::string const & tag, std::string const & attr, explicit ParTag(std::string const & tag, std::string const & attr,
std::string const & parid) std::string const & parid)
: StartTag(tag, attr), parid_(parid) : StartTag(tag, attr), parid_(parid)
{} {}
@ -73,6 +80,47 @@ struct ParTag : public StartTag
}; };
///
enum FontTypes {
FT_EMPH,
FT_BOLD,
FT_NOUN,
FT_UBAR,
FT_DBAR,
FT_SOUT,
FT_WAVE,
FT_ITALIC,
FT_SLANTED,
FT_SMALLCAPS,
FT_ROMAN,
FT_SANS,
FT_TYPER
// SIZES?
};
///
struct FontTag : public StartTag
{
///
explicit FontTag(FontTypes type);
///
docstring asTag() const;
///
docstring asEndTag() const;
///
bool isFontTag() { return true; }
///
bool operator==(FontTag const & rhs)
{ return font_type_ == rhs.font_type_; }
/// Asserts.
bool operator==(StartTag const &);
///
FontTypes font_type_;
};
///
struct EndTag struct EndTag
{ {
/// ///
@ -80,6 +128,12 @@ struct EndTag
/// </tag_> /// </tag_>
docstring asEndTag() const; docstring asEndTag() const;
/// ///
bool operator==(StartTag const & rhs) const
{ return tag_ == rhs.tag_; }
///
bool operator!=(StartTag const & rhs) const
{ return !(*this == rhs); }
///
std::string tag_; std::string tag_;
}; };
@ -167,9 +221,11 @@ private:
/// ///
void clearTagDeque(); void clearTagDeque();
/// ///
bool isTagOpen(std::string const &) const; bool isTagOpen(html::StartTag const &) const;
/// ///
bool isTagPending(std::string const &) const; bool isTagOpen(html::EndTag const &) const;
///
bool isTagPending(html::StartTag const &) const;
/// ///
void writeError(std::string const &) const; void writeError(std::string const &) const;
/// ///