Demo entry 6769618

wcms

   

Submitted by anonymous on Nov 07, 2018 at 06:09
Language: Python 3. Code size: 1.8 kB.

# -*- coding: utf-8 -*-
__version__ = '0.1.0'
__author__ = 'smallick'
__all__ = ['serve']

import bottle, mistune, os, configparser
app = bottle.Bottle()
config_file = './data/app.conf'

def serve(to=None, *args, **kwargs):
 '''
  a simple serving function
  to -> to what? cgi, wsgi, or others methods to serve this with Bottle
  other arguments are passed as is
 '''
 if to is None:
  app.run(*args, **kwargs)
 else:
  app.run(server=to, *args, **kwargs)

def read_and_format(file):
 " return formatted markdown text from file to be rendered "
 content = ''
 with open(file) as f:
  content = f.read()
 return mistune.markdown(content, escape=False, hard_wrap=True)


@app.route('/<page:path>')
@app.route('/<page:path>/')
@app.route('/')
@bottle.view('page')
def show_page(page='index'):
 file = './data/' + page + '.txt'
 if os.path.exists(file):
  return render(file, page)
 else:
  bottle.redirect('404')

def load_app_conf():
 global config_file
 return c2d(config_file)

def render(file, page):
 config = load_app_conf()
 content = read_and_format(file)
 wcms = config.get('wcms')
 title = wcms.get('title')
 tagline = wcms.get('tagline')
 base_url = wcms.get('base')
 meta = config.get('wcms.meta')
 links = config.get('wcms.links')
 html_title = '{} - {}'.format(title, links.get(page, 'Untitled'))
 d = dict(
  content=content,
  main=dict(
   title=html_title,
   meta=meta,
   base_url=base_url,
   head=dict(
    links=links,
    page_title=title,
    tagline=tagline
   )
  )
 )
 return d

def c2d(file):
 from collections import OrderedDict
 c = configparser.ConfigParser()
 c.optionxform = str
 c.read(file)
 d = dict()
 for k in c.sections():
  d[k] = OrderedDict() if k == 'wcms.links' else dict()
  for v in c.options(k):
   d[k][v] = c[k][v]
 return d

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).