import html as html_module
from datetime import datetime as dt
from zoneinfo import ZoneInfo
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401
from markdown import Extension, markdown
from markdown.inlinepatterns import EmStrongItem, UnderscoreProcessor
ERROR_TEMPLATE = "ERROR: SendEmailReply - {function_name}: {reason}"
UNDERLINE_RE = r"(\+)([^+]+)\1" # +underline+ -> underline
STRIKETHROUGH_RE = r"(~{2})(.+?)\1" # ~~Strikethrough~~ -> Strikethrough
class DemistoUnderlineProcessor(UnderscoreProcessor):
"""Processor for handling Underline."""
PATTERNS = [EmStrongItem(re.compile(UNDERLINE_RE, re.DOTALL | re.UNICODE), "single", "u")]
class DemistoStrikethroughProcessor(UnderscoreProcessor):
"""Processor for handling Strikethrough."""
PATTERNS = [EmStrongItem(re.compile(STRIKETHROUGH_RE, re.DOTALL | re.UNICODE), "single", "s")]
class DemistoExtension(Extension):
"""Add Custom Demisto Markdown support."""
def extendMarkdown(self, md):
"""Modify inline patterns."""
md.inlinePatterns.register(DemistoUnderlineProcessor(r"\+"), "underline", 50)
md.inlinePatterns.register(DemistoStrikethroughProcessor(r"~"), "strikethrough", 50)
def get_current_time_in_timezone(tz_name: str = "UTC") -> str:
"""
Returns current time formatted in the requested timezone.
Falls back to UTC if timezone is invalid.
"""
try:
tz = ZoneInfo(tz_name)
except Exception:
demisto.debug(f"Invalid timezone '{tz_name}' provided. Falling back to UTC.")
tz = ZoneInfo("UTC")
return dt.now(tz).isoformat()
def apply_direction(line: str) -> str:
"""
Processes a single line of Markdown text and converts
custom direction tags into corresponding HTML divs.
Supported tags:
- '<-:->' : Center aligned text
- '<--:>' : Left-to-right text (LTR)
- '<:-->' : Right-to-left text (RTL)
Args:
line (str): A single line of Markdown text.
Returns:
str: The processed line with HTML div for direction if tag found,
or the original line unchanged.
"""
# Remove leading spaces to detect tags even if line is indented
stripped_line = line.lstrip()
# Check for center alignment tag
if stripped_line.startswith("<-:->"):
# Remove the tag prefix (5 chars) and strip any extra spaces
content = stripped_line[5:].lstrip()
# Wrap content in a div with center text alignment
return f'