Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

마크다운 렌더러 라이브러리 적용 #37

Merged
merged 8 commits into from Aug 26, 2019
38 changes: 31 additions & 7 deletions django-blog/mysite/blog/models.py
@@ -1,6 +1,9 @@
from django.db import models
from django.utils import timezone
from django.utils.html import mark_safe
from django.urls import reverse_lazy
import misaka, hoedown, mistune
import logging

try:
from unicode import unicode
Expand All @@ -12,26 +15,43 @@ def unicode(tag):

class Tag(models.Model):
text = models.CharField(max_length=250)
# slug = models.SlugField(unique=True, max_length=200)
created_date = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True, auto_now_add=False)

class Meta:
ordering = ('created_date',)

# def __str__(self):
# return self.slug

def get_absolute_url(self):
pass


class BaseFormatter:
def format(self, text):
pass

class FormatterMisaka(BaseFormatter):

def format(self, text):
return misaka.html(text)

class FormatterHoedown(BaseFormatter):
def format(self, text):
return hoedown.html(text)

class FormatterMistune(BaseFormatter):
def format(self, text):
return mistune.markdown(text)

class Post(models.Model):
title = models.CharField(max_length=200, help_text='title of message.')
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
slug = models.SlugField()
text = models.TextField(help_text='무슨 생각을 하고 계세요?')
draft = models.BooleanField(default=False)

# Here are Markdown Parsers
# formatter = FormatterMisaka()
# formatter = FormatterHoedown()
formatter = FormatterMistune()
draft = models.BooleanField(default=False)
tag = models.ManyToManyField(Tag)
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
Expand All @@ -45,4 +65,8 @@ def __str__(self):

def publish(self):
self.published_date = timezone.now()
self.save()
self.save()

def formatted_text(self):
logging.error(self.text)
return self.formatter.format(self.text)
4 changes: 3 additions & 1 deletion django-blog/mysite/blog/templates/blog/post_detail.html
Expand Up @@ -13,6 +13,8 @@ <h1><a href="">{{ post.title }}</a></h1>
<a href="{% url 'post_edit' pk=post.pk %}">
<button>글 수정</button>
</a>
<p>{{ post.text|linebreaksbr }}</p>
<p>
{{ post.formatted_text|safe|linebreaksbr }}
</p>
</div>
{% endblock %}
4 changes: 3 additions & 1 deletion django-blog/mysite/blog/templates/blog/post_list.html
Expand Up @@ -8,7 +8,9 @@
<li class="post-lst">
<h2><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h2>
<p>published: {{ post.published_date }}</p>
<p>{{ post.text|linebreaksbr }}</p>
<p>
{{ post.formatted_text|safe|linebreaksbr }}
</p>
</li>
</ul>
</main>
Expand Down
8 changes: 6 additions & 2 deletions django-blog/mysite/blog/views.py
@@ -1,12 +1,16 @@
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from .models import Post
from .models import Post, Tag
from .forms import PostForm


def post_list(request):
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'blog/post_list.html', {'posts': posts})

# markdown_method == 'markdownify':
# formatter = FormatterMarkdownify

def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
Expand All @@ -24,6 +28,7 @@ def post_new(request):
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})


def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
Expand All @@ -42,4 +47,3 @@ def post_edit(request, pk):
def tag_list(request):
tags = Tag.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'blog/tag_list.html', {'tags': tags})

10 changes: 9 additions & 1 deletion django-blog/requirements.txt
@@ -1,16 +1,24 @@
astroid==2.2.5
beautifulsoup4==4.8.0
certifi==2019.3.9
Django==2.0.13
cffi==1.12.3
Django==2.0
hoedown==0.3.0
isort==4.3.13
lazy-object-proxy==1.3.1
mccabe==0.6.1
misaka==2.1.1
mistune==0.8.4
mysqlclient==1.4.3
Pillow==6.1.0
pipenv==2018.11.26
pycparser==2.19
Pygments==2.4.2
pylint==2.3.1
pytz==2019.1
rope==0.14.0
six==1.12.0
soupsieve==1.9.3
typed-ast==1.3.1
virtualenv==16.4.3
virtualenv-clone==0.5.2
Expand Down