python Python中的简单Markdown服务器。假设您在〜/ Text中有Markdown文件。在http:// localhost:5000 / w / Hello访问〜/ Text /

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python中的简单Markdown服务器。假设您在〜/ Text中有Markdown文件。在http:// localhost:5000 / w / Hello访问〜/ Text / 相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# -*- coding: utf8 -*-
from flask import Flask, redirect, url_for
from markdown import markdown
import os
import re


# create the app
# TODO: load config/template from files, with fallbacks
app = Flask(__name__)
root_dir = os.path.expanduser('2016')
root_dir = '2016'
template = """
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<title>%s | Brainerd</title>
	<style>
		body { padding: 0; margin: 0; }
		header { padding: 1em; background: royalblue; color: white; }
		#container { width: 650px; margin: 0 auto; }
		#content { padding: 1em; }
	</style>
</head>
<body>
<div id="container">
	<header>
		<p>Brainerd wiki system</p>
		<h1>%s</h1>
	</header>
	<div id="content">
		%s
	</div>
</div>
</body>
</html>
"""

txt_template = """
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<title>%s | Brainerd</title>
	<style>
		body { padding: 0; margin: 0; }
		header { padding: 1em; background: royalblue; color: white; }
		#container { width: 650px; margin: 0 auto; }
		pre#content {
		    word-wrap: break-word;
		    overflow: auto;
		    width: 100%%;
}

	</style>
</head>
<body>
<div id="container">
	<header>
		<p>Brainerd wiki system</p>
		<h1>%s</h1>
	</header>
	<pre id="content">
		%s
	</pre>
</div>
</body>
</html>
"""



def link_maker(i):
	return u"<li><a href='/{i}.txt'>Gün {i}</a></li>".format(i=i)

@app.route("/")
def homepage():
	# TODO: make this a directory listing of root_dir
	html = ""
	for i in xrange(1,90):
		html += link_maker(i)
	return html


@app.route("/<path:page>.html")
def wiki(page):
	import codecs
	# reformat the URL - space becomes hyphen
	# also, strip out path injection attacks
	# TODO: substitute back in the spaces, somehow
	new_page = page.replace(" ", "-")
	new_page = re.sub(r"\.+/", "", new_page)
	if new_page is not page:
		return redirect(url_for("wiki", page=new_page))

	# render file or directory
	# TODO: directory listings, non–Markdown files, catch Markdown errors
	file_path = "%s/%s.markdown" % (root_dir, page)
	if os.path.isfile(file_path):

		try:
			with codecs.open(file_path, 'r','windows-1254') as f:
				return template % (page, page, markdown(f.read()))
		except:
			return "EXCEPTION: Couldn’t render %s.mdown." % page

	# render the page
	return "Wiki page: %s" % page


@app.route("/<path:page>.txt")
def txt(page):
	import codecs
	# reformat the URL - space becomes hyphen
	# also, strip out path injection attacks
	# TODO: substitute back in the spaces, somehow
	new_page = page.replace(" ", "-")
	new_page = re.sub(r"\.+/", "", new_page)
	if new_page is not page:
		return redirect(url_for("wiki", page=new_page))

	# render file or directory
	# TODO: directory listings, non–Markdown files, catch Markdown errors
	file_path = "%s/%s.markdown" % (root_dir, page)
	if os.path.isfile(file_path):

		try:
			with codecs.open(file_path, 'r','windows-1254') as f:
				return txt_template % (page, page, f.read())
		except:
			return "EXCEPTION: Couldn’t render %s.mdown." % page

	# render the page
	return "Wiki page: %s" % page


# if running brainerd directly, boot the app
if __name__ == "__main__":
	if not os.path.exists(root_dir) and not os.path.isdir(root_dir):
		print "Error: Root directory %s doesn't exist." % root_dir
		exit()
	app.debug = True

	app.run()

以上是关于python Python中的简单Markdown服务器。假设您在〜/ Text中有Markdown文件。在http:// localhost:5000 / w / Hello访问〜/ Text / 的主要内容,如果未能解决你的问题,请参考以下文章

怎么用c或python读取markdown文件中的信息

python markdown干啥用的

Python 中的 Github 风格的 Markdown

markdown 比较Python和R. #python #r中的常见util函数

CSDN写作Markdown编辑器中的Python命令帮手

markdown 获取Zendesk搜索Python脚本中的标签数量