Skip to content

Commit

Permalink
[#225,#108][Crum/HTML] Populate next, prev, and search Attributes.
Browse files Browse the repository at this point in the history
Rumor has it that this can attract search engine traffic.
  • Loading branch information
pishoyg committed Sep 3, 2024
1 parent e8f44a0 commit a3ddd1c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
13 changes: 13 additions & 0 deletions flashcards/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,19 @@ def _explanatory_alt(path: str) -> str:
),
),
title=roots_col("word-title"),
prev=field.fmt(
f"{CRUM_ROOT}/{{key_prev}}.html",
{"key_prev": roots_col("key-prev", force=False)},
force=False,
aon=True,
),
next=field.fmt(
f"{CRUM_ROOT}/{{key_next}}.html",
{"key_next": roots_col("key-next", force=False)},
force=False,
aon=True,
),
search=f"{CRUM_ROOT}/",
force_front=force_front,
)

Expand Down
29 changes: 27 additions & 2 deletions flashcards/deck.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import pathlib
import shutil
import typing

import field
import genanki # type: ignore[import-untyped]
Expand All @@ -15,6 +16,7 @@
<title>{{title}}</title>
<link rel="stylesheet" type="text/css" href="{CSS_BASENAME}">
<script type="text/javascript" src="{JS_BASENAME}" defer></script>
{{links}}
</head>
<body>
<div class="front">
Expand Down Expand Up @@ -81,6 +83,9 @@ def __init__(
front: field.field,
back: field.field,
title: field.field,
prev: typing.Optional[field.field] = None,
next: typing.Optional[field.field] = None,
search: str = "",
force_key: bool = True,
force_no_duplicate_keys: bool = True,
force_front: bool = True,
Expand Down Expand Up @@ -139,6 +144,9 @@ def __init__(
self.fronts: list[str] = []
self.backs: list[str] = []
self.titles: list[str] = []
self.prevs: list[str] = []
self.nexts: list[str] = []
self.search: str = search
self.length: int = field.num_entries(key, front, back)

assert self.length != field.NO_LENGTH
Expand All @@ -149,6 +157,8 @@ def __init__(
f = front.next()
b = back.next()
t = title.next()
p = prev.next() if prev else ""
n = next.next() if next else ""

assert k or not force_key
assert k not in seen_keys or not force_no_duplicate_keys
Expand Down Expand Up @@ -187,12 +197,16 @@ def __init__(
assert isinstance(f, str)
assert isinstance(b, str)
assert isinstance(t, str)
assert isinstance(n, str)
assert isinstance(p, str)
self.keys.append(f"{deck_name} - {k}")
self.raw_keys.append(k)
self.fronts.append(f)
self.backs.append(b)
self.titles.append(t)
self.nexts.append(n)
self.prevs.append(p)
ss._exported_notes += 1
self.raw_keys.append(k)

utils.info("Deck:", deck_name)
ss.print()
Expand All @@ -207,20 +221,31 @@ def clean_dir(self, dir: str) -> None:

def write_web(self, dir: str) -> None:
self.clean_dir(dir)
for rk, front, back, title in zip(
for rk, front, back, title, prev, next in zip(
self.raw_keys,
self.fronts,
self.backs,
self.titles,
self.prevs,
self.nexts,
):
with open(os.path.join(dir, rk + ".html"), "w") as f:
links: list[str] = []
if prev:
links.append(f'<link rel="prev" href="{prev}">')
if next:
links.append(f'<link rel="next" href="{next}">')
if self.search:
links.append(f'<link rel="search" href="{self.search}">')

f.write(
HTML_FMT.format(
css=self.css,
javascript=self.javascript,
title=title,
front=front,
back=back,
links="\n".join(links),
),
)
with open(os.path.join(dir, JS_BASENAME), "w") as f:
Expand Down

0 comments on commit a3ddd1c

Please sign in to comment.