aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-06-06 20:14:28 -0400
committerBobby <[email protected]>2023-06-06 20:14:28 -0400
commit5de7630148dd9ef69a66ea269e0b8077628e51ca (patch)
tree4688c02eb479cc03133e41a39f825cf289719c35
parentb71094f06d4dce6777daaa7ae01104b8251c4c72 (diff)
downloadthatcomputerscientist-5de7630148dd9ef69a66ea269e0b8077628e51ca.tar.xz
thatcomputerscientist-5de7630148dd9ef69a66ea269e0b8077628e51ca.zip
Fix code highlight for Japanese
-rw-r--r--blog/context_processors.py5
-rw-r--r--blog/recommender.py6
-rw-r--r--blog/views.py4
-rw-r--r--middleware/translationMiddleware.py14
-rw-r--r--middleware/uuidmiddleware.py4
5 files changed, 22 insertions, 11 deletions
diff --git a/blog/context_processors.py b/blog/context_processors.py
index f43274d8..6a8f1e7f 100644
--- a/blog/context_processors.py
+++ b/blog/context_processors.py
@@ -1,9 +1,9 @@
-import akismet
import os
import re
+
+import akismet
import dotenv
import requests
-
from bs4 import BeautifulSoup
from django.conf import settings
from django.core.cache import cache
@@ -11,7 +11,6 @@ from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name, guess_lexer
-
from .models import Category, Comment, Post
dotenv.load_dotenv()
diff --git a/blog/recommender.py b/blog/recommender.py
index 3193181a..230bf536 100644
--- a/blog/recommender.py
+++ b/blog/recommender.py
@@ -1,12 +1,14 @@
# This is a very simple recommender system that recommends posts based on the
# current post user is reading.
-from .models import Post
import numpy as np
+from bs4 import BeautifulSoup
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
-from bs4 import BeautifulSoup
+
from .context_processors import add_excerpt, add_num_comments
+from .models import Post
+
def next_read(post):
current_post = Post.objects.get(id=post.id)
diff --git a/blog/views.py b/blog/views.py
index 126c4355..e36fc9d7 100644
--- a/blog/views.py
+++ b/blog/views.py
@@ -24,8 +24,8 @@ from users.models import UserProfile
from users.tokens import CaptchaTokenGenerator
from .context_processors import (add_excerpt, add_num_comments, avatar_list,
- comment_processor, highlight_code_blocks,
- recent_posts, check_spam)
+ check_spam, comment_processor,
+ highlight_code_blocks, recent_posts)
from .models import AnonymousCommentUser, Category, Comment, Post
from .recommender import next_read
diff --git a/middleware/translationMiddleware.py b/middleware/translationMiddleware.py
index de143286..ba1eda1f 100644
--- a/middleware/translationMiddleware.py
+++ b/middleware/translationMiddleware.py
@@ -1,7 +1,9 @@
-from google.cloud import translate_v2 as translate
+import os
+
+from bs4 import BeautifulSoup
from django.conf import settings
-import os
from django.core.cache import cache
+from google.cloud import translate_v2 as translate
cred_path = os.path.join(settings.BASE_DIR, 'credentials-translate.json')
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = cred_path
@@ -21,7 +23,13 @@ class TranslationMiddleware:
if 'text' not in content_type:
return response
- HTML_content = response.content.decode('utf-8')
+ HTML_content =response.content.decode('utf-8').strip()
+
+ # add no translate class to the 'highlight' class
+ soup = BeautifulSoup(HTML_content, 'html.parser')
+ for tag in soup.find_all(class_='highlight'):
+ tag['class'].append('notranslate')
+ HTML_content = str(soup)
HTML_content = HTML_content.replace(
"That Computer Scientist",
diff --git a/middleware/uuidmiddleware.py b/middleware/uuidmiddleware.py
index 859000c5..212e67b2 100644
--- a/middleware/uuidmiddleware.py
+++ b/middleware/uuidmiddleware.py
@@ -1,6 +1,8 @@
+import json
import uuid
+
import redis
-import json
+
redis_instance = redis.StrictRedis(host='localhost', port=6379, db=0)