From 46dff9c42c819667d80fdfdffb43a3385359e162 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Wed, 1 Feb 2023 01:55:55 +0100 Subject: [PATCH] Some more clever bavlna stuff. --- test.md | 2 ++ whitespace.py | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/test.md b/test.md index 8f68efb..4d14b83 100644 --- a/test.md +++ b/test.md @@ -35,6 +35,8 @@ A non-breakable space bro A lot of spaces +A text with some $math$. + This should be seen by all. [!opendatatask]{} diff --git a/whitespace.py b/whitespace.py index bf27d26..5b1ecc1 100644 --- a/whitespace.py +++ b/whitespace.py @@ -1,4 +1,4 @@ -from panflute import Space,SoftBreak,Str +from panflute import Space, SoftBreak, Str, Math from typing import Union # Import local files @@ -12,11 +12,26 @@ class NBSP(Space): def bavlna(e: Whitespace, c: Context) -> bool: """Determine if given piece of whitespace should be non-breakable.""" + if c.get_metadata("language") == "cs": - - # TODO: Add more clever stuff - if isinstance(e.prev, Str) and isinstance(e.next, Str) and e.prev.text.lower() in ['k', 's', 'v', 'z', 'o', 'u', 'a', 'i']: + if isinstance(e.prev, Str) and isinstance(e.next, Str): + if e.prev.text.lower() in ['k', 's', 'v', 'z', 'o', 'u', 'a', 'i']: + return True + + if isinstance(e.prev, Str) and isinstance(e.next, Str): + prevC = e.prev.text[-1] + nextC = e.next.text[0] + numbers = ["0123456789"] + operators = ["+-/*^%:"] + if prevC in numbers and nextC in numbers: + return True + if prevC in numbers and nextC in operators: return True + if prevC in operators and nextC in numbers: + return True + + if isinstance(e.prev, Math) or isinstance(e.next, Math): + return True