mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-04 11:55:50 +00:00
Code samples: parse READMEs better and use smarter default titles
This commit is contained in:
@@ -45,7 +45,7 @@
|
||||
} %}
|
||||
|
||||
{% for card in code_samples %}
|
||||
<a class="card" href="{{target.prefix}}{{card.href}}">
|
||||
<a class="card" href="{{target.github_forkurl}}/tree/{{target.github_branch}}/{{card.href}}">
|
||||
<div class="card-header">
|
||||
{% for lang in card.langs %}
|
||||
<span class="circled-logo"><img src="{{lang_icons[lang]}}" /></span>
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
################################################################################
|
||||
|
||||
import os
|
||||
import re
|
||||
from bs4 import BeautifulSoup
|
||||
from markdown import markdown
|
||||
|
||||
cs_dir = "content/_code-samples/" #TODO: make a configurable field
|
||||
@@ -17,6 +19,18 @@ skip_dirs = [
|
||||
".git",
|
||||
"__pycache__"
|
||||
]
|
||||
words_to_caps = [
|
||||
"xrp",
|
||||
]
|
||||
|
||||
def to_title_case(s):
|
||||
"""
|
||||
Convert a folder name string to title case, for code samples that don't have
|
||||
a README file. The input string is expected to be in kebab-case or snake_case.
|
||||
"""
|
||||
words = re.split(r"_|[^\w']", s)
|
||||
words = [w.upper() if w in words_to_caps else w.title() for w in words if w] #remove empty splits
|
||||
return " ".join(words).replace("'S", "'s").replace(" A ", " a ")
|
||||
|
||||
def all_code_samples():
|
||||
cses = []
|
||||
@@ -40,9 +54,20 @@ def all_code_samples():
|
||||
if "README.md" in filenames:
|
||||
with open(os.path.join(dirpath, "README.md"), "r") as f:
|
||||
md = f.read()
|
||||
cs["description"] = markdown(md)
|
||||
md = markdown(md)
|
||||
soup = BeautifulSoup(md, "html.parser")
|
||||
h = soup.find(["h1", "h2", "h3"]) # get first header in the file
|
||||
if h:
|
||||
cs["title"] = h.get_text()
|
||||
else:
|
||||
cs["description"] = os.path.basename(dirpath)
|
||||
cs["title"] = to_title_case(os.path.basename(dirpath))
|
||||
p = soup.find("p") # first paragraph defines the blurb
|
||||
cs["description"] = p.get_text()
|
||||
|
||||
else:
|
||||
cs["title"] = to_title_case(os.path.basename(dirpath))
|
||||
cs["description"] = ""
|
||||
cs["href"] = dirpath
|
||||
cses.append(cs)
|
||||
|
||||
return cses
|
||||
|
||||
Reference in New Issue
Block a user