netwerk/streamconv/converters/nsTXTToHTMLConv.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/streamconv/converters/nsTXTToHTMLConv.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef ____nstxttohtmlconv___h___
    1.10 +#define ____nstxttohtmlconv___h___
    1.11 +
    1.12 +#include "nsITXTToHTMLConv.h"
    1.13 +#include "nsCOMPtr.h"
    1.14 +#include "nsTArray.h"
    1.15 +#include "nsString.h"
    1.16 +
    1.17 +#define NS_NSTXTTOHTMLCONVERTER_CID                         \
    1.18 +{ /* 9ef9fa14-1dd1-11b2-9d65-d72d6d1f025e */ \
    1.19 +    0x9ef9fa14, \
    1.20 +    0x1dd1, \
    1.21 +    0x11b2, \
    1.22 +    {0x9d, 0x65, 0xd7, 0x2d, 0x6d, 0x1f, 0x02, 0x5e} \
    1.23 +}
    1.24 +
    1.25 +// Internal representation of a "token"
    1.26 +typedef struct convToken {
    1.27 +    nsString token;     // the actual string (i.e. "http://")
    1.28 +    nsString modText;   // replacement text or href prepend text.
    1.29 +    bool     prepend;   // flag indicating how the modText should be used.
    1.30 +} convToken;
    1.31 +
    1.32 +template<class T> class nsAutoPtr;
    1.33 +
    1.34 +/**
    1.35 + * Convert plain text to HTML.
    1.36 + *
    1.37 + * OVERVIEW OF HOW THIS CLASS WORKS:
    1.38 + *
    1.39 + * This class stores an array of tokens that should be replaced by something,
    1.40 + * or something that should be prepended.
    1.41 + * The "token" member of convToken is the text to search for. This is a
    1.42 + * substring of the desired token. Tokens are delimited by TOKEN_DELIMITERS.
    1.43 + * That entire token will be replaced by modText (if prepend is false); or it
    1.44 + * will be linkified and modText will be prepended to the token if prepend is
    1.45 + * true.
    1.46 + *
    1.47 + * Note that all of the text will be in a preformatted block, so there is no
    1.48 + * need to emit line-end tags, or set the font face to monospace.
    1.49 + *
    1.50 + * This works as a stream converter, so data will arrive by
    1.51 + * OnStartRequest/OnDataAvailable/OnStopRequest calls.
    1.52 + *
    1.53 + * OStopR will possibly process a remaining token.
    1.54 + *
    1.55 + * If the data of one pass contains a part of a token, that part will be stored
    1.56 + * in mBuffer. The rest of the data will be sent to the next listener.
    1.57 + * 
    1.58 + * XXX this seems suboptimal. this means that this design will only work for
    1.59 + * links. and it is impossible to append anything to the token. this means that,
    1.60 + * for example, making *foo* bold is not possible.
    1.61 + */
    1.62 +class nsTXTToHTMLConv : public nsITXTToHTMLConv {
    1.63 +public:
    1.64 +    NS_DECL_ISUPPORTS
    1.65 +    NS_DECL_NSISTREAMCONVERTER
    1.66 +    NS_DECL_NSITXTTOHTMLCONV
    1.67 +    NS_DECL_NSIREQUESTOBSERVER
    1.68 +    NS_DECL_NSISTREAMLISTENER
    1.69 +
    1.70 +    nsTXTToHTMLConv();
    1.71 +    virtual ~nsTXTToHTMLConv();
    1.72 +    nsresult Init();
    1.73 +
    1.74 +    // For factory creation.
    1.75 +    static NS_METHOD
    1.76 +    Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) {
    1.77 +        nsresult rv;
    1.78 +        if (aOuter)
    1.79 +            return NS_ERROR_NO_AGGREGATION;
    1.80 +
    1.81 +        nsTXTToHTMLConv* _s = new nsTXTToHTMLConv();
    1.82 +        if (_s == nullptr)
    1.83 +            return NS_ERROR_OUT_OF_MEMORY;
    1.84 +        NS_ADDREF(_s);
    1.85 +        rv = _s->Init();
    1.86 +        if (NS_FAILED(rv)) {
    1.87 +            delete _s;
    1.88 +            return rv;
    1.89 +        }
    1.90 +        rv = _s->QueryInterface(aIID, aResult);
    1.91 +        NS_RELEASE(_s);
    1.92 +        return rv;
    1.93 +    }
    1.94 +
    1.95 +
    1.96 +protected:
    1.97 +    // return the token and it's location in the underlying buffer.
    1.98 +    int32_t FindToken(int32_t cursor, convToken* *_retval);
    1.99 +
   1.100 +    // return the cursor location after munging HTML into the 
   1.101 +    // underlying buffer, according to mToken
   1.102 +    int32_t CatHTML(int32_t front, int32_t back);
   1.103 +
   1.104 +    nsCOMPtr<nsIStreamListener>     mListener; // final listener (consumer)
   1.105 +    nsString                        mBuffer;   // any carry over data
   1.106 +    nsTArray<nsAutoPtr<convToken> > mTokens;   // list of tokens to search for
   1.107 +    convToken                       *mToken;   // current token (if any)
   1.108 +    nsString                        mPageTitle; // Page title
   1.109 +    bool                            mPreFormatHTML; // Whether to use <pre> tags
   1.110 +};
   1.111 +
   1.112 +#endif // ____nstxttohtmlconv___h___
   1.113 +

mercurial