Files
i2p.www/i2p2www/spec/views.py

121 lines
3.5 KiB
Python
Raw Normal View History

2015-11-10 08:15:11 +00:00
import codecs
2016-03-06 06:23:19 +00:00
from docutils.core import (
publish_doctree,
publish_from_doctree,
publish_parts,
)
2015-11-10 08:15:11 +00:00
from flask import (
abort,
g,
make_response,
redirect,
render_template,
render_template_string,
request,
safe_join,
url_for,
)
import os.path
2016-04-01 11:03:30 +00:00
from i2p2www import SPEC_DIR
2015-11-10 08:15:11 +00:00
from i2p2www import helpers
SPEC_METATAGS = {
'accuratefor': None,
2016-04-01 23:28:07 +00:00
'category': '',
2015-11-10 08:15:11 +00:00
'lastupdated': None,
}
SPEC_LIST_METATAGS = [
]
2016-04-01 23:28:07 +00:00
SPEC_CATEGORY_SORT = {
'Design': 1,
'Transports': 2,
'Protocols': 3,
'': 999,
}
2015-11-10 08:15:11 +00:00
def spec_index():
specs = []
for f in os.listdir(SPEC_DIR):
if f.endswith('.rst'):
path = safe_join(SPEC_DIR, f)
# read file header
header = ''
2015-11-10 08:15:11 +00:00
with codecs.open(path, encoding='utf-8') as fd:
for line in fd:
header += line
if not line.strip():
break
parts = publish_parts(source=header, source_path=SPEC_DIR, writer_name="html")
2015-11-10 08:15:11 +00:00
meta = get_metadata_from_meta(parts['meta'])
spec = {
'name': f[:-4],
'title': parts['title'],
}
spec.update(meta)
specs.append(spec)
2016-04-01 23:28:07 +00:00
specs.sort(key=lambda s: (SPEC_CATEGORY_SORT[s['category']], s['title']))
2015-11-10 08:15:11 +00:00
return render_template('spec/index.html', specs=specs)
def spec_show(name, txt=False):
# check if that file actually exists
path = safe_join(SPEC_DIR, name + '.rst')
if not os.path.exists(path):
abort(404)
# read file
with codecs.open(path, encoding='utf-8') as fd:
content = fd.read()
if txt:
# Strip out RST
content = content.replace('.. meta::\n', '')
2016-03-06 06:23:19 +00:00
content = content.replace('.. contents::\n\n', '')
2015-11-10 08:15:11 +00:00
content = content.replace('.. raw:: html\n\n', '')
content = content.replace('\n.. [', '\n[')
2015-11-14 20:41:05 +00:00
content = content.replace(']_.', '].')
content = content.replace(']_', '] ')
2015-11-10 08:15:11 +00:00
# Change highlight formatter
content = content.replace('{% highlight', "{% highlight formatter='textspec'")
# Other string changes
content = content.replace(' :accuratefor', '- Accurate for')
2016-04-01 23:28:07 +00:00
content = content.replace(' :category', '- Category')
content = content.replace(' :lastupdated', '- Last updated')
2015-11-10 08:15:11 +00:00
# render the post with Jinja2 to handle URLs etc.
rendered_content = render_template_string(content)
rendered_content = rendered_content.replace('</pre></div>', ' </pre></div>')
if txt:
# Send response
r = make_response(rendered_content)
r.mimetype = 'text/plain'
return r
2016-03-06 06:23:19 +00:00
# Render the ToC
doctree = publish_doctree(source=rendered_content)
bullet_list = doctree[1][1]
doctree.clear()
doctree.append(bullet_list)
toc = publish_from_doctree(doctree, writer_name='html')
# Remove the ToC from the main document
rendered_content = rendered_content.replace('.. contents::\n', '')
# publish the spec with docutils
2015-11-10 08:15:11 +00:00
parts = publish_parts(source=rendered_content, source_path=SPEC_DIR, writer_name="html")
meta = get_metadata_from_meta(parts['meta'])
2016-03-06 06:23:19 +00:00
return render_template('spec/show.html', title=parts['title'], toc=toc, body=parts['fragment'], name=name, meta=meta)
2015-11-10 08:15:11 +00:00
def spec_show_txt(name):
return spec_show(name, True)
def get_metadata_from_meta(meta):
return helpers.get_metadata_from_meta(meta, SPEC_METATAGS, SPEC_LIST_METATAGS)