Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ipari
committed
Aug 20, 2018
1 parent
4be3c1e
commit 8648b6a
Showing
9 changed files
with
195 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import re | ||
from flask import Blueprint, redirect, request, render_template | ||
|
||
from .config import config | ||
from .note import file_path, menu_list, note_meta, process_page, raw_page, \ | ||
render_markdown | ||
from .user import logged_in | ||
|
||
|
||
blueprint = Blueprint('edit', __name__) | ||
|
||
|
||
def save_note(page_path, content): | ||
path = file_path(page_path) | ||
with open(path, 'w') as f: | ||
f.write(content) | ||
|
||
|
||
def edit_page(page_path): | ||
base_url = config('note')['base_url'] | ||
content = raw_page(page_path) | ||
# ` 문자는 ES6에서 템플릿 문자로 사용되므로 escape 해줘야 한다. | ||
content = content.replace('`', '\`') | ||
return render_template('edit.html', | ||
pagename=page_path, | ||
meta=note_meta(), | ||
base_url=base_url, | ||
menu=menu_list(), | ||
content=content) | ||
|
||
|
||
@blueprint.route('/preview', methods=['POST']) | ||
def preview(): | ||
html = render_markdown(request.get_json()['raw_md']) | ||
|
||
# 이미지 주소가 /edit 기준으로 렌더링되어있어 base_url 기준으로 고친다. | ||
page_root = request.referrer.split('/')[-2] | ||
base_url = config('note')['base_url'] | ||
rel_url = '{}/{}'.format(base_url, page_root) | ||
|
||
def replace_path(matchobj): | ||
return '{}/{}/{}{}'.format(matchobj.group(1), rel_url, | ||
matchobj.group(2), matchobj.group(3)) | ||
|
||
pattern = r'(src=\")([^\"]*)(\")' | ||
html = re.sub(pattern, replace_path, html) | ||
return html | ||
|
||
|
||
@blueprint.route('/edit/<path:page_path>', methods=['GET', 'POST']) | ||
def view_edit(page_path): | ||
if not logged_in(): | ||
return redirect('/login') | ||
else: | ||
if request.method == 'GET': | ||
return edit_page(page_path) | ||
else: | ||
save_note(page_path, request.form['md']) | ||
return process_page(page_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{% extends "_base.html" %} | ||
|
||
{% block script %} | ||
<script src="{{ url_for('static', filename='js/simplemde.min.js') }}"></script> | ||
<script src="https://cdn.jsdelivr.net/highlight.js/latest/highlight.min.js"></script> | ||
{% endblock %} | ||
|
||
{% block css %} | ||
<link rel="stylesheet" href="{{ url_for('static', filename='css/simplemde.min.css') }}" /> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/latest/styles/github.min.css"> | ||
{% endblock %} | ||
|
||
{% block pagename %}{{ pagename }}{% endblock %} | ||
|
||
{% block content %} | ||
<h1>{{ pagename }}</h1> | ||
|
||
<form method="post" class="editor"> | ||
<textarea name="md" id="md"></textarea> | ||
<div class="buttons"> | ||
<input type="submit" value="저장"/> | ||
<a class="button" href="/{{ base_url }}/{{ pagename }}">취소</a> | ||
</div> | ||
</form> | ||
|
||
{# https://github.com/sparksuite/simplemde-markdown-editor #} | ||
<script> | ||
let simplemde = new SimpleMDE({ | ||
autofocus: true, | ||
autosave: { | ||
enabled: true, | ||
uniqueId: "{{ pagename }}", | ||
delay: 10000 | ||
}, | ||
element: document.getElementById("md"), | ||
indentWithTabs: false, | ||
previewRender: function(plainText, preview) { | ||
setTimeout(function() { | ||
previewMarkdown(preview, plainText); | ||
}, 250); | ||
return "Loading..."; | ||
}, | ||
promptURLs: false, | ||
renderingConfig: { | ||
codeSyntaxHighlighting: true | ||
}, | ||
spellChecker: false, | ||
tabSize: 4 | ||
}); | ||
{# ES6 템플릿 문자열 ` 을 사용해야 한다. #} | ||
simplemde.value(`{{ content|safe }}`); | ||
</script> | ||
{% endblock %} |