Git Product home page Git Product logo

attributedmarkdown's Introduction

AttributedMarkdown: Native Markdown Parsing on iOS

Markdown is intended to be as easy-to-read and easy-to-write as is feasible.

-- Daring Fireball

This library takes Markdown formatted text and turns it into an NSAttributedString, suitable for rendering in native UIKit components on iOS 6 (UITextView, UILabel, etc).

In short, this allows you to apply styling to Markdown without having to use UIWebView and HTML tags.

This project is based-upon / modifies a Cocoa fork of peg markdown.

Usage:

// start with a raw markdown string
NSString *rawText = @"Hello, world. *This* is native Markdown.";

// create a font attribute for emphasized text
UIFont *emFont = [UIFont fontWithName:@"AvenirNext-MediumItalic" size:15.0];

// create a color attribute for paragraph text
UIColor *color = [UIColor purpleColor];

// create a dictionary to hold your custom attributes for any Markdown types
NSDictionary *attributes = @{
  @(EMPH): @{NSFontAttributeName : emFont,},
  @(PARA): @{NSForegroundColorAttributeName : color,}
};

// parse the markdown
NSAttributedString *prettyText = markdown_to_attr_string(rawText,0,attributes);

// assign it to a view object
myTextView.attributedText = prettyText;

Check out the HelloMarkdown example app to see it in action.

Requirements & Setup

AttributedMarkdown makes use of a parser-generator called greg. This is included as a submodule, and you'll need to first run this from the command-line (from your project's root directory):

git submodule update --init --recursive

You'll also need to include the CoreText Framework in your project.

To use AttributedMarkdown in one of your projects, follow the standard Apple guidelines for linking against a static library

Finally, create a group in your project called "Headers" and copy these files into it:

markdown_lib.h
markdown_peg.h 

Leave the option to copy the files into your project unselected.

(Note that you don't have to call the group "Headers", this is simply a suggestion. The important bit is making sure the header references exist somewhere in your project.)

The import statements in your project, wherever you want to make use of the library (eg., in a View Controller) should look like:

#import "markdown_lib.h"
#import "markdown_peg.h"

Basic Cascading Styles

AttributedMarkdown performs some very basic cascading styles, merging the string attributes of parent elements into child elements by extracting their font traits via CoreText. This allows for things like emphasized words within an h1 tag to be bold as well as italicized.

Performance

Although I have yet to perform any optimizations, parsing and display of markdown in a UITableView filled with many cells of long-form markdown sample text performs rather well.

Limitations

This is a work in progress. Some tags are not yet supported, like img, etc.

Credit

AttributedMarkdown was created by Gregory Wieber and Jim Radford. It is based upon peg-markdown.

License

AttributedMarkdown is released under both the GPL and the MIT license; see LICENSE for details.

Peg-Markdown License

peg-markdown is written and maintained by John MacFarlane (jgm on github), with significant contributions by Ryan Tomayko (rtomayko). It is released under both the GPL and the MIT license; see LICENSE for details. peg-markdown was adapted for Cocoa by David Whetstone.

attributedmarkdown's People

Contributors

dreamwieber avatar humblehacker avatar jgm avatar neonichu avatar rhysforyou avatar rtomayko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.