Skip to content

Commit

Permalink
加入代码高亮和目录
Browse files Browse the repository at this point in the history
  • Loading branch information
newpanjing committed Aug 24, 2019
1 parent 36cc418 commit 3eef6b9
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 55 deletions.
5 changes: 3 additions & 2 deletions models/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ class PageAdmin(admin.ModelAdmin):

@admin.register(Menu)
class MenuAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'icon', 'href', "display")
list_display = ('id', 'name', 'icon', 'href', 'sort', "display")
list_display_links = ('id', 'name')
search_fields = ('name',)
list_editable = ('display',)
list_editable = ('display', 'sort')
ordering = ('sort',)

def save_model(self, request, obj, form, change):
super(MenuAdmin, self).save_model(request, obj, form, change)
Expand Down
1 change: 1 addition & 0 deletions models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Menu(models.Model):
href = models.CharField(max_length=128, verbose_name='链接地址')
createDate = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
display = models.BooleanField(verbose_name='显示', default=True, db_index=True)
sort = models.IntegerField(verbose_name='排序', default=0, db_index=True)

class Meta:
verbose_name = '菜单'
Expand Down
19 changes: 19 additions & 0 deletions myblog/mdtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import markdown

print(markdown.markdown('''
[TOC]
# 这是什么操作
这是一个无人机
## 后来
```python
aa=123
print(aa)
for i in range(1,100):
print(i)
```
''', extensions=[
'markdown.extensions.extra',
'markdown.extensions.codehilite',
'markdown.extensions.toc',
]))
2 changes: 1 addition & 1 deletion myblog/templatetags/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_cache():
return {
'sites': list(Site.objects.order_by("sort").values('site', 'name')),
'categorys': list(Category.objects.filter(display=True).order_by("sort").values('name', 'alias')),
'menus': list(Menu.objects.filter(display=True).values('name', 'icon', 'href')),
'menus': list(Menu.objects.filter(display=True).order_by('sort').values('name', 'icon', 'href')),
'notice': Notice.objects.values('createDate', 'content').last(),
'configs': get_config('site')
}
Expand Down
55 changes: 35 additions & 20 deletions myblog/utils/pager.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
import math


def get_page_range(page_num, show_num, current_page):
# 分偶数和奇数
current_page = int(current_page)
page_num = int(page_num)
show_num = int(show_num)

if show_num % 2 == 0:
fore = int((show_num - 1) / 2)
after = int(show_num / 2)
else:
fore = int(show_num / 2)
after = math.ceil((show_num - 1) / 2)

star = current_page - fore
end = current_page + after
if star == 0:
star = 1
end += star
elif star < 0:
end += int(math.fabs(star))
end += 1
star = 1

if end > page_num:
star -= end - page_num
end = page_num

if star < 1:
star = 1
if end > page_num:
end = page_num
return star, end


# 获取页码
def get_numbers(total, size, current, show_number):
current = int(current)
total_page_num = int((total - 1) / size + 1)
val = show_number / 2
# 向上取整,-1 减去当前页
left = math.ceil(val) - 1
# 向下取整
right = math.floor(val)
start, end = get_page_range(total_page_num, show_number, current)

array = []

# 计算开始和结束
start = current - left
end = current + right

# 如果开始小于1,求从0开始的负数绝对值
if start < 1:
val = 0 - start
end += abs(val) + 1
start = 1

# 结束大于总页数,结束就等于总页数
if end > total_page_num:
end = total_page_num

# 循环计算页码
for i in range(start, end + 1):
array.append(i)
Expand Down
8 changes: 4 additions & 4 deletions myblog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def detail(request, id):
article.save()

# 如果是markdown,就用markdown渲染

article.content = '[TOC]\n' + article.content
article.content = markdown.markdown(article.content, extensions=[
'markdown.extensions.extra',
'markdown.extensions.codehilite',
'markdown.extensions.toc',
], safe_mode=True, enable_attributes=False)
])

sid = short_id.get_short_id()
request.session['sid'] = sid
Expand Down Expand Up @@ -152,7 +152,7 @@ def category_page(request, alias, page):
'title', 'subject', 'createDate', 'hits').order_by("-id")

size = 10
show = 10
show = 5

paginator = Paginator(articles, size)
articles = paginator.page(page)
Expand All @@ -171,7 +171,7 @@ def category_page(request, alias, page):
# 自定义页面
def page(request, alias):
page = Page.objects.values('title', 'content', 'id').get(alias=alias)
page['content']=markdown.markdown(page.get('content'), extensions=[
page['content'] = markdown.markdown(page.get('content'), extensions=[
'markdown.extensions.extra',
'markdown.extensions.codehilite',
'markdown.extensions.toc',
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ redis
oss2
jieba
Whoosh
pymysql
pymysql
Pygments
60 changes: 59 additions & 1 deletion static/css/style.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 74 additions & 5 deletions static/css/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
}

body {
font: 14px Helvetica Neue, Helvetica, PingFang SC, \5FAE\8F6F\96C5\9ED1, Tahoma, Arial, sans-serif;
//font: 14px Helvetica Neue, Helvetica, PingFang SC, \5FAE\8F6F\96C5\9ED1, Tahoma, Arial, sans-serif;
font-size: 14px;
font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif !important;
line-height: 1.428571429;
background-color: #e4e4e4;
}
Expand Down Expand Up @@ -276,6 +278,30 @@ header .top {
.article {
word-wrap: break-word;
word-break: break-all;
font-weight: 300 !important;
font-size: 15px !important;
line-height: 1.7 !important;
color: #314659 !important;

h1 {
font-size: 28px !important;
}

h2 {
font-size: 20px !important;
}

h3 {
font-size: 16px !important;
}

h4 {
font-size: 14px !important;
}

h5 {
font-size: 12px!important;
}
}

.article img {
Expand Down Expand Up @@ -366,6 +392,8 @@ header .top {

.article-title {
line-height: 50px;
margin-top: 0px;
font-size: 28px;
}

.infobar {
Expand Down Expand Up @@ -706,22 +734,25 @@ header .top {
padding-bottom: 0px;
border-bottom: none;
}
.article{
.info .date{

.article {
.info .date {
margin-right: 10px;
}
.info{

.info {
margin-bottom: 10px;
border-bottom: #eee 1px solid;
padding-bottom: 10px;
}
}

.aritcle-item {
padding-top: 20px;
padding-bottom: 20px;
border-bottom: 1px solid #e5e5e5;

.info .user,.info .date {
.info .user, .info .date {
margin-right: 10px;
}

Expand Down Expand Up @@ -805,4 +836,42 @@ header .top {
width: 30px;
margin-top: -5px;
}
}

.btn-success, .btn-success:hover {
background-color: @basic-color;
border-color: @basic-color;
}

.btn-success:active, .btn-success:focus {
background-color: rgba(@basic-color, 0.7);
border-color: rgba(@basic-color, 0.7);
}

.article-tips {
border-left: solid 3px @basic-color;
background-color: #f8f8f8;
padding: 12px 24px 12px 30px;
color: #314659;
font-weight: 300;
font-size: 15px;
line-height: 2;
margin: 10px auto;
position: relative;

&:before {
content: "!";
background-color: #2D8CF0;
position: absolute;
top: 25px;
left: -12px;
color: #fff;
width: 20px;
height: 20px;
border-radius: 100%;
text-align: center;
line-height: 20px;
font-weight: 700;
font-size: 14px;
}
}
Loading

0 comments on commit 3eef6b9

Please sign in to comment.