-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into adrdir
- Loading branch information
Showing
12 changed files
with
474 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import Iterator, Optional, Dict | ||
from bs4 import BeautifulSoup | ||
from dataclasses import dataclass | ||
|
||
import mistune | ||
|
||
|
||
@dataclass | ||
class Adr: | ||
title: str | ||
status: str | ||
body: str | ||
index: int = 0 | ||
|
||
|
||
def extract_statuses_from_adr(page_object) -> Iterator[str]: | ||
status_section = page_object.find("h2", text="Status") | ||
|
||
if status_section and status_section.nextSibling: | ||
current_node = status_section.nextSibling | ||
|
||
while current_node.name != "h2" and current_node.nextSibling: | ||
current_node = current_node.nextSibling | ||
|
||
if current_node.name == "p": | ||
yield current_node.text | ||
elif current_node.name == "ul": | ||
yield from (li.text for li in current_node.children if li.name == "li") | ||
else: | ||
continue | ||
|
||
|
||
def parse_adr(content: str) -> Optional[Adr]: | ||
adr_as_html = mistune.markdown(content) | ||
|
||
soup = BeautifulSoup(adr_as_html, features="html.parser") | ||
|
||
statuses = list(extract_statuses_from_adr(soup)) | ||
|
||
if any([line.startswith("Amended by") for line in statuses]): | ||
status = "amended" | ||
elif any([line.startswith("Accepted") for line in statuses]): | ||
status = "accepted" | ||
elif any([line.startswith("Superseded by") for line in statuses]): | ||
status = "superseded" | ||
elif any( | ||
[line.startswith("Proposed") or line.startswith("Pending") for line in statuses] | ||
): | ||
status = "pending" | ||
else: | ||
status = "unknown" | ||
|
||
header = soup.find("h1") | ||
|
||
if header: | ||
return Adr(header.text, status, adr_as_html) | ||
else: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from jinja2 import Environment, PackageLoader, select_autoescape | ||
from jinja2.loaders import FileSystemLoader, BaseLoader | ||
|
||
|
||
def render_html(config, template_dir_override=None) -> str: | ||
loader: BaseLoader | ||
|
||
if template_dir_override: | ||
loader = FileSystemLoader(template_dir_override) | ||
else: | ||
loader = PackageLoader("adr_viewer", "templates") | ||
|
||
env = Environment( | ||
loader=loader, | ||
autoescape=select_autoescape(["html", "xml"]), | ||
) | ||
|
||
template = env.get_template("index.html") | ||
|
||
return template.render(config=config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from bottle import Bottle, run | ||
|
||
|
||
def run_server(content, port) -> None: | ||
print(f"Starting server at http://localhost:{port}/") | ||
app = Bottle() | ||
app.route("/", "GET", lambda: content) | ||
run(app, host="localhost", port=port, quiet=True) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.