Python django.utils.text 模块,normalize_newlines() 实例源码

我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用django.utils.text.normalize_newlines()

项目:alterchef    作者:libremesh    | 项目源码 | 文件源码
def write_to_disk(self):
        """
        Write profile to disk so that openwrt ImageBuilder can pickup and build the image.
        """
        to_path = os.path.join(settings.NETWORK_INCLUDES_PATH, self.network.slug, self.name)
        if not os.path.exists(to_path):
            os.makedirs(to_path)

        inc_packages = IncludePackages.from_str(self.include_packages)
        inc_packages.dump(open(os.path.join(to_path, "include_packages"), "w"))
        files = {}
        for fname, content in self.include_files.iteritems():
            t = Template(content)
            c = Context({"NETWORK_NAME": self.network.name,
                         "SSH_KEYS": "\n".join([k.key for k in self.ssh_keys.all()])},
                        autoescape=False)
            files[fname] = normalize_newlines(t.render(c))

        inc_files = IncludeFiles(files)
        inc_files.dump(os.path.join(to_path, "include_files"))
项目:CodingDojo    作者:ComputerSocietyUNB    | 项目源码 | 文件源码
def linebreaks(value, autoescape=False):
    """Converts newlines into <p> and <br />s."""
    value = normalize_newlines(value)
    paras = re.split('\n{2,}', value)
    if autoescape:
        paras = ['<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
    else:
        paras = ['<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
    return '\n\n'.join(paras)
项目:dart    作者:lmco    | 项目源码 | 文件源码
def standardize_report_output_field(field_content):
    # Normalize newlines replaces \r\n from windows with \n
    standardized_text = normalize_newlines(field_content)

    # Replace blank fields with "N/A"
    if len(standardized_text) == 0:
        standardized_text = "N/A"

    return standardized_text
项目:stormtrooper    作者:CompileInc    | 项目源码 | 文件源码
def remove_newlines(text):
    """
    Removes all newline characters from a block of text.
    """
    # First normalize the newlines using Django's nifty utility
    normalized_text = normalize_newlines(text)
    # Then simply remove the newlines like so.
    return mark_safe(normalized_text.replace('\n', ' '))
项目:lifesoundtrack    作者:MTG    | 项目源码 | 文件源码
def linebreaks(value, autoescape=False):
    """Converts newlines into <p> and <br />s."""
    value = normalize_newlines(force_text(value))
    paras = re.split('\n{2,}', value)
    if autoescape:
        paras = ['<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
    else:
        paras = ['<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
    return '\n\n'.join(paras)
项目:liberator    作者:libscie    | 项目源码 | 文件源码
def linebreaks(value, autoescape=False):
    """Converts newlines into <p> and <br />s."""
    value = normalize_newlines(force_text(value))
    paras = re.split('\n{2,}', value)
    if autoescape:
        paras = ['<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
    else:
        paras = ['<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
    return '\n\n'.join(paras)
项目:djanoDoc    作者:JustinChavez    | 项目源码 | 文件源码
def linebreaks(value, autoescape=False):
    """Converts newlines into <p> and <br />s."""
    value = normalize_newlines(value)
    paras = re.split('\n{2,}', value)
    if autoescape:
        paras = ['<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
    else:
        paras = ['<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
    return '\n\n'.join(paras)
项目:alterchef    作者:libremesh    | 项目源码 | 文件源码
def include_files(self):
        files = {}
        for form in self.cleaned_data:
            if form.get("DELETE"):
                continue
            files[form["path"]] = normalize_newlines(form["content"])
        return files
项目:alterchef    作者:libremesh    | 项目源码 | 文件源码
def dump(self, to_path):
        """
        Write all include files to disk
        """
        if os.path.exists(to_path):
            shutil.rmtree(to_path)
        for abspath, filecontent in self.files.iteritems():
            to_dir = os.path.join(to_path, os.path.dirname(abspath[1:]))
            filename = os.path.basename(abspath)
            if not os.path.exists(to_dir):
                os.makedirs(to_dir)
            with codecs.open(os.path.join(to_dir, filename), "w", "utf-8") as f:
                f.write(normalize_newlines(filecontent))
项目:django-next-train    作者:bitpixdigital    | 项目源码 | 文件源码
def linebreaks(value, autoescape=False):
    """Converts newlines into <p> and <br />s."""
    value = normalize_newlines(value)
    paras = re.split('\n{2,}', value)
    if autoescape:
        paras = ['<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
    else:
        paras = ['<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
    return '\n\n'.join(paras)
项目:nsfw    作者:vied12    | 项目源码 | 文件源码
def angular_templates():
    partials_dir = settings.STATICFILES_DIRS[0]
    exclude = ('bower_components',)
    for (root, dirs, files) in os.walk(partials_dir, topdown=True):
        dirs[:] = [d for d in dirs if d not in exclude]
        for file_name in files:
            if file_name.endswith('.html'):
                file_path = os.path.join(root, file_name)
                with open(file_path, 'rb') as fh:
                    file_name = file_path[len(partials_dir) + 1:]
                    yield (file_name, normalize_newlines(fh.read().decode('utf-8')).replace('\n', ' '))
项目:mogan-ui    作者:openstack    | 项目源码 | 文件源码
def clean_uploaded_files(self, prefix, files):
        upload_str = prefix + "_upload"

        has_upload = upload_str in files
        if has_upload:
            upload_file = files[upload_str]
            log_script_name = upload_file.name
            LOG.info('got upload %s', log_script_name)

            if upload_file._size > 16 * units.Ki:  # 16kb
                msg = _('File exceeds maximum size (16kb)')
                raise forms.ValidationError(msg)
            else:
                script = upload_file.read()
                if script != "":
                    try:
                        normalize_newlines(script)
                    except Exception as e:
                        msg = _('There was a problem parsing the'
                                ' %(prefix)s: %(error)s')
                        msg = msg % {'prefix': prefix,
                                     'error': six.text_type(e)}
                        raise forms.ValidationError(msg)
                return script
        else:
            return None
项目:mogan-ui    作者:openstack    | 项目源码 | 文件源码
def handle(self, request, context):
        custom_script = context.get('script_data', '')
        image_id = context.get('image_id', None)
        netids = context.get('network_id', None)
        if netids:
            nics = [{"net_id": netid} for netid in netids]
        else:
            nics = None

        avail_zone = context.get('availability_zone', None)
        if not avail_zone:
            avail_zone = None

        try:
            mogan.server_create(request,
                                name=context['name'],
                                image=image_id,
                                flavor=context['flavor'],
                                key_name=context['keypair_id'],
                                user_data=normalize_newlines(custom_script),
                                nics=nics,
                                availability_zone=avail_zone,
                                server_count=int(context['count']))
            return True
        except Exception:
            exceptions.handle(request)
        return False
项目:django-wechat-api    作者:crazy-canux    | 项目源码 | 文件源码
def linebreaks(value, autoescape=False):
    """Converts newlines into <p> and <br />s."""
    value = normalize_newlines(value)
    paras = re.split('\n{2,}', value)
    if autoescape:
        paras = ['<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
    else:
        paras = ['<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
    return '\n\n'.join(paras)