Browse Source

add nicer links to sections, and permalink icons after section headers

tags/v2019.9.0
adam j hartz 2 months ago
parent
commit
83ce6e857a

+ 7
- 0
.hive-bt/issues/3b5edef6c00b4be6a5ba176615bea34e74f9a8614425200f2feb68fc80f4ffbd/183-1565809995037-bce8deddd3f729e1cd15e1929e9177f9 View File

@@ -0,0 +1,7 @@
[
{
"action": "close",
"author": "adam j hartz <hz@mit.edu>",
"id": "183-1565809995037-bce8deddd3f729e1cd15e1929e9177f9"
}
]

+ 7
- 0
CHANGELOG View File

@@ -36,6 +36,13 @@ ADDED:
* Added `logread`, `logwrite`, and `logedit` commands for working with log
entries in a human-readable format.

* Added nicer names for URL fragments of sections and updated the
<tableofcontents/> generator to use these when linking to sections.

* Added permalinks next to section headers (these can be disabled by setting
cs_show_section_permalinks=False).


CHANGED:

* File locks are now stored with a `.lock` extension, to avoid potential

+ 20
- 0
catsoop/__STATIC__/themes/base.css View File

@@ -38,6 +38,26 @@
a, b, del, em, i, ins, q, span, strong, u {
font-size: 1em;
}

a.cs_permalink {
margin-left: 10px;
font-size: 50%;
vertical-align: middle;
padding-bottom: 0.5em;
text-decoration: none;
opacity:0.5;
visibility: hidden;
}

a.cs_permalink:hover {
opacity: 1;
text-decoration: none;
}

.cs_section_title:hover > a.cs_permalink {
visibility: visible;
}

a {
color: var(--cs-base-bg-color);
text-decoration: none;

+ 36
- 3
catsoop/language.py View File

@@ -49,6 +49,7 @@ from markdown.extensions import tables
from markdown.extensions import fenced_code
from markdown.extensions import sane_lists
from bs4 import BeautifulSoup
from unidecode import unidecode

_nodoc = {
"BeautifulSoup",
@@ -66,6 +67,20 @@ _nodoc = {
_malformed_question = "<font color='red'>malformed <tt>question</tt></font>"

_valid_qname = re.compile(r"^[_A-Za-z][_A-Za-z0-9]*$")
_unsafe_title = re.compile(r"[^A-Za-z0-9_]")


def _safe_title(t, disallowed=None):
disallowed = disallowed if disallowed is not None else set()
title = otitle = (
re.sub(r"_+", "_", _unsafe_title.sub("_", unidecode(t))).lower().rstrip("_")
)
count = 2
while title in disallowed:
title = "%s_%d" % (otitle, count)
count += 1
disallowed.add(title)
return title


def xml_pre_handle(context):
@@ -660,6 +675,8 @@ def handle_custom_tags(context, text):
chapter = None
toc_sections = []

all_title_links = set()

for i in tree.find_all(re.compile(section)):
if i.name == "chapter":
chapter = i.attrs.get("num", "0")
@@ -682,26 +699,42 @@ def handle_custom_tags(context, text):

linknum = num.replace(".", "_")
linkname = "catsoop_section_%s" % linknum
title = i.text
linkname_2 = _safe_title(title, all_title_links)

lbl = i.attrs.get("label", None)
if lbl is not None:
labels[lbl] = {
"type": i.name,
"number": num,
"title": i.string,
"link": "#%s" % linkname,
"title": i.decode_contents(),
"link": "#%s" % linkname_2,
}
toc_sections.append((num, linkname, i))
toc_sections.append((num, linkname_2, i))
sec = copy.copy(i)
sec.name = tag
sec["class"] = "cs_section_title"
sec.insert(0, "%s) " % num)
if lbl is not None:
sec.attrs["id"] = "catsoop_label_%s" % lbl
i.replace_with(sec)

if context.get("cs_show_section_permalinks", True):
permalink = tree.new_tag("a")
permalink["class"] = "cs_permalink"
permalink.attrs["href"] = "#%s" % linkname_2
permalink.string = "§"
sec.append(permalink)

# references
link = tree.new_tag("a")
link["class"] = "anchor"
link.attrs["name"] = linkname
sec.insert_before(link)
link = tree.new_tag("a")
link["class"] = "anchor"
link.attrs["name"] = linkname_2
sec.insert_before(link)

# handle refs


+ 1
- 0
requirements.txt View File

@@ -11,3 +11,4 @@ websockets
lxml
pylti
oauthlib
unidecode

Loading…
Cancel
Save