markupparser.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/private/markupparser.h
  3. // Purpose: Classes for parsing simple markup.
  4. // Author: Vadim Zeitlin
  5. // Created: 2011-02-16
  6. // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_PRIVATE_MARKUPPARSER_H_
  10. #define _WX_PRIVATE_MARKUPPARSER_H_
  11. #include "wx/string.h"
  12. // ----------------------------------------------------------------------------
  13. // wxMarkupSpanAttributes: information about attributes for a markup span.
  14. // ----------------------------------------------------------------------------
  15. struct wxMarkupSpanAttributes
  16. {
  17. enum OptionalBool
  18. {
  19. Unspecified = -1,
  20. No,
  21. Yes
  22. };
  23. wxMarkupSpanAttributes()
  24. {
  25. m_sizeKind = Size_Unspecified;
  26. m_isBold =
  27. m_isItalic =
  28. m_isUnderlined =
  29. m_isStrikethrough = Unspecified;
  30. }
  31. // If a string is empty, it means that the corresponding attribute is not
  32. // set.
  33. wxString m_fgCol,
  34. m_bgCol,
  35. m_fontFace;
  36. // There are many ways of specifying the size. First of all, the size may
  37. // be relative in which case m_fontSize is either -1 or +1 meaning that
  38. // it's one step smaller or larger than the current font. Second, it may be
  39. // absolute in which case m_fontSize contains either the size in 1024th of
  40. // a point (Pango convention) or its values are in [-3, 3] interval and map
  41. // to [xx-small, xx-large] CSS-like font size specification. And finally it
  42. // may be not specified at all, of course, in which case the value of
  43. // m_fontSize doesn't matter and it shouldn't be used.
  44. enum
  45. {
  46. Size_Unspecified,
  47. Size_Relative,
  48. Size_Symbolic,
  49. Size_PointParts
  50. } m_sizeKind;
  51. int m_fontSize;
  52. // If the value is Unspecified, the attribute wasn't given.
  53. OptionalBool m_isBold,
  54. m_isItalic,
  55. m_isUnderlined,
  56. m_isStrikethrough;
  57. };
  58. // ----------------------------------------------------------------------------
  59. // wxMarkupParserOutput: gathers the results of parsing markup.
  60. // ----------------------------------------------------------------------------
  61. // A class deriving directly from this one needs to implement all the pure
  62. // virtual functions below but as the handling of all simple tags (bold, italic
  63. // &c) is often very similar, it is usually more convenient to inherit from
  64. // wxMarkupParserFontOutput defined in wx/private/markupparserfont.h instead.
  65. class wxMarkupParserOutput
  66. {
  67. public:
  68. wxMarkupParserOutput() { }
  69. virtual ~wxMarkupParserOutput() { }
  70. // Virtual functions called by wxMarkupParser while parsing the markup.
  71. // Called for a run of normal text.
  72. virtual void OnText(const wxString& text) = 0;
  73. // These functions correspond to the simple tags without parameters.
  74. virtual void OnBoldStart() = 0;
  75. virtual void OnBoldEnd() = 0;
  76. virtual void OnItalicStart() = 0;
  77. virtual void OnItalicEnd() = 0;
  78. virtual void OnUnderlinedStart() = 0;
  79. virtual void OnUnderlinedEnd() = 0;
  80. virtual void OnStrikethroughStart() = 0;
  81. virtual void OnStrikethroughEnd() = 0;
  82. virtual void OnBigStart() = 0;
  83. virtual void OnBigEnd() = 0;
  84. virtual void OnSmallStart() = 0;
  85. virtual void OnSmallEnd() = 0;
  86. virtual void OnTeletypeStart() = 0;
  87. virtual void OnTeletypeEnd() = 0;
  88. // The generic span start and end functions.
  89. virtual void OnSpanStart(const wxMarkupSpanAttributes& attrs) = 0;
  90. virtual void OnSpanEnd(const wxMarkupSpanAttributes& attrs) = 0;
  91. private:
  92. wxDECLARE_NO_COPY_CLASS(wxMarkupParserOutput);
  93. };
  94. // ----------------------------------------------------------------------------
  95. // wxMarkupParser: parses the given markup text into wxMarkupParserOutput.
  96. // ----------------------------------------------------------------------------
  97. class WXDLLIMPEXP_CORE wxMarkupParser
  98. {
  99. public:
  100. // Initialize the parser with the object that will receive parsing results.
  101. // This object lifetime must be greater than ours.
  102. explicit wxMarkupParser(wxMarkupParserOutput& output)
  103. : m_output(output)
  104. {
  105. }
  106. // Parse the entire string and call wxMarkupParserOutput methods.
  107. //
  108. // Return true if the string was successfully parsed or false if it failed
  109. // (presumably because of syntax errors in the markup).
  110. bool Parse(const wxString& text);
  111. // Quote a normal string, not meant to be interpreted as markup, so that it
  112. // produces the same string when parsed as markup. This means, for example,
  113. // replacing '<' in the input string with "&lt;" to prevent them from being
  114. // interpreted as tag opening characters.
  115. static wxString Quote(const wxString& text);
  116. // Strip markup from a string, i.e. simply remove all tags and replace
  117. // XML entities with their values (or with "&&" in case of "&amp;" to
  118. // prevent it from being interpreted as mnemonic marker).
  119. static wxString Strip(const wxString& text);
  120. private:
  121. // Simple struct combining the name of a tag and its attributes.
  122. struct TagAndAttrs
  123. {
  124. TagAndAttrs(const wxString& name_) : name(name_) { }
  125. wxString name;
  126. wxMarkupSpanAttributes attrs;
  127. };
  128. // Call the wxMarkupParserOutput method corresponding to the given tag.
  129. //
  130. // Return false if the tag doesn't match any of the known ones.
  131. bool OutputTag(const TagAndAttrs& tagAndAttrs, bool start);
  132. // Parse the attributes and fill the provided TagAndAttrs object with the
  133. // information about them. Does nothing if attrs string is empty.
  134. //
  135. // Returns empty string on success of a [fragment of an] error message if
  136. // we failed to parse the attributes.
  137. wxString ParseAttrs(wxString attrs, TagAndAttrs& tagAndAttrs);
  138. wxMarkupParserOutput& m_output;
  139. wxDECLARE_NO_COPY_CLASS(wxMarkupParser);
  140. };
  141. #endif // _WX_PRIVATE_MARKUPPARSER_H_