nsTXTToHTMLConv.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef ____nstxttohtmlconv___h___
  6. #define ____nstxttohtmlconv___h___
  7. #include "nsITXTToHTMLConv.h"
  8. #include "nsCOMPtr.h"
  9. #include "nsTArray.h"
  10. #include "nsString.h"
  11. #define NS_NSTXTTOHTMLCONVERTER_CID \
  12. { /* 9ef9fa14-1dd1-11b2-9d65-d72d6d1f025e */ \
  13. 0x9ef9fa14, \
  14. 0x1dd1, \
  15. 0x11b2, \
  16. {0x9d, 0x65, 0xd7, 0x2d, 0x6d, 0x1f, 0x02, 0x5e} \
  17. }
  18. // Internal representation of a "token"
  19. typedef struct convToken {
  20. nsString token; // the actual string (i.e. "http://")
  21. nsString modText; // replacement text or href prepend text.
  22. bool prepend; // flag indicating how the modText should be used.
  23. } convToken;
  24. template<class T> class nsAutoPtr;
  25. /**
  26. * Convert plain text to HTML.
  27. *
  28. * OVERVIEW OF HOW THIS CLASS WORKS:
  29. *
  30. * This class stores an array of tokens that should be replaced by something,
  31. * or something that should be prepended.
  32. * The "token" member of convToken is the text to search for. This is a
  33. * substring of the desired token. Tokens are delimited by TOKEN_DELIMITERS.
  34. * That entire token will be replaced by modText (if prepend is false); or it
  35. * will be linkified and modText will be prepended to the token if prepend is
  36. * true.
  37. *
  38. * Note that all of the text will be in a preformatted block, so there is no
  39. * need to emit line-end tags, or set the font face to monospace.
  40. *
  41. * This works as a stream converter, so data will arrive by
  42. * OnStartRequest/OnDataAvailable/OnStopRequest calls.
  43. *
  44. * OStopR will possibly process a remaining token.
  45. *
  46. * If the data of one pass contains a part of a token, that part will be stored
  47. * in mBuffer. The rest of the data will be sent to the next listener.
  48. *
  49. * XXX this seems suboptimal. this means that this design will only work for
  50. * links. and it is impossible to append anything to the token. this means that,
  51. * for example, making *foo* bold is not possible.
  52. */
  53. class nsTXTToHTMLConv : public nsITXTToHTMLConv {
  54. public:
  55. NS_DECL_ISUPPORTS
  56. NS_DECL_NSISTREAMCONVERTER
  57. NS_DECL_NSITXTTOHTMLCONV
  58. NS_DECL_NSIREQUESTOBSERVER
  59. NS_DECL_NSISTREAMLISTENER
  60. nsTXTToHTMLConv();
  61. nsresult Init();
  62. protected:
  63. virtual ~nsTXTToHTMLConv();
  64. // return the token and it's location in the underlying buffer.
  65. int32_t FindToken(int32_t cursor, convToken* *_retval);
  66. // return the cursor location after munging HTML into the
  67. // underlying buffer, according to mToken
  68. int32_t CatHTML(int32_t front, int32_t back);
  69. nsCOMPtr<nsIStreamListener> mListener; // final listener (consumer)
  70. nsString mBuffer; // any carry over data
  71. nsTArray<nsAutoPtr<convToken> > mTokens; // list of tokens to search for
  72. convToken *mToken; // current token (if any)
  73. nsString mPageTitle; // Page title
  74. bool mPreFormatHTML; // Whether to use <pre> tags
  75. };
  76. #endif // ____nstxttohtmlconv___h___