статичный вариант с докером
This commit is contained in:
commit
96fdbeb3df
12 files changed
+387
No files matched your search
@@ -0,0 +1,102 @@
|
||||
"""Build a static documentation page using a documented Markdown subset."""
|
||||
from pathlib import Path
|
||||
import html
|
||||
import re
|
||||
import shutil
|
||||
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
|
||||
|
||||
def inline(value):
|
||||
# Escape HTML first; only explicitly supported markup becomes HTML.
|
||||
value = html.escape(value)
|
||||
tokens = []
|
||||
|
||||
def token(markup):
|
||||
tokens.append(markup)
|
||||
return f'\x00{len(tokens) - 1}\x00'
|
||||
|
||||
value = re.sub(r'`([^`]+)`', lambda m: token(f'<code>{m[1]}</code>'), value)
|
||||
|
||||
def link(match):
|
||||
label, url = match.groups()
|
||||
if not re.match(r'^(https?://|mailto:|#)', html.unescape(url), re.I):
|
||||
return label
|
||||
return token(f'<a href="{url}">{label}</a>')
|
||||
|
||||
value = re.sub(r'\[([^\]]+)\]\(([^\s)]+)\)', link, value)
|
||||
value = re.sub(r'\*\*([^*]+)\*\*', r'<strong>\1</strong>', value)
|
||||
for i in reversed(range(len(tokens))):
|
||||
value = value.replace(f'\x00{i}\x00', tokens[i])
|
||||
return value
|
||||
|
||||
|
||||
def render(source):
|
||||
lines = source.splitlines()
|
||||
output, navigation = [], []
|
||||
index, section, heading = 0, False, 0
|
||||
while index < len(lines):
|
||||
line = lines[index].strip()
|
||||
index += 1
|
||||
if not line:
|
||||
continue
|
||||
if line.startswith('```'):
|
||||
code = []
|
||||
while index < len(lines) and not lines[index].strip().startswith('```'):
|
||||
code.append(lines[index])
|
||||
index += 1
|
||||
index += 1
|
||||
output.append('<pre><code>' + html.escape('\n'.join(code)) + '</code></pre>')
|
||||
elif re.match(r'^#{1,3} ', line):
|
||||
level = len(line.split(' ', 1)[0])
|
||||
title = line[level + 1:]
|
||||
if level == 2:
|
||||
if section:
|
||||
output.append('</section>')
|
||||
heading += 1
|
||||
anchor = f'section-{heading}'
|
||||
navigation.append(f'<a href="#{anchor}"><span>{heading:02}</span>{html.escape(title)}</a>')
|
||||
output.append(f'<section id="{anchor}">')
|
||||
section = True
|
||||
output.append(f'<h{level}>{inline(title)}</h{level}>')
|
||||
elif line.startswith('|') and index < len(lines) and re.match(r'^\|[\s:|\-]+\|$', lines[index].strip()):
|
||||
cells = lambda row: [inline(cell.strip()) for cell in row.strip().strip('|').split('|')]
|
||||
output.append('<div class="table-wrap" tabindex="0" role="region" aria-label="Таблица"><table><thead><tr>' + ''.join(f'<th scope="col">{cell}</th>' for cell in cells(line)) + '</tr></thead><tbody>')
|
||||
index += 1
|
||||
while index < len(lines) and lines[index].strip().startswith('|'):
|
||||
output.append('<tr>' + ''.join(f'<td>{cell}</td>' for cell in cells(lines[index])) + '</tr>')
|
||||
index += 1
|
||||
output.append('</tbody></table></div>')
|
||||
elif re.match(r'^(\d+\. |[-*] )', line):
|
||||
ordered = bool(re.match(r'^\d+\.', line))
|
||||
tag, pattern = ('ol', r'^\d+\. ') if ordered else ('ul', r'^[-*] ')
|
||||
items = [re.sub(pattern, '', line)]
|
||||
while index < len(lines) and re.match(pattern, lines[index].strip()):
|
||||
items.append(re.sub(pattern, '', lines[index].strip()))
|
||||
index += 1
|
||||
output.append(f'<{tag}>' + ''.join(f'<li>{inline(item)}</li>' for item in items) + f'</{tag}>')
|
||||
elif line.startswith('> '):
|
||||
output.append(f'<blockquote>{inline(line[2:])}</blockquote>')
|
||||
else:
|
||||
output.append(f'<p>{inline(line)}</p>')
|
||||
if section:
|
||||
output.append('</section>')
|
||||
return '\n'.join(output), '\n'.join(navigation)
|
||||
|
||||
|
||||
def build():
|
||||
version = (ROOT / 'VERSION').read_text(encoding='utf-8').strip()
|
||||
if not re.fullmatch(r'\d+\.\d+\.\d+', version):
|
||||
raise ValueError('VERSION must contain major.minor.patch')
|
||||
content, navigation = render((ROOT / 'docs/content.md').read_text())
|
||||
template = (ROOT / 'web/index.html').read_text()
|
||||
target = ROOT / 'dist'
|
||||
target.mkdir(exist_ok=True)
|
||||
(target / 'index.html').write_text(template.replace('{{version}}', version).replace('{{navigation}}', navigation).replace('{{content}}', content))
|
||||
for name in ('style.css', 'app.js'):
|
||||
shutil.copyfile(ROOT / 'web' / name, target / name)
|
||||
print('Built dist/index.html')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
build()
|
||||
Reference in new issue
Block a user