I believe everyone knows the topic, such as Weibo topic, click on the topic to display all the posts under the topic
The main technology stack is to replace the topic with a hyperlink label that can be clicked
The following code shows (python, tornado framework implementation) >>>>>>>>
# 正则替换
async def change_object(self, sublist, stype):
re_dict = {
"talk": {"re": r'#([\w]+)#', "model": TalkModel, "column": "TalkModel.name.in_(items)",
"getkey": "myid.name", "replace": "#{}#", "a": "<a href='/talk?tid={}'>#{}#</a>"}
}
myre = re_dict[stype]["re"]
for x in sublist:
# 替换话题
items_dict = {}
items = re.findall(myre, x["content"])
ids = await self.database.execute(
re_dict[stype]["model"].select().where(eval(re_dict[stype]["column"])))
for myid in ids:
items_dict[eval(re_dict[stype]["getkey"])] = myid.id
for item in items:
try:
x["content"] = x["content"].replace(re_dict[stype]["replace"].format(item),
re_dict[stype]["a"].format(items_dict[item], item))
except Exception as e:
print(str(e))
pass
return sublist
This code, using the idea of encapsulation, encapsulates the replacement as a method for easy calling and reduces code reuse!
After the replacement, the front-end can convert the obtained replacement content into tags using v-html, and get the specified tag id by clicking the hyperlink to get all the content under the tag!
There are many similar ones that need to be replaced. @Friends is also the same principle. Interested friends can try it