### C Main Function Example Source: https://github.com/indragiek/cocoamarkdown/blob/master/CocoaMarkdownTests/Resources/test.md A standard C main function that prints 'Hello world'. ```c int main(int argc, char *argv[]) { puts("Hello world"); return EXIT_SUCCESS; } ``` -------------------------------- ### Initialize Git Submodules Recursively Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md After adding CocoaMarkdown as a submodule, initialize and update all of its nested dependencies. ```bash cd CocoaMarkdown git submodule update --init --recursive ``` -------------------------------- ### Convenience HTML Rendering Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Render Markdown content to an HTML string using the convenience method on CMDocument. ```swift let HTML = CMDocument(contentsOfFile: path).HTMLString() ``` -------------------------------- ### Building Custom Renderers with CMParserDelegate Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Implement the `CMParserDelegate` protocol to create custom renderers by receiving callbacks during AST traversal. This provides flexibility for custom rendering logic. ```APIDOC ## Building Custom Renderers Implement the `CMParserDelegate` protocol to create custom renderers by receiving callbacks during AST traversal. ### Description This provides flexibility for custom rendering logic, allowing you to define how different Markdown elements are processed. ### Protocol Definition ```objective-c @protocol CMParserDelegate @optional - (void)parserDidStartDocument:(CMParser *)parser; - (void)parserDidEndDocument:(CMParser *)parser; ... - (void)parser:(CMParser *)parser foundText:(NSString *)text; - (void)parserFoundHRule:(CMParser *)parser; ... @end ``` ``` -------------------------------- ### Convenience Method for Attributed String Rendering Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md A convenience method on CMDocument to directly obtain an attributed string representation of the Markdown content. ```swift textView.attributedText = CMDocument(contentsOfFile: path, options: []).attributedStringWithAttributes(CMTextAttributes()) ``` -------------------------------- ### Add CocoaMarkdown as a Git Submodule Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Add the CocoaMarkdown project as a submodule to your existing project to include it. ```bash git submodule add https://github.com/indragiek/CocoaMarkdown.git ``` -------------------------------- ### Render Markdown to HTML Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Render Markdown content to an HTML string using CMHTMLRenderer. ```swift let document = CMDocument(contentsOfFile: path, options: []) let renderer = CMHTMLRenderer(document: document) let HTML = renderer.render() ``` -------------------------------- ### CMParserDelegate Protocol for Custom Renderers Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Implement the CMParserDelegate protocol to create custom renderers by defining callbacks for various parsing events. This API is similar to NSXMLParser. ```objective-c @protocol CMParserDelegate @optional - (void)parserDidStartDocument:(CMParser *)parser; - (void)parserDidEndDocument:(CMParser *)parser; ... - (void)parser:(CMParser *)parser foundText:(NSString *)text; - (void)parserFoundHRule:(CMParser *)parser; ... @end ``` -------------------------------- ### Traversing the Markdown AST Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Use CMNode and an iterator to traverse the Markdown Abstract Syntax Tree (AST). This allows for low-level inspection and manipulation of the parsed Markdown structure. ```APIDOC ## Traversing the Markdown AST Use `CMNode` and `CMIterator` to traverse the Markdown Abstract Syntax Tree (AST). ### Description This allows for low-level inspection and manipulation of the parsed Markdown structure. ### Usage ```swift let document = CMDocument(contentsOfFile: path, options: []) document.rootNode.iterator().enumerateUsingBlock { (node, _, _) in print("String value: \(node.stringValue)") } ``` ``` -------------------------------- ### Set Background Color for Links Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Customize the background color for link attributes using string attributes. ```swift let textAttributes = CMTextAttributes() textAttributes.linkAttributes.stringAttributes[NSAttributedString.Key.backgroundColor] = UIColor.yellow ``` -------------------------------- ### Customize Ordered List Formatting Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Customize the number format and indentation for ordered list items. ```swift // Customize numbered list item labels format and distance between label and paragraph textAttributes.addParagraphStyleAttributes([ .listItemNumberFormat: "(%02ld)", .listItemLabelIndent: 30 ], forElementWithKinds: .orderedList) ``` -------------------------------- ### Add String Attributes for Headers Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Set the text color for all header elements using addStringAttributes. ```swift 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 Background Color for Code Elements Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Apply a background color to inline code and code block elements. ```swift let textAttributes = CMTextAttributes() // Set a background color for code elements textAttributes.addStringAttributes([ .backgroundColor: UIColor(white: 0.9, alpha: 0.5)], forElementWithKinds: [.inlineCode, .codeBlock]) ``` -------------------------------- ### Traverse Markdown AST with Iterator Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Iterate over the Markdown Abstract Syntax Tree (AST) nodes using CMNode and CMAttributedStringRenderer. This allows for custom processing of each node's string value. ```swift let document = CMDocument(contentsOfFile: path, options: []) document.rootNode.iterator().enumerateUsingBlock { (node, _, _) in print("String value: \(node.stringValue)") } ``` -------------------------------- ### Add Font Attributes for Headers Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Set a specific font and font traits for all header elements using addFontAttributes. ```swift let textAttributes = CMTextAttributes() // 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) ``` -------------------------------- ### Center Block-Quote Paragraphs Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Center the alignment of block-quote paragraphs using addParagraphStyleAttributes. ```swift let textAttributes = CMTextAttributes() // Center block-quote paragraphs textAttributes.addParagraphStyleAttributes([ .alignment: NSTextAlignment.center.rawValue], forElementWithKinds: .blockQuote) ``` -------------------------------- ### Set Font Traits for Specific Headers Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Apply specific font traits to header1 and header2 elements using setFontTraits. ```swift let textAttributes = CMTextAttributes() // Set specific font traits for header1 and header2 textAttributes.setFontTraits([.weight: UIFont.Weight.heavy], forElementWithKinds: [.header1, .header2]) ``` -------------------------------- ### Rendering Attributed Strings with CMAttributedStringRenderer Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Utilize `CMAttributedStringRenderer` to directly convert Markdown into an `NSAttributedString`, simplifying the process of displaying Markdown content natively on iOS and macOS. ```APIDOC ## Rendering Attributed Strings Use `CMAttributedStringRenderer` to directly convert Markdown into an `NSAttributedString`. ### Description This simplifies the process of displaying Markdown content natively on iOS and macOS by skipping the HTML conversion step. ### Basic Usage ```swift let document = CMDocument(contentsOfFile: path, options: []) let renderer = CMAttributedStringRenderer(document: document, attributes: CMTextAttributes()) textView.attributedText = renderer.render() ``` ### Convenience Method ```swift textView.attributedText = CMDocument(contentsOfFile: path, options: []).attributedStringWithAttributes(CMTextAttributes()) ``` ``` -------------------------------- ### Render Markdown to NSAttributedString Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Use CMAttributedStringRenderer to directly convert a Markdown document into an NSAttributedString for easy rendering on iOS and macOS. This bypasses HTML conversion. ```swift let document = CMDocument(contentsOfFile: path, options: []) let renderer = CMAttributedStringRenderer(document: document, attributes: CMTextAttributes()) textView.attributedText = renderer.render() ``` -------------------------------- ### Registering HTML Element Transformers Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Extend `CMAttributedStringRenderer`'s capabilities by registering custom HTML element transformers, such as `CMHTMLStrikethroughTransformer`, to handle specific HTML tags within the Markdown. ```APIDOC ## Registering HTML Element Transformers Extend `CMAttributedStringRenderer`'s capabilities by registering custom HTML element transformers. ### Description This allows the renderer to handle specific HTML tags within the Markdown content. The framework includes transformers for common tags like strikethrough, superscript, and subscript. ### Example Usage ```swift let document = CMDocument(contentsOfFile: path, options: []) let renderer = CMAttributedStringRenderer(document: document, attributes: CMTextAttributes()) renderer.registerHTMLElementTransformer(CMHTMLStrikethroughTransformer()) renderer.registerHTMLElementTransformer(CMHTMLSuperscriptTransformer()) textView.attributedText = renderer.render() ``` ``` -------------------------------- ### Register HTML Element Transformers Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Register custom transformers like CMHTMLStrikethroughTransformer to handle specific HTML elements within the Markdown rendering process. This allows for custom styling of elements like strikethrough, superscript, and subscript. ```swift let document = CMDocument(contentsOfFile: path, options: []) let renderer = CMAttributedStringRenderer(document: document, attributes: CMTextAttributes()) renderer.registerHTMLElementTransformer(CMHTMLStrikethroughTransformer()) renderer.registerHTMLElementTransformer(CMHTMLSuperscriptTransformer()) textView.attributedText = renderer.render() ``` -------------------------------- ### Customize Unordered List Bullets Source: https://github.com/indragiek/cocoamarkdown/blob/master/README.md Customize the bullet character for unordered lists and sublists. ```swift // Customize the list bullets textAttributes.addParagraphStyleAttributes([ .listItemBulletString: "🍏" ], forElementWithKinds: .unorderedList) textAttributes.addParagraphStyleAttributes([ .listItemBulletString: "🌼" ], forElementWithKinds: .unorderedSublist) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.