Skip to content

Commit

Permalink
📝 Add doc hooks and custom javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
astariul committed May 10, 2024
1 parent d00cafe commit db2bee0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
78 changes: 78 additions & 0 deletions docs/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Module declaring the hooks for Mkdocs."""

import json
import os

from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.nav import Page


def on_page_markdown(markdown: str, page: Page, config: MkDocsConfig, **kwargs) -> str:
"""Function that runs before rendering the markdown.
See https://www.mkdocs.org/dev-guide/plugins/#events for more details.
Args:
markdown (str): The markdown content of the page.
page (Page): The page being rendered.
config (MkDocsConfig): The configuration.
**kwargs: Keywords arguments to pass to the given function.
Returns:
str: The updated markdown content.
"""
if page.file.src_uri == "leaderboard.md":
lines = markdown.split("\n")
entries = []
for line in lines:
if line.startswith(">>>"):
# This is a line with a path to a result file
# -> parse it and extract the results
kb_name, result_file_path = line[3:].split("|")

with open(os.path.join(config.docs_dir, result_file_path), "r") as f:
res = json.load(f)

entries.append(
{
"kb_name": kb_name,
"score": res["overall_score"],
"nwp": res["next_word_prediction"]["score"]["top3_accuracy"],
"acp": res["auto_completion"]["score"]["top3_accuracy"],
"acr": res["auto_correction"]["score"]["fscore"],
}
)

# Sort according to the overall score
entries.sort(reverse=True, key=lambda x: x["score"])

# Find the best scores to highlight
best_score = max(entries, key=lambda x: x["score"])["score"]
best_nwp = max(entries, key=lambda x: x["nwp"])["nwp"]
best_acp = max(entries, key=lambda x: x["acp"])["acp"]
best_acr = max(entries, key=lambda x: x["acr"])["acr"]

# Replace the lines accordingly
for i, line in enumerate(lines):
if line.startswith(">>>"):
e = entries.pop(0)

score = f"{round(e['score'], 2):g}"
nwp = f"{round(e['nwp'], 2):g}"
acp = f"{round(e['acp'], 2):g}"
acr = f"{round(e['acr'], 2):g}"

# Highlight the best scores
if e["score"] == best_score:
score = f"**{score}**"
if e["nwp"] == best_nwp:
nwp = f"**{nwp}**"
if e["acp"] == best_acp:
acp = f"**{acp}**"
if e["acr"] == best_acr:
acr = f"**{acr}**"

# Overwrite the line
lines[i] = f"| {e['kb_name']} | {score} | {nwp} | {acp} | {acr} |"

return "\n".join(lines)
6 changes: 6 additions & 0 deletions docs/javascripts/tablesort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
document$.subscribe(function() {
var tables = document.querySelectorAll("article table:not([class])")
tables.forEach(function(table) {
new Tablesort(table)
})
})
10 changes: 9 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ nav:
- Welcome: "index.md"
- "usage.md"
- "emulated_keyboard.md"
- "emu_setup.md"
- "how_testing_is_done.md"
- "architecture.md"
- "emu_setup.md"
- "leaderboard.md"
- Code reference:
- "public_api.md"
- "internals.md"

hooks:
- docs/hooks.py

plugins:
- search
- mkdocstrings:
Expand All @@ -64,3 +68,7 @@ extra:

extra_css:
- css/mkdocstrings.css

extra_javascript:
- https://unpkg.com/[email protected]/dist/tablesort.min.js
- javascripts/tablesort.js

0 comments on commit db2bee0

Please sign in to comment.