Skip to content

Commit c0dc17f

Browse files
DOC-5093 added TextPosInfo to hold shortcode position
1 parent 95ca9fe commit c0dc17f

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

build/image_report.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@
44
from pylibs.hugotools import ShortcodeIterator
55

66
import argparse
7+
import os
8+
9+
10+
def scan_file(path: str) -> int:
11+
"""Scans a file for all `image` shortcodes.
12+
13+
Args:
14+
path (str): Path to file.
15+
16+
Returns:
17+
(int) Number of shortcodes found.
18+
"""
19+
20+
img_list = []
21+
22+
with open(path, encoding="utf_8") as md_file:
23+
text = md_file.read()
24+
25+
for img, pos_info in ShortcodeIterator(
26+
text, lambda t: t.tag == "image"
27+
):
28+
img_list.append((img, pos_info))
29+
30+
if len(img_list) > 0:
31+
print(f"File '{path}':")
32+
33+
for img in img_list:
34+
print(
35+
f" Line {img[1].line}: '{img[0].named_params['filename']}'"
36+
)
37+
38+
return len(img_list)
39+
740

841
parser = argparse.ArgumentParser(
942
"Image report",
@@ -16,8 +49,15 @@
1649

1750
print(f"Scanning '{args.pathname}'")
1851

19-
with open(args.pathname, encoding="utf_8") as md_file:
20-
filetext = md_file.read()
52+
num_found = 0
53+
54+
for root, dirs, files in os.walk(args.pathname):
55+
for file in files:
56+
if file.endswith(".md"):
57+
fullpath = os.path.join(root, file)
58+
num_found += scan_file(fullpath)
2159

22-
for shortcode, line in ShortcodeIterator(filetext):
23-
print(f"Line: {line}: {shortcode}")
60+
if num_found == 0:
61+
print(f"No image shortcodes found in '{args.pathname}'")
62+
else:
63+
print(f"Found {num_found} image shortcodes.")

build/pylibs/hugotools.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33

44
import re
55

6+
7+
class TextPosInfo:
8+
line: int
9+
start: int
10+
end: int
11+
12+
def __init__(self, line, start, end):
13+
self.line = line
14+
self.start = start
15+
self.end = end
16+
17+
618
shortcode_re_pattern_start = r"(\n)|\{\{[<%]\s*"
719
shortcode_re_body = r"(/)?([\w\-]+)\s*(.+?)?"
820
shortcode_re_pattern_end = r"\s*[>%]\}\}"
@@ -103,7 +115,7 @@ def __init__(
103115
def __iter__(self):
104116
return self
105117

106-
def __next__(self) -> tuple[ShortcodeInfo, int]:
118+
def __next__(self) -> tuple[ShortcodeInfo, TextPosInfo]:
107119
next_match = self.re_iterator.__next__()
108120

109121
while True:
@@ -115,7 +127,14 @@ def __next__(self) -> tuple[ShortcodeInfo, int]:
115127
)
116128

117129
if self.sc_filter(self, result):
118-
return (result, self.linenum)
130+
return (
131+
result,
132+
TextPosInfo(
133+
self.linenum,
134+
next_match.start(),
135+
next_match.end()
136+
)
137+
)
119138
else:
120139
result = ShortcodeInfo(
121140
next_match[3],
@@ -124,6 +143,13 @@ def __next__(self) -> tuple[ShortcodeInfo, int]:
124143
)
125144

126145
if self.sc_filter(self, result):
127-
return (result, self.linenum)
146+
return (
147+
result,
148+
TextPosInfo(
149+
self.linenum,
150+
next_match.start(),
151+
next_match.end()
152+
)
153+
)
128154

129155
next_match = self.re_iterator.__next__()

0 commit comments

Comments
 (0)