Skip to content

Commit 38173bf

Browse files
authored
Merge pull request #511 from liangliangyy/dev
增加toc目录支持,close #509
2 parents 7e96de0 + 79a0651 commit 38173bf

File tree

9 files changed

+383
-180
lines changed

9 files changed

+383
-180
lines changed

DjangoBlog/tests.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@
1313
@time: 2017/10/25 下午10:16
1414
"""
1515

16-
from django.test import Client, RequestFactory, TestCase
17-
from blog.models import Article, Category, Tag
18-
from django.contrib.auth import get_user_model
19-
from DjangoBlog.utils import get_current_site
20-
from django.urls import reverse
21-
import datetime
16+
from django.test import TestCase
17+
2218
from DjangoBlog.utils import *
2319

2420

@@ -49,8 +45,3 @@ def test_utils(self):
4945
}
5046
data = parse_dict_to_url(d)
5147
self.assertIsNotNone(data)
52-
render = BlogMarkDownRenderer()
53-
s = render.autolink('http://www.baidu.com')
54-
self.assertTrue(s.find('nofollow') > 0)
55-
s = render.link('http://www.baidu.com', 'test', 'test')
56-
self.assertTrue(s.find('nofollow') > 0)

DjangoBlog/utils.py

Lines changed: 22 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@
99
import uuid
1010
from hashlib import sha256
1111

12-
import mistune
1312
import requests
1413
from django.contrib.sites.models import Site
1514
from django.core.cache import cache
16-
from mistune import escape, escape_link
17-
from pygments import highlight
18-
from pygments.formatters import html
19-
from pygments.lexers import get_lexer_by_name
2015

2116
logger = logging.getLogger(__name__)
2217

@@ -93,82 +88,37 @@ def expire_view_cache(path, servername, serverport, key_prefix=None):
9388
return False
9489

9590

96-
def block_code(text, lang, inlinestyles=False, linenos=False):
97-
'''
98-
markdown代码高亮
99-
:param text:
100-
:param lang:
101-
:param inlinestyles:
102-
:param linenos:
103-
:return:
104-
'''
105-
if not lang:
106-
text = text.strip()
107-
return u'<pre><code>%s</code></pre>\n' % mistune.escape(text)
108-
109-
try:
110-
lexer = get_lexer_by_name(lang, stripall=True)
111-
formatter = html.HtmlFormatter(
112-
noclasses=inlinestyles, linenos=linenos
113-
)
114-
code = highlight(text, lexer, formatter)
115-
if linenos:
116-
return '<div class="highlight">%s</div>\n' % code
117-
return code
118-
except BaseException:
119-
return '<pre class="%s"><code>%s</code></pre>\n' % (
120-
lang, mistune.escape(text)
121-
)
122-
123-
12491
@cache_decorator()
12592
def get_current_site():
12693
site = Site.objects.get_current()
12794
return site
12895

12996

130-
class BlogMarkDownRenderer(mistune.Renderer):
131-
'''
132-
markdown渲染
133-
'''
97+
class CommonMarkdown:
98+
@staticmethod
99+
def _convert_markdown(value):
100+
import markdown
101+
md = markdown.Markdown(
102+
extensions=[
103+
'extra',
104+
'codehilite',
105+
'toc',
106+
'tables',
107+
]
108+
)
109+
body = md.convert(value)
110+
toc = md.toc
111+
return body, toc
134112

135-
def block_code(self, text, lang=None):
136-
# renderer has an options
137-
inlinestyles = self.options.get('inlinestyles')
138-
linenos = self.options.get('linenos')
139-
return block_code(text, lang, inlinestyles, linenos)
140-
141-
def autolink(self, link, is_email=False):
142-
text = link = escape(link)
143-
144-
if is_email:
145-
link = 'mailto:%s' % link
146-
if not link:
147-
link = "#"
148-
site = get_current_site()
149-
nofollow = "" if link.find(site.domain) > 0 else "rel='nofollow'"
150-
return '<a href="%s" %s>%s</a>' % (link, nofollow, text)
151-
152-
def link(self, link, title, text):
153-
link = escape_link(link)
154-
site = get_current_site()
155-
nofollow = "" if link.find(site.domain) > 0 else "rel='nofollow'"
156-
if not link:
157-
link = "#"
158-
if not title:
159-
return '<a href="%s" %s>%s</a>' % (link, nofollow, text)
160-
title = escape(title, quote=True)
161-
return '<a href="%s" title="%s" %s>%s</a>' % (
162-
link, title, nofollow, text)
163-
164-
165-
class CommonMarkdown():
166113
@staticmethod
167-
def get_markdown(value):
168-
renderer = BlogMarkDownRenderer(inlinestyles=False)
114+
def get_markdown_with_toc(value):
115+
body, toc = CommonMarkdown._convert_markdown(value)
116+
return body, toc
169117

170-
mdp = mistune.Markdown(escape=True, renderer=renderer)
171-
return mdp(value)
118+
@staticmethod
119+
def get_markdown(value):
120+
body, toc = CommonMarkdown._convert_markdown(value)
121+
return body
172122

173123

174124
def send_email(emailto, title, content):

blog/models.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import logging
2-
from abc import ABCMeta, abstractmethod, abstractproperty
2+
from abc import abstractmethod
33

4-
from django.db import models
5-
from django.urls import reverse
64
from django.conf import settings
7-
from uuslug import slugify
85
from django.core.exceptions import ValidationError
9-
from django.utils.translation import gettext_lazy as _
10-
from DjangoBlog.utils import get_current_site
11-
from DjangoBlog.utils import cache_decorator, cache
6+
from django.db import models
7+
from django.urls import reverse
128
from django.utils.timezone import now
9+
from django.utils.translation import gettext_lazy as _
1310
from mdeditor.fields import MDTextField
11+
from uuslug import slugify
12+
13+
from DjangoBlog.utils import cache_decorator, cache
14+
from DjangoBlog.utils import get_current_site
1415

1516
logger = logging.getLogger(__name__)
1617

@@ -94,6 +95,7 @@ class Article(BaseModel):
9495
on_delete=models.CASCADE)
9596
article_order = models.IntegerField(
9697
'排序,数字越大越靠前', blank=False, null=False, default=0)
98+
show_toc = models.BooleanField("是否显示toc目录", blank=False, null=False, default=False)
9799
category = models.ForeignKey(
98100
'Category',
99101
verbose_name='分类',

blog/static/blog/css/style.css

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,17 +2479,26 @@ li #reply-title {
24792479
}
24802480

24812481
.breadcrumb {
2482-
margin-bottom: 20px;
2483-
list-style: none;
2484-
border-radius: 4px;
2482+
margin-bottom: 20px;
2483+
list-style: none;
2484+
border-radius: 4px;
24852485
}
2486+
24862487
.breadcrumb > li {
2487-
display: inline-block;
2488+
display: inline-block;
24882489
}
2490+
24892491
.breadcrumb > li + li:before {
2490-
color: #ccc;
2491-
content: "/\00a0";
2492+
color: #ccc;
2493+
content: "/\00a0";
24922494
}
2495+
24932496
.breadcrumb > .active {
2494-
color: #777;
2497+
color: #777;
2498+
}
2499+
2500+
.break_line {
2501+
height: 1px;
2502+
border: none;
2503+
/*border-top: 1px dashed #f5d6d6;*/
24952504
}

0 commit comments

Comments
 (0)