Skip to content

Commit

Permalink
Fixed CTA buttons, fixed titles
Browse files Browse the repository at this point in the history
  • Loading branch information
ngrayluna committed Jan 21, 2025
1 parent b9883aa commit 22cc556
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
23 changes: 20 additions & 3 deletions docugen/pretty_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def _format_docstring(item, *, table_title_template: Optional[str] = None) -> st
return str(item)


def _github_button(href_links):
"""To do: Add hugo scripting to add this function. For now, just add code line # for debugging.
Args:
href_links (str): URL for the GitHub button.
"""
return "{{< cta-button githubLink=" + href_links.url + " >}}"+ "\n\n"


def _build_function_page(page_info: parser.FunctionPageInfo) -> str:
"""Constructs a markdown page given a `FunctionPageInfo` object.
Expand All @@ -80,7 +89,10 @@ def _build_function_page(page_info: parser.FunctionPageInfo) -> str:

parts.append("\n\n")

parts.append(_top_source_link(page_info.defined_in))
# Add the github button.
if page_info.defined_in and page_info.defined_in.url:
parts.append(_github_button(page_info.defined_in))

parts.append("\n\n")

parts.append(page_info.doc.brief + "\n\n")
Expand Down Expand Up @@ -232,7 +244,9 @@ def _build_class_page(page_info: parser.ClassPageInfo) -> str:
parts.append("\n\n")

# Add the github button.
parts.append(_top_source_link(page_info.defined_in))
if page_info.defined_in and page_info.defined_in.url:
parts.append(_github_button(page_info.defined_in))

parts.append("\n\n")

# Add the one line docstring of the class.
Expand Down Expand Up @@ -434,7 +448,10 @@ def _build_module_page(page_info: parser.ModulePageInfo) -> str:

parts.append("<!-- Insert buttons and diff -->\n")

parts.append(_top_source_link(page_info.defined_in))
# Add the github button.
if page_info.defined_in and page_info.defined_in.url:
parts.append(_github_button(page_info.defined_in))

parts.append("\n\n")

# First line of the docstring i.e. a brief introduction about the symbol.
Expand Down
52 changes: 43 additions & 9 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import cli
import library

import re


# Replace auto-generated title as a key, provide the preferred title as the value
MARKDOWN_TITLES = {
Expand Down Expand Up @@ -64,7 +66,7 @@ def main(args):
reformat_title_to_frontmatter(ref_dir)

# Rename README.md to _index.md
rename_readme_to_index(ref_dir)
reformat_and_rename_readme(ref_dir, title_mapping)


def rename_to_readme(directory):
Expand Down Expand Up @@ -145,17 +147,49 @@ def single_folder_format(directory):
)
os.rmdir(root)

def rename_readme_to_index(directory):

def reformat_and_rename_readme(directory, title_mapping):
for root, _, files in os.walk(directory):
for file in files:
if file == "README.md": # Only target README.md files
old_path = os.path.join(root, file)
new_path = os.path.join(root, "_index.md")

# Rename the file
os.rename(old_path, new_path)
print(f"Renamed: {old_path} -> {new_path}")
if file == "README.md" or file == "_index.md":
file_path = os.path.join(root, file)

with open(file_path, "r", encoding="utf-8") as f:
lines = f.readlines()

# Process the lines
updated_lines = []
for line in lines:
# Remove CTA buttons
if re.match(r"\{\{\<\s*cta-button.*?\>\}\}", line.strip()):
continue

# Update the title
if line.startswith("title:"):
old_title = line[len("title:"):].strip()
# Check if the title is in the mapping
new_title = title_mapping.get(old_title, old_title.capitalize())
line = f"title: {new_title}\n"

updated_lines.append(line)

# Rename README.md to _index.md
new_file_path = os.path.join(root, "_index.md")
if file == "README.md":
os.rename(file_path, new_file_path)
file_path = new_file_path

# Write back the updated file
with open(file_path, "w", encoding="utf-8") as f:
f.writelines(updated_lines)
print(f"Processed: {file_path}")

title_mapping = {
"python": "Python Library",
"data-types": "Data Types",
"integrations": "Integrations",
"public-api": "Import & Export API",
}

def get_args():
parser = argparse.ArgumentParser(
Expand Down

0 comments on commit 22cc556

Please sign in to comment.