Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Last active August 9, 2023 14:13
Show Gist options
  • Save connordavenport/9285e97daf73caf1b7488a892eaee434 to your computer and use it in GitHub Desktop.
Save connordavenport/9285e97daf73caf1b7488a892eaee434 to your computer and use it in GitHub Desktop.
A WIP script to add markdown functionality to DrawBot.
import re
sampleText = '''
A spectre is haunting [Europe](www.robofont.com) — the spectre of **communism**. All the __powers of old Europe have__ entered into a holy alliance to exorcise this spectre: Pope and Tsar, Metternich and Guizot, French Radicals and German police-spies.
Where is the party in opposition that has not been decried as communistic by its opponents in power? Where is the opposition that has not hurled back the branding reproach of **communism**, against the more advanced opposition parties, as well as against its reactionary adversaries?
Two things result from this fact:
I. **Communism** is *already acknowledged* by all _European_ powers to be itself a power.
II. It _is high time that **Communists** should openly, in the_ face of the whole world, publish their views, their aims, their tendencies, and meet this nursery tale of the Spectre of Communism with a manifesto of the party itself.
To this end, **Communists** of various nationalities have [assembled](www.google.com) in London and sketched the following ~~manifesto~~, to be published in the English, French, German, Italian, Flemish and Danish languages.
'''
# ------------------------------------------------------------
def inTextRange(number, ranges):
for r in ranges:
if r[0] <= number <= r[1]:
return True
return False
def drawbotMarkdown(text, textColor, fontAttr):
fontFile, fontSize = fontAttr
TOKENS = ["*", "_", "~", "[", "]", "(", ")"]
#regex searches
BOLD = re.finditer(r'\*\*.*?\*\*|\_\_.*?\_\_', text, re.DOTALL)
ITAL = [t for t in re.finditer(r'\*.*?\*|\_.*?\_', text, re.DOTALL) if t.group() != "**" or t.group() != "__"]
STRK = re.finditer(r'~~.*?~~', text, re.DOTALL)
BOLD_RANGE = [p.span() for p in BOLD]
ITAL_RANGE = [i.span() for i in ITAL]
STRK_RANGE = [s.span() for s in STRK]
markdown = FormattedString()
for ii, letter in enumerate(text):
weight = 60
slant = 0
p = False
url = False
if inTextRange(ii, BOLD_RANGE):
if letter in TOKENS:
p = True
else:
weight = 150
if inTextRange(ii,ITAL_RANGE):
if letter in TOKENS:
p = True
else:
slant = -8
if not p:
markdown.append(letter, fill=textColor, font=fontFile, fontSize=fontSize, fontVariations=dict(wght=weight, slnt=slant))
return markdown
# ------------------------------------------------------------
ff = "IBMPlexMono-Thin" # this is a VF
newPage(1000,1000)
md = drawbotMarkdown(sampleText,(1,0,0,1),(ff, 20))
textBox(md, (10,10, 518, 1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment