Skip to content

Instantly share code, notes, and snippets.

@typemytype
Created July 27, 2023 11:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typemytype/d494d6ee5c43361db05c448c27095bd7 to your computer and use it in GitHub Desktop.
Save typemytype/d494d6ee5c43361db05c448c27095bd7 to your computer and use it in GitHub Desktop.
markdown with NSAttributedString for drawBot
md = """
# title
## sub title
*foo*
__italic foo__
[our website](https://example.com).
~noooo~
just text
hello world `code block` yeah
* a list
* more list
* oh
"""
import AppKit
styling = dict(
font="Times",
monospacedFont="Menlo",
size=12,
fill=(0, 0, 0, 1),
strikeThrough=AppKit.NSUnderlineStyleSingle
)
fontManager = AppKit.NSFontManager.sharedFontManager()
options = AppKit.NSAttributedStringMarkdownParsingOptions.alloc().init()
options.setAllowsExtendedAttributes_(True)
options.setInterpretedSyntax_(AppKit.NSAttributedStringMarkdownInterpretedSyntaxInlineOnlyPreservingWhitespace)
txt, _ = AppKit.NSAttributedString.alloc().initWithMarkdownString_options_baseURL_error_(
md,
options,
None,
None
)
def block(value, rng, stop):
if value == AppKit.NSInlinePresentationIntentStronglyEmphasized:
# __italic__
fontDescriptor = AppKit.NSFontDescriptor.fontDescriptorWithName_size_(styling["font"], styling["size"])
fontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits_(AppKit.NSFontDescriptorTraitItalic)
font = AppKit.NSFont.fontWithDescriptor_size_(fontDescriptor, styling["size"])
txt.addAttribute_value_range_(AppKit.NSFontAttributeName, font, rng)
elif value == AppKit.NSInlinePresentationIntentEmphasized:
# *bold*
fontDescriptor = AppKit.NSFontDescriptor.fontDescriptorWithName_size_(styling["font"], styling["size"])
fontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits_(AppKit.NSFontDescriptorTraitBold)
font = AppKit.NSFont.fontWithDescriptor_size_(fontDescriptor, styling["size"])
txt.addAttribute_value_range_(AppKit.NSFontAttributeName, font, rng)
elif value == AppKit.NSInlinePresentationIntentStrikethrough:
# ~strikethrough~
txt.addAttribute_value_range_(AppKit.NSStrikethroughStyleAttributeName, styling["strikeThrough"], rng)
elif value == AppKit.NSInlinePresentationIntentCode:
# `codeblock`
font = AppKit.NSFont.fontWithName_size_(styling["monospacedFont"], styling["size"])
txt.addAttribute_value_range_(AppKit.NSFontAttributeName, font, rng)
else:
print("presention intent:", value)
defaults = {
AppKit.NSFontAttributeName : AppKit.NSFont.fontWithName_size_(styling["font"], styling["size"]),
AppKit.NSForegroundColorAttributeName : AppKit.NSColor.blackColor()
}
txt.addAttributes_range_(defaults, (0, len(txt)))
txt.enumerateAttribute_inRange_options_usingBlock_(AppKit.NSInlinePresentationIntentAttributeName, (0, len(txt)), 0, block)
f = FormattedString()
f.fill(1, 0, 0)
f += "foo bar\n"
f._attributedString.appendAttributedString_(txt)
text(f, (100, 500))#, 300, 300))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment