Commit 7b669154 authored by jaywink's avatar jaywink
Browse files

Merge branch 'tags' into 'master'

Extract tag linkifier to the federation library

Closes #571

See merge request socialhome/socialhome!557
parents 44c6e859 b2effa84
Pipeline #3583 passed with stages
in 14 minutes and 34 seconds
......@@ -561,5 +561,5 @@ FEDERATION = {
"nodeinfo2_function": "socialhome.federate.utils.generic.get_nodeinfo2_data",
"process_payload_function": "socialhome.federate.utils.generic.queue_payload",
"search_path": "/search/?q=",
"tags_path": "/tag/:tag:/",
"tags_path": "/streams/tag/:tag:/",
}
......@@ -75,7 +75,9 @@ Internal changes
* Store an Activity on follow/unfollow. This allows retriggering follows/unfollows in
the event of a regression.
* Move `socialhome.content.utils.process_text_links` to the `federation` library.
* Move ``socialhome.content.utils.process_text_links`` to the ``federation`` library.
* Move the tags extraction logic from ``Conten`` model to the ``federation`` library.
0.11.1 (2019-12-30)
-------------------
......
......@@ -5,7 +5,7 @@
# ./compile-requirements.sh
#
-e git+https://github.com/jaywink/django-markdownx.git@ae0d882891972a79334fde0af2a06b66714b101a#egg=django-markdownx==2.0.28.1
-e git+https://git.feneas.org/jaywink/federation.git@3678c520dd0e2ae6377c2e0aac9c3a5b4588c7f6#egg=federation==0.20.0-dev1
-e git+https://git.feneas.org/jaywink/federation.git@529a3bc5cc84b6d85b6a385ca89ac7f204d42010#egg=federation==0.20.0-dev1
-e git+https://github.com/jaywink/pyembed.git@6f8c1cc98d61ee3083e9803255e4b2cc90b5a0dd#egg=pyembed==1.3.3.1
-e git+https://github.com/rtfd/recommonmark.git@c410abb565a3c4ad4de0ce204ab2fa2db8031cf1#egg=recommonmark==0.4.0
aioredis==1.3.1
......
......@@ -52,7 +52,7 @@ pyyaml
# - GIF upload (upstream rejected)
-e git+https://github.com/jaywink/django-markdownx.git@ae0d882891972a79334fde0af2a06b66714b101a#egg=django-markdownx==2.0.28.1
-e git+https://git.feneas.org/jaywink/federation.git@3678c520dd0e2ae6377c2e0aac9c3a5b4588c7f6#egg=federation==0.20.0-dev1
-e git+https://git.feneas.org/jaywink/federation.git@529a3bc5cc84b6d85b6a385ca89ac7f204d42010#egg=federation==0.20.0-dev1
# Own pyembed fork for some tweaks:
# - passing additional options (TODO make PR)
......
......@@ -5,7 +5,7 @@
# ./compile-requirements.sh
#
-e git+https://github.com/jaywink/django-markdownx.git@ae0d882891972a79334fde0af2a06b66714b101a#egg=django-markdownx==2.0.28.1
-e git+https://git.feneas.org/jaywink/federation.git@3678c520dd0e2ae6377c2e0aac9c3a5b4588c7f6#egg=federation==0.20.0-dev1
-e git+https://git.feneas.org/jaywink/federation.git@529a3bc5cc84b6d85b6a385ca89ac7f204d42010#egg=federation==0.20.0-dev1
-e git+https://github.com/jaywink/pyembed.git@6f8c1cc98d61ee3083e9803255e4b2cc90b5a0dd#egg=pyembed==1.3.3.1
aioredis==1.3.1 # via channels-redis
arrow==0.15.5
......
......@@ -11,21 +11,19 @@ from django.db import models
from django.db.models.aggregates import Max
from django.template.defaultfilters import truncatechars
from django.template.loader import render_to_string
from django.urls import NoReverseMatch
from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
from enumfields import EnumIntegerField
from federation.entities.activitypub.enums import ActivityType
from federation.utils.text import process_text_links
from federation.utils.text import process_text_links, find_tags
from memoize import memoize, delete_memoized
from model_utils.fields import AutoCreatedField, AutoLastModifiedField
from socialhome.activities.models import Activity
from socialhome.content.enums import ContentType
from socialhome.content.querysets import TagQuerySet, ContentManager
from socialhome.content.utils import test_tag
from socialhome.enums import Visibility
......@@ -426,51 +424,12 @@ class Content(models.Model):
Save found tags to the content.
"""
found_tags = set()
lines = self.text.splitlines(keepends=True)
final_words = []
code_block = False
# Check each line separately
for line in lines:
if line[0:3] == "```":
code_block = not code_block
# noinspection PyTypeChecker
if line.find("#") == -1 or line[0:4] == " " or code_block:
# Just add the whole line
final_words.append(line)
continue
# Check each word separately
# noinspection PyTypeChecker
words = line.split(" ")
for word in words:
# noinspection PyTypeChecker
candidate = word.strip().strip("([]),.!?:")
# noinspection PyTypeChecker
if candidate.startswith("#"):
# noinspection PyTypeChecker
candidate = candidate.strip("#")
if test_tag(candidate.lower()):
# Tag
found_tags.add(candidate.lower())
try:
# noinspection PyTypeChecker
tag_word = word.replace(
"#%s" % candidate,
"[#%s](%s)" % (
candidate,
reverse("streams:tag", kwargs={"name": candidate.lower()})
)
)
final_words.append(tag_word)
except NoReverseMatch:
# Don't linkify, seems we can't generate an url for it
final_words.append(word)
else:
# Not tag
final_words.append(word)
else:
final_words.append(word)
text = " ".join(final_words)
def linkifier(tag: str) -> str:
return "[#%s](%s)" % (
tag,
reverse("streams:tag", kwargs={"name": tag.lower()})
)
found_tags, text = find_tags(self.text, replacer=linkifier)
self.save_tags(found_tags)
return text
......
......@@ -4,8 +4,6 @@ import bleach
from bleach import callbacks
from django.conf import settings
ILLEGAL_TAG_CHARS = "!#$%^&*+.,@£/()=?`'\\{[]}~;:\"’”—\xa0"
def safe_text_for_markdown(text: str) -> str:
"""Clean the text using bleach but keep certain Markdown sections.
......@@ -101,13 +99,3 @@ def find_urls_in_text(text):
bleach.linkify(text, callbacks=[link_collector], parse_email=False, skip_tags=["code"])
return urls
def test_tag(tag):
"""Test a word whether it could be accepted as a tag."""
if not tag:
return False
for char in ILLEGAL_TAG_CHARS:
if char in tag:
return False
return True
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment