-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopml_to_md
More file actions
executable file
·80 lines (63 loc) · 1.99 KB
/
Copy pathopml_to_md
File metadata and controls
executable file
·80 lines (63 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
import sys
import xml.etree.ElementTree as ET
def extract_entries_from_xml(xml_content: str):
"""
Wraps the content in a <root> tag so we can parse partial XML documents.
Yields dicts with text, title, xmlUrl, htmlUrl when found.
"""
wrapped = f"<root>\n{xml_content}\n</root>"
try:
root = ET.fromstring(wrapped)
except ET.ParseError as e:
print(f"Error: failed to parse XML-ish content: {e}", file=sys.stderr)
sys.exit(1)
for elem in root.iter():
attrs = elem.attrib
# We care about items that look like feed outline entries
if "xmlUrl" in attrs and "htmlUrl" in attrs:
text = attrs.get("text", "").strip()
title = attrs.get("title", "").strip()
xml_url = attrs.get("xmlUrl", "").strip()
html_url = attrs.get("htmlUrl", "").strip()
# Skip if core URLs are missing
if not xml_url or not html_url:
continue
# Fallbacks if text/title missing
if not text and title:
text = title
elif not title and text:
title = text
# If still nothing meaningful for label, skip
if not text and not title:
continue
yield {
"text": text,
"title": title,
"xmlUrl": xml_url,
"htmlUrl": html_url,
}
def main():
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} PATH_TO_XML_OR_FRAGMENT", file=sys.stderr)
sys.exit(1)
path = sys.argv[1]
try:
with open(path, "r", encoding="utf-8") as f:
content = f.read()
except FileNotFoundError:
print(f"Error: file not found: {path}", file=sys.stderr)
sys.exit(1)
for entry in extract_entries_from_xml(content):
text = entry["text"]
title = entry["title"]
html_url = entry["htmlUrl"]
xml_url = entry["xmlUrl"]
# If text and title are identical, don't duplicate them
if text == title:
label = text
else:
label = f"{text} - {title}"
print(f"- [{label}]({html_url}) [RSS]({xml_url})")
if __name__ == "__main__":
main()