开源软件名称(OpenSource Name):indragiek/CocoaMarkdown开源软件地址(OpenSource Url):https://github.com/indragiek/CocoaMarkdown开源编程语言(OpenSource Language):Objective-C 92.9%开源软件介绍(OpenSource Introduction):CocoaMarkdownMarkdown parsing and rendering for iOS and macOSCocoaMarkdown is a cross-platform framework for parsing and rendering Markdown, built on top of the C reference implementation of CommonMark. Why?CocoaMarkdown aims to solve two primary problems better than existing libraries:
InstallationFirst you will want to add this project as a submodule to your project:
Then, you need to pull down all of its dependencies.
Next, drag the APITraversing the Markdown AST
let document = CMDocument(contentsOfFile: path, options: [])
document.rootNode.iterator().enumerateUsingBlock { (node, _, _) in
print("String value: \(node.stringValue)")
} Building Custom RenderersThe @protocol CMParserDelegate <NSObject>
@optional
- (void)parserDidStartDocument:(CMParser *)parser;
- (void)parserDidEndDocument:(CMParser *)parser;
...
- (void)parser:(CMParser *)parser foundText:(NSString *)text;
- (void)parserFoundHRule:(CMParser *)parser;
...
@end
Rendering Attributed Strings
Going from a Markdown document to rendering it on screen is as easy as: let document = CMDocument(contentsOfFile: path, options: [])
let renderer = CMAttributedStringRenderer(document: document, attributes: CMTextAttributes())
textView.attributedText = renderer.render() Or, using the convenience method on textView.attributedText = CMDocument(contentsOfFile: path, options: []).attributedStringWithAttributes(CMTextAttributes()) HTML elements can be supported by implementing Transformers can be registered with the renderer to use them: let document = CMDocument(contentsOfFile: path, options: [])
let renderer = CMAttributedStringRenderer(document: document, attributes: CMTextAttributes())
renderer.registerHTMLElementTransformer(CMHTMLStrikethroughTransformer())
renderer.registerHTMLElementTransformer(CMHTMLSuperscriptTransformer())
textView.attributedText = renderer.render() Customizing Attributed strings renderingAll attributes used to style the text are customizable using the Every Markdown element type can be customized using the corresponding
Attributes for any Markdown element kind can be directly set: let textAttributes = CMTextAttributes()
textAttributes.linkAttributes.stringAttributes[NSAttributedString.Key.backgroundColor] = UIColor.yellow A probably better alternative for style customization is to use grouped attributes setting methods available in let textAttributes = CMTextAttributes()
// Set the text color for all headers
textAttributes.addStringAttributes([ .foregroundColor: UIColor(red: 0.0, green: 0.446, blue: 0.657, alpha: 1.0)],
forElementWithKinds: .anyHeader)
// Set a specific font + font-traits for all headers
let boldItalicTrait: UIFontDescriptor.SymbolicTraits = [.traitBold, .traitItalic]
textAttributes.addFontAttributes([ .family: "Avenir Next" ,
.traits: [ UIFontDescriptor.TraitKey.symbolic: boldItalicTrait.rawValue]],
forElementWithKinds: .anyHeader)
// Set specific font traits for header1 and header2
textAttributes.setFontTraits([.weight: UIFont.Weight.heavy],
forElementWithKinds: [.header1, .header2])
// Center block-quote paragraphs
textAttributes.addParagraphStyleAttributes([ .alignment: NSTextAlignment.center.rawValue],
forElementWithKinds: .blockQuote)
// Set a background color for code elements
textAttributes.addStringAttributes([ .backgroundColor: UIColor(white: 0.9, alpha: 0.5)],
forElementWithKinds: [.inlineCode, .codeBlock]) List styles can be customized using dedicated paragraph style attributes: 全部评论
专题导读
上一篇:googleworkspace/md2googleslides: Generate Google Slides from markdown发布时间:2022-08-18下一篇:smshen/MarkdownPhotos: Markdown Photos发布时间:2022-08-18热门推荐
热门话题
阅读排行榜
|
请发表评论