wip
This commit is contained in:
parent
7ce4914ea1
commit
94ededdf69
9 changed files with 2894 additions and 44 deletions
|
@ -7,35 +7,75 @@ bp = Blueprint("filters", __name__)
|
|||
|
||||
|
||||
def github_style_admonition(text):
|
||||
"""Transform GitHub-style alerts (> [!NOTE], etc.) to custom HTML"""
|
||||
patterns = {
|
||||
r"> \[!NOTE\](.*?)(?:\n\n|\Z)": '<div class="markdown-alert markdown-alert-note"><p class="markdown-alert-title">Note</p>\\1</div>',
|
||||
r"> \[!TIP\](.*?)(?:\n\n|\Z)": '<div class="markdown-alert markdown-alert-tip"><p class="markdown-alert-title">Tip</p>\\1</div>',
|
||||
r"> \[!IMPORTANT\](.*?)(?:\n\n|\Z)": '<div class="markdown-alert markdown-alert-important"><p class="markdown-alert-title">Important</p>\\1</div>',
|
||||
r"> \[!WARNING\](.*?)(?:\n\n|\Z)": '<div class="markdown-alert markdown-alert-warning"><p class="markdown-alert-title">Warning</p>\\1</div>',
|
||||
r"> \[!CAUTION\](.*?)(?:\n\n|\Z)": '<div class="markdown-alert markdown-alert-caution"><p class="markdown-alert-title">Caution</p>\\1</div>',
|
||||
}
|
||||
|
||||
for pattern, replacement in patterns.items():
|
||||
text = re.sub(pattern, replacement, text, flags=re.DOTALL)
|
||||
|
||||
return text
|
||||
"""Transform GitHub-style alerts (> [!NOTE], etc.) to custom HTML with GitHub styling"""
|
||||
# Process the text line by line for better control
|
||||
lines = text.split('\n')
|
||||
result_lines = []
|
||||
i = 0
|
||||
|
||||
while i < len(lines):
|
||||
line = lines[i]
|
||||
|
||||
# Check if this line starts a GitHub-style alert
|
||||
alert_match = re.match(r'>\s*\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*(.*)', line)
|
||||
|
||||
if alert_match:
|
||||
# Found an alert start
|
||||
alert_type = alert_match.group(1).lower()
|
||||
first_line_content = alert_match.group(2).strip()
|
||||
|
||||
# Collect all continuation lines (those starting with >)
|
||||
alert_content = [first_line_content] if first_line_content else []
|
||||
j = i + 1
|
||||
|
||||
while j < len(lines) and lines[j].strip().startswith('>'):
|
||||
# Remove the > and one optional space after it
|
||||
content = re.sub(r'^>\s?', '', lines[j].strip())
|
||||
alert_content.append(content)
|
||||
j += 1
|
||||
|
||||
# Create the HTML for the alert with proper styling
|
||||
alert_html = f'<div class="markdown-alert markdown-alert-{alert_type}">\n'
|
||||
alert_html += f'<p class="markdown-alert-title">{alert_type.capitalize()}</p>\n'
|
||||
|
||||
# Join the content with line breaks
|
||||
if alert_content:
|
||||
alert_html += '\n'.join(alert_content)
|
||||
|
||||
alert_html += '\n</div>'
|
||||
|
||||
# Add the processed alert HTML
|
||||
result_lines.append(alert_html)
|
||||
|
||||
# Move past all the lines we've processed
|
||||
i = j
|
||||
else:
|
||||
# Not an alert line, add it unchanged
|
||||
result_lines.append(line)
|
||||
i += 1
|
||||
|
||||
return '\n'.join(result_lines)
|
||||
|
||||
|
||||
@bp.app_template_filter("markdown")
|
||||
def markdown_filter(text):
|
||||
"""Convert markdown text to HTML with support for GitHub-style features"""
|
||||
if text:
|
||||
# Pre-process GitHub-style alerts
|
||||
text = github_style_admonition(text)
|
||||
|
||||
# Convert to HTML with regular markdown
|
||||
html = md_package.markdown(
|
||||
text, extensions=["tables", "fenced_code", "codehilite", "nl2br"]
|
||||
)
|
||||
|
||||
return html
|
||||
return ""
|
||||
"""Convert markdown text to HTML with proper GitHub styling for all elements"""
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
# First process GitHub-style alerts
|
||||
processed_text = github_style_admonition(text)
|
||||
|
||||
# Convert to HTML with standard markdown
|
||||
html = md_package.markdown(
|
||||
processed_text,
|
||||
extensions=["tables", "fenced_code", "codehilite", "nl2br", "sane_lists", "smarty"]
|
||||
)
|
||||
|
||||
# Wrap in GitHub's markdown reading view class for consistent styling
|
||||
html = f'<div class="markdown-reading-view">{html}</div>'
|
||||
|
||||
return html
|
||||
|
||||
|
||||
@bp.app_template_filter("ip_network")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue