-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathinject-headers.py
More file actions
executable file
·142 lines (105 loc) · 8.82 KB
/
inject-headers.py
File metadata and controls
executable file
·142 lines (105 loc) · 8.82 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
"""
Inject navigation headers into HTML documentation pages.
Usage: python3 inject-headers.py <output_dir>
This script adds a consistent navigation header to all generated HTML pages,
with appropriate styling and links based on the page location (root, templates, guidelines).
"""
import sys
import re
import os
from pathlib import Path
# Version link placeholder — replaced at build time based on --dev flag
VERSION_LINK_PLACEHOLDER = '{{VERSION_LINK}}'
# Navigation header templates (use placeholder for version link)
HEADERS = {
'root': f'''<header class="doc-header"><div class="doc-header-brand"><a href="./index.html">SDRF-Proteomics</a></div><nav class="doc-header-nav"><a href="./index.html">Home</a><a href="./specification.html" class="nav-current">Specification</a><a href="./index.html#metadata-guidelines">Metadata Guidelines</a><a href="./index.html#templates">Templates</a><a href="./sdrf-builder.html">Template Builder</a><a href="./index.html#tools">Tools</a><a href="./sdrf-explorer.html">Explorer</a><a href="./sdrf-editor.html">Editor</a><a href="./index.html#contributors">Contributors</a>{VERSION_LINK_PLACEHOLDER}<a href="https://github.com/bigbio/proteomics-metadata-standard" target="_blank">GitHub</a></nav></header>''',
'tools': f'''<header class="doc-header"><div class="doc-header-brand"><a href="./index.html">SDRF-Proteomics</a></div><nav class="doc-header-nav"><a href="./index.html">Home</a><a href="./specification.html">Specification</a><a href="./index.html#metadata-guidelines">Metadata Guidelines</a><a href="./index.html#templates">Templates</a><a href="./sdrf-builder.html">Template Builder</a><a href="./index.html#tools" class="nav-current">Tools</a><a href="./sdrf-explorer.html">Explorer</a><a href="./sdrf-editor.html">Editor</a><a href="./index.html#contributors">Contributors</a>{VERSION_LINK_PLACEHOLDER}<a href="https://github.com/bigbio/proteomics-metadata-standard" target="_blank">GitHub</a></nav></header>''',
'guidelines': f'''<header class="doc-header"><div class="doc-header-brand"><a href="../index.html">SDRF-Proteomics</a></div><nav class="doc-header-nav"><a href="../index.html">Home</a><a href="../specification.html">Specification</a><a href="../index.html#metadata-guidelines" class="nav-current">Metadata Guidelines</a><a href="../index.html#templates">Templates</a><a href="../sdrf-builder.html">Template Builder</a><a href="../index.html#tools">Tools</a><a href="../sdrf-explorer.html">Explorer</a><a href="../sdrf-editor.html">Editor</a><a href="../index.html#contributors">Contributors</a>{VERSION_LINK_PLACEHOLDER}<a href="https://github.com/bigbio/proteomics-metadata-standard" target="_blank">GitHub</a></nav></header>''',
'templates': f'''<header class="doc-header"><div class="doc-header-brand"><a href="../index.html">SDRF-Proteomics</a></div><nav class="doc-header-nav"><a href="../index.html">Home</a><a href="../specification.html">Specification</a><a href="../index.html#metadata-guidelines">Metadata Guidelines</a><a href="../index.html#templates" class="nav-current">Templates</a><a href="../sdrf-builder.html">Template Builder</a><a href="../index.html#tools">Tools</a><a href="../sdrf-explorer.html">Explorer</a><a href="../sdrf-editor.html">Editor</a><a href="../index.html#contributors">Contributors</a>{VERSION_LINK_PLACEHOLDER}<a href="https://github.com/bigbio/proteomics-metadata-standard" target="_blank">GitHub</a></nav></header>''',
'sample_guidelines': f'''<header class="doc-header"><div class="doc-header-brand"><a href="./index.html">SDRF-Proteomics</a></div><nav class="doc-header-nav"><a href="./index.html">Home</a><a href="./specification.html">Specification</a><a href="./index.html#metadata-guidelines" class="nav-current">Metadata Guidelines</a><a href="./index.html#templates">Templates</a><a href="./sdrf-builder.html">Template Builder</a><a href="./index.html#tools">Tools</a><a href="./sdrf-explorer.html">Explorer</a><a href="./sdrf-editor.html">Editor</a><a href="./index.html#contributors">Contributors</a>{VERSION_LINK_PLACEHOLDER}<a href="https://github.com/bigbio/proteomics-metadata-standard" target="_blank">GitHub</a></nav></header>''',
'templates_guide': f'''<header class="doc-header"><div class="doc-header-brand"><a href="./index.html">SDRF-Proteomics</a></div><nav class="doc-header-nav"><a href="./index.html">Home</a><a href="./specification.html">Specification</a><a href="./index.html#metadata-guidelines">Metadata Guidelines</a><a href="./index.html#templates" class="nav-current">Templates</a><a href="./sdrf-builder.html">Template Builder</a><a href="./index.html#tools">Tools</a><a href="./sdrf-explorer.html">Explorer</a><a href="./sdrf-editor.html">Editor</a><a href="./index.html#contributors">Contributors</a>{VERSION_LINK_PLACEHOLDER}<a href="https://github.com/bigbio/proteomics-metadata-standard" target="_blank">GitHub</a></nav></header>'''
}
def inject_header(filepath: str, header_html: str, is_dev: bool = False) -> None:
"""Inject navigation header into an HTML file."""
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Resolve version link: only show in dev builds (link back to stable)
if is_dev:
version_link = '<a href="/" class="version-link">Stable Version</a>'
else:
version_link = ''
resolved_header = header_html.replace(VERSION_LINK_PLACEHOLDER, version_link)
# Add has-doc-header class to body
content = re.sub(r'<body class="([^"]*)"', r'<body class="has-doc-header \1"', content)
content = re.sub(r'<body>', '<body class="has-doc-header">', content)
# Insert header after opening body tag
content = re.sub(r'(<body[^>]*>)', r'\1\n' + resolved_header, content)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
def inject_version_link_into_static(filepath: str, is_dev: bool) -> None:
"""Inject a 'Stable Version' link into static HTML pages for dev builds."""
if not is_dev:
return # Stable builds don't show a version link
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Already has a version link — skip
if 'class="version-link"' in content:
return
# Insert "Stable Version" link before the GitHub link
stable_link = '<a href="/" class="version-link">Stable Version</a>'
content = content.replace(
'<a href="https://github.com/bigbio/proteomics-metadata-standard"',
stable_link + '<a href="https://github.com/bigbio/proteomics-metadata-standard"'
)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
def main():
if len(sys.argv) < 2:
print("Usage: python3 inject-headers.py <output_dir> [--dev]")
sys.exit(1)
is_dev = '--dev' in sys.argv
output_dir = Path(sys.argv[1])
print(f"Dev mode: {is_dev}")
# Inject header into specification.html
spec_file = output_dir / "specification.html"
if spec_file.exists():
print(f"Injecting header into: {spec_file}")
inject_header(str(spec_file), HEADERS['root'], is_dev)
# Inject header into tools.html
tools_file = output_dir / "tools.html"
if tools_file.exists():
print(f"Injecting header into: {tools_file}")
inject_header(str(tools_file), HEADERS['tools'], is_dev)
# Inject header into sample-guidelines.html
sg_file = output_dir / "sample-guidelines.html"
if sg_file.exists():
print(f"Injecting header into: {sg_file}")
inject_header(str(sg_file), HEADERS['sample_guidelines'], is_dev)
# Inject header into templates.html (templates guide)
tpl_guide = output_dir / "templates.html"
if tpl_guide.exists():
print(f"Injecting header into: {tpl_guide}")
inject_header(str(tpl_guide), HEADERS['templates_guide'], is_dev)
# Inject headers into metadata-guidelines pages
guidelines_dir = output_dir / "metadata-guidelines"
if guidelines_dir.exists():
for html_file in guidelines_dir.glob("*.html"):
print(f"Injecting header into: {html_file}")
inject_header(str(html_file), HEADERS['guidelines'], is_dev)
# Inject headers into template pages
templates_dir = output_dir / "templates"
if templates_dir.exists():
for html_file in templates_dir.glob("*.html"):
print(f"Injecting header into: {html_file}")
inject_header(str(html_file), HEADERS['templates'], is_dev)
# Inject version link into static HTML pages for dev builds
for static_page in ["index.html", "quickstart.html", "sdrf-terms.html",
"sdrf-explorer.html", "sdrf-editor.html", "sdrf-builder.html"]:
static_file = output_dir / static_page
if static_file.exists():
print(f"Processing version link in: {static_file}")
inject_version_link_into_static(str(static_file), is_dev)
print("Header injection complete!")
if __name__ == "__main__":
main()