2018-03-31 21:12:40 +00:00
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
import bs4
|
|
|
|
|
2018-04-21 20:09:14 +00:00
|
|
|
import config
|
|
|
|
|
2018-03-31 21:12:40 +00:00
|
|
|
def generate_nav(*, soup):
|
2018-04-21 20:30:49 +00:00
|
|
|
"""Returns nav_tag"""
|
2018-03-31 21:12:40 +00:00
|
|
|
# TODO: Don't generate link to a board if we're at the index
|
|
|
|
nav_tag = soup.new_tag('nav')
|
|
|
|
|
2018-05-09 21:02:21 +00:00
|
|
|
# TODO: Read these from the database
|
2018-03-31 21:12:40 +00:00
|
|
|
for board in ['a', 'b', 'his', 'g']:
|
2018-04-21 20:09:14 +00:00
|
|
|
url = config.url_prefix + '/' + urllib.parse.quote(board, safe = '') + '/'
|
2018-03-31 21:12:40 +00:00
|
|
|
a_tag = soup.new_tag('a', href = url)
|
|
|
|
a_tag.string = '/' + board + '/'
|
|
|
|
nav_tag.append(a_tag)
|
|
|
|
|
|
|
|
return nav_tag
|
|
|
|
|
|
|
|
def generate_header(*, page_title, soup):
|
2018-04-21 20:30:49 +00:00
|
|
|
"""Returns header_tag"""
|
2018-03-31 21:12:40 +00:00
|
|
|
header_tag = soup.new_tag('header')
|
|
|
|
|
|
|
|
h1_tag = soup.new_tag('h1')
|
|
|
|
h1_tag.string = page_title
|
|
|
|
header_tag.append(h1_tag)
|
|
|
|
|
|
|
|
return header_tag
|
|
|
|
|
|
|
|
def generate_footer(*, soup):
|
2018-04-21 20:30:49 +00:00
|
|
|
"""Returns footer_tag"""
|
2018-03-31 21:12:40 +00:00
|
|
|
# TODO: Add footer generation
|
|
|
|
return soup.new_tag('footer')
|
|
|
|
|
|
|
|
def page_skeleton(*, page_title, contents, soup):
|
2018-04-21 20:30:49 +00:00
|
|
|
"""Returns html
|
|
|
|
Given page title (string) and contents (iteratable of beautifulsoup tags), create the html
|
|
|
|
Since most pages have same basic structure, it makes sense to factor this out from page creation functions"""
|
2018-03-31 21:12:40 +00:00
|
|
|
# Doctype and head are more or less the same for each page, no need to do anything fancy when adding them
|
|
|
|
soup.append(bs4.Doctype('html'))
|
|
|
|
|
|
|
|
head_tag = soup.new_tag('head')
|
|
|
|
title_tag = soup.new_tag('title')
|
|
|
|
title_tag.string = page_title
|
|
|
|
head_tag.append(title_tag)
|
|
|
|
soup.append(head_tag)
|
|
|
|
|
|
|
|
# Body consists of (more or less same) header and footer, and varying content
|
|
|
|
body_tag = soup.new_tag('body')
|
|
|
|
|
|
|
|
body_tag.append(generate_nav(soup = soup))
|
|
|
|
body_tag.append(generate_header(page_title = page_title, soup = soup))
|
|
|
|
|
|
|
|
# contents is an iterable that can contain several tags
|
|
|
|
for tag in contents:
|
|
|
|
body_tag.append(tag)
|
|
|
|
|
|
|
|
body_tag.append(generate_footer(soup = soup))
|
|
|
|
|
|
|
|
soup.append(body_tag)
|
|
|
|
|
|
|
|
# Use .prettify() to generate nice-looking HTML
|
|
|
|
# We are probably never going to serve enough pages for the additional whitespace to count for data usage
|
|
|
|
return soup.prettify()
|
|
|
|
|
|
|
|
def new_soup():
|
2018-04-21 20:30:49 +00:00
|
|
|
"""Returns soup
|
|
|
|
Since we need a soup object to create tags, split this from page_skeleton"""
|
2018-03-31 21:12:40 +00:00
|
|
|
# Use python's built-in parser for portability
|
|
|
|
# We'll be constructing the document programmatically, so start with empty tree
|
|
|
|
soup = bs4.BeautifulSoup('', 'html.parser')
|
|
|
|
|
|
|
|
return soup
|
|
|
|
|
|
|
|
def board(board_name):
|
2018-04-21 20:30:49 +00:00
|
|
|
"""Returns html
|
|
|
|
Creates the board index page"""
|
2018-03-31 21:12:40 +00:00
|
|
|
# TODO: Creae a board index page
|
|
|
|
soup = new_soup()
|
|
|
|
|
|
|
|
page_title = '/' + board_name + '/'
|
|
|
|
|
|
|
|
return page_skeleton(page_title = page_title, contents = [], soup = soup)
|
|
|
|
|
|
|
|
def index():
|
2018-04-21 20:30:49 +00:00
|
|
|
"""Returns html
|
|
|
|
Create the site index"""
|
2018-03-31 21:12:40 +00:00
|
|
|
# TODO: Create an index page
|
|
|
|
soup = new_soup()
|
2018-04-21 20:09:14 +00:00
|
|
|
return page_skeleton(page_title = config.site_name, contents = [], soup = soup)
|
2018-03-31 21:12:40 +00:00
|
|
|
|
|
|
|
def error_404(path):
|
2018-04-21 20:30:49 +00:00
|
|
|
"""Returns html"""
|
2018-03-31 21:12:40 +00:00
|
|
|
soup = new_soup()
|
|
|
|
|
|
|
|
p_tag = soup.new_tag('p')
|
|
|
|
p_tag.string = path + ' does not exist'
|
|
|
|
|
|
|
|
return page_skeleton(page_title = '404 Not Found', contents = [p_tag], soup = soup)
|