diff options
Diffstat (limited to 'tests/examplefiles/test.html')
-rw-r--r-- | tests/examplefiles/test.html | 770 |
1 files changed, 0 insertions, 770 deletions
diff --git a/tests/examplefiles/test.html b/tests/examplefiles/test.html index ea723232..39365c0e 100644 --- a/tests/examplefiles/test.html +++ b/tests/examplefiles/test.html @@ -348,774 +348,4 @@ pre.syntax { padding: 5px; margin-top: 0px; } <span class="bn bn-pseudo">self</span>.<span class="name">subject</span>.<span class="name">subject_id</span> ) -<span class="cm"># -*- coding: utf-8 -*-</span> -<span class="st st-db">"""</span><span class="st"> - pocoo.pkg.core.auth - ~~~~~~~~~~~~~~~~~~~ - - Default authentication module. - - :copyright: 2006-2007 by Armin Ronacher. - :license: GNU GPL, see LICENSE for more details. -</span><span class="st st-db">"""</span> - -<span class="kw">from </span><span class="cls">datetime</span><span class="kw"> import</span> <span class="name">datetime</span> -<span class="kw">from </span><span class="cls">pocoo.context</span><span class="kw"> import</span> <span class="name">Component</span> - -<span class="kw">from </span><span class="cls">pocoo.utils.net</span><span class="kw"> import</span> <span class="name">IP</span> -<span class="kw">from </span><span class="cls">pocoo.application</span><span class="kw"> import</span> <span class="name">RequestWrapper</span> -<span class="kw">from </span><span class="cls">pocoo.settings</span><span class="kw"> import</span> <span class="name">cfg</span> - -<span class="kw">from </span><span class="cls">pocoo.pkg.core.user</span><span class="kw"> import</span> <span class="name">User</span>, <span class="name">check_login_data</span> - - -<span class="kw">class </span><span class="cls">AuthProvider</span>(<span class="name">Component</span>): - - <span class="deco">@property</span> - - <span class="kw">def </span><span class="fun">auth_name</span>(<span class="bn bn-pseudo">self</span>): - <span class="st st-db">"""</span><span class="st"> - has to return the name of the auth module for the configuration - file. This name defaults to the classname. - </span><span class="st st-db">"""</span> - <span class="kw">return</span> <span class="bn bn-pseudo">self</span>.<span class="name">__class__</span>.<span class="name">__name__</span> - - <span class="kw">def </span><span class="fun">get_user</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>): - <span class="st st-db">"""</span><span class="st"> - This method should either return a valid `User object`_ or ``None``. - - .. _User object: pocoo.pkg.core.user - </span><span class="st st-db">"""</span> - - <span class="kw">def </span><span class="fun">get_user_id</span>(<span class="bn bn-pseudo">self</span>, <span class="name">session_dict</span>): - <span class="st st-db">"""</span><span class="st"> - - This method should either return the user_id of the user or ``None``. - </span><span class="st st-db">"""</span> - - <span class="kw">def </span><span class="fun">do_login</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>, <span class="name">username</span>, <span class="name">password</span>): - <span class="st st-db">"""</span><span class="st"> - - This method should update the user session so that the auth provider - can recognize the user in the ``get_user`` method. - It has to return a valid ``HttpResponse``, for redirecting to external - login scripts or ``False``, to display an error message (login failed). - If it returns ``True`` pocoo will redirect to the last visited page. - </span><span class="st st-db">"""</span> - - <span class="kw">def </span><span class="fun">do_logout</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>): - <span class="st st-db">"""</span><span class="st"> - This method should return a valid ``Response`` for redirecting - to external scripts or ``None``. - </span><span class="st st-db">"""</span> - -<span class="kw">class </span><span class="cls">SessionAuth</span>(<span class="name">AuthProvider</span>): - - <span class="kw">def </span><span class="fun">get_user</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>): - <span class="kw">try</span>: - <span class="name">user_id</span> <span class="op">=</span> <span class="name">req</span>.<span class="name">session</span>[<span class="st st-sg">'</span><span class="st">user_id</span><span class="st st-sg">'</span>] - <span class="kw">return</span> <span class="name">User</span>(<span class="bn bn-pseudo">self</span>.<span class="name">ctx</span>, <span class="name">user_id</span>) - <span class="kw">except</span> (<span class="exc">KeyError</span>, <span class="name">User</span>.<span class="name">NotFound</span>): - <span class="kw">return</span> <span class="bn bn-pseudo">None</span> - - <span class="kw">def </span><span class="fun">do_login</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>, <span class="name">username</span>, <span class="name">password</span>): - <span class="name">user_id</span> <span class="op">=</span> <span class="name">check_login_data</span>(<span class="name">req</span>.<span class="name">ctx</span>, <span class="name">username</span>, <span class="name">password</span>) - <span class="kw">if</span> <span class="name">user_id</span> <span class="op op-word">is</span> <span class="op op-word">not</span> <span class="bn bn-pseudo">None</span>: - <span class="name">req</span>.<span class="name">session</span>[<span class="st st-sg">'</span><span class="st">user_id</span><span class="st st-sg">'</span>] <span class="op">=</span> <span class="name">user_id</span> - - <span class="kw">return</span> <span class="bn bn-pseudo">True</span> - <span class="kw">return</span> <span class="bn bn-pseudo">False</span> - - <span class="kw">def </span><span class="fun">do_logout</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>): - <span class="kw">if</span> <span class="st st-sg">'</span><span class="st">user_id</span><span class="st st-sg">'</span> <span class="op op-word">in</span> <span class="name">req</span>.<span class="name">session</span>: - <span class="name">req</span>.<span class="name">session</span>.<span class="name">pop</span>(<span class="st st-sg">'</span><span class="st">user_id</span><span class="st st-sg">'</span>) - - <span class="kw">def </span><span class="fun">get_user_id</span>(<span class="bn bn-pseudo">self</span>, <span class="name">session_dict</span>): - <span class="kw">return</span> <span class="name">session_dict</span>.<span class="name">get</span>(<span class="st st-sg">'</span><span class="st">user_id</span><span class="st st-sg">'</span>) - - - -<span class="kw">class </span><span class="cls">AuthWrapper</span>(<span class="name">RequestWrapper</span>): - - <span class="kw">def </span><span class="fun">get_priority</span>(<span class="bn bn-pseudo">self</span>): - <span class="cm"># after SessionWrapper</span> - <span class="kw">return</span> <span class="nb nb-int">3</span> - - <span class="kw">def </span><span class="fun">process_request</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>): - <span class="cm"># XXX: what to do with uid?</span> - <span class="name">uid</span> <span class="op">=</span> <span class="name">req</span>.<span class="name">session</span>.<span class="name">get</span>(<span class="st st-sg">'</span><span class="st">user_id</span><span class="st st-sg">'</span>, <span class="op">-</span><span class="nb nb-int">1</span>) - <span class="name">req</span>.<span class="name">auth</span> <span class="op">=</span> <span class="name">AuthController</span>(<span class="name">req</span>) - <span class="name">req</span>.<span class="name">user</span> <span class="op">=</span> <span class="name">req</span>.<span class="name">auth</span>.<span class="name">get_user</span>() - - <span class="kw">def </span><span class="fun">process_response</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>, <span class="name">resp</span>): - <span class="kw">return</span> <span class="name">resp</span> - - -<span class="kw">def </span><span class="fun">get_auth_provider_mapping</span>(<span class="name">ctx</span>): - <span class="st st-db">"""</span><span class="st">Returns a list of auth providers.</span><span class="st st-db">"""</span> - <span class="name">providers</span> <span class="op">=</span> {} - <span class="kw">for</span> <span class="name">comp</span> <span class="op op-word">in</span> <span class="name">ctx</span>.<span class="name">get_components</span>(<span class="name">AuthProvider</span>): - <span class="name">providers</span>[<span class="name">comp</span>.<span class="name">auth_name</span>] <span class="op">=</span> <span class="name">comp</span> - - <span class="kw">return</span> <span class="name">providers</span> - - -<span class="kw">def </span><span class="fun">get_auth_provider</span>(<span class="name">ctx</span>): - <span class="st st-db">"""</span><span class="st">Returns the enabled auth provider.</span><span class="st st-db">"""</span> - <span class="kw">if</span> <span class="st st-sg">'</span><span class="st">auth/provider</span><span class="st st-sg">'</span> <span class="op op-word">not</span> <span class="op op-word">in</span> <span class="name">ctx</span>.<span class="name">_cache</span>: - <span class="name">providers</span> <span class="op">=</span> <span class="name">get_auth_provider_mapping</span>(<span class="name">ctx</span>) - <span class="name">provider</span> <span class="op">=</span> <span class="name">providers</span>[<span class="name">ctx</span>.<span class="name">cfg</span>.<span class="name">get</span>(<span class="st st-sg">'</span><span class="st">general</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">auth_module</span><span class="st st-sg">'</span>)] - <span class="name">ctx</span>.<span class="name">_cache</span>[<span class="st st-sg">'</span><span class="st">auth/provider</span><span class="st st-sg">'</span>] <span class="op">=</span> <span class="name">provider</span> - - <span class="kw">return</span> <span class="name">ctx</span>.<span class="name">_cache</span>[<span class="st st-sg">'</span><span class="st">auth/provider</span><span class="st st-sg">'</span>] - - -<span class="kw">class </span><span class="cls">AuthController</span>(<span class="bn">object</span>): - <span class="name">auth_provider</span> <span class="op">=</span> <span class="name">cfg</span>.<span class="name">str</span>(<span class="st st-sg">'</span><span class="st">general</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">auth_module</span><span class="st st-sg">'</span>) - - <span class="kw">def </span><span class="fun">__init__</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>): - <span class="bn bn-pseudo">self</span>.<span class="name">ctx</span> <span class="op">=</span> <span class="name">req</span>.<span class="name">ctx</span> - - <span class="bn bn-pseudo">self</span>.<span class="name">req</span> <span class="op">=</span> <span class="name">req</span> - <span class="bn bn-pseudo">self</span>.<span class="name">provider</span> <span class="op">=</span> <span class="name">get_auth_provider</span>(<span class="name">req</span>.<span class="name">ctx</span>) - - <span class="kw">def </span><span class="fun">get_user</span>(<span class="bn bn-pseudo">self</span>): - <span class="st st-db">"""</span><span class="st"> - - Returns the user for this request - </span><span class="st st-db">"""</span> - <span class="name">user</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">provider</span>.<span class="name">get_user</span>(<span class="bn bn-pseudo">self</span>.<span class="name">req</span>) - <span class="kw">if</span> <span class="name">user</span> <span class="op op-word">is</span> <span class="op op-word">not</span> <span class="bn bn-pseudo">None</span>: - <span class="name">user</span>.<span class="name">ip</span> <span class="op">=</span> <span class="name">IP</span>(<span class="bn bn-pseudo">self</span>.<span class="name">req</span>.<span class="name">environ</span>[<span class="st st-sg">'</span><span class="st">REMOTE_ADDR</span><span class="st st-sg">'</span>]) - <span class="kw">return</span> <span class="name">user</span> - - <span class="cm"># return anonymous user</span> - <span class="kw">return</span> <span class="name">User</span>(<span class="bn bn-pseudo">self</span>.<span class="name">ctx</span>, <span class="op">-</span><span class="nb nb-int">1</span>) - - <span class="kw">def </span><span class="fun">do_login</span>(<span class="bn bn-pseudo">self</span>, <span class="name">username</span>, <span class="name">password</span>): - <span class="st st-db">"""</span><span class="st"> - - Returns a valid ``Response``, for redirecting to external - login scripts or ``False``, to display an error message (login failed). - If it returns ``True`` pocoo should redirect to the last visited page. - </span><span class="st st-db">"""</span> - <span class="name">rv</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">provider</span>.<span class="name">do_login</span>(<span class="bn bn-pseudo">self</span>.<span class="name">req</span>, <span class="name">username</span>, <span class="name">password</span>) - <span class="kw">if</span> <span class="name">rv</span> <span class="op op-word">is</span> <span class="op op-word">not</span> <span class="bn bn-pseudo">False</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">req</span>.<span class="name">user</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">get_user</span>() - <span class="kw">return</span> <span class="name">rv</span> - - <span class="kw">return</span> <span class="bn bn-pseudo">False</span> - - <span class="kw">def </span><span class="fun">do_logout</span>(<span class="bn bn-pseudo">self</span>): - <span class="st st-db">"""</span><span class="st"> - Loggs the user out. Can eiter return None or a Response for - external redirects. - </span><span class="st st-db">"""</span> - <span class="cm"># update last login time</span> - - <span class="bn bn-pseudo">self</span>.<span class="name">req</span>.<span class="name">user</span>.<span class="name">last_login</span> <span class="op">=</span> <span class="name">datetime</span>.<span class="name">now</span>() - <span class="bn bn-pseudo">self</span>.<span class="name">req</span>.<span class="name">user</span>.<span class="name">save</span>() - <span class="bn bn-pseudo">self</span>.<span class="name">provider</span>.<span class="name">do_logout</span>(<span class="bn bn-pseudo">self</span>.<span class="name">req</span>) - <span class="cm">#XXX: maybe a bit slow</span> - - <span class="bn bn-pseudo">self</span>.<span class="name">req</span>.<span class="name">user</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">get_user</span>() -<span class="cm"># -*- coding: utf-8 -*-</span> -<span class="st st-db">"""</span><span class="st"> - pocoo.pkg.core.bbcode - ~~~~~~~~~~~~~~~~~~~~~ - - Pocoo BBCode parser. - - :copyright: 2006-2007 by Georg Brandl, Armin Ronacher. - :license: GNU GPL, see LICENSE for more details. - -</span><span class="st st-db">"""</span> -<span class="kw">import </span><span class="cls">re</span> - -<span class="kw">from </span><span class="cls">pocoo</span><span class="kw"> import</span> <span class="name">Component</span> -<span class="kw">from </span><span class="cls">pocoo.pkg.core.textfmt</span><span class="kw"> import</span> <span class="name">MarkupFormat</span> - -<span class="kw">from </span><span class="cls">pocoo.pkg.core.smilies</span><span class="kw"> import</span> <span class="name">get_smiley_buttons</span>, <span class="name">replace_smilies</span> -<span class="kw">from </span><span class="cls">pocoo.utils.html</span><span class="kw"> import</span> <span class="name">escape_html</span>, <span class="name">translate_color</span> - -<span class="kw">from </span><span class="cls">pocoo.utils.activecache</span><span class="kw"> import</span> <span class="name">Node</span>, <span class="name">CallbackNode</span>, <span class="name">NodeList</span> - -<span class="name">tag_re</span> <span class="op">=</span> <span class="name">re</span>.<span class="name">compile</span>(<span class="st st-sg">r'</span><span class="st">(\[(/?[a-zA-Z0-9]+)(?:=(&quot;.+?&quot;|.+?))?\])</span><span class="st st-sg">'</span>) - - - -<span class="kw">class </span><span class="cls">EndOfText</span>(<span class="exc">Exception</span>): - <span class="st st-db">"""</span><span class="st">Raise when the end of the text is reached.</span><span class="st st-db">"""</span> - - -<span class="kw">class </span><span class="cls">TokenList</span>(<span class="bn">list</span>): - <span class="st st-db">"""</span><span class="st">A subclass of a list for tokens which allows to flatten - the tokens so that the original bbcode is the return value.</span><span class="st st-db">"""</span> - - <span class="kw">def </span><span class="fun">flatten</span>(<span class="bn bn-pseudo">self</span>): - <span class="kw">return</span> <span class="name">u</span><span class="st st-sg">''</span>.<span class="name">join</span>(<span class="name">token</span>.<span class="name">raw</span> <span class="kw">for</span> <span class="name">token</span> <span class="op op-word">in</span> <span class="bn bn-pseudo">self</span>) - - <span class="kw">def </span><span class="fun">__repr__</span>(<span class="bn bn-pseudo">self</span>): - <span class="kw">return</span> <span class="st st-sg">'</span><span class="st"><</span><span class="st st-int">%s</span><span class="st"> </span><span class="st st-int">%s</span><span class="st">></span><span class="st st-sg">'</span> <span class="op">%</span> ( - <span class="bn bn-pseudo">self</span>.<span class="name">__class__</span>.<span class="name">__name__</span>, - <span class="bn">list</span>.<span class="name">__repr__</span>(<span class="bn bn-pseudo">self</span>) - ) - - - -<span class="kw">class </span><span class="cls">Token</span>(<span class="bn">object</span>): - <span class="st st-db">"""</span><span class="st">Token Baseclass</span><span class="st st-db">"""</span> - - <span class="kw">def </span><span class="fun">__repr__</span>(<span class="bn bn-pseudo">self</span>): - <span class="kw">return</span> <span class="st st-sg">'</span><span class="st"><</span><span class="st st-int">%s</span><span class="st"> </span><span class="st st-int">%s</span><span class="st">></span><span class="st st-sg">'</span> <span class="op">%</span> ( - <span class="bn bn-pseudo">self</span>.<span class="name">__class__</span>.<span class="name">__name__</span>, - <span class="bn bn-pseudo">self</span>.<span class="name">raw</span> - - ) - - -<span class="kw">class </span><span class="cls">TextToken</span>(<span class="name">Token</span>): - <span class="st st-db">"""</span><span class="st">A token for plain text.</span><span class="st st-db">"""</span> - - <span class="kw">def </span><span class="fun">__init__</span>(<span class="bn bn-pseudo">self</span>, <span class="name">data</span>): - <span class="bn bn-pseudo">self</span>.<span class="name">data</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">raw</span> <span class="op">=</span> <span class="name">data</span> - - -<span class="kw">class </span><span class="cls">TagToken</span>(<span class="name">Token</span>): - <span class="st st-db">"""</span><span class="st">A token for tags.</span><span class="st st-db">"""</span> - - <span class="kw">def </span><span class="fun">__init__</span>(<span class="bn bn-pseudo">self</span>, <span class="name">raw</span>, <span class="name">tagname</span>, <span class="name">attr</span>): - <span class="bn bn-pseudo">self</span>.<span class="name">raw</span> <span class="op">=</span> <span class="name">raw</span> - - <span class="bn bn-pseudo">self</span>.<span class="name">name</span> <span class="op">=</span> <span class="name">tagname</span> - <span class="bn bn-pseudo">self</span>.<span class="name">attr</span> <span class="op">=</span> <span class="name">attr</span> - - -<span class="kw">class </span><span class="cls">Parser</span>(<span class="bn">object</span>): - <span class="st st-db">"""</span><span class="st"> - BBCode Parser Class - </span><span class="st st-db">"""</span> - - <span class="kw">def </span><span class="fun">__init__</span>(<span class="bn bn-pseudo">self</span>, <span class="name">ctx</span>, <span class="name">text</span>, <span class="name">handlers</span>, <span class="name">allowed_tags</span>): - <span class="bn bn-pseudo">self</span>.<span class="name">ctx</span> <span class="op">=</span> <span class="name">ctx</span> - - <span class="bn bn-pseudo">self</span>.<span class="name">_tokens</span> <span class="op">=</span> <span class="name">tag_re</span>.<span class="name">split</span>(<span class="name">text</span>) - <span class="bn bn-pseudo">self</span>.<span class="name">_tokens</span>.<span class="name">reverse</span>() - <span class="bn bn-pseudo">self</span>.<span class="name">_is_text</span> <span class="op">=</span> <span class="bn bn-pseudo">True</span> - - <span class="bn bn-pseudo">self</span>.<span class="name">_cache</span> <span class="op">=</span> [] - <span class="bn bn-pseudo">self</span>.<span class="name">_handlers</span> <span class="op">=</span> <span class="name">handlers</span> - <span class="bn bn-pseudo">self</span>.<span class="name">_allowed_tags</span> <span class="op">=</span> <span class="name">allowed_tags</span> - - <span class="kw">def </span><span class="fun">tag_allowed</span>(<span class="bn bn-pseudo">self</span>, <span class="name">tagname</span>): - <span class="st st-db">"""</span><span class="st"> - Check if a tagname is allowed for this parser. - </span><span class="st st-db">"""</span> - <span class="kw">if</span> <span class="bn bn-pseudo">self</span>.<span class="name">_allowed_tags</span> <span class="op op-word">is</span> <span class="bn bn-pseudo">None</span>: - <span class="kw">return</span> <span class="bn bn-pseudo">True</span> - - <span class="kw">return</span> <span class="name">tagname</span> <span class="op op-word">in</span> <span class="bn bn-pseudo">self</span>.<span class="name">_allowed_tags</span> - - <span class="kw">def </span><span class="fun">get_next_token</span>(<span class="bn bn-pseudo">self</span>): - <span class="st st-db">"""</span><span class="st"> - - Fetch the next raw token from the text - Raise ``EndOfText`` if not further token exists. - </span><span class="st st-db">"""</span> - <span class="kw">if</span> <span class="bn bn-pseudo">self</span>.<span class="name">_cache</span>: - <span class="kw">return</span> <span class="bn bn-pseudo">self</span>.<span class="name">_cache</span>.<span class="name">pop</span>() - <span class="name">get_token</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">_tokens</span>.<span class="name">pop</span> - - <span class="kw">if</span> <span class="op op-word">not</span> <span class="bn bn-pseudo">self</span>.<span class="name">_tokens</span>: - <span class="kw">raise</span> <span class="name">EndOfText</span>() - <span class="kw">if</span> <span class="bn bn-pseudo">self</span>.<span class="name">_is_text</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">_is_text</span> <span class="op">=</span> <span class="bn bn-pseudo">False</span> - - <span class="kw">return</span> <span class="name">TextToken</span>(<span class="name">get_token</span>()) - <span class="kw">else</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">_is_text</span> <span class="op">=</span> <span class="bn bn-pseudo">True</span> - - <span class="name">raw</span> <span class="op">=</span> <span class="name">get_token</span>() - <span class="name">tagname</span> <span class="op">=</span> <span class="name">get_token</span>().<span class="name">lower</span>() - <span class="name">attr</span> <span class="op">=</span> <span class="name">get_token</span>() - <span class="kw">if</span> <span class="name">attr</span> <span class="op op-word">and</span> <span class="name">attr</span>[:<span class="nb nb-int">6</span>] <span class="op">==</span> <span class="name">attr</span>[<span class="op">-</span><span class="nb nb-int">6</span>:] <span class="op">==</span> <span class="st st-sg">'</span><span class="st">&quot;</span><span class="st st-sg">'</span>: - <span class="name">attr</span> <span class="op">=</span> <span class="name">attr</span>[<span class="nb nb-int">6</span>:<span class="op">-</span><span class="nb nb-int">6</span>] - <span class="kw">return</span> <span class="name">TagToken</span>(<span class="name">raw</span>, <span class="name">tagname</span>, <span class="name">attr</span>) - - <span class="kw">def </span><span class="fun">push_token</span>(<span class="bn bn-pseudo">self</span>, <span class="name">token</span>): - <span class="st st-db">"""</span><span class="st"> - - Pushes the last fetched token in a cache so that the next time - you call ``get_next_token`` returns the pushed token. - </span><span class="st st-db">"""</span> - <span class="bn bn-pseudo">self</span>.<span class="name">_cache</span>.<span class="name">append</span>(<span class="name">token</span>) - - <span class="kw">def </span><span class="fun">parse</span>(<span class="bn bn-pseudo">self</span>, <span class="name">needle</span><span class="op">=</span><span class="bn bn-pseudo">None</span>, <span class="name">preserve_needle</span><span class="op">=</span><span class="bn bn-pseudo">False</span>): - <span class="st st-db">"""</span><span class="st"> - - Parses the text until ``needle`` or the end of text if not defined. - If it finds the needle it will delete the needle token. If you want - the needle token too set ``preserve_needle`` to ``True``. - - In comparison with the ``get_tokens`` method this method will call - the node handlers for each node. - </span><span class="st st-db">"""</span> - <span class="name">result</span> <span class="op">=</span> <span class="name">NodeList</span>() - <span class="kw">try</span>: - <span class="kw">while</span> <span class="bn bn-pseudo">True</span>: - <span class="name">token</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">get_next_token</span>() - <span class="kw">if</span> <span class="bn">isinstance</span>(<span class="name">token</span>, <span class="name">TagToken</span>) <span class="op op-word">and</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="name">needle</span>: - <span class="kw">if</span> <span class="name">preserve_needle</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">push_token</span>(<span class="name">token</span>) - <span class="kw">break</span> - - <span class="name">result</span>.<span class="name">append</span>(<span class="bn bn-pseudo">self</span>.<span class="name">get_node</span>(<span class="name">token</span>)) - <span class="kw">except</span> <span class="name">EndOfText</span>: - <span class="kw">pass</span> - - <span class="kw">return</span> <span class="name">result</span> - - <span class="kw">def </span><span class="fun">get_tokens</span>(<span class="bn bn-pseudo">self</span>, <span class="name">needle</span><span class="op">=</span><span class="bn bn-pseudo">None</span>, <span class="name">preserve_needle</span><span class="op">=</span><span class="bn bn-pseudo">False</span>): - <span class="st st-db">"""</span><span class="st"> - - Like ``parse`` but returns an unparsed TokenList. Basically you - would never need this method except for preserved areas like - Code blocks etc. - </span><span class="st st-db">"""</span> - <span class="name">result</span> <span class="op">=</span> <span class="name">TokenList</span>() - <span class="kw">try</span>: - <span class="kw">while</span> <span class="bn bn-pseudo">True</span>: - <span class="name">token</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">get_next_token</span>() - <span class="kw">if</span> <span class="bn">isinstance</span>(<span class="name">token</span>, <span class="name">TagToken</span>) <span class="op op-word">and</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="name">needle</span>: - <span class="kw">if</span> <span class="name">preserve_needle</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">push_token</span>(<span class="name">token</span>) - <span class="kw">break</span> - - <span class="name">result</span>.<span class="name">append</span>(<span class="name">token</span>) - <span class="kw">except</span> <span class="name">EndOfText</span>: - <span class="kw">pass</span> - <span class="kw">return</span> <span class="name">result</span> - - <span class="kw">def </span><span class="fun">get_node</span>(<span class="bn bn-pseudo">self</span>, <span class="name">token</span>): - <span class="st st-db">"""</span><span class="st"> - Return the node for a token. If the token was a ``TextToken`` - the resulting node will call ``get_text_node`` which returns a - </span><span class="st st-esc">\n</span><span class="st"> to <br/> replaced version of the token value wrapped in a - plain ``Node``. In all other cases it will try to lookup the node - in the list of registered token handlers. - - If this fails it wraps the raw token value in a ``Node``. - </span><span class="st st-db">"""</span> - - <span class="kw">if</span> <span class="bn">isinstance</span>(<span class="name">token</span>, <span class="name">TextToken</span>): - <span class="kw">return</span> <span class="bn bn-pseudo">self</span>.<span class="name">get_text_node</span>(<span class="name">token</span>.<span class="name">data</span>) - <span class="kw">if</span> <span class="bn bn-pseudo">self</span>.<span class="name">tag_allowed</span>(<span class="name">token</span>.<span class="name">name</span>): - <span class="kw">for</span> <span class="name">handler</span> <span class="op op-word">in</span> <span class="bn bn-pseudo">self</span>.<span class="name">_handlers</span>: - <span class="name">rv</span> <span class="op">=</span> <span class="name">handler</span>.<span class="name">get_node</span>(<span class="name">token</span>, <span class="bn bn-pseudo">self</span>) - <span class="kw">if</span> <span class="name">rv</span> <span class="op op-word">is</span> <span class="op op-word">not</span> <span class="bn bn-pseudo">None</span>: - <span class="kw">if</span> <span class="bn">isinstance</span>(<span class="name">rv</span>, <span class="name">Node</span>): - <span class="kw">return</span> <span class="name">rv</span> - - <span class="kw">return</span> <span class="name">Node</span>(<span class="name">rv</span>) - <span class="kw">return</span> <span class="bn bn-pseudo">self</span>.<span class="name">get_text_node</span>(<span class="name">token</span>.<span class="name">raw</span>) - - <span class="kw">def </span><span class="fun">get_text_node</span>(<span class="bn bn-pseudo">self</span>, <span class="name">data</span>): - <span class="st st-db">"""</span><span class="st"> - - Newline replaces the text and wraps it in an ``Node``. - </span><span class="st st-db">"""</span> - <span class="name">text</span> <span class="op">=</span> <span class="name">replace_smilies</span>(<span class="bn bn-pseudo">self</span>.<span class="name">ctx</span>, <span class="name">data</span>) - <span class="kw">return</span> <span class="name">Node</span>(<span class="name">re</span>.<span class="name">sub</span>(<span class="st st-sg">r'</span><span class="st">\r?\n</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st"><br /></span><span class="st st-esc">\n</span><span class="st st-sg">'</span>, <span class="name">text</span>)) - - <span class="kw">def </span><span class="fun">wrap_render</span>(<span class="bn bn-pseudo">self</span>, <span class="name">tag</span>, <span class="name">parse_until</span>): - <span class="st st-db">"""</span><span class="st"> - - Renders untile ``parse_until`` and wraps it in the html tag ``tag``. - </span><span class="st st-db">"""</span> - <span class="kw">return</span> <span class="name">NodeList</span>(<span class="name">Node</span>(<span class="st st-sg">'</span><span class="st"><</span><span class="st st-int">%s</span><span class="st">></span><span class="st st-sg">'</span> <span class="op">%</span> <span class="name">tag</span>), <span class="bn bn-pseudo">self</span>.<span class="name">parse</span>(<span class="name">parse_until</span>), - <span class="name">Node</span>(<span class="st st-sg">'</span><span class="st"></</span><span class="st st-int">%s</span><span class="st">></span><span class="st st-sg">'</span> <span class="op">%</span> <span class="name">tag</span>)) - - <span class="kw">def </span><span class="fun">joined_render</span>(<span class="bn bn-pseudo">self</span>, <span class="op">*</span><span class="name">args</span>): - <span class="st st-db">"""</span><span class="st"> - - Takes a number of arguments which are either strings, unicode objects - or nodes. It creates a new newlist, iterates over all arguments and - converts all to nodes if not happened by now. - </span><span class="st st-db">"""</span> - <span class="name">result</span> <span class="op">=</span> <span class="name">NodeList</span>() - <span class="kw">for</span> <span class="name">arg</span> <span class="op op-word">in</span> <span class="name">args</span>: - <span class="kw">if</span> <span class="bn">isinstance</span>(<span class="name">arg</span>, <span class="name">Node</span>): - <span class="name">result</span>.<span class="name">append</span>(<span class="name">arg</span>) - <span class="kw">else</span>: - <span class="name">result</span>.<span class="name">append</span>(<span class="name">Node</span>(<span class="name">arg</span>)) - <span class="kw">return</span> <span class="name">result</span> - - <span class="kw">def </span><span class="fun">callback</span>(<span class="bn bn-pseudo">self</span>, <span class="name">callback</span>, <span class="name">data</span>): - <span class="st st-db">"""</span><span class="st"> - Returns a new ``CallbackNode``. Don't create callback nodes on your - own, this method might do some further magic in the future. - </span><span class="st st-db">"""</span> - <span class="kw">return</span> <span class="name">CallbackNode</span>(<span class="name">callback</span>, <span class="op">*</span><span class="name">data</span>) - - - -<span class="kw">class </span><span class="cls">BBCodeTagProvider</span>(<span class="name">Component</span>): - <span class="cm">#: list of handled tags</span> - <span class="name">tags</span> <span class="op">=</span> [] - - <span class="cm">#: list of callbacks</span> - <span class="name">callbacks</span> <span class="op">=</span> [] - - <span class="kw">def </span><span class="fun">get_node</span>(<span class="bn bn-pseudo">self</span>, <span class="name">token</span>, <span class="name">parser</span>): - <span class="st st-db">"""</span><span class="st"> - - Is called when a tag is found. It must return a valid ``Node`` - or a string which is automatically wrapped into a plain ``Node``. - </span><span class="st st-db">"""</span> - - <span class="kw">def </span><span class="fun">render_callback</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>, <span class="name">callback</span>, <span class="name">data</span>): - <span class="st st-db">"""</span><span class="st"> - - Has to handle a callback for ``callback`` with ``data`` and return a - string - </span><span class="st st-db">"""</span> - <span class="kw">return</span> <span class="name">u</span><span class="st st-sg">''</span> - - <span class="kw">def </span><span class="fun">get_buttons</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>): - <span class="st st-db">"""</span><span class="st"> - - Return a valid button definition for "tagname" or - None if no button is required. - - A valid button definition is a dict in the following - form:: - - {'name': _('Bold'), - 'description': _('Insert bold text'), - 'icon': self.ctx.make_url('!cobalt/...'), - 'insert': '[b]{text}[/b]'} - </span><span class="st st-db">"""</span> - - <span class="kw">return</span> () - - -<span class="kw">class </span><span class="cls">BBCode</span>(<span class="name">MarkupFormat</span>): - <span class="st st-db">"""</span><span class="st"> - BBCode markup format. - </span><span class="st st-db">"""</span> - <span class="name">name</span> <span class="op">=</span> <span class="st st-sg">'</span><span class="st">bbcode</span><span class="st st-sg">'</span> - - <span class="name">editor_javascript</span> <span class="op">=</span> <span class="st st-sg">'</span><span class="st">!cobalt/core/pocoo/app/BBCodeEditor.js</span><span class="st st-sg">'</span> - - <span class="kw">def </span><span class="fun">__init__</span>(<span class="bn bn-pseudo">self</span>, <span class="name">ctx</span>): - <span class="bn">super</span>(<span class="name">BBCode</span>, <span class="bn bn-pseudo">self</span>).<span class="name">__init__</span>(<span class="name">ctx</span>) - <span class="bn bn-pseudo">self</span>.<span class="name">handlers</span> <span class="op">=</span> {} - <span class="bn bn-pseudo">self</span>.<span class="name">callbacks</span> <span class="op">=</span> {} - <span class="kw">for</span> <span class="name">comp</span> <span class="op op-word">in</span> <span class="name">ctx</span>.<span class="name">get_components</span>(<span class="name">BBCodeTagProvider</span>): - <span class="kw">for</span> <span class="name">tag</span> <span class="op op-word">in</span> <span class="name">comp</span>.<span class="name">tags</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">handlers</span>.<span class="name">setdefault</span>(<span class="name">tag</span>, []).<span class="name">append</span>(<span class="name">comp</span>) - <span class="kw">for</span> <span class="name">callback</span> <span class="op op-word">in</span> <span class="name">comp</span>.<span class="name">callbacks</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">callbacks</span>[<span class="name">callback</span>] <span class="op">=</span> <span class="name">comp</span> - - <span class="kw">def </span><span class="fun">get_signature_tags</span>(<span class="bn bn-pseudo">self</span>): - <span class="st st-db">"""</span><span class="st">Returns the allowed signature tags or None if all</span><span class="st st-db">"""</span> - <span class="kw">if</span> <span class="op op-word">not</span> <span class="bn">hasattr</span>(<span class="bn bn-pseudo">self</span>, <span class="st st-sg">'</span><span class="st">_signature_tags</span><span class="st st-sg">'</span>): - <span class="name">r</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">ctx</span>.<span class="name">cfg</span>.<span class="name">get</span>(<span class="st st-sg">'</span><span class="st">board</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">bbcode_signature_tags</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">ALL</span><span class="st st-sg">'</span>) - <span class="kw">if</span> <span class="name">r</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">ALL</span><span class="st st-sg">'</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">_signature_tags</span> <span class="op">=</span> <span class="bn bn-pseudo">None</span> - - <span class="kw">else</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">_signature_tags</span> <span class="op">=</span> [<span class="name">s</span>.<span class="name">strip</span>().<span class="name">lower</span>() <span class="kw">for</span> <span class="name">s</span> <span class="op op-word">in</span> <span class="name">r</span>.<span class="name">split</span>(<span class="st st-sg">'</span><span class="st">,</span><span class="st st-sg">'</span>)] - <span class="kw">return</span> <span class="bn bn-pseudo">self</span>.<span class="name">_signature_tags</span> - - <span class="kw">def </span><span class="fun">parse</span>(<span class="bn bn-pseudo">self</span>, <span class="name">text</span>, <span class="name">signature</span>): - <span class="name">handlers</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">ctx</span>.<span class="name">get_components</span>(<span class="name">BBCodeTagProvider</span>) - <span class="name">allowed_tags</span> <span class="op">=</span> <span class="bn bn-pseudo">None</span> - - <span class="kw">if</span> <span class="name">signature</span>: - <span class="name">allowed_tags</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">get_signature_tags</span>() - <span class="name">p</span> <span class="op">=</span> <span class="name">Parser</span>(<span class="bn bn-pseudo">self</span>.<span class="name">ctx</span>, <span class="name">escape_html</span>(<span class="name">text</span>), <span class="name">handlers</span>, <span class="name">allowed_tags</span>) - <span class="kw">return</span> <span class="name">p</span>.<span class="name">parse</span>() - - <span class="kw">def </span><span class="fun">render_callback</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>, <span class="name">callback</span>, <span class="name">data</span>): - <span class="st st-db">"""</span><span class="st">Redirect the callback to the BBCode Provider.</span><span class="st st-db">"""</span> - - <span class="kw">for</span> <span class="name">comp</span> <span class="op op-word">in</span> <span class="bn bn-pseudo">self</span>.<span class="name">ctx</span>.<span class="name">get_components</span>(<span class="name">BBCodeTagProvider</span>): - <span class="name">rv</span> <span class="op">=</span> <span class="name">comp</span>.<span class="name">render_callback</span>(<span class="name">req</span>, <span class="name">callback</span>, <span class="name">data</span>) - <span class="kw">if</span> <span class="name">rv</span> <span class="op op-word">is</span> <span class="op op-word">not</span> <span class="bn bn-pseudo">None</span>: - <span class="kw">return</span> <span class="name">rv</span> - - <span class="kw">raise</span> <span class="exc">Exception</span>(<span class="st st-sg">'</span><span class="st">unhandled callback </span><span class="st st-int">%r</span><span class="st st-sg">'</span> <span class="op">%</span> <span class="name">callback</span>) - - <span class="kw">def </span><span class="fun">quote_text</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>, <span class="name">text</span>, <span class="name">username</span><span class="op">=</span><span class="bn bn-pseudo">None</span>): - <span class="kw">if</span> <span class="name">username</span> <span class="op op-word">is</span> <span class="bn bn-pseudo">None</span>: - <span class="kw">return</span> <span class="st st-sg">'</span><span class="st">[quote]</span><span class="st st-int">%s</span><span class="st">[/quote]</span><span class="st st-sg">'</span> <span class="op">%</span> <span class="name">text</span> - - <span class="kw">return</span> <span class="st st-sg">'</span><span class="st">[quote="</span><span class="st st-int">%s</span><span class="st">"]</span><span class="st st-int">%s</span><span class="st">[/quote]</span><span class="st st-sg">'</span> <span class="op">%</span> (<span class="name">username</span>, <span class="name">text</span>) - - <span class="kw">def </span><span class="fun">get_editor_options</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>, <span class="name">signature</span>): - <span class="name">buttons</span> <span class="op">=</span> [] - <span class="kw">if</span> <span class="name">signature</span>: - <span class="name">signature_tags</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">get_signature_tags</span>() - <span class="kw">for</span> <span class="name">comp</span> <span class="op op-word">in</span> <span class="bn bn-pseudo">self</span>.<span class="name">ctx</span>.<span class="name">get_components</span>(<span class="name">BBCodeTagProvider</span>): - <span class="kw">for</span> <span class="name">button</span> <span class="op op-word">in</span> <span class="name">comp</span>.<span class="name">get_buttons</span>(<span class="name">req</span>): - <span class="kw">if</span> <span class="name">signature</span> <span class="op op-word">and</span> <span class="name">button</span>[<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>] <span class="op op-word">not</span> <span class="op op-word">in</span> <span class="name">signature_tags</span>: - <span class="kw">continue</span> - - <span class="name">buttons</span>.<span class="name">append</span>(<span class="name">button</span>) - <span class="kw">return</span> { - <span class="st st-sg">'</span><span class="st">buttons</span><span class="st st-sg">'</span>: <span class="name">buttons</span>, - <span class="st st-sg">'</span><span class="st">smilies</span><span class="st st-sg">'</span>: <span class="name">get_smiley_buttons</span>(<span class="name">req</span>.<span class="name">ctx</span>) - } - - - -<span class="kw">class </span><span class="cls">BasicBBCodeTagProvider</span>(<span class="name">BBCodeTagProvider</span>): - <span class="name">tags</span> <span class="op">=</span> [<span class="st st-sg">'</span><span class="st">b</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">i</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">u</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">s</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">url</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">email</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">color</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">size</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">code</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">quote</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">list</span><span class="st st-sg">'</span>] - <span class="name">callbacks</span> <span class="op">=</span> [<span class="st st-sg">'</span><span class="st">quote</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">list</span><span class="st st-sg">'</span>] - - <span class="kw">def </span><span class="fun">get_node</span>(<span class="bn bn-pseudo">self</span>, <span class="name">token</span>, <span class="name">parser</span>): - <span class="name">ctx</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">ctx</span> - - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">b</span><span class="st st-sg">'</span>: - <span class="kw">if</span> <span class="name">token</span>.<span class="name">attr</span>: - <span class="kw">return</span> - - <span class="kw">return</span> <span class="name">parser</span>.<span class="name">wrap_render</span>(<span class="st st-sg">'</span><span class="st">strong</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">/b</span><span class="st st-sg">'</span>) - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">i</span><span class="st st-sg">'</span>: - <span class="kw">if</span> <span class="name">token</span>.<span class="name">attr</span>: - <span class="kw">return</span> - - <span class="kw">return</span> <span class="name">parser</span>.<span class="name">wrap_render</span>(<span class="st st-sg">'</span><span class="st">em</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">/i</span><span class="st st-sg">'</span>) - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">u</span><span class="st st-sg">'</span>: - <span class="kw">if</span> <span class="name">token</span>.<span class="name">attr</span>: - <span class="kw">return</span> - - <span class="kw">return</span> <span class="name">parser</span>.<span class="name">wrap_render</span>(<span class="st st-sg">'</span><span class="st">ins</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">/u</span><span class="st st-sg">'</span>) - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">s</span><span class="st st-sg">'</span>: - <span class="kw">if</span> <span class="name">token</span>.<span class="name">attr</span>: - <span class="kw">return</span> - - <span class="kw">return</span> <span class="name">parser</span>.<span class="name">wrap_render</span>(<span class="st st-sg">'</span><span class="st">del</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">/s</span><span class="st st-sg">'</span>) - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">url</span><span class="st st-sg">'</span>: - <span class="kw">if</span> <span class="name">token</span>.<span class="name">attr</span>: - <span class="name">content</span> <span class="op">=</span> <span class="name">parser</span>.<span class="name">parse</span>(<span class="st st-sg">'</span><span class="st">/url</span><span class="st st-sg">'</span>) - <span class="name">url</span> <span class="op">=</span> <span class="name">token</span>.<span class="name">attr</span> - - <span class="kw">else</span>: - <span class="name">tokenlist</span> <span class="op">=</span> <span class="name">parser</span>.<span class="name">get_tokens</span>(<span class="st st-sg">'</span><span class="st">/url</span><span class="st st-sg">'</span>) - <span class="name">content</span> <span class="op">=</span> <span class="name">url</span> <span class="op">=</span> <span class="name">tokenlist</span>.<span class="name">flatten</span>() - <span class="kw">if</span> <span class="name">url</span>.<span class="name">startswith</span>(<span class="st st-sg">'</span><span class="st">javascript:</span><span class="st st-sg">'</span>): - <span class="name">url</span> <span class="op">=</span> <span class="name">url</span>[<span class="nb nb-int">11</span>:] - <span class="kw">return</span> <span class="name">parser</span>.<span class="name">joined_render</span>(<span class="st st-sg">'</span><span class="st"><a href="</span><span class="st st-sg">'</span>, <span class="name">url</span>, <span class="st st-sg">'</span><span class="st">"></span><span class="st st-sg">'</span>, <span class="name">content</span>, <span class="st st-sg">'</span><span class="st"></a></span><span class="st st-sg">'</span>) - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">email</span><span class="st st-sg">'</span>: - <span class="kw">if</span> <span class="name">token</span>.<span class="name">attr</span>: - <span class="name">content</span> <span class="op">=</span> <span class="name">parser</span>.<span class="name">parse</span>(<span class="st st-sg">'</span><span class="st">/email</span><span class="st st-sg">'</span>) - <span class="name">mail</span> <span class="op">=</span> <span class="name">token</span>.<span class="name">attr</span> - - <span class="kw">else</span>: - <span class="name">tokenlist</span> <span class="op">=</span> <span class="name">parser</span>.<span class="name">get_tokens</span>(<span class="st st-sg">'</span><span class="st">/email</span><span class="st st-sg">'</span>) - <span class="name">mail</span> <span class="op">=</span> <span class="name">content</span> <span class="op">=</span> <span class="name">tokenlist</span>.<span class="name">flatten</span>() - <span class="kw">return</span> <span class="name">parser</span>.<span class="name">joined_render</span>(<span class="st st-sg">'</span><span class="st"><a href="mailto:"</span><span class="st st-sg">'</span>, <span class="name">mail</span>, <span class="st st-sg">'</span><span class="st">"></span><span class="st st-sg">'</span>, - <span class="name">content</span>, <span class="st st-sg">'</span><span class="st"></a></span><span class="st st-sg">'</span>) - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">color</span><span class="st st-sg">'</span>: - <span class="name">content</span> <span class="op">=</span> <span class="name">parser</span>.<span class="name">parse</span>(<span class="st st-sg">'</span><span class="st">/color</span><span class="st st-sg">'</span>) - <span class="kw">try</span>: - <span class="name">color</span> <span class="op">=</span> <span class="name">translate_color</span>(<span class="name">token</span>.<span class="name">attr</span>) - <span class="kw">except</span> <span class="exc">ValueError</span>: - <span class="kw">return</span> <span class="name">token</span>.<span class="name">raw</span> - - <span class="kw">return</span> <span class="name">parser</span>.<span class="name">joined_render</span>(<span class="st st-sg">'</span><span class="st"><span style="color: </span><span class="st st-sg">'</span>, <span class="name">color</span>, <span class="st st-sg">'</span><span class="st">"></span><span class="st st-sg">'</span>, - <span class="name">content</span>, <span class="st st-sg">'</span><span class="st"></span></span><span class="st st-sg">'</span>) - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">size</span><span class="st st-sg">'</span>: - <span class="name">content</span> <span class="op">=</span> <span class="name">parser</span>.<span class="name">parse</span>(<span class="st st-sg">'</span><span class="st">/size</span><span class="st st-sg">'</span>) - <span class="kw">if</span> <span class="op op-word">not</span> <span class="name">token</span>.<span class="name">attr</span> <span class="op op-word">or</span> <span class="op op-word">not</span> <span class="name">token</span>.<span class="name">attr</span>.<span class="name">isdigit</span>() <span class="op op-word">or</span> <span class="bn">len</span>(<span class="name">token</span>.<span class="name">attr</span>) <span class="op">></span> <span class="nb nb-int">2</span>: - <span class="kw">return</span> <span class="name">token</span>.<span class="name">raw</span> - - <span class="kw">return</span> <span class="name">parser</span>.<span class="name">joined_render</span>(<span class="st st-sg">'</span><span class="st"><span style="font-size: </span><span class="st st-sg">'</span>, <span class="name">token</span>.<span class="name">attr</span>, - <span class="st st-sg">'</span><span class="st">px"></span><span class="st st-sg">'</span>, <span class="name">content</span>, <span class="st st-sg">'</span><span class="st"></span></span><span class="st st-sg">'</span>) - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">img</span><span class="st st-sg">'</span>: - <span class="kw">if</span> <span class="name">token</span>.<span class="name">attr</span>: - <span class="kw">return</span> - - <span class="name">tokenlist</span> <span class="op">=</span> <span class="name">parser</span>.<span class="name">get_tokens</span>(<span class="st st-sg">'</span><span class="st">/img</span><span class="st st-sg">'</span>) - <span class="name">url</span> <span class="op">=</span> <span class="name">tokenlist</span>.<span class="name">flatten</span>() - <span class="kw">if</span> <span class="name">url</span>.<span class="name">startswith</span>(<span class="st st-sg">'</span><span class="st">javascript:</span><span class="st st-sg">'</span>): - <span class="name">url</span> <span class="op">=</span> <span class="name">url</span>[<span class="nb nb-int">11</span>:] - <span class="kw">return</span> <span class="name">u</span><span class="st st-sg">'</span><span class="st"><img src="</span><span class="st st-int">%s</span><span class="st">" /></span><span class="st st-sg">'</span> <span class="op">%</span> <span class="name">url</span> - - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">code</span><span class="st st-sg">'</span>: - <span class="kw">if</span> <span class="name">token</span>.<span class="name">attr</span>: - <span class="kw">return</span> - - <span class="kw">return</span> <span class="name">u</span><span class="st st-sg">'</span><span class="st"><pre></span><span class="st st-int">%s</span><span class="st"></pre></span><span class="st st-sg">'</span> <span class="op">%</span> <span class="name">parser</span>.<span class="name">get_tokens</span>(<span class="st st-sg">'</span><span class="st">/code</span><span class="st st-sg">'</span>).<span class="name">flatten</span>() - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">quote</span><span class="st st-sg">'</span>: - <span class="kw">return</span> <span class="name">parser</span>.<span class="name">callback</span>(<span class="st st-sg">'</span><span class="st">quote</span><span class="st st-sg">'</span>, (<span class="name">token</span>.<span class="name">attr</span> <span class="op op-word">or</span> <span class="name">u</span><span class="st st-sg">''</span>, - <span class="name">parser</span>.<span class="name">parse</span>(<span class="st st-sg">'</span><span class="st">/quote</span><span class="st st-sg">'</span>))) - <span class="kw">if</span> <span class="name">token</span>.<span class="name">name</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">list</span><span class="st st-sg">'</span>: - <span class="kw">return</span> <span class="name">parser</span>.<span class="name">callback</span>(<span class="st st-sg">'</span><span class="st">list</span><span class="st st-sg">'</span>, (<span class="name">token</span>.<span class="name">attr</span> <span class="op op-word">or</span> <span class="name">u</span><span class="st st-sg">'</span><span class="st">*</span><span class="st st-sg">'</span>, - <span class="name">parser</span>.<span class="name">parse</span>(<span class="st st-sg">'</span><span class="st">/list</span><span class="st st-sg">'</span>))) - - <span class="kw">def </span><span class="fun">render_callback</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>, <span class="name">callback</span>, <span class="name">data</span>): - <span class="kw">if</span> <span class="name">callback</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">quote</span><span class="st st-sg">'</span>: - <span class="name">_</span> <span class="op">=</span> <span class="name">req</span>.<span class="name">gettext</span> - - <span class="name">written</span>, <span class="name">body</span> <span class="op">=</span> <span class="name">data</span> - <span class="kw">if</span> <span class="name">written</span>: - <span class="kw">if</span> <span class="op op-word">not</span> <span class="name">written</span>.<span class="name">endswith</span>(<span class="st st-sg">'</span><span class="st">:</span><span class="st st-sg">'</span>): - <span class="name">written</span> <span class="op">=</span> (<span class="name">_</span>(<span class="st st-sg">'</span><span class="st st-int">%s</span><span class="st"> wrote</span><span class="st st-sg">'</span>) <span class="op">%</span> <span class="name">written</span>) <span class="op">+</span> <span class="name">u</span><span class="st st-sg">'</span><span class="st">:</span><span class="st st-sg">'</span> - - <span class="name">written</span> <span class="op">=</span> <span class="name">u</span><span class="st st-sg">'</span><span class="st"><div class="written_by"></span><span class="st st-int">%s</span><span class="st"></div></span><span class="st st-sg">'</span> <span class="op">%</span> <span class="name">written</span> - <span class="kw">return</span> <span class="name">u</span><span class="st st-sg">'</span><span class="st"><blockquote></span><span class="st st-int">%s%s</span><span class="st"></blockquote></span><span class="st st-sg">'</span> <span class="op">%</span> ( - <span class="name">written</span>, <span class="name">body</span>.<span class="name">render</span>(<span class="name">req</span>, <span class="bn bn-pseudo">self</span>) - ) - <span class="kw">if</span> <span class="name">callback</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">list</span><span class="st st-sg">'</span>: - <span class="bn">type</span>, <span class="name">body</span> <span class="op">=</span> <span class="name">data</span> - - <span class="name">lines</span> <span class="op">=</span> [] - <span class="kw">for</span> <span class="name">line</span> <span class="op op-word">in</span> <span class="name">re</span>.<span class="name">split</span>(<span class="st st-sg">r'</span><span class="st">^\s*\[\*\](?m)</span><span class="st st-sg">'</span>, <span class="name">body</span>.<span class="name">render</span>(<span class="name">req</span>, <span class="bn bn-pseudo">self</span>)): - <span class="name">line</span> <span class="op">=</span> <span class="name">line</span>.<span class="name">strip</span>() - <span class="kw">if</span> <span class="name">line</span>: - <span class="name">lines</span>.<span class="name">append</span>(<span class="name">u</span><span class="st st-sg">'</span><span class="st"><li></span><span class="st st-int">%s</span><span class="st"></li></span><span class="st st-sg">'</span> <span class="op">%</span> <span class="name">line</span>) - <span class="kw">return</span> <span class="name">u</span><span class="st st-sg">'</span><span class="st"><ul></span><span class="st st-int">%s</span><span class="st"></ul></span><span class="st st-sg">'</span> <span class="op">%</span> <span class="name">u</span><span class="st st-sg">'</span><span class="st st-esc">\n</span><span class="st st-sg">'</span>.<span class="name">join</span>(<span class="name">lines</span>) - - <span class="kw">def </span><span class="fun">get_buttons</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>): - <span class="name">_</span> <span class="op">=</span> <span class="name">req</span>.<span class="name">gettext</span> - - <span class="name">make_url</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">ctx</span>.<span class="name">make_url</span> - <span class="cm">#XXX: themeable</span> - <span class="name">icon_url</span> <span class="op">=</span> <span class="kw">lambda</span> <span class="name">x</span>: <span class="name">make_url</span>(<span class="st st-sg">'</span><span class="st">!cobalt/core/default/img/bbcode/</span><span class="st st-sg">'</span> <span class="op">+</span> <span class="name">x</span>) - - <span class="kw">return</span> [ - {<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">b</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">name</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Bold</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">description</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Insert bold text</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">insert</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">[b]{text}[/b]</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">icon</span><span class="st st-sg">'</span>: <span class="name">icon_url</span>(<span class="st st-sg">'</span><span class="st">bold.png</span><span class="st st-sg">'</span>)}, - {<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">i</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">name</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Italic</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">description</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Insert italic text</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">insert</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">[i]{text}[/i]</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">icon</span><span class="st st-sg">'</span>: <span class="name">icon_url</span>(<span class="st st-sg">'</span><span class="st">italic.png</span><span class="st st-sg">'</span>)}, - {<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">u</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">name</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Underline</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">description</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Insert underlined text</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">insert</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">[u]{text}[/u]</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">icon</span><span class="st st-sg">'</span>: <span class="name">icon_url</span>(<span class="st st-sg">'</span><span class="st">underline.png</span><span class="st st-sg">'</span>)}, - {<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">s</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">name</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Strikethrough</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">description</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Insert striked text</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">insert</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">[i]{text}[/i]</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">icon</span><span class="st st-sg">'</span>: <span class="name">icon_url</span>(<span class="st st-sg">'</span><span class="st">strikethrough.png</span><span class="st st-sg">'</span>)}, - {<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">size</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">name</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Font Size</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">description</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Change the font size</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">insert</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">[size={attr}]{text}[/size]</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">values</span><span class="st st-sg">'</span>: [ - (<span class="nb nb-int">8</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Tiny</span><span class="st st-sg">'</span>)), - (<span class="nb nb-int">11</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Small</span><span class="st st-sg">'</span>)), - (<span class="nb nb-int">13</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Normal</span><span class="st st-sg">'</span>)), - (<span class="nb nb-int">18</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Big</span><span class="st st-sg">'</span>)), - (<span class="nb nb-int">24</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Huge</span><span class="st st-sg">'</span>)) - ]}, - {<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">color</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">name</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Font Color</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">description</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Change Font Color</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">insert</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">[color={attr}]{text}[/size]</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">values</span><span class="st st-sg">'</span>: [ - (<span class="st st-sg">'</span><span class="st">black</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Black</span><span class="st st-sg">'</span>)), - (<span class="st st-sg">'</span><span class="st">blue</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Blue</span><span class="st st-sg">'</span>)), - (<span class="st st-sg">'</span><span class="st">brown</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Brown</span><span class="st st-sg">'</span>)), - (<span class="st st-sg">'</span><span class="st">cyan</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Cyan</span><span class="st st-sg">'</span>)), - (<span class="st st-sg">'</span><span class="st">gray</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Gray</span><span class="st st-sg">'</span>)), - (<span class="st st-sg">'</span><span class="st">green</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Green</span><span class="st st-sg">'</span>)), - (<span class="st st-sg">'</span><span class="st">magenta</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Magenta</span><span class="st st-sg">'</span>)), - (<span class="st st-sg">'</span><span class="st">purple</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Purple</span><span class="st st-sg">'</span>)), - (<span class="st st-sg">'</span><span class="st">red</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Red</span><span class="st st-sg">'</span>)), - (<span class="st st-sg">'</span><span class="st">white</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">White</span><span class="st st-sg">'</span>)), - (<span class="st st-sg">'</span><span class="st">yellow</span><span class="st st-sg">'</span>, <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Yellow</span><span class="st st-sg">'</span>)) - ]}, - {<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">url</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">name</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Link</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">description</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Create a Link</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">icon</span><span class="st st-sg">'</span>: <span class="name">icon_url</span>(<span class="st st-sg">'</span><span class="st">link.png</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">insert</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">[url]{text}[/url]</span><span class="st st-sg">'</span>}, - {<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">img</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">name</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Image</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">description</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Insert an image</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">icon</span><span class="st st-sg">'</span>: <span class="name">icon_url</span>(<span class="st st-sg">'</span><span class="st">img.png</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">insert</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">[img]{text}[/img]</span><span class="st st-sg">'</span>}, - {<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">code</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">name</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Code</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">description</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Insert a codeblock</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">icon</span><span class="st st-sg">'</span>: <span class="name">icon_url</span>(<span class="st st-sg">'</span><span class="st">code.png</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">insert</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">[code]{text}[/code]</span><span class="st st-sg">'</span>}, - {<span class="st st-sg">'</span><span class="st">tagname</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">quote</span><span class="st st-sg">'</span>, - <span class="st st-sg">'</span><span class="st">name</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Quote</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">description</span><span class="st st-sg">'</span>: <span class="name">_</span>(<span class="st st-sg">'</span><span class="st">Insert a blockquote</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">icon</span><span class="st st-sg">'</span>: <span class="name">icon_url</span>(<span class="st st-sg">'</span><span class="st">quote.png</span><span class="st st-sg">'</span>), - <span class="st st-sg">'</span><span class="st">insert</span><span class="st st-sg">'</span>: <span class="st st-sg">'</span><span class="st">[quote]{text}[/quote]</span><span class="st st-sg">'</span>} - ] - -<span class="cm"># -*- coding: utf-8 -*-</span> -<span class="st st-db">"""</span><span class="st"> - pocoo.pkg.core.cache - ~~~~~~~~~~~~~~~~~~~~ - - Provides a very simple caching system for persistent processes. - - :copyright: 2006-2007 by Armin Ronacher. - :license: GNU GPL, see LICENSE for more details. -</span><span class="st st-db">"""</span> -<span class="kw">from </span><span class="cls">pocoo.application</span><span class="kw"> import</span> <span class="name">RequestWrapper</span> -<span class="kw">from </span><span class="cls">pocoo.exceptions</span><span class="kw"> import</span> <span class="name">PocooRuntimeError</span> - -<span class="kw">from </span><span class="cls">pocoo.utils.cache</span><span class="kw"> import</span> <span class="name">Cache</span> - -<span class="cm"># This is currently unused.</span> - -<span class="kw">class </span><span class="cls">CacheSystem</span>(<span class="name">RequestWrapper</span>): - - <span class="kw">def </span><span class="fun">__init__</span>(<span class="bn bn-pseudo">self</span>, <span class="name">ctx</span>): - <span class="bn bn-pseudo">self</span>.<span class="name">cache</span> <span class="op">=</span> <span class="name">Cache</span>(<span class="name">autoprune</span><span class="op">=</span><span class="name">ctx</span>.<span class="name">cfg</span>.<span class="name">get</span>(<span class="st st-sg">'</span><span class="st">cache</span><span class="st st-sg">'</span>, <span class="st st-sg">'</span><span class="st">autoprune</span><span class="st st-sg">'</span>, <span class="bn bn-pseudo">False</span>)) - <span class="bn bn-pseudo">self</span>.<span class="name">uri2key</span> <span class="op">=</span> {} - <span class="name">RequestWrapper</span>.<span class="name">__init__</span>(<span class="bn bn-pseudo">self</span>, <span class="name">ctx</span>) - - <span class="kw">def </span><span class="fun">get_priority</span>(<span class="bn bn-pseudo">self</span>): - <span class="cm"># caching has highest priority</span> - - <span class="kw">return</span> <span class="nb nb-int">1</span> - - <span class="kw">def </span><span class="fun">process_request</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>): - <span class="name">req</span>.<span class="name">cache_control</span> <span class="op">=</span> <span class="bn bn-pseudo">None</span> - - <span class="name">req</span>.<span class="name">cache</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">cache</span> - - <span class="kw">if</span> <span class="name">req</span>.<span class="name">environ</span>[<span class="st st-sg">'</span><span class="st">REQUEST_METHOD</span><span class="st st-sg">'</span>] <span class="op">!=</span> <span class="st st-sg">'</span><span class="st">GET</span><span class="st st-sg">'</span>: - <span class="kw">return</span> - - <span class="kw">if</span> <span class="name">req</span>.<span class="name">environ</span>[<span class="st st-sg">'</span><span class="st">REQUEST_URI</span><span class="st st-sg">'</span>] <span class="op op-word">not</span> <span class="op op-word">in</span> <span class="bn bn-pseudo">self</span>.<span class="name">uri2key</span>: - <span class="kw">return</span> - - <span class="name">key</span> <span class="op">=</span> <span class="bn bn-pseudo">self</span>.<span class="name">uri2key</span>[<span class="name">req</span>.<span class="name">environ</span>[<span class="st st-sg">'</span><span class="st">REQUEST_URI</span><span class="st st-sg">'</span>]] - <span class="kw">return</span> <span class="bn bn-pseudo">self</span>.<span class="name">cache</span>.<span class="name">fetch</span>(<span class="name">key</span>, <span class="bn bn-pseudo">None</span>) - - <span class="kw">def </span><span class="fun">process_response</span>(<span class="bn bn-pseudo">self</span>, <span class="name">req</span>, <span class="name">resp</span>): - <span class="kw">if</span> <span class="op op-word">not</span> <span class="name">req</span>.<span class="name">cache_control</span>: - <span class="kw">return</span> <span class="name">resp</span> - - <span class="name">action</span>, <span class="name">key</span> <span class="op">=</span> <span class="name">req</span>.<span class="name">cache_control</span> - <span class="kw">if</span> <span class="name">action</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">set</span><span class="st st-sg">'</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">cache</span>.<span class="name">dump</span>(<span class="name">key</span>, <span class="name">resp</span>) - <span class="bn bn-pseudo">self</span>.<span class="name">uri2key</span>[<span class="name">req</span>.<span class="name">environ</span>[<span class="st st-sg">'</span><span class="st">REQUEST_URI</span><span class="st st-sg">'</span>]] <span class="op">=</span> <span class="name">key</span> - - <span class="kw">elif</span> <span class="name">action</span> <span class="op">==</span> <span class="st st-sg">'</span><span class="st">update</span><span class="st st-sg">'</span>: - <span class="kw">if</span> <span class="bn">isinstance</span>(<span class="name">key</span>, <span class="bn">basestring</span>): - <span class="bn bn-pseudo">self</span>.<span class="name">cache</span>.<span class="name">remove</span>(<span class="name">key</span>) - <span class="kw">else</span>: - <span class="kw">for</span> <span class="name">k</span> <span class="op op-word">in</span> <span class="name">key</span>: - <span class="bn bn-pseudo">self</span>.<span class="name">cache</span>.<span class="name">remove</span>(<span class="name">k</span>) - <span class="kw">else</span>: - <span class="kw">raise</span> <span class="name">PocooRuntimeError</span>(<span class="st st-sg">'</span><span class="st">req.cache_control invalid</span><span class="st st-sg">'</span>) |