Skip to content

Commit

Permalink
Merge 09bc114 into 765ab98
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Jul 21, 2019
2 parents 765ab98 + 09bc114 commit 49184f9
Show file tree
Hide file tree
Showing 37 changed files with 881 additions and 911 deletions.
6 changes: 6 additions & 0 deletions misago/conftest.py
Expand Up @@ -7,6 +7,7 @@
from .conf.dynamicsettings import DynamicSettings
from .conf.staticsettings import StaticSettings
from .socialauth import SOCIALAUTH_CACHE
from .test import MisagoClient
from .themes import THEME_CACHE
from .threads.test import post_thread
from .users import BANS_CACHE
Expand Down Expand Up @@ -115,6 +116,11 @@ def other_superuser(db, user_password):
)


@pytest.fixture
def client():
return MisagoClient()


@pytest.fixture
def user_client(mocker, client, user):
client.force_login(user)
Expand Down
2 changes: 1 addition & 1 deletion misago/markup/__init__.py
@@ -1,4 +1,4 @@
from .finalise import finalise_markup
from .finalize import finalize_markup
from .flavours import common as common_flavour, signature as signature_flavour
from .parser import parse

Expand Down
7 changes: 4 additions & 3 deletions misago/markup/api.py
Expand Up @@ -2,12 +2,13 @@
from rest_framework.decorators import api_view
from rest_framework.response import Response

from . import common_flavour, finalise_markup
from . import common_flavour, finalize_markup
from .serializers import MarkupSerializer


@api_view(["POST"])
def parse_markup(request):
print(request.data)
serializer = MarkupSerializer(
data=request.data, context={"settings": request.settings}
)
Expand All @@ -18,6 +19,6 @@ def parse_markup(request):
parsing_result = common_flavour(
request, request.user, serializer.data["post"], force_shva=True
)
finalised = finalise_markup(parsing_result["parsed_text"])
finalized = finalize_markup(parsing_result["parsed_text"])

return Response({"parsed": finalised})
return Response({"parsed": finalized})
22 changes: 22 additions & 0 deletions misago/markup/bbcode/code.py
@@ -0,0 +1,22 @@
import re

import markdown
from markdown.extensions.fenced_code import FencedBlockPreprocessor


class CodeBlockExtension(markdown.Extension):
def extendMarkdown(self, md):
md.registerExtension(self)

md.preprocessors.add(
"misago_code_bbcode", CodeBlockPreprocessor(md), ">normalize_whitespace"
)


class CodeBlockPreprocessor(FencedBlockPreprocessor):
FENCED_BLOCK_RE = re.compile(
r"""
\[code(=("?)(?P<lang>.*?)("?))?](([ ]*\n)+)?(?P<code>.*?)((\s|\n)+)?\[/code\]
""",
re.IGNORECASE | re.MULTILINE | re.DOTALL | re.VERBOSE,
)
10 changes: 10 additions & 0 deletions misago/markup/bbcode/hr.py
@@ -0,0 +1,10 @@
import re

from markdown.blockprocessors import HRProcessor


class BBCodeHRProcessor(HRProcessor):
RE = r"^\[hr\]*"

# Detect hr on any line of a block.
SEARCH_RE = re.compile(RE, re.MULTILINE | re.IGNORECASE)
12 changes: 6 additions & 6 deletions misago/markup/bbcode/inline.py
Expand Up @@ -82,12 +82,12 @@ class BBCodeUrlPattern(BBcodePattern, LinkPattern):
def handleMatch(self, m):
el = util.etree.Element("a")

if m.group(6):
el.text = m.group(8)
href = m.group(5)
if m.group("arg"):
el.text = m.group("content")
href = m.group("arg")
else:
el.text = m.group(8).strip()
href = m.group(8)
el.text = m.group("content").strip()
href = m.group("content")

if href:
el.set("href", self.sanitize_url(self.unescape(href.strip())))
Expand All @@ -96,7 +96,7 @@ def handleMatch(self, m):
return el


URL_PATTERN = r'((\[url=("?)(.*?)("?)\])|(\[url\]))(.*?)\[/url\]'
URL_PATTERN = r'((\[url=("?)(?P<arg>.*?)("?)\])|(\[url\]))(?P<content>.*?)\[/url\]'


def url(md):
Expand Down
28 changes: 1 addition & 27 deletions misago/markup/bbcode/blocks.py → misago/markup/bbcode/quote.py
Expand Up @@ -2,22 +2,14 @@

import markdown
from django.utils.crypto import get_random_string
from markdown.blockprocessors import BlockProcessor, HRProcessor
from markdown.extensions.fenced_code import FencedBlockPreprocessor
from markdown.blockprocessors import BlockProcessor
from markdown.preprocessors import Preprocessor
from markdown.util import etree

QUOTE_START = get_random_string(32)
QUOTE_END = get_random_string(32)


class BBCodeHRProcessor(HRProcessor):
RE = r"^\[hr\]*"

# Detect hr on any line of a block.
SEARCH_RE = re.compile(RE, re.MULTILINE | re.IGNORECASE)


class QuoteExtension(markdown.Extension):
def extendMarkdown(self, md):
md.registerExtension(self)
Expand Down Expand Up @@ -102,21 +94,3 @@ def run(self, parent, blocks):
heading.text = title

self.parser.parseBlocks(blockquote, children)


class CodeBlockExtension(markdown.Extension):
def extendMarkdown(self, md):
md.registerExtension(self)

md.preprocessors.add(
"misago_code_bbcode", CodeBlockPreprocessor(md), ">normalize_whitespace"
)


class CodeBlockPreprocessor(FencedBlockPreprocessor):
FENCED_BLOCK_RE = re.compile(
r"""
\[code(=("?)(?P<lang>.*?)("?))?](([ ]*\n)+)?(?P<code>.*?)((\s|\n)+)?\[/code\]
""",
re.IGNORECASE | re.MULTILINE | re.DOTALL | re.VERBOSE,
)
2 changes: 1 addition & 1 deletion misago/markup/finalise.py → misago/markup/finalize.py
Expand Up @@ -10,7 +10,7 @@
)


def finalise_markup(post):
def finalize_markup(post):
return HEADER_RE.sub(replace_headers, post)


Expand Down
Expand Up @@ -4,9 +4,9 @@
STRIKETROUGH_RE = r"(~{2})(.+?)\2"


class StriketroughExtension(markdown.Extension):
class StrikethroughExtension(markdown.Extension):
def extendMarkdown(self, md):
md.registerExtension(self)
md.inlinePatterns.add(
"misago_striketrough", SimpleTagPattern(STRIKETROUGH_RE, "del"), "_end"
"misago_strikethrough", SimpleTagPattern(STRIKETROUGH_RE, "del"), "_end"
)
29 changes: 16 additions & 13 deletions misago/markup/parser.py
Expand Up @@ -7,9 +7,12 @@
from markdown.extensions.fenced_code import FencedCodeExtension

from ..conf import settings
from .bbcode import blocks, inline
from .bbcode.code import CodeBlockExtension
from .bbcode.hr import BBCodeHRProcessor
from .bbcode.inline import bold, image, italics, underline, url
from .bbcode.quote import QuoteExtension
from .md.shortimgs import ShortImagesExtension
from .md.striketrough import StriketroughExtension
from .md.strikethrough import StrikethroughExtension
from .mentions import add_mentions
from .pipeline import pipeline

Expand Down Expand Up @@ -88,17 +91,17 @@ def md_factory(allow_links=True, allow_images=True, allow_blocks=True):
del md.inlinePatterns["short_reference"]

# Add [b], [i], [u]
md.inlinePatterns.add("bb_b", inline.bold, "<strong")
md.inlinePatterns.add("bb_i", inline.italics, "<emphasis")
md.inlinePatterns.add("bb_u", inline.underline, "<emphasis2")
md.inlinePatterns.add("bb_b", bold, "<strong")
md.inlinePatterns.add("bb_i", italics, "<emphasis")
md.inlinePatterns.add("bb_u", underline, "<emphasis2")

# Add ~~deleted~~
striketrough_md = StriketroughExtension()
striketrough_md = StrikethroughExtension()
striketrough_md.extendMarkdown(md)

if allow_links:
# Add [url]
md.inlinePatterns.add("bb_url", inline.url(md), "<link")
md.inlinePatterns.add("bb_url", url(md), "<link")
else:
# Remove links
del md.inlinePatterns["link"]
Expand All @@ -107,7 +110,7 @@ def md_factory(allow_links=True, allow_images=True, allow_blocks=True):

if allow_images:
# Add [img]
md.inlinePatterns.add("bb_img", inline.image(md), "<image_link")
md.inlinePatterns.add("bb_img", image(md), "<image_link")
short_images_md = ShortImagesExtension()
short_images_md.extendMarkdown(md)
else:
Expand All @@ -116,17 +119,15 @@ def md_factory(allow_links=True, allow_images=True, allow_blocks=True):

if allow_blocks:
# Add [hr] and [quote] blocks
md.parser.blockprocessors.add(
"bb_hr", blocks.BBCodeHRProcessor(md.parser), ">hr"
)
md.parser.blockprocessors.add("bb_hr", BBCodeHRProcessor(md.parser), ">hr")

fenced_code = FencedCodeExtension()
fenced_code.extendMarkdown(md, None)

code_bbcode = blocks.CodeBlockExtension()
code_bbcode = CodeBlockExtension()
code_bbcode.extendMarkdown(md)

quote_bbcode = blocks.QuoteExtension()
quote_bbcode = QuoteExtension()
quote_bbcode.extendMarkdown(md)
else:
# Remove blocks
Expand Down Expand Up @@ -227,6 +228,8 @@ def clean_internal_link(link, host):
def clean_attachment_link(link, force_shva=False):
try:
resolution = resolve(link)
if not resolution.namespaces:
return link
url_name = ":".join(resolution.namespaces + [resolution.url_name])
except (Http404, ValueError):
return link
Expand Down
8 changes: 8 additions & 0 deletions misago/markup/tests/conftest.py
@@ -0,0 +1,8 @@
from unittest.mock import Mock

import pytest


@pytest.fixture
def request_mock(user):
return Mock(scheme="http", get_host=Mock(return_value="example.com"), user=user)
Empty file.
28 changes: 28 additions & 0 deletions misago/markup/tests/snapshots/snap_test_code_bbcode.py
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# snapshottest: v1 - https://goo.gl/zC4yUc
from __future__ import unicode_literals

from snapshottest import Snapshot


snapshots = Snapshot()

snapshots["test_single_line_code 1"] = '<pre><code>echo("Hello!");</code></pre>'

snapshots[
"test_multi_line_code 1"
] = """<pre><code>echo("Hello!");
echo("World!");</code></pre>"""

snapshots[
"test_code_with_language_parameter 1"
] = '<pre><code class="php">echo("Hello!");</code></pre>'

snapshots[
"test_code_with_quoted_language_parameter 1"
] = '<pre><code class="php">echo("Hello!");</code></pre>'

snapshots[
"test_code_block_disables_parsing 1"
] = "<pre><code>Dolor [b]met.[/b]</code></pre>"
12 changes: 12 additions & 0 deletions misago/markup/tests/snapshots/snap_test_finalization.py
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
# snapshottest: v1 - https://goo.gl/zC4yUc
from __future__ import unicode_literals

from snapshottest import Snapshot


snapshots = Snapshot()

snapshots[
"test_finalization_sets_translation_strings_in_quotes 1"
] = '<div class="quote-heading">Quoted message:</div>'
14 changes: 14 additions & 0 deletions misago/markup/tests/snapshots/snap_test_hr_bbcode.py
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
# snapshottest: v1 - https://goo.gl/zC4yUc
from __future__ import unicode_literals

from snapshottest import Snapshot


snapshots = Snapshot()

snapshots[
"test_hr_bbcode_is_replaced_if_its_alone_in_paragraph 1"
] = """<p>Lorem ipsum dolor met.</p>
<hr/>
<p>Sit amet elit.</p>"""
46 changes: 46 additions & 0 deletions misago/markup/tests/snapshots/snap_test_inline_bbcode.py
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# snapshottest: v1 - https://goo.gl/zC4yUc
from __future__ import unicode_literals

from snapshottest import Snapshot


snapshots = Snapshot()

snapshots["test_bold_bbcode 1"] = "<p>Lorem <b>ipsum</b>!</p>"

snapshots["test_italics_bbcode 1"] = "<p>Lorem <i>ipsum</i>!</p>"

snapshots["test_underline_bbcode 1"] = "<p>Lorem <u>ipsum</u>!</p>"

snapshots[
"test_inline_bbcode_can_be_mixed_with_markdown 1"
] = "<p>Lorem <b><strong>ipsum</strong></b>!</p>"

snapshots[
"test_image_bbcode 1"
] = '<p>Lorem <img alt="placekitten.com/g/1200/500" src="https://placekitten.com/g/1200/500"/> ipsum</p>'

snapshots[
"test_image_bbcode_is_case_insensitive 1"
] = '<p>Lorem <img alt="placekitten.com/g/1200/500" src="https://placekitten.com/g/1200/500"/> ipsum</p>'

snapshots[
"test_url_bbcode 1"
] = '<p>Lorem <a href="https://placekitten.com/g/1200/500" rel="nofollow noopener">placekitten.com/g/1200/500</a> ipsum</p>'

snapshots[
"test_url_bbcode_with_link_text 1"
] = '<p>Lorem <a href="https://placekitten.com/g/1200/500" rel="nofollow noopener">dolor</a> ipsum</p>'

snapshots[
"test_url_bbcode_with_long_link_text 1"
] = '<p>Lorem <a href="https://placekitten.com/g/1200/500" rel="nofollow noopener">dolor met</a> ipsum</p>'

snapshots[
"test_url_bbcode_with_quotes_and_link_text 1"
] = '<p>Lorem <a href="https://placekitten.com/g/1200/500" rel="nofollow noopener">dolor</a> ipsum</p>'

snapshots[
"test_url_bbcode_with_quotes_and_long_link_text 1"
] = '<p>Lorem <a href="https://placekitten.com/g/1200/500" rel="nofollow noopener">dolor met</a> ipsum</p>'

0 comments on commit 49184f9

Please sign in to comment.