Add support for python3

This commit is contained in:
Victor Cabello
2019-05-07 00:27:25 -05:00
parent db22fbfa34
commit 70ece88089
4 changed files with 92 additions and 71 deletions
+16 -7
View File
@@ -1,23 +1,32 @@
from bottle import route,run,template,static_file
from markdown_converter import MarkdownConverter
from env import *
from __future__ import absolute_import
from bottle import route, run, static_file
from .markdown_converter import MarkdownConverter
from .env import root_path, ms_host, ms_port, ms_debug
import os
converter = MarkdownConverter()
@route('/<resource:re:.*\.md>')
@route(r'/<resource:re:.*\.md>')
def gfmize(resource):
if resource == 'favicon.ico':
return ''
html_file_name = os.path.basename(converter.convert(resource))
if '/' in resource:
html_file_name = '/'.join(resource.split('/')[:-1]) + '/' + html_file_name
return static_file(os.path.join('resources/html', html_file_name), root=root_path)
html_file_name = '/'.join(resource.split('/')[:-1]) + \
'/' + html_file_name
return static_file(os.path.join('resources/html',
html_file_name),
root=root_path)
def main():
run(host=ms_host,port=ms_port,debug=ms_debug,reloader=ms_reloader)
run(host=ms_host,
port=ms_port,
debug=ms_debug,
reloader=False)
if __name__ == '__main__':
main()
+14 -14
View File
@@ -1,16 +1,16 @@
import os
ms_encoding = 'utf-8'
ms_port = '8009'
ms_host = 'localhost'
ms_debug = True
ms_reloader = True
html_extension = '.html'
root_path = os.path.dirname(__file__)
resource_dir = os.path.join(root_path,'resources')
markdown_dir = os.path.join(resource_dir,'markdown')
html_dir = os.path.join(resource_dir,'html')
css_dir = os.path.join(resource_dir,'css')
css_name = 'github.css'
css_path = os.path.join(css_dir,css_name)
markdown_type = 'gfm'
ms_encoding = "utf-8"
ms_port = "8009"
ms_host = "localhost"
ms_debug = True
ms_reloader = True
html_extension = ".html"
root_path = os.path.dirname(__file__)
resource_dir = os.path.join(root_path, "resources")
markdown_dir = os.path.join(resource_dir, "markdown")
html_dir = os.path.join(resource_dir, "html")
css_dir = os.path.join(resource_dir, "css")
css_name = "github.css"
css_path = os.path.join(css_dir, css_name)
markdown_type = "gfm"
+34 -20
View File
@@ -1,61 +1,75 @@
from __future__ import print_function
from __future__ import absolute_import
from builtins import object
import markdown as md
import codecs
import sys
import os
from env import *
from .env import css_path, ms_encoding, markdown_type, \
html_dir, html_extension
class MarkdownConverter(object):
def __init__(self):
css = codecs.open(css_path,encoding=ms_encoding,mode='r')
self.html_header = '''
css = codecs.open(css_path, encoding=ms_encoding, mode="r")
self.html_header = (
"""
<html>
<head>
<style type='text/css'>
<!--
''' + css.read() + \
'''
"""
+ css.read()
+ """
//-->
</style>
</head>
<body>
<div class='markdown-body'>
'''
self.html_footer = '''
"""
)
self.html_footer = """
</div>
</body>
</html>
'''
"""
def convert(self,src,dst=""):
def convert(self, src, dst=""):
code = md.markdown(self.read_md(src), extensions=[markdown_type])
return self.write_html(code,src,dst)
return self.write_html(code, src, dst)
def read_md(self,file_name):
md_file = codecs.open(os.path.join(markdown_dir, file_name),encoding=ms_encoding,mode='r')
def read_md(self, file_name):
workingdir = os.getcwd()
md_file = codecs.open(
os.path.join(workingdir, file_name),
encoding=ms_encoding,
mode="r"
)
return md_file.read()
def write_html(self,body,file_name,dst):
def write_html(self, body, file_name, dst):
html_path = os.path.join(html_dir, file_name + html_extension)
if dst != "":
html_path = dst
try:
os.makedirs('/'.join(html_path.replace('\\', '/').split('/')[:-1]))
except OSError as exc:
os.makedirs("/".join(html_path.replace("\\", "/").split("/")[:-1]))
except OSError:
pass
html_file = codecs.open(html_path,encoding=ms_encoding,mode='w')
html_file = codecs.open(html_path, encoding=ms_encoding, mode="w")
html_file.write(self.html_header + body + self.html_footer)
return html_path
def main():
args = sys.argv
if len(args) != 3:
print('usage: convert source_md_file target_html_file')
print("usage: convert source_md_file target_html_file")
else:
converter = MarkdownConverter()
converter.convert(args[1],args[2])
converter.convert(args[1], args[2])
if __name__ == '__main__':
if __name__ == "__main__":
main()
+27 -29
View File
@@ -1,44 +1,42 @@
import os
from setuptools import setup, find_packages
def read_file(filename):
basepath = os.path.dirname(os.path.dirname(__file__))
filepath = os.path.join(basepath, filename)
if os.path.exists(filepath):
return open(filepath).read()
else:
return ''
return ""
setup(
name = 'markdown-server',
version = '0.1.3',
description = 'A simple markdown server.',
long_description = read_file('README.rst'),
author = 'Masato Ohba',
author_email = 'over.rye@gmail.com',
url = 'https://github.com/ohbarye/markdown-server',
classifiers = [
'Topic :: Utilities',
'Development Status :: 4 - Beta',
'Framework :: Bottle',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
name="markdown-server",
version="0.1.4",
description="A simple markdown server.",
long_description=read_file("README.rst"),
author="Masato Ohba",
author_email="over.rye@gmail.com",
url="https://github.com/ohbarye/markdown-server",
classifiers=[
"Topic :: Utilities",
"Development Status :: 4 - Beta",
"Framework :: Bottle",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
],
packages = find_packages(),
include_package_data = True,
keywords = ['web', 'markdown'],
license = 'MIT License',
install_requires = [
'bottle',
'Markdown==2.6.11',
'Pygments',
'py-gfm',
],
entry_points = {
'console_scripts': [
'markdownserver = markdownserver:main',
'convert = markdownserver.markdown_converter:main'
packages=find_packages(),
include_package_data=True,
keywords=["web", "markdown"],
license="MIT License",
install_requires=["bottle", "Markdown==2.6.11", "Pygments", "py-gfm"],
entry_points={
"console_scripts": [
"markdownserver=markdownserver:main",
"convert=markdownserver.markdown_converter:main",
]
},
)