Skip to content. | Skip to navigation

Personal tools


You are here: Home / Tips / HTML and URL Transforms

HTML and URL Transforms

惡意標籤的過濾或網址轉換,包括內容的 Search Replace 功能。

Python Search and Replace

Python3 的 fileinput 支援 Inplace Editing 把 stdout 轉到檔案。

import fileinput

with fileinput.FileInput(fileToSearch, inplace=True, backup='.bak') as file:
    for line in file:
        print)line.replace(textToSearch, textToReplace), end='')

搭配 Regular Expression 的方法

import fileinput
import re
for line in fileinput.input():
    line = re.sub(r'\* \[(.*)\]\(#(.*)\)', r'<h2 id="\2">\1</h2>', line.rstrip())

HTML Transform

HTML Transform in RichTextValue Returns Invalid <iframe> 被修改

collective.searchandreplace href 裡的網址似乎無法更換 取消 Fast Search 後會發生 AttributeError: recurrence 錯誤

rt.bulkmodify 能夠把 Internal Link 轉成 ResolveUID 連結 據說 Dexterity 尚未直接支援

GoReplace 是舊版模組


lxml.html.clean.Cleaner: htmllaundry

lxml compatibility: collective.vdexvocabulary


from lxml import html
The method:
    def get_body_image(self, brain):
        object = brain.getObject()
        if base_hasattr(object, 'Text'):
            text = object.getText()
            tree = html.fromstring(text)
            imgs = tree.xpath('//img')
            if imgs:
                my_img = imgs[0]
                my_img_path = my_img.attrib['src']
                return my_img_path
            return None
        return None

URL Transform

Allow URL with round brackets: regular expression gotcha to apply first match only

ERROR PortalTransforms Cannot register transform text_to_emoticons url_to_hyperlink (ImportError), using BrokenTransform: Error

The above error is related with Plonebord uninstallation.