• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

xing/XNGMarkdownParser: A Markdown NSAttributedString parser.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

xing/XNGMarkdownParser

开源软件地址(OpenSource Url):

https://github.com/xing/XNGMarkdownParser

开源编程语言(OpenSource Language):

Mercury 97.5%

开源软件介绍(OpenSource Introduction):

XNGMarkdownParser - A Markdown NSAttributedString Parser

Build Status Coverage Status Dependency Status CocoaPods Version CocoaPods License CocoaPods Platform

This is a Markdown => NSAttributedString parser built on top of a flex parser. It takes an NSString and returns an NSAttributedString with markdown tags replaced by CoreText formatting attributes.

This project is a fork of NSAttributedMarkdownParser by NimbusKit: https://github.com/NimbusKit/markdown

Adding it to your Project

Using cocoapods

Add pod 'XNGMarkdownParser' to your Podfile and do a pod install.

Manual

  1. Drag all of the files from the src/ directory into your project.
  2. Import XNGMarkdownParser.h in your project.
  3. Create an instance of the parser object and pass it the string you wish to parse.
  4. Plug the resulting NSAttributedString into your favorite NSAttributedString label implementation (like an UITextView)

Supported Features

*italics*
**bold**
***bold italic***
~~strikethrough~~

# Header 1
## Header 2
### Header 3
#### Header 4
##### Header 5
###### Header 6

Header 1
========

Header 2
--------

http://google.com urls
[Text] (http://google.com "alt text") urls
[email protected]

Extended Features

  • UTF-8 support
  • Vastly improved speed
  • Extended formatting for paragraphs
  • Support different link fonts
  • Tests and example project
  • Support for CocoaPods

Examples

###Simplest example

XNGMarkdownParser *parser = [[XNGMarkdownParser alloc] init];
NSAttributedString *string = [parser attributedStringFromMarkdownString:@"This is __rad__."];

Further text customization

// this parser initializes only once and customizes fonts, line height and link color
+ (XNGMarkdownParser *)titleMarkdownParser {
    static dispatch_once_t onceToken;
    static XNGMarkdownParser *parser;
    dispatch_once(&onceToken, ^{
        parser = [[XNGMarkdownParser alloc] init];

        parser.paragraphFont = [UIFont xng_14Font];
        parser.boldFontName = [UIFont xng_14Font].fontName;
        parser.linkFontName = [UIFont xng_14Font].fontName;

        const CGFloat lineHeight = 18;
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.minimumLineHeight = lineHeight;
        parser.topAttributes = @{
            NSParagraphStyleAttributeName: paragraphStyle,
            NSForegroundColorAttributeName: self.textColor
        };
    });

    return parser;
}

See the included Example project and the tests to check for further options.

Want to collaborate?

If you would like to collaborate with the project by adding features or fixing bugs, you can do it like this:

  • If you need to change the grammar we use to parse Markdown test, you have to change the Lex file grammar/markdown.grammar, used to generate the lexical analizer. If you need to add new tokens, you will need to add them to the files XNGMarkdownTokens.h and XNGMarkdownTokens.cpp, in order to be able to execute actions later when they are detected.
  • If you change the grammar, you need to run the script generate.sh to generate the code for the lexical analyzer.
  • In order to convert the detected text to an NSAttributedString, you will need to perform changes in the file src/XNGMarkdownParser.m, more concisely in the method -consumeToken:text:.

After you have fixed a bug or added a feature, please check the tests and extend them if needed, and you will be ready to create a pull request. Thanks!




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
saleem-hadad/larecipe: 发布时间:2022-08-18
下一篇:
c-rainstorm/blog: 本仓库存放个人博客的 markdown 源文件发布时间:2022-08-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap