<commit_before>"""
Rich text area widget.
"""

from nevow import inevow, loaders, rend, tags as T, util
from formal import iformal, widget, types
from formal.util import render_cssid
from zope.interface import Interface

class RichTextArea(widget.TextArea):
    """
    A large text entry area that can be used for different formats of formatted text (rest, html, markdown, texy)
    """
    def __init__(self, original, **kwds):
        parsers = kwds.pop('parsers', None)
        super(RichTextArea, self).__init__(original, **kwds)
        self.parsers = parsers

    def _namer(self, prefix):
        def _(part):
            return '%s__%s' % (prefix,part)
        return _

    def _renderTag(self, ctx, tparser, tvalue, namer, readonly):
        tag=T.invisible()
        if len(self.parsers) > 1:
            tp = T.select(name=namer('tparser'),id=render_cssid(namer('tparser')))
            for k,v in self.parsers:
                if k == tparser:
                    tp[T.option(selected='selected',value=k)[ v ]]
                else:
                    tp[T.option(value=k)[ v ]]
        else:
            tp = T.input(type='hidden',name=namer('tparser'),id=render_cssid(namer('tparser')),value=self.parsers[0][0])
        ta=T.textarea(name=namer('tvalue'), id=render_cssid(namer('tvalue')), cols=self.cols, rows=self.rows)[tvalue or '']
        if readonly:
            ta(class_='readonly', readonly='readonly')
        tag[tp,ta]
        return tag


    def render(self, ctx, key, args, errors):
        namer = self._namer(key)
        if errors:
            tparser = args.get(namer('tparser'), [''])[0]
            tvalue = args.get(namer('tvalue'), [''])[0]
        else:
            value = args.get(key)
            if value is not None:
                tparser = value.type
                tvalue = value.value
            else:
                tparser = None
                tvalue = ''
        
        return self._renderTag(ctx, tparser, tvalue, namer, False)
        
    def renderImmutable(self, ctx, key, args, errors):
        tparsers = self.original.type
        tvalue = self.original.value
        namer = self._namer(key)
        tparser = args.get(namer('tp'), [''])[0]
        tvalue = args.get(key, [''])[0]
        return self._renderTag(ctx, tparser, tvalue, namer, True)
    
    def processInput(self, ctx, key, args):
        namer = self._namer(key)
        value = [args.get(namer(part), [''])[0].strip().decode(util.getPOSTCharset(ctx)) for part in ('tparser', 'tvalue')]
        return self.original.validate(types.RichText(*value))



    


    <commit_msg>fixed the immutable settings (readonly, etc)<commit_after>"""
Rich text area widget.
"""

from nevow import inevow, loaders, rend, tags as T, util
from formal import iformal, widget, types
from formal.util import render_cssid
from zope.interface import Interface

class RichTextArea(widget.TextArea):
    """
    A large text entry area that can be used for different formats of formatted text (rest, html, markdown, texy)
    """
    def __init__(self, original, **kwds):
        parsers = kwds.pop('parsers', None)
        super(RichTextArea, self).__init__(original, **kwds)
        self.parsers = parsers

    def _namer(self, prefix):
        def _(part):
            return '%s__%s' % (prefix,part)
        return _

    def _renderTag(self, ctx, tparser, tvalue, namer, readonly):
        tag=T.invisible()
        if len(self.parsers) > 1:
            tp = T.select(name=namer('tparser'),id=render_cssid(namer('tparser')))
            if readonly:
                tp(class_='disabled', disabled='disabled')        
            
            for k,v in self.parsers:
                if k == tparser:
                    tp[T.option(selected='selected',value=k)[ v ]]
                else:
                    tp[T.option(value=k)[ v ]]
        else:
            tp = T.input(type='hidden',name=namer('tparser'),id=render_cssid(namer('tparser')),value=self.parsers[0][0])
        ta=T.textarea(name=namer('tvalue'), id=render_cssid(namer('tvalue')), cols=self.cols, rows=self.rows)[tvalue or '']
        if readonly:
            ta(class_='readonly', readonly='readonly')
        tag[tp,ta]
        return tag


    def render(self, ctx, key, args, errors):
        namer = self._namer(key)
        if errors:
            tparser = args.get(namer('tparser'), [''])[0]
            tvalue = args.get(namer('tvalue'), [''])[0]
        else:
            value = args.get(key)
            if value is not None:
                tparser = value.type
                tvalue = value.value
            else:
                tparser = None
                tvalue = ''
        
        return self._renderTag(ctx, tparser, tvalue, namer, False)
        
    def renderImmutable(self, ctx, key, args, errors):
        namer = self._namer(key)
        if errors:
            tparser = args.get(namer('tparser'), [''])[0]
            tvalue = args.get(namer('tvalue'), [''])[0]
        else:
            value = args.get(key)
            if value is not None:
                tparser = value.type
                tvalue = value.value
            else:
                tparser = None
                tvalue = ''
        
        return self._renderTag(ctx, tparser, tvalue, namer, True)
    
    def processInput(self, ctx, key, args):
        namer = self._namer(key)
        value = [args.get(namer(part), [''])[0].strip().decode(util.getPOSTCharset(ctx)) for part in ('tparser', 'tvalue')]
        return self.original.validate(types.RichText(*value))



    


    
{'fraction_non_alphanumeric': 0.1362901777697971, 'fraction_numerical': 0.002873047225713773, 'mean_word_length': 2.1204481792717087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4528937', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1708, 'n_words': 403}