From aa431fe361e5aab44e8b8a435eb07150ca2786c6 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Sun, 11 Aug 2024 02:17:43 -0600 Subject: [PATCH] revised callout content parsing, which made nested callouts automagically work --- src/dragonglass/mparse.py | 40 +++++++++++++++++++++++++++++---------- src/dragonglass/style.py | 6 ++++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/dragonglass/mparse.py b/src/dragonglass/mparse.py index 3951269..b6177f8 100644 --- a/src/dragonglass/mparse.py +++ b/src/dragonglass/mparse.py @@ -867,11 +867,13 @@ class ObsidianStyleBlockquotes(Extension): CALLOUT = re.compile(r'^\[!([a-z]+)\]([-+])?(?:[ ]+(.*))?') CALLOUT_DEFICON = 'pencil' CALLOUT_ICONS = { + 'example': 'list', 'faq': 'circle-help', 'help': 'circle-help', 'info': 'info', 'question': 'circle-help', - 'tip': 'flame' + 'tip': 'flame', + 'todo': 'circle-check' } def normal_blockquote(self, parent: etree.Element, block: str) -> None: @@ -920,15 +922,9 @@ class ObsidianStyleBlockquotes(Extension): if folding: styleval = 'display: none;' if folding == '-' else '' content_div.attrib['style'] = styleval - first = True - for line in lines: - if first: - first = False - else: - etree.SubElement(content_div, 'br') - self.parser.state.set('list') - self.parser.parseBlocks(content_div, [line]) - self.parser.state.reset() + self.parser.state.set('callout') + self.parser.parseBlocks(content_div, ['\n'.join(lines)]) + self.parser.state.reset() def run(self, parent: etree.Element, blocks: list[str]) -> None: block = blocks.pop(0) @@ -948,8 +944,32 @@ class ObsidianStyleBlockquotes(Extension): else: self.normal_blockquote(parent, block) + class CalloutLinesProcessor(BlockProcessor): + def test(self, parent: etree.Element, block: str) -> bool: + return self.parser.state.isstate('callout') + + def run(self, parent: etree.Element, blocks: list[str]) -> bool: + block = blocks.pop(0) + if block.strip(): + sibling = self.lastChild(parent) + lines = block.split('\n') + for line in lines: + if sibling is not None: + if sibling.tail: + sibling = etree.SubElement(parent, 'br') + else: + if parent.text: + sibling = etree.SubElement(parent, 'br') + else: + parent.text = line + continue + sibling.tail = line + return True + def extendMarkdown(self, md) -> None: md.parser.blockprocessors.register(ObsidianStyleBlockquotes.ObsidianBlockQuote(md.parser), 'quote', 20) + md.parser.blockprocessors.register(ObsidianStyleBlockquotes.CalloutLinesProcessor(md.parser), + 'callout-text', 11) def create_markdown_parser(context: Context) -> markdown.Markdown: diff --git a/src/dragonglass/style.py b/src/dragonglass/style.py index 398871b..30e42d6 100644 --- a/src/dragonglass/style.py +++ b/src/dragonglass/style.py @@ -63,6 +63,9 @@ th { background-color: rgba(var(--callout-color), 0.1); padding: 12px 12px 12px 24px; } +.callout[data-callout="example"] { + --callout-color: 120, 82, 238; +} .callout[data-callout="question"], .callout[data-callout="help"], .callout[data-callout="faq"] { --callout-color: 236, 117, 0; } @@ -72,6 +75,9 @@ th { .callout[data-callout="tip"] { --callout-color: 0, 191, 188; } +.callout[data-callout="todo"] { + --callout-color: 8, 109, 221; +} .callout-title { padding: 0; display: flex;