Skip to content

Commit

Permalink
work on index headings
Browse files Browse the repository at this point in the history
  • Loading branch information
jodygarnett committed Sep 19, 2023
1 parent bfdcc0d commit 45e8fde
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 14 deletions.
50 changes: 36 additions & 14 deletions docs/translate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import translate.translate
from translate import __app_name__, __version__
from .translate import collect_paths
from .translate import index_rst
from .translate import load_anchors
from .translate import fix_anchors
from .translate import convert_rst
Expand Down Expand Up @@ -53,7 +54,41 @@ def french(
print(md_fr,"\n")

@app.command()
def anchor(
def index(
base_path: Annotated[str, typer.Argument(help="base path for referencs")],
rst_path: Annotated[List[str], typer.Argument(help="path to rst file(s)")],
anchor_txt: Optional[str] = typer.Option(
"anchors.txt",
"--anchor",
help="anchors.txt file recording reference locations",
),
):
"""
Scan rst files collecting doc and ref targets into anchors.txt
"""
if not os.path.exists(base_path):
raise FileNotFoundError(errno.ENOENT, f"The base_path does not exist at location:", base_path)
anchor_path = os.path.join(base_path,anchor_txt)

collected = collect_paths(rst_path,'rst')

index = ''
for file in collected:
index += index_rst(base_path,file)

print(index)

# anchor_path = base_path+'/'+anchor_txt
# anchor_dir = os.path.dirname(anchor_path)
# if not os.path.exists(anchor_dir):
# print("RST index directory:",anchor_dir)
# os.makedirs(anchor_dir)
#
# with open(anchor_path,'w') as anchor_file:
# anchor_file.write(index)

@app.command()
def fix_references(
anchor_txt: str, md_path: Annotated[List[str], typer.Argument(help="path to md file(s)")]
):
"""
Expand All @@ -67,19 +102,6 @@ def anchor(
print(md_file,"fixed",count)
print()

@app.command()
def collect(
rst_path: Annotated[List[str], typer.Argument(help="path to rst file(s)")]
):
"""
List all rst files for conversion.
"""
collected = collect_paths(rst_path,'rst')

for file in collected:
print(file)
print()

@app.command()
def rst(
rst_path: Annotated[List[str], typer.Argument(help="path to rst file(s)")],
Expand Down
77 changes: 77 additions & 0 deletions docs/translate/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,83 @@ def collect_paths(paths: list[str], extension: str) -> list[str]:

return files

def index_rst(base_path: str, rst_file: str) -> str:
"""
Scan through rst_file producing doc and ref indexs
"""
if not os.path.exists(base_path):
raise FileNotFoundError(errno.ENOENT, f"RST base_path does not exist at location: {base_path}")

common_path = os.path.commonpath([base_path,rst_file])
if common_path != base_path:
raise FileNotFoundError(errno.ENOENT, f"RST base_path '{base_path}' does not contain rst_file: '{rst_file}'")

with open(rst_file, 'r') as file:
text = file.read()

relative_path = rst_file[len(base_path):]
print("base_path path:", base_path)
print("rst_file path:", rst_file)
print("relative path:", relative_path)
ref = relative_path
heading = None
index = ''

# Scan line by line for references and headings
# # with overline, for parts
h1 = '#############################################################################################################'
# * with overline, for chapters
h2 = '*************************************************************************************************************'
# =, for sections
h3 = '============================================================================================================='
# -, for subsections
h4 = '-------------------------------------------------------------------------------------------------------------'
# ^, for subsubsections
h5 = '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
# “, for paragraphs
h6 = '"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""'

with open(rst_file, 'r') as file:
text = file.read()

lines = text.splitlines()
for i in range(2,len(lines)):
line = lines[i]
length = len(line)
print("scan:",line)

if length == 0:
continue

if ref:
# scan for headlines
if line == h1[0:length] or line == h2[0:length]:
if i < len(lines)-2:
# if we are an overline, expect an underline 2 lines down
line2 = lines[i+2]
length2 = len(line2)
if length2 > 0 and (line2 == h1[0:length] or line2 == h2[0:length]):
# wait to process until underline
continue
if (
line == h1[0:length] or line == h2[0:length] or
line == h2[0:length] or line == h3[0:length]
):
before = lines[i-1]
if len(before) > 0 and len(before) <= length:
# heading identified
heading = before
anchor = heading
index += ref + '.path=' + relative_path + '#' + anchor + "\n"
index += ref + '.text=' + heading + "\n"
return index
else:
continue
else:
# scan for next reference
continue
return index

# administrator-guide/managing-metadata-standards/configure-validation.md
def fix_anchors(anchors: dict[str,str], md_file: str) -> int:
"""
Expand Down

0 comments on commit 45e8fde

Please sign in to comment.